Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(type): keep the layer just selected when ending a Type Mask (#6483 review)

Khawar Zamman Wani444ee06e1a84
diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs
index b8d6167c3..d382310fe 100644
--- a/crates/engine/src/lib.rs
+++ b/crates/engine/src/lib.rs
@@ -867,6 +867,31 @@ impl Session {
Ok(())
}
+ /// Put back a layer selection (active layer, selected layers, anchor) that a step moved, such
+ /// as the undo ending an empty Type Mask, which restores the selection from before it. `None`
+ /// is no layer selected. Selected layers that are gone are dropped; an active layer that is
+ /// gone is an error and changes nothing. Like [`Self::select_layer`], not an edit.
+ pub fn restore_layer_selection(&mut self, active: Option<LayerId>, selected: &[LayerId], anchor: Option<LayerId>) -> Result<()> {
+ let st = self.active_mut().ok_or(EngineError::NoDocument)?;
+ if let Some(id) = active
+ && st.doc.layer(id).is_none()
+ {
+ return Err(EngineError::NoLayer(id));
+ }
+ st.active_layer = active;
+ st.selected_layers = selected.to_vec();
+ st.layer_anchor = anchor.filter(|_| active.is_some());
+ fix_selection(st);
+ channel_cmds::fix_view(st);
+ let clean = st.saved_revision == st.revision;
+ st.revision += 1;
+ if clean {
+ st.saved_revision = st.revision;
+ }
+ st.last_damage = Some(photocraft_geom::Rect::EMPTY);
+ Ok(())
+ }
+
/// Re-renders the pixels of type layers whose font arrived after they were drawn (the web
/// build fetches served fonts on demand, `photocraft_text::served`). The layers' model is
/// unchanged, so this is no history step and leaves a clean document clean. Unknown documents
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 04063d307..934471164 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -1580,3 +1580,25 @@ fn merge_down_on_a_group_merges_the_group() {
s.execute("edit.undo", json!({})).unwrap();
assert!(s.active().unwrap().doc.layer(photocraft_doc::LayerId(group)).is_some());
}
+
+/// `restore_layer_selection` puts a multi-layer selection back, drops gone layers, keeps a clean
+/// document clean, and refuses a gone active layer without changing anything.
+#[test]
+fn restore_layer_selection_puts_back_what_still_exists() {
+ let mut s = session_with_doc();
+ let id = |v: Value| LayerId(v["layer"].as_u64().unwrap());
+ let a = id(s.execute("layer.new.layer", json!({})).unwrap());
+ let b = id(s.execute("layer.new.layer", json!({})).unwrap());
+ let gone = LayerId(u64::MAX);
+ s.active_mut().unwrap().saved_revision = s.active().unwrap().revision;
+ s.restore_layer_selection(Some(b), &[a, gone, b], Some(a)).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!((st.active_layer, st.selected_layers(), st.layer_anchor), (Some(b), vec![a, b], Some(a)));
+ assert_eq!(st.saved_revision, st.revision, "selection is not an edit");
+ assert!(matches!(s.restore_layer_selection(Some(gone), &[gone], None), Err(EngineError::NoLayer(_))));
+ assert_eq!(s.active().unwrap().active_layer, Some(b), "an error changes nothing");
+ s.restore_layer_selection(None, &[a], Some(a)).unwrap();
+ let st = s.active().unwrap();
+ assert_eq!((st.active_layer, st.selected_layers(), st.layer_anchor), (None, vec![], None), "no layer selected");
+ assert!(Session::new().restore_layer_selection(None, &[], None).is_err(), "no document");
+}
diff --git a/crates/ui-egui/src/type_commit_tests.rs b/crates/ui-egui/src/type_commit_tests.rs
index 551706ec0..e25fc3397 100644
--- a/crates/ui-egui/src/type_commit_tests.rs
+++ b/crates/ui-egui/src/type_commit_tests.rs
@@ -155,3 +155,31 @@ fn tool_shortcuts_work_after_another_layer_is_selected() {
assert_eq!(h.state().ui.tool, crate::state::Tool::Move, "V picks the Move tool");
assert_eq!(text_of(h.state(), 0, text).as_deref(), Some("Hello"), "and types nothing");
}
+
+/// Ending an empty Type Mask undoes its session, and undo restores the layer selection from
+/// before it: the layer just selected (or Deselect Layers) must stand. So must it when a Type
+/// Mask with text becomes the selection and its working layer goes.
+#[test]
+fn ending_a_type_mask_keeps_the_new_layer_selection() {
+ for (deselect, text) in [(false, ""), (true, ""), (false, "Mask"), (true, "Mask")] {
+ let mut app = new_app();
+ let first = LayerId(app.run("layer.new.layer", json!({"name": "First"})).unwrap()["layer"].as_u64().unwrap());
+ let second = LayerId(app.run("layer.new.layer", json!({"name": "Second"})).unwrap()["layer"].as_u64().unwrap());
+ app.run("layer.select", json!({"layer": first.0})).unwrap();
+ app.ui.tool = crate::state::Tool::HorizontalTypeMask;
+ let mask = type_new(&mut app, text);
+ if deselect {
+ app.run("select.deselectLayers", json!({})).unwrap();
+ } else {
+ app.run("layer.select", json!({"layer": second.0})).unwrap();
+ }
+ super::end_if_left(&mut app);
+ assert!(app.ui.text_edit.is_none());
+ let st = app.session.active().unwrap();
+ assert!(st.doc.layer(mask).is_none(), "{text:?}: the mask's working layer is gone");
+ assert_eq!(st.doc.selection.is_some(), !text.is_empty(), "{text:?}: text becomes the selection");
+ let want = if deselect { None } else { Some(second) };
+ assert_eq!(st.active_layer, want, "{text:?}, deselect={deselect}: the new selection stands");
+ assert_eq!(st.selected_layers(), want.into_iter().collect::<Vec<_>>(), "{text:?}, deselect={deselect}");
+ }
+}
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index 1bed24c2c..2fe19a1ec 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -1083,9 +1083,21 @@ fn commit_here(app: &mut PhotocraftApp, mods: egui::Modifiers) {
/// shortcuts as typing and typed into whichever document was active.
pub fn end_if_left(app: &mut PhotocraftApp) {
let Some(ed) = app.ui.text_edit.as_ref() else { return };
- let here = edit_home(app, ed).is_some_and(|h| Some(h) == app.session.active_index());
- if !here || app.session.active().is_some_and(|st| st.active_layer != Some(LayerId(ed.layer))) {
- commit(app);
+ let layer = LayerId(ed.layer);
+ if edit_home(app, ed).is_none_or(|h| Some(h) != app.session.active_index()) {
+ return commit(app);
+ }
+ let Some(st) = app.session.active() else { return };
+ if st.active_layer == Some(layer) {
+ return;
+ }
+ // The selection just made stands: ending an empty Type Mask undoes its session, and undo
+ // restores the selection from before it.
+ let chosen = (st.active_layer, st.selected_layers.clone(), st.layer_anchor);
+ commit(app);
+ let moved = app.session.active().is_some_and(|st| (st.active_layer, &st.selected_layers, st.layer_anchor) != (chosen.0, &chosen.1, chosen.2));
+ if moved {
+ let _ = app.session.restore_layer_selection(chosen.0, &chosen.1, chosen.2);
}
}