diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rs index e74b7845194..81b0fdb8b9c 100644 --- a/crates/engine/src/lib.rs +++ b/crates/engine/src/lib.rs @@ -216,6 +216,10 @@ pub struct Session { active_generation: u64, /// The document id [`Self::active_generation`] was last bumped for. active_doc_for_generation: Option, + /// Counts the [`Session::execute`] calls that changed the active document or layer (selection + /// commands, Action playback, undo, a canvas pick). A caller that remembers how a change came + /// about compares it to see that nothing changed the active layer since. + active_layer_changes: u64, pub tools: ToolState, /// Log of executed commands (for action recording and debugging). pub journal: Vec<(String, Value)>, diff --git a/crates/engine/src/session/documents.rs b/crates/engine/src/session/documents.rs index 3e6bc4abc71..ead3894e48a 100644 --- a/crates/engine/src/session/documents.rs +++ b/crates/engine/src/session/documents.rs @@ -43,6 +43,11 @@ impl Session { self.active_generation } + /// Count of [`Self::execute`] calls that changed the active document or layer; see the field. + pub fn active_layer_changes(&self) -> u64 { + self.active_layer_changes + } + /// Bump [`Self::active_generation`] when the active document id changed. Call after every /// assignment to `self.active`. pub(crate) fn note_active_change(&mut self) { diff --git a/crates/engine/src/session/execute.rs b/crates/engine/src/session/execute.rs index 3577f5e1df4..53212bce3da 100644 --- a/crates/engine/src/session/execute.rs +++ b/crates/engine/src/session/execute.rs @@ -15,7 +15,12 @@ impl Session { /// Runs synchronously, including job-capable commands (see [`jobs`]; [`Session::start`] runs /// those in the background). pub fn execute(&mut self, id: &str, params: Value) -> Result { - match self.dispatch(id, params, false)? { + let before = self.active().map(|st| (st.doc.id, st.active_layer)); + let result = self.dispatch(id, params, false); + if self.active().map(|st| (st.doc.id, st.active_layer)) != before { + self.active_layer_changes = self.active_layer_changes.wrapping_add(1); + } + match result? { jobs::Started::Done(v) => Ok(v), // Not reached: inline dispatch never starts a job. Waiting is still correct. jobs::Started::Job(j) => self.wait_job(j), diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 599d0ce2d6f..3cf1950b55c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -1986,3 +1986,21 @@ fn the_document_activation_generation_bumps_on_switch_and_back() { assert_eq!(s.active_generation(), g3); assert_eq!(s.active().unwrap().doc.id, first); } + +#[test] +fn the_active_layer_change_count_moves_only_when_a_command_changes_the_active_layer() { + let mut s = session_with_doc(); + s.execute("layer.new.layer", json!({})).unwrap(); + let a = s.active().unwrap().active_layer.unwrap(); + s.execute("layer.new.layer", json!({})).unwrap(); + let b = s.active().unwrap().active_layer.unwrap(); + let n = s.active_layer_changes(); + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + assert_eq!(s.active_layer_changes(), n, "selecting the active layer again changes nothing"); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + assert!(s.active_layer_changes() > n); + let m = s.active_layer_changes(); + s.execute("layer.select", json!({"layer": b.0})).unwrap(); + s.execute("layer.select", json!({"layer": a.0})).unwrap(); + assert!(s.active_layer_changes() >= m + 2, "a round trip back to the same layer still counts"); +} diff --git a/crates/ui-egui/src/canvas/tool_input.rs b/crates/ui-egui/src/canvas/tool_input.rs index 5db1cad9085..f6e709133d3 100644 --- a/crates/ui-egui/src/canvas/tool_input.rs +++ b/crates/ui-egui/src/canvas/tool_input.rs @@ -212,12 +212,13 @@ fn filter_event_before_tool(app: &mut PhotocraftApp, ev: ToolEvent, mods: egui:: Some((raw, ev, mods)) } -/// Remember a Move-way pick that changed the active layer (with this frame), so the Layers panel's +/// Remember a Move-way pick that changed the active layer (with the engine's change count after it, so +/// any later change invalidates the marker), so the Layers panel's /// reveal can honour Expand layer group on click (layer_reveal.rs). fn note_move_pick(app: &mut PhotocraftApp, active_before: Option) { let active_after = app.session.active().and_then(|st| st.active_layer); if active_after != active_before { - app.move_pick = active_after.map(|id| (id, app.frame)); + app.move_pick = active_after.map(|id| (id, app.session.active_layer_changes())); } } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 8759efe987a..fed3e7f69da 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -429,8 +429,9 @@ pub struct PhotocraftApp { /// A ⌘⌥⌃-click layer pick is in progress; its drag and release are swallowed (`quick_pick`). pub(crate) quick_pick: bool, /// The layer a canvas click picked the Move tool way (its Auto-Select, or a ⌘-click from another - /// tool) and the frame (`frame`) of the pick, until the Layers panel's reveal reads it. The - /// reveal accepts it in that frame or the next only (`layer_reveal`, #11381). + /// tool) and the engine's active-layer change count right after that pick + /// (`Session::active_layer_changes`). The Layers panel's reveal reads it when it next draws, and + /// accepts it only if nothing changed the active layer since, however long ago (`layer_reveal`, #11381). pub(crate) move_pick: Option<(photocraft_doc::LayerId, u64)>, /// The next tool `Down` is an Alt+right-drag that resizes the brush (#297). `tool_event` /// takes it on every event, so a press another handler consumes can't leave it set. diff --git a/crates/ui-egui/src/move_bar_distribute_gear_tests.rs b/crates/ui-egui/src/move_bar_distribute_gear_tests.rs index 1bc5b5c76f7..1026009b09c 100644 --- a/crates/ui-egui/src/move_bar_distribute_gear_tests.rs +++ b/crates/ui-egui/src/move_bar_distribute_gear_tests.rs @@ -389,13 +389,39 @@ fn a_pick_marker_the_hidden_layers_panel_never_read_does_not_hide_a_later_comman } #[test] -fn a_pick_marker_is_read_in_its_frame_and_the_next() { +fn a_pick_marker_survives_any_number_of_frames_while_the_layers_panel_is_hidden() { + let (mut app, inner, group, ctx) = closed_group_doc(Tool::Brush, false); + app.ui.tool_options.move_auto_select = false; + ctrl_click(&mut app); + app.frame += 5; + assert_eq!(crate::layer_reveal::track(&mut app, &ctx), Some(inner)); + assert!(!expanded(&app, group), "the pick is still the latest change of the active layer"); +} + +#[test] +fn commands_after_a_pick_in_the_same_frame_or_the_next_make_it_a_command_change() { for gap in [0, 1] { let (mut app, inner, group, ctx) = closed_group_doc(Tool::Brush, false); app.ui.tool_options.move_auto_select = false; ctrl_click(&mut app); app.frame += gap; + app.run("layer.select", json!({"layer": group.0})).unwrap(); + app.run("layer.select", json!({"layer": inner.0})).unwrap(); assert_eq!(crate::layer_reveal::track(&mut app, &ctx), Some(inner)); - assert!(!expanded(&app, group), "a pick {gap} frame(s) old still governs the reveal"); + assert!(expanded(&app, group), "selects {gap} frame(s) after the pick reveal the layer"); } } + +#[test] +fn a_pick_marker_is_read_once_and_governs_the_next_change_only() { + let (mut app, inner, group, ctx) = closed_group_doc(Tool::Brush, false); + app.ui.tool_options.move_auto_select = false; + ctrl_click(&mut app); + assert_eq!(crate::layer_reveal::track(&mut app, &ctx), Some(inner)); + assert!(!expanded(&app, group)); + app.run("layer.select", json!({"layer": group.0})).unwrap(); + assert_eq!(crate::layer_reveal::track(&mut app, &ctx), Some(group)); + app.run("layer.select", json!({"layer": inner.0})).unwrap(); + assert_eq!(crate::layer_reveal::track(&mut app, &ctx), Some(inner)); + assert!(expanded(&app, group), "a later command change opens the group"); +} diff --git a/crates/ui-egui/src/panels/layer_reveal.rs b/crates/ui-egui/src/panels/layer_reveal.rs index 737fb7cfc21..9ae5eb01cb8 100644 --- a/crates/ui-egui/src/panels/layer_reveal.rs +++ b/crates/ui-egui/src/panels/layer_reveal.rs @@ -27,9 +27,11 @@ pub fn track(app: &mut PhotocraftApp, ctx: &egui::Context) -> Option { let index = app.session.active_index()?; let active = app.session.active()?.active_layer; let now = Seen(index, active); - // The canvas sets the marker in its frame and the panel may draw before or after it, so it reads the - // marker in that frame or the next. An older one is a pick the hidden panel never saw: discard. - let pick = app.move_pick.take().filter(|&(_, frame)| frame + 1 >= app.frame).map(|(id, _)| id); + // The marker says the latest active-layer change was a Move-way pick of that layer: it holds only + // while no later change happened (the engine's change count is unchanged), however many frames a + // hidden panel missed, and whether or not the final layer is the same id. + let changes = app.session.active_layer_changes(); + let pick = app.move_pick.take().filter(|&(id, at)| Some(id) == active && at == changes).map(|(id, _)| id); let before = ctx.data(|d| d.get_temp::(seen_id())); ctx.data_mut(|d| d.insert_temp(seen_id(), now)); match before {