PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Selections don't mark a document modified (#6865)
Higgs32584e8834e8d938d
diff --git a/crates/engine/src/history_select_cmds.rs b/crates/engine/src/history_select_cmds.rsindex e05a859a5..240972212 100644--- a/crates/engine/src/history_select_cmds.rs+++ b/crates/engine/src/history_select_cmds.rs@@ -59,8 +59,10 @@ fn select_state(s: &mut Session, p: &Value) -> Result<Value> {}let damage = crate::layer_multi_cmds::step_damage(&start, &doc);st.doc = doc;+ // Stepping only over selection changes keeps a clean document clean (#6865).+ let keep_clean = st.keeps_clean(&start);crate::restore_target(st, layers);- st.revision += 1;+ st.bump_revision(keep_clean);st.last_damage = damage;if at != target {// Not reached (the bounds were checked): the document is consistent at the state reached.diff --git a/crates/engine/src/layer_multi_cmds.rs b/crates/engine/src/layer_multi_cmds.rsindex 1b71e966a..f01046db3 100644--- a/crates/engine/src/layer_multi_cmds.rs+++ b/crates/engine/src/layer_multi_cmds.rs@@ -745,6 +745,70 @@ pub fn same_pixels(a: &Document, b: &Document) -> bool {doc_pixels_equal(a, b) && walk(&a.layers, &b.layers, 0)}+/// Whether `a` and `b` differ at most in their selection (#6865): a selection step, which must+/// not mark a clean document modified. Pixels compare by tile pointer ([`same_pixels`], which+/// also turns down video layers, whose own equality is shallow); every other field compares+/// structurally, where copy-on-write `Arc`s of `Eq` data short-circuit on pointer identity. The+/// destructure is exhaustive, so a new `Document` field fails to compile until it is classified.+pub fn selection_only_change(a: &Document, b: &Document) -> bool {+ let Document {+ id,+ name,+ size: _,+ resolution_dpi: _,+ mode: _,+ depth: _,+ icc_profile: _,+ layers,+ channels: _,+ guides,+ ruler_origin,+ selection: _,+ metadata,+ global_light: _,+ paths,+ work_path,+ clipping_path,+ quick_mask: _,+ quick_mask_unselected,+ patterns: _,+ color_table: _,+ duotone: _,+ layer_comps,+ last_applied_comp,+ last_document_state,+ measurement,+ notes,+ text_styles,+ slices,+ variables,+ timeline,+ file_encoding,+ } = a;+ // The ignored fields above are the pixel ones `same_pixels` already compares.+ same_pixels(a, b)+ && *id == b.id+ && *name == b.name+ && *layers == b.layers+ && *guides == b.guides+ && *ruler_origin == b.ruler_origin+ && *metadata == b.metadata+ && *paths == b.paths+ && *work_path == b.work_path+ && *clipping_path == b.clipping_path+ && *quick_mask_unselected == b.quick_mask_unselected+ && *layer_comps == b.layer_comps+ && *last_applied_comp == b.last_applied_comp+ && *last_document_state == b.last_document_state+ && *measurement == b.measurement+ && *notes == b.notes+ && *text_styles == b.text_styles+ && *slices == b.slices+ && *variables == b.variables+ && *timeline == b.timeline+ && *file_encoding == b.file_encoding+}+/// Pixels a single history step can have changed between `a` and `b` (the documents on either/// side of an edit, its undo or its redo): the union of the change bounds of every layer whose/// pixels differ, so a local edit recomposites only that area instead of the whole canvas. Adiff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 22b1e6d72..2c69d977a 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -297,6 +297,21 @@ impl DocState {pub fn is_dirty(&self) -> bool {self.revision != self.saved_revision}+ /// Whether the step from `before` to the current document leaves a clean document clean:+ /// it changed nothing but the selection (#6865). Making, changing or dropping a selection+ /// (marquee, lasso, Magic Wand, Quick / Object Selection, Select › …) is a history step but+ /// not an edit, so it neither marks the file modified nor makes closing ask to save.+ pub(crate) fn keeps_clean(&self, before: &Document) -> bool {+ self.saved_revision == self.revision && layer_multi_cmds::selection_only_change(before, &self.doc)+ }+ /// Move to the next revision; `keep_clean` (from [`Self::keeps_clean`]) moves the saved+ /// revision with it.+ pub(crate) fn bump_revision(&mut self, keep_clean: bool) {+ self.revision += 1;+ if keep_clean {+ self.saved_revision = self.revision;+ }+ }/// The targeted layers, as history stores them with each state.fn layer_target(&self) -> LayerTarget {LayerTarget { active: self.active_layer, selected: self.selected_layers.clone() }@@ -830,6 +845,7 @@ impl Session {// duplicate or a paste), and everything when that can't be bounded. A command may still// report its own, tighter damage after this.let damage = layer_multi_cmds::step_damage(&before, &st.doc);+ let keep_clean = st.keeps_clean(&before);let key = self.coalesce_request.clone();let st = self.active_mut().ok_or(EngineError::NoDocument)?;let layers = st.layer_target();@@ -841,7 +857,7 @@ impl Session {st.history.set_current_layers(layers);}st.coalesce = key;- st.revision += 1;+ st.bump_revision(keep_clean);st.last_damage = damage;self.samplers_removed += samplers_removed;if damage.is_none_or(|d| !d.is_empty()) {@@ -930,9 +946,10 @@ impl Session {// Pixels this step can have touched, so the canvas recomposites only that// (it recomposited everything before).let damage = layer_multi_cmds::step_damage(&d, &st.doc);- st.doc = d;+ let before = std::mem::replace(&mut st.doc, d);+ let keep_clean = st.keeps_clean(&before);restore_target(st, layers);- st.revision += 1;+ st.bump_revision(keep_clean);st.last_damage = damage;true}@@ -952,9 +969,10 @@ impl Session {Arc::make_mut(&mut d).name.clone_from(&st.doc.name);}let damage = layer_multi_cmds::step_damage(&st.doc, &d);- st.doc = d;+ let before = std::mem::replace(&mut st.doc, d);+ let keep_clean = st.keeps_clean(&before);restore_target(st, layers);- st.revision += 1;+ st.bump_revision(keep_clean);st.last_damage = damage;true}diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rsindex 04063d307..6e4c59c51 100644--- a/crates/engine/src/tests.rs+++ b/crates/engine/src/tests.rs@@ -1129,6 +1129,38 @@ fn selecting_a_layer_does_not_dirty_the_document() {assert!(s.active().unwrap().is_dirty());}+#[test]+fn selection_tools_do_not_dirty_the_document() {+ // #6865: marquee, lasso, Select All / Inverse / Deselect and their undo and redo are+ // history steps, but not edits, so a clean document stays clean.+ for depth in [8, 16, 32] {+ let mut s = Session::new();+ s.execute("file.new", json!({"width": 64, "height": 48, "depth": depth})).unwrap();+ let past = s.active().unwrap().history.past_len();+ s.execute("select.rect", json!({"x": 2, "y": 2, "width": 20, "height": 10})).unwrap();+ s.execute("select.rect", json!({"x": 30, "y": 20, "width": 8, "height": 8, "ellipse": true, "mode": "add"})).unwrap();+ s.execute("select.lasso", json!({"points": [[0, 0], [40, 0], [20, 30]], "mode": "subtract"})).unwrap();+ s.execute("select.inverse", json!({})).unwrap();+ s.execute("select.all", json!({})).unwrap();+ s.execute("select.deselect", json!({})).unwrap();+ assert!(s.active().unwrap().history.past_len() > past, "selections are still history steps");+ assert!(!s.active().unwrap().is_dirty(), "depth {depth}: selecting is not an edit");+ assert!(s.undo() && s.undo());+ assert!(s.redo());+ assert!(!s.active().unwrap().is_dirty(), "depth {depth}: undoing a selection is not an edit");+ s.execute("history.selectState", json!({"state": past})).unwrap();+ assert!(!s.active().unwrap().is_dirty(), "depth {depth}: stepping back over selections is not an edit");+ // A real edit still dirties, with or without a selection, and a selection can't clean it.+ s.execute("select.rect", json!({"x": 0, "y": 0, "width": 4, "height": 4})).unwrap();+ s.execute("layer.new.layer", json!({})).unwrap();+ assert!(s.active().unwrap().is_dirty());+ s.execute("select.deselect", json!({})).unwrap();+ assert!(s.active().unwrap().is_dirty());+ assert!(s.undo());+ assert!(s.active().unwrap().is_dirty(), "the new layer is still unsaved");+ }+}+#[test]fn coalesced_edits_share_one_history_step() {let mut s = Session::new();