mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-21 15:00:51 +01:00
Keep failed command groups atomic and preserve redo history
This commit is contained in:
commit
0a4ef6f039
6 files changed
+343
-25
No files matched your search
@@ -124,6 +124,7 @@ struct Snapshot {
|
||||
label: String,
|
||||
modified: bool,
|
||||
dirty: bool,
|
||||
cook_epoch: u64,
|
||||
}
|
||||
|
||||
/// Nodes and connections copied to the clipboard (as JSON, so other sessions can paste them).
|
||||
@@ -169,6 +170,8 @@ pub struct Session {
|
||||
redo: Vec<Snapshot>,
|
||||
/// Edits with the same key coalesce into one undo step (slider drags).
|
||||
merge_key: Option<String>,
|
||||
/// Snapshot and successful-edit flag for the current command group.
|
||||
group: Option<(Snapshot, bool)>,
|
||||
decoded: Arc<Mutex<HashMap<(u64, bool), Image>>>,
|
||||
}
|
||||
|
||||
@@ -195,6 +198,7 @@ impl Session {
|
||||
undo: Vec::new(),
|
||||
redo: Vec::new(),
|
||||
merge_key: None,
|
||||
group: None,
|
||||
decoded: Arc::new(Mutex::new(HashMap::new())),
|
||||
}
|
||||
}
|
||||
@@ -216,6 +220,7 @@ impl Session {
|
||||
label: label.into(),
|
||||
modified: self.modified,
|
||||
dirty: self.dirty,
|
||||
cook_epoch: self.cook_epoch,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -228,6 +233,9 @@ impl Session {
|
||||
/// Like [`Session::edit`], but consecutive edits with the same `key` (dragging one slider)
|
||||
/// make a single undo step.
|
||||
pub fn edit_merged<T>(&mut self, key: &str, label: &str, f: impl FnOnce(&mut Session) -> Result<T>) -> Result<T> {
|
||||
if self.group.is_some() {
|
||||
return self.edit_inner(label, f);
|
||||
}
|
||||
if self.merge_key.as_deref() == Some(key) && !self.undo.is_empty() {
|
||||
// The merged step keeps its original snapshot; restore the session if `f` fails.
|
||||
let snap = self.snapshot(label);
|
||||
@@ -251,8 +259,75 @@ impl Session {
|
||||
self.merge_key = None;
|
||||
}
|
||||
|
||||
/// Start grouping: every edit until [`Session::end_group`] becomes one undo entry labelled
|
||||
/// `label` (a gesture that runs several commands, such as rewiring a link). Nothing is
|
||||
/// recorded if no edit ran. A group started inside a group is ignored.
|
||||
pub fn begin_group(&mut self, label: &str) {
|
||||
if self.group.is_none() {
|
||||
self.merge_key = None;
|
||||
self.group = Some((self.snapshot(label), false));
|
||||
}
|
||||
}
|
||||
|
||||
/// Close the group opened by [`Session::begin_group`], recording one undo entry if any edit ran.
|
||||
pub fn end_group(&mut self) {
|
||||
if let Some((snap, edited)) = self.group.take()
|
||||
&& edited
|
||||
{
|
||||
self.undo.push(snap);
|
||||
if self.undo.len() > MAX_UNDO {
|
||||
self.undo.remove(0);
|
||||
}
|
||||
self.redo.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/// Run `f` as one undo entry, however many edits it makes. On error everything `f` did is
|
||||
/// undone and nothing is recorded.
|
||||
pub fn group<T>(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result<T>) -> Result<T> {
|
||||
if self.group.is_some() {
|
||||
let snap = self.snapshot(label);
|
||||
let edited = self.group.as_ref().is_some_and(|(_, edited)| *edited);
|
||||
let result = f(self);
|
||||
if result.is_err() {
|
||||
self.restore(snap);
|
||||
if let Some((_, flag)) = self.group.as_mut() {
|
||||
*flag = edited;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
self.begin_group(label);
|
||||
let r = f(self);
|
||||
match r {
|
||||
Ok(_) => self.end_group(),
|
||||
Err(_) => {
|
||||
if let Some((snap, _)) = self.group.take() {
|
||||
self.restore(snap);
|
||||
}
|
||||
}
|
||||
}
|
||||
r
|
||||
}
|
||||
|
||||
fn edit_inner<T>(&mut self, label: &str, f: impl FnOnce(&mut Session) -> Result<T>) -> Result<T> {
|
||||
let snap = self.snapshot(label);
|
||||
if self.group.is_some() {
|
||||
// Only successful edits make the group an undo entry.
|
||||
return match f(self) {
|
||||
Ok(v) => {
|
||||
if let Some((_, edited)) = self.group.as_mut() {
|
||||
*edited = true;
|
||||
}
|
||||
self.changed();
|
||||
Ok(v)
|
||||
}
|
||||
Err(e) => {
|
||||
self.restore(snap);
|
||||
Err(e)
|
||||
}
|
||||
};
|
||||
}
|
||||
match f(self) {
|
||||
Ok(v) => {
|
||||
self.undo.push(snap);
|
||||
@@ -275,7 +350,7 @@ impl Session {
|
||||
self.graph = s.graph.min(self.pkg.graphs.len().saturating_sub(1));
|
||||
self.selection = s.selection;
|
||||
self.modified = s.modified;
|
||||
self.dirty = s.dirty;
|
||||
self.dirty = s.dirty || self.cook_epoch != s.cook_epoch;
|
||||
}
|
||||
|
||||
/// Mark the document modified and in need of a cook.
|
||||
@@ -298,6 +373,7 @@ impl Session {
|
||||
}
|
||||
|
||||
pub fn undo(&mut self) -> Option<String> {
|
||||
self.end_group();
|
||||
let s = self.undo.pop()?;
|
||||
let label = s.label.clone();
|
||||
let cur = self.snapshot(&label);
|
||||
@@ -309,6 +385,7 @@ impl Session {
|
||||
}
|
||||
|
||||
pub fn redo(&mut self) -> Option<String> {
|
||||
self.end_group();
|
||||
let s = self.redo.pop()?;
|
||||
let label = s.label.clone();
|
||||
let cur = self.snapshot(&label);
|
||||
@@ -324,6 +401,7 @@ impl Session {
|
||||
self.undo.clear();
|
||||
self.redo.clear();
|
||||
self.merge_key = None;
|
||||
self.group = None;
|
||||
}
|
||||
|
||||
/// A decoder for the package's bitmap resources (memoized, shareable with worker threads).
|
||||
|
||||
@@ -97,3 +97,134 @@ fn failed_merged_edits_restore_clean_flags_and_history() {
|
||||
assert_eq!(s.history(), history);
|
||||
assert!(!s.modified && !s.dirty);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod group_tests {
|
||||
use crate::{EngineError, Session, execute};
|
||||
use serde_json::json;
|
||||
|
||||
fn two_nodes() -> (Session, u64, u64) {
|
||||
let mut s = crate::sample_session();
|
||||
execute(&mut s, "file.new", &json!({"template": "empty"})).unwrap();
|
||||
let a = execute(&mut s, "graph.node.add", &json!({"def": "clouds_1"})).unwrap()["id"].as_u64().unwrap();
|
||||
let b = execute(&mut s, "graph.node.add", &json!({"def": "levels"})).unwrap()["id"].as_u64().unwrap();
|
||||
(s, a, b)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_group_is_one_undo_entry_and_one_undo_reverts_all() {
|
||||
let (mut s, a, b) = two_nodes();
|
||||
let base = s.history().len();
|
||||
s.group("Rewire", |s| {
|
||||
execute(s, "graph.connect", &json!({"from": a, "to": b, "toPort": "input"}))?;
|
||||
execute(s, "graph.disconnect", &json!({"to": b, "toPort": "input"}))?;
|
||||
execute(s, "graph.connect", &json!({"from": a, "to": b, "toPort": "input"}))?;
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(s.history().len(), base + 1);
|
||||
assert_eq!(s.history().last().map(String::as_str), Some("Rewire"));
|
||||
assert_eq!(s.undo().as_deref(), Some("Rewire"));
|
||||
assert!(s.graph().unwrap().connections.is_empty(), "the single undo removed every step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failing_group_restores_everything_and_records_nothing() {
|
||||
let (mut s, a, b) = two_nodes();
|
||||
let base = s.history().len();
|
||||
let r: Result<(), EngineError> = s.group("Rewire", |s| {
|
||||
execute(s, "graph.connect", &json!({"from": a, "to": b, "toPort": "input"}))?;
|
||||
Err(EngineError::Other("boom".into()))
|
||||
});
|
||||
assert!(r.is_err());
|
||||
assert_eq!(s.history().len(), base);
|
||||
assert!(s.graph().unwrap().connections.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn begin_and_end_group_span_several_calls_and_an_untouched_group_records_nothing() {
|
||||
let (mut s, a, b) = two_nodes();
|
||||
let base = s.history().len();
|
||||
s.begin_group("Nothing");
|
||||
s.end_group();
|
||||
assert_eq!(s.history().len(), base);
|
||||
s.begin_group("Both");
|
||||
execute(&mut s, "graph.connect", &json!({"from": a, "to": b, "toPort": "input"})).unwrap();
|
||||
execute(&mut s, "graph.node.param", &json!({"id": b, "param": "in_low", "value": 0.2})).unwrap();
|
||||
s.end_group();
|
||||
assert_eq!(s.history().len(), base + 1);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_group_with_only_failed_edits_preserves_redo_and_clean_flags() {
|
||||
let mut s = crate::sample_session();
|
||||
s.edit("Prior", |s| {
|
||||
s.pkg.name = "Prior".into();
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
s.undo();
|
||||
assert!(s.can_redo());
|
||||
s.modified = false;
|
||||
s.dirty = false;
|
||||
let history = s.history();
|
||||
let name = s.pkg.name.clone();
|
||||
s.begin_group("Rejected");
|
||||
let result: crate::Result<()> = s.edit("Bad", |s| {
|
||||
s.pkg.name = "Partial".into();
|
||||
Err(crate::EngineError::Other("failed".into()))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
s.end_group();
|
||||
assert_eq!(s.pkg.name, name);
|
||||
assert_eq!(s.history(), history);
|
||||
assert!(s.can_redo());
|
||||
assert!(!s.modified && !s.dirty);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_caught_nested_group_error_rolls_back_only_its_own_edits() {
|
||||
let mut s = crate::sample_session();
|
||||
let original = s.pkg.name.clone();
|
||||
let count = s.history().len();
|
||||
s.group("Outer", |s| {
|
||||
s.edit("Outer edit", |s| {
|
||||
s.pkg.name = "Outer".into();
|
||||
Ok(())
|
||||
})?;
|
||||
let result: crate::Result<()> = s.group("Inner", |s| {
|
||||
s.edit("Inner edit", |s| {
|
||||
s.pkg.name = "Partial inner".into();
|
||||
Ok(())
|
||||
})?;
|
||||
Err(crate::EngineError::Other("failed".into()))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
assert_eq!(s.pkg.name, "Outer");
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(s.history().len(), count + 1);
|
||||
s.undo();
|
||||
assert_eq!(s.pkg.name, original);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_failed_group_recooks_if_it_cooked_a_temporary_document() {
|
||||
use crate::{core::NodeId, execute};
|
||||
use serde_json::json;
|
||||
let mut s = crate::new_session();
|
||||
s.preview_size = Some(16);
|
||||
let id = NodeId(execute(&mut s, "graph.node.add", &json!({"def": "uniform"})).unwrap()["id"].as_u64().unwrap());
|
||||
execute(&mut s, "graph.node.param", &json!({"id": id.0, "param": "mode", "value": 1})).unwrap();
|
||||
let before = s.cook().image(id, 0).unwrap().data.clone();
|
||||
let result: crate::Result<()> = s.group("Temporary", |s| {
|
||||
execute(s, "graph.node.param", &json!({"id": id.0, "param": "color", "value": [0.1, 0.8, 0.2, 1.0]}))?;
|
||||
s.cook();
|
||||
Err(crate::EngineError::Other("failed".into()))
|
||||
});
|
||||
assert!(result.is_err());
|
||||
s.cook_if_dirty();
|
||||
assert_eq!(s.cooked.image(id, 0).unwrap().data, before);
|
||||
}
|
||||
@@ -377,6 +377,58 @@ pub fn add_node_near_selection(app: &mut MaterialCraftApp, def: &str) {
|
||||
app.run("graph.node.add", params);
|
||||
}
|
||||
|
||||
/// Add a Bitmap node (wired to the selection when that makes sense) showing `resource`.
|
||||
pub fn add_bitmap_node(app: &mut MaterialCraftApp, resource: &str) {
|
||||
add_node_near_selection(app, "bitmap");
|
||||
if let Some(id) = app.session.selection.last().copied() {
|
||||
app.run("graph.node.param", json!({"id": id.0, "param": "resource", "value": resource}));
|
||||
}
|
||||
}
|
||||
|
||||
/// File ▸ Import bitmap from a path: import, add the node and bind it, as one undo entry.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
pub fn import_bitmap_file(app: &mut MaterialCraftApp, path: &str) {
|
||||
app.session.begin_group("Import bitmap");
|
||||
if let Some(r) = app.run("file.importBitmap", json!({"path": path})) {
|
||||
add_bitmap_node(app, r["name"].as_str().unwrap_or_default());
|
||||
}
|
||||
app.session.end_group();
|
||||
}
|
||||
|
||||
fn run_gesture(app: &mut MaterialCraftApp, label: &str, commands: &[(&str, serde_json::Value)]) {
|
||||
let result = app.session.group(label, |session| {
|
||||
for (id, params) in commands {
|
||||
crate::cook::execute_guarded(session, id, params).map_err(materialcraft_engine::EngineError::Other)?;
|
||||
}
|
||||
Ok(())
|
||||
});
|
||||
if let Err(error) = result {
|
||||
app.toast(error.to_string(), true);
|
||||
}
|
||||
}
|
||||
|
||||
/// Finish dragging a wire onto input `to_port` of `to`: connect it, first removing the link it
|
||||
/// was pulled off (`detached`). The gesture is one undo entry.
|
||||
fn rewire(app: &mut MaterialCraftApp, from: &(NodeId, String), detached: Option<&(NodeId, String)>, to: NodeId, to_port: &str) {
|
||||
let mut commands = Vec::new();
|
||||
if let Some((n, p)) = detached
|
||||
&& (*n != to || p != to_port)
|
||||
{
|
||||
commands.push(("graph.disconnect", json!({"to": n.0, "toPort": p})));
|
||||
}
|
||||
commands.push(("graph.connect", json!({"from": from.0.0, "fromPort": from.1, "to": to.0, "toPort": to_port})));
|
||||
run_gesture(app, "Connect", &commands);
|
||||
}
|
||||
|
||||
/// Finish dragging a frame: set its rectangle and move the nodes it carries, as one undo entry.
|
||||
fn move_frame(app: &mut MaterialCraftApp, id: u64, rect: [f32; 4], nodes: &[NodeId], delta: Vec2) {
|
||||
let mut commands = vec![("graph.frame.edit", json!({"id": id, "rect": rect}))];
|
||||
if !nodes.is_empty() {
|
||||
commands.push(("graph.node.move", json!({"ids": nodes.iter().map(|n| n.0).collect::<Vec<_>>(), "by": [delta.x, delta.y]})));
|
||||
}
|
||||
run_gesture(app, "Move frame", &commands);
|
||||
}
|
||||
|
||||
/// Frame the graph (or the selection) in the view.
|
||||
fn frame(app: &mut MaterialCraftApp, view: Rect, only_selection: bool) {
|
||||
let thumbs = app.ui.prefs.thumbnails;
|
||||
@@ -945,14 +997,7 @@ fn handle_input(
|
||||
Drag::Wire { from, detached } => {
|
||||
let target = pointer.and_then(|p| nearest_port(&app.graph.ports, p, 12.0)).filter(|h| !h.output).cloned();
|
||||
match target {
|
||||
Some(t) => {
|
||||
if let Some((n, p)) = &detached
|
||||
&& (*n != t.node || *p != t.port)
|
||||
{
|
||||
app.run("graph.disconnect", json!({"to": n.0, "toPort": p}));
|
||||
}
|
||||
app.run("graph.connect", json!({"from": from.0.0, "fromPort": from.1, "to": t.node.0, "toPort": t.port}));
|
||||
}
|
||||
Some(t) => rewire(app, &from, detached.as_ref(), t.node, &t.port),
|
||||
None => {
|
||||
if let Some((n, p)) = &detached {
|
||||
app.run("graph.disconnect", json!({"to": n.0, "toPort": p}));
|
||||
@@ -979,10 +1024,7 @@ fn handle_input(
|
||||
} else {
|
||||
[f.rect[0] + delta.x, f.rect[1] + delta.y, f.rect[2], f.rect[3]]
|
||||
};
|
||||
app.run("graph.frame.edit", json!({"id": id, "rect": r}));
|
||||
if !nodes.is_empty() {
|
||||
app.run("graph.node.move", json!({"ids": nodes.iter().map(|n| n.0).collect::<Vec<_>>(), "by": [delta.x, delta.y]}));
|
||||
}
|
||||
move_frame(app, id, r, &nodes, delta);
|
||||
}
|
||||
}
|
||||
Drag::Comment { id, delta } => {
|
||||
@@ -1351,6 +1393,57 @@ fn canvas_menu(app: &mut MaterialCraftApp, ui: &mut Ui, at: Pos2) {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn a_failed_rewire_keeps_the_original_connection_and_history() {
|
||||
let mut app = empty_app();
|
||||
let source = app.run("graph.node.add", json!({"def": "uniform"})).unwrap()["id"].as_u64().unwrap();
|
||||
let target = app.run("graph.node.add", json!({"def": "levels"})).unwrap()["id"].as_u64().unwrap();
|
||||
app.run("graph.connect", json!({"from": source, "to": target, "toPort": "input"})).unwrap();
|
||||
let before = app.session.graph().unwrap().connections.clone();
|
||||
let history = app.session.history();
|
||||
rewire(&mut app, &(NodeId(source), "output".into()), Some(&(NodeId(target), "input".into())), NodeId(target), "missing");
|
||||
assert_eq!(app.session.graph().unwrap().connections, before);
|
||||
assert_eq!(app.session.history(), history);
|
||||
assert!(app.toasts.iter().any(|t| t.error));
|
||||
}
|
||||
|
||||
fn empty_app() -> MaterialCraftApp {
|
||||
let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), crate::UiState::default());
|
||||
app.run("file.new", json!({"template": "empty"}));
|
||||
app
|
||||
}
|
||||
|
||||
fn add(app: &mut MaterialCraftApp, def: &str) -> NodeId {
|
||||
NodeId(app.run("graph.node.add", json!({"def": def})).and_then(|v| v["id"].as_u64()).unwrap())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rewiring_a_dragged_link_is_one_undo_entry() {
|
||||
let mut app = empty_app();
|
||||
let (a, b, c) = (add(&mut app, "clouds_1"), add(&mut app, "levels"), add(&mut app, "levels"));
|
||||
app.run("graph.connect", json!({"from": a.0, "fromPort": "output", "to": b.0, "toPort": "input"}));
|
||||
let base = app.session.history().len();
|
||||
rewire(&mut app, &(a, "output".to_string()), Some(&(b, "input".to_string())), c, "input");
|
||||
assert_eq!(app.session.history().len(), base + 1, "{:?}", app.session.history());
|
||||
let conns = &app.session.graph().unwrap().connections;
|
||||
assert_eq!(conns.len(), 1);
|
||||
assert_eq!(conns[0].to, c);
|
||||
app.session.undo();
|
||||
assert_eq!(app.session.graph().unwrap().connections[0].to, b, "one undo restores the old link");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dragging_a_frame_with_its_nodes_is_one_undo_entry() {
|
||||
let mut app = empty_app();
|
||||
let n = add(&mut app, "levels");
|
||||
let id = app.run("graph.frame.add", json!({"rect": [0, 0, 300, 200]})).and_then(|v| v["id"].as_u64()).unwrap();
|
||||
let base = app.session.history().len();
|
||||
move_frame(&mut app, id, [10.0, 10.0, 300.0, 200.0], &[n], vec2(10.0, 10.0));
|
||||
assert_eq!(app.session.history().len(), base + 1, "{:?}", app.session.history());
|
||||
app.session.undo();
|
||||
assert_eq!(app.session.graph().unwrap().frames[0].rect, [0.0, 0.0, 300.0, 200.0]);
|
||||
}
|
||||
|
||||
/// The whole app in an offscreen egui context, driven one frame at a time.
|
||||
struct Frames {
|
||||
ctx: egui::Context,
|
||||
|
||||
@@ -63,16 +63,16 @@ pub fn load(app: &mut MaterialCraftApp, name: &str, bytes: Vec<u8>, bitmap: bool
|
||||
return;
|
||||
}
|
||||
if bitmap || materialcraft_engine::formats::IMAGE_EXTENSIONS.contains(&ext.as_str()) {
|
||||
// Import, add the node and bind it: one undo entry.
|
||||
app.session.begin_group("Import bitmap");
|
||||
match materialcraft_engine::file_cmds::add_resource(&mut app.session, name, bytes, None) {
|
||||
Ok(res) => {
|
||||
crate::graph_view::add_node_near_selection(app, "bitmap");
|
||||
if let Some(id) = app.session.selection.last().copied() {
|
||||
app.run("graph.node.param", serde_json::json!({"id": id.0, "param": "resource", "value": res}));
|
||||
}
|
||||
crate::graph_view::add_bitmap_node(app, &res);
|
||||
app.toast(format!("{} {res}", tr("Imported")), false);
|
||||
}
|
||||
Err(e) => app.toast(format!("{} {name}: {e}", tr("Couldn't import")), true),
|
||||
}
|
||||
app.session.end_group();
|
||||
return;
|
||||
}
|
||||
if app.ui.windows.unsaved.is_some() {
|
||||
|
||||
@@ -611,14 +611,8 @@ pub fn file_action(app: &mut MaterialCraftApp, ctx: &egui::Context, id: &str) ->
|
||||
true
|
||||
}
|
||||
"file.importBitmap" => {
|
||||
if let Some(p) = rfd::FileDialog::new().add_filter("Images", materialcraft_engine::formats::IMAGE_EXTENSIONS).pick_file()
|
||||
&& let Some(r) = app.run("file.importBitmap", json!({"path": p.display().to_string()}))
|
||||
{
|
||||
let name = r["name"].as_str().unwrap_or_default().to_string();
|
||||
crate::graph_view::add_node_near_selection(app, "bitmap");
|
||||
if let Some(id) = app.session.selection.last().copied() {
|
||||
app.run("graph.node.param", json!({"id": id.0, "param": "resource", "value": name}));
|
||||
}
|
||||
if let Some(p) = rfd::FileDialog::new().add_filter("Images", materialcraft_engine::formats::IMAGE_EXTENSIONS).pick_file() {
|
||||
crate::graph_view::import_bitmap_file(app, &p.display().to_string());
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -23,3 +23,25 @@ mod open_guard_tests {
|
||||
assert!(!app.toasts.is_empty(), "the open was attempted and its error reported");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod import_tests {
|
||||
use crate::{MaterialCraftApp, state::UiState};
|
||||
use materialcraft_engine::formats::{Depth, encode};
|
||||
use materialcraft_engine::image::{Image, Kind};
|
||||
|
||||
#[test]
|
||||
fn importing_a_bitmap_file_is_one_undo_entry() {
|
||||
let png = encode(&Image::filled(4, 4, Kind::Color, [0.5, 0.2, 0.1, 1.0]), "png", Depth::U8, true).unwrap();
|
||||
let path = std::env::temp_dir().join(format!("mc-import-{}.png", std::process::id()));
|
||||
std::fs::write(&path, png).unwrap();
|
||||
let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default());
|
||||
let base = app.session.history().len();
|
||||
crate::graph_view::import_bitmap_file(&mut app, &path.display().to_string());
|
||||
std::fs::remove_file(&path).ok();
|
||||
assert_eq!(app.session.history().len(), base + 1, "history: {:?}", app.session.history());
|
||||
assert_eq!(app.session.history().last().map(String::as_str), Some("Import bitmap"));
|
||||
app.session.undo();
|
||||
assert_eq!(app.session.history().len(), base);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user