From d1985f586a7be39d870c5108dc4f118bf5964541 Mon Sep 17 00:00:00 2001 From: yoriswind Date: Sun, 8 Nov 2026 04:38:54 -0800 Subject: [PATCH] Route Edit Undo to the most recent document or Actions change --- .../cmd/actions_edit_cmds/library_commands.rs | 20 +++++++++++++++++-- crates/ui-egui/src/input/shortcuts/tests.rs | 4 ++++ crates/ui-egui/src/menus/invoke.rs | 2 +- crates/ui-egui/src/menus/items.rs | 2 +- crates/ui-egui/src/menus/native_menu.rs | 2 +- crates/ui-egui/src/menus/state.rs | 4 ++-- crates/ui-egui/src/panels/actions/library.rs | 13 ++++++++++++ crates/ui-egui/src/panels/actions/tests.rs | 7 +++++++ crates/ui-egui/src/tools/crop_ui/tests.rs | 4 +++- crates/ui-egui/src/tools/vector_ui.rs | 5 +++++ crates/ui-egui/src/transform_undo_tests.rs | 4 ++++ 11 files changed, 59 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/cmd/actions_edit_cmds/library_commands.rs b/crates/engine/src/cmd/actions_edit_cmds/library_commands.rs index 7e1f908b4a3..c2dca22d074 100644 --- a/crates/engine/src/cmd/actions_edit_cmds/library_commands.rs +++ b/crates/engine/src/cmd/actions_edit_cmds/library_commands.rs @@ -16,9 +16,11 @@ mod tests; type Shortcuts = BTreeMap; type Alternates = BTreeMap>; +/// Snapshot of the definitions and shortcut bindings changed by one library edit. #[derive(Clone, Debug, PartialEq)] pub struct LibraryUndo { label: &'static str, + document_history: Vec<(photocraft_doc::DocId, u64, bool)>, list: Vec, sets: Vec, rows: Vec, @@ -97,8 +99,14 @@ pub(crate) fn publish_library(s: &mut Session, list: Vec, sets: Vec (shortcuts.clone(), alternates.clone()), }; + let document_history = s.documents().iter().map(|st| (st.doc.id, st.history.row_ids().max().unwrap_or(0), st.history.is_cleared())).collect(); + // A document edit after a library command must start a new undo step. + for st in &mut s.docs { + st.coalesce = None; + } let undo = LibraryUndo { label, + document_history, list: std::mem::replace(&mut s.actions.list, list), sets: std::mem::replace(&mut s.actions.sets, sets), rows: std::mem::replace(&mut s.actions.set_rows, rows), @@ -153,9 +161,17 @@ fn undo_state(s: &Session) -> Result<&LibraryUndo> { Ok(undo) } +/// Label for Edit Undo when the latest library change precedes no undoable document edit. pub fn undo_library_label(s: &Session) -> Option<&'static str> { - s.actions.library_undo.as_ref()?; - undo_state(s).ok().map(|undo| undo.label) + let undo = undo_state(s).ok()?; + if let Some(st) = s.active().filter(|st| st.history.can_undo()) { + let (_, last_id, was_cleared) = undo.document_history.iter().find(|(id, _, _)| *id == st.doc.id).copied().unwrap_or((st.doc.id, 0, false)); + let current_id = st.history.row_id(st.history.past_len()).unwrap_or(0); + if current_id > last_id || (st.history.is_cleared() && !was_cleared) { + return None; + } + } + Some(undo.label) } pub(super) fn undo_enabled(s: &Session) -> std::result::Result<(), String> { diff --git a/crates/ui-egui/src/input/shortcuts/tests.rs b/crates/ui-egui/src/input/shortcuts/tests.rs index fd68edff51d..c397ecf0326 100644 --- a/crates/ui-egui/src/input/shortcuts/tests.rs +++ b/crates/ui-egui/src/input/shortcuts/tests.rs @@ -157,6 +157,7 @@ fn pen_custom_undo_binding_and_menu_retract_pending_points() { crate::vector_ui::pen_down(&mut app, x, y, false); crate::vector_ui::pen_up(&mut app); } + app.run("actions.reset", serde_json::json!({"confirm":true,"mode":"append"})).unwrap(); app.session.prefs.edit(|p| p.shortcuts.insert("edit.undo".into(), "Cmd+Shift+Y".into())); let ctx = egui::Context::default(); let press = |app: &mut PhotocraftApp, key: Key, modifiers: Modifiers| { @@ -173,8 +174,11 @@ fn pen_custom_undo_binding_and_menu_retract_pending_points() { press(&mut app, Key::Y, Modifiers::COMMAND | Modifiers::SHIFT); assert_eq!(app.ui.pen.as_ref().unwrap().knots.len(), 1); assert!(crate::menus::is_enabled(&app, "edit.undo")); + let undo_label = |app: &PhotocraftApp| crate::menus::menu_items(app).into_iter().find(|item| item.id == "edit.undo").unwrap().label; + assert_ne!(undo_label(&app), "Undo Reset Actions"); crate::menus::invoke(&mut app, &ctx, "edit.undo", serde_json::json!({})).unwrap(); assert!(app.ui.pen.is_none()); + assert_eq!(undo_label(&app), "Undo Reset Actions"); } /// A 200×200 document for the ⌘⇧↩ / ⌘⌥↩ tests. diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 511dd304efe..444fd741141 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -100,7 +100,7 @@ fn invoke_early_commands(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, if id == "edit.undo" && crate::vector_ui::pen_undo_last_point(app) { return Some(Ok(Value::Null)); } - if id == "edit.undo" && photocraft_engine::actions_edit_cmds::undo_library_label(&app.session).is_some() { + if id == "edit.undo" && crate::actions::library::edit_undo_label(app).is_some() { return Some(crate::actions::library::undo(app)); } // Image › Crop while the Crop tool has a pending frame commits that frame (#1918). diff --git a/crates/ui-egui/src/menus/items.rs b/crates/ui-egui/src/menus/items.rs index 5e274e39d73..a98a3f4e6ba 100644 --- a/crates/ui-egui/src/menus/items.rs +++ b/crates/ui-egui/src/menus/items.rs @@ -241,7 +241,7 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec { item.label = "Release Clipping Mask".into(); item.enabled = is_enabled(app, &item.id); } - if let Some(label) = photocraft_engine::actions_edit_cmds::undo_library_label(&app.session) + if let Some(label) = crate::actions::library::edit_undo_label(app) && let Some(item) = items.iter_mut().find(|item| item.id == "edit.undo") { item.label = label.into(); diff --git a/crates/ui-egui/src/menus/native_menu.rs b/crates/ui-egui/src/menus/native_menu.rs index 1deb857b36d..e6c3663caf0 100644 --- a/crates/ui-egui/src/menus/native_menu.rs +++ b/crates/ui-egui/src/menus/native_menu.rs @@ -755,7 +755,7 @@ pub(crate) fn state_hash(app: &PhotocraftApp, input: Option) -> u64 { s.clipboard.is_some().hash(&mut h); // A running job greys the commands that would change its document. s.has_jobs().hash(&mut h); - photocraft_engine::actions_edit_cmds::undo_library_label(s).hash(&mut h); + (crate::actions::library::edit_undo_label(app), crate::vector_ui::pen_can_undo(app)).hash(&mut h); if let Some(d) = s.active() { (d.doc.id.0, d.revision, d.active_layer.map(|l| l.0), d.selected_layers.len(), d.isolated_layers.len()).hash(&mut h); } diff --git a/crates/ui-egui/src/menus/state.rs b/crates/ui-egui/src/menus/state.rs index 52d818415c4..1f401c2bd43 100644 --- a/crates/ui-egui/src/menus/state.rs +++ b/crates/ui-egui/src/menus/state.rs @@ -13,10 +13,10 @@ pub fn is_enabled(app: &PhotocraftApp, id: &str) -> bool { return crate::symmetry_ui::can_transform(app); } // Draft Pen points can be undone even when the document has no History entries. - if id == "edit.undo" && app.ui.pen.as_ref().is_some_and(|pen| !pen.knots.is_empty()) { + if id == "edit.undo" && crate::vector_ui::pen_can_undo(app) { return true; } - if id == "edit.undo" && photocraft_engine::actions_edit_cmds::undo_library_label(&app.session).is_some() { + if id == "edit.undo" && crate::actions::library::edit_undo_label(app).is_some() { return true; } // Photoshop greys these for the Background layer, other layer kinds or single-layer documents. diff --git a/crates/ui-egui/src/panels/actions/library.rs b/crates/ui-egui/src/panels/actions/library.rs index 88454aeeb7c..21521756cdf 100644 --- a/crates/ui-egui/src/panels/actions/library.rs +++ b/crates/ui-egui/src/panels/actions/library.rs @@ -237,6 +237,19 @@ pub(crate) fn cancel(app: &mut PhotocraftApp, id: u64) { } } +/// The library label is shown only when Edit Undo will reach the library. +pub(crate) fn edit_undo_label(app: &PhotocraftApp) -> Option<&'static str> { + if crate::vector_ui::pen_can_undo(app) + || crate::transform_tool::is_enabled(app, "edit.undo").is_some() + || crate::crop_ui::is_enabled(app, "edit.undo").is_some() + || idle(app, "actions.undoLibrary").is_err() + { + return None; + } + photocraft_engine::actions_edit_cmds::undo_library_label(&app.session) +} + +/// Route an eligible Edit Undo through normal command authorization. pub(crate) fn undo(app: &mut PhotocraftApp) -> Result { app.run("actions.undoLibrary", json!({})) } diff --git a/crates/ui-egui/src/panels/actions/tests.rs b/crates/ui-egui/src/panels/actions/tests.rs index f0809869537..ee1acdbeaf1 100644 --- a/crates/ui-egui/src/panels/actions/tests.rs +++ b/crates/ui-egui/src/panels/actions/tests.rs @@ -19,6 +19,13 @@ fn library_dialog_cancel_stale_and_undo_leave_document_history_untouched() { crate::dialogs::confirm(&mut app, id).unwrap(); assert!(app.session.actions.sets.is_empty()); let ctx = egui::Context::default(); + assert_eq!(library::edit_undo_label(&app), Some("Undo Clear All Actions")); + app.run("layer.new.layer", json!({})).unwrap(); + assert!(library::edit_undo_label(&app).is_none()); + crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().history.past_len(), history); + assert!(app.session.actions.sets.is_empty(), "the newer document edit was undone first"); + assert_eq!(library::edit_undo_label(&app), Some("Undo Clear All Actions")); crate::menus::invoke(&mut app, &ctx, "edit.undo", json!({})).unwrap(); assert_eq!(app.session.actions.sets, ["Keep", "Newer"]); assert_eq!(app.session.active().unwrap().history.past_len(), history); diff --git a/crates/ui-egui/src/tools/crop_ui/tests.rs b/crates/ui-egui/src/tools/crop_ui/tests.rs index 93e2de25560..cf078404310 100644 --- a/crates/ui-egui/src/tools/crop_ui/tests.rs +++ b/crates/ui-egui/src/tools/crop_ui/tests.rs @@ -522,6 +522,7 @@ fn a_click_on_the_frame_or_a_handle_never_snaps_it() { #[test] fn edit_undo_steps_the_crop_box_not_the_document() { let mut app = app(SampleType::U8); + app.run("actions.reset", json!({"confirm":true,"mode":"append"})).unwrap(); app.ui.crop_rect = Some([40.0, 30.0, 100.0, 70.0]); app.crop.default_frame = false; let before = app.ui.crop_rect.unwrap(); @@ -529,7 +530,8 @@ fn edit_undo_steps_the_crop_box_not_the_document() { drag(&mut app, &[[70.0, 50.0], [90.0, 50.0]], NONE); // move the frame right 20 let moved = app.ui.crop_rect.unwrap(); assert!((moved[0] - 60.0).abs() < 1e-3, "{moved:?}"); - app.run("edit.undo", json!({})).unwrap(); + crate::menus::invoke(&mut app, &egui::Context::default(), "edit.undo", json!({})).unwrap(); + assert_eq!(app.session.actions.set_rows.len(), 1, "the library edit remains available"); assert_eq!(app.ui.crop_rect, Some(before), "undo restores the box"); assert_eq!(app.session.active().unwrap().history.past_len(), doc_steps, "the document is untouched"); app.run("edit.redo", json!({})).unwrap(); diff --git a/crates/ui-egui/src/tools/vector_ui.rs b/crates/ui-egui/src/tools/vector_ui.rs index bbd674c4f4c..ab75210606f 100644 --- a/crates/ui-egui/src/tools/vector_ui.rs +++ b/crates/ui-egui/src/tools/vector_ui.rs @@ -297,6 +297,11 @@ use path_ops::{selected_document_geometry, targeted_vector_mask_id}; pub(crate) use pen::{finish_pen_if_left, freeform_finish_owned}; use shapes::{fill_param, stroke_param}; +/// Whether Edit Undo currently belongs to a live Pen draft. +pub(crate) fn pen_can_undo(app: &PhotocraftApp) -> bool { + app.ui.pen.as_ref().is_some_and(|pen| pen.owner.as_ref().is_some_and(|owner| owner.valid(app)) && (!pen.knots.is_empty() || pen.resumes.is_some())) +} + /// Commit a valid source-owned draft on a tab switch; otherwise cancel with an actionable diagnostic. pub(crate) fn cancel_stale_pen(app: &mut PhotocraftApp) -> bool { let Some(pen) = app.ui.pen.as_ref() else { return false }; diff --git a/crates/ui-egui/src/transform_undo_tests.rs b/crates/ui-egui/src/transform_undo_tests.rs index b863eb8fffa..a4a4aafd324 100644 --- a/crates/ui-egui/src/transform_undo_tests.rs +++ b/crates/ui-egui/src/transform_undo_tests.rs @@ -117,6 +117,7 @@ fn free_transform_starts_where_the_selected_pixels_were_moved() { #[test] fn undo_steps_back_through_the_transform_not_the_document() { let mut h = harness(); + h.state_mut().run("actions.reset", json!({"confirm":true,"mode":"append"})).unwrap(); let steps = history(&h); begin(&mut h); let q0 = quad(&h); @@ -153,6 +154,9 @@ fn undo_steps_back_through_the_transform_not_the_document() { // Outside the transform, ⌘Z is the document's Undo again. key(&mut h, Modifiers::COMMAND, Key::Z); assert_eq!(width(&h), 100, "the committed transform is undone"); + assert_eq!(h.state().session.actions.set_rows.len(), 1); + key(&mut h, Modifiers::COMMAND, Key::Z); + assert!(h.state().session.actions.set_rows.is_empty(), "the preceding library edit is next"); } /// `engine.execute` over the control channel (and MCP, which goes through it) as a programmatic