diff --git a/crates/ui-egui/src/dialogs/caf_ui.rs b/crates/ui-egui/src/dialogs/caf_ui.rs index 8cd947511d..3bc23257ae 100644 --- a/crates/ui-egui/src/dialogs/caf_ui.rs +++ b/crates/ui-egui/src/dialogs/caf_ui.rs @@ -51,6 +51,10 @@ const CANCELLED: &str = "cancelled"; /// The hosted workspace as the dialog host stores it. pub(crate) type Hosted = HostedWorkspace; +pub(crate) fn owns_history_shortcuts(app: &PhotocraftApp) -> bool { + app.caf.as_ref().is_some_and(|workspace| app.top_dialog() == Some(workspace.id)) +} + /// The app's side of the open workspace. pub(crate) struct CafApp { /// The hosted workspace's id on the dialog host. diff --git a/crates/ui-egui/src/dialogs/caf_ui/tests.rs b/crates/ui-egui/src/dialogs/caf_ui/tests.rs index de280dfdc2..d8caf4e17d 100644 --- a/crates/ui-egui/src/dialogs/caf_ui/tests.rs +++ b/crates/ui-egui/src/dialogs/caf_ui/tests.rs @@ -8,6 +8,9 @@ use super::preview::{compute_preview, plan_params}; use super::*; use crate::control::dialogs::workspace_ui; +#[path = "undo_keys_tests.rs"] +mod undo_keys_tests; + /// A textured layer (a checker of two colours over a gradient) with a 20 px square selected. fn app_with_selection() -> Box { let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); diff --git a/crates/ui-egui/src/dialogs/caf_ui/undo_keys_tests.rs b/crates/ui-egui/src/dialogs/caf_ui/undo_keys_tests.rs new file mode 100644 index 0000000000..4af3262e98 --- /dev/null +++ b/crates/ui-egui/src/dialogs/caf_ui/undo_keys_tests.rs @@ -0,0 +1,45 @@ +use craft::keymap::CommandRef; +use egui::{Key, Modifiers}; +use serde_json::json; + +use super::{app_with_selection, close_all, facts, open, pass, sampled, ui}; +use crate::canvas::testing::key; + +#[test] +fn undo_redo_keys_step_fill_workspace_history_only() { + let mut app = app_with_selection(); + let before = app.session.active().unwrap().doc.clone(); + let history = app.session.active().unwrap().history.past_len(); + assert!(history > 0, "the document has real undoable edits"); + open(&mut app).unwrap(); + pass(&mut app); + assert!(!crate::menus::allows(&app, &CommandRef::new("edit.undo")), "the document command stays blocked"); + + key(&mut app, Key::Z, Modifiers::COMMAND); + assert_eq!(app.session.active().unwrap().doc, before, "empty workspace history cannot undo a document edit"); + + ui(&mut app, json!({"sampling": "rectangular"})).unwrap(); + let unpainted = sampled(&app); + ui(&mut app, json!({"stroke": {"points": [[20.0, 20.0], [80.0, 20.0]], "size": 10}})).unwrap(); + let painted = sampled(&app); + assert!(painted < unpainted); + assert_eq!(facts(&app)["strokes"], 1); + + key(&mut app, Key::Z, Modifiers::COMMAND); + pass(&mut app); + assert_eq!(facts(&app)["strokes"], 0); + assert_eq!(sampled(&app), unpainted); + + key(&mut app, Key::Z, Modifiers { shift: true, ..Modifiers::COMMAND }); + pass(&mut app); + assert_eq!(facts(&app)["strokes"], 1); + assert_eq!(sampled(&app), painted); + assert_eq!(app.session.active().unwrap().history.past_len(), history); + + ui(&mut app, json!({"cancel": true})).unwrap(); + close_all(&mut app); + assert!(app.caf.is_none()); + let document = app.session.active().unwrap(); + assert_eq!(document.history.past_len(), history); + assert_eq!(document.doc, before); +} diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 651d08eaec..0f14c82a86 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -58,7 +58,11 @@ pub(crate) fn command_gate(app: &PhotocraftApp) -> CommandGate { let (gate, rules) = menu_gate(app); // The refusals follow the rows' rules, so they are derived from the facts the menu model is built from. let refused = craft::ui::runtime::derived::derived(app.ctx(), egui::Id::unique("photocraft-refused-commands"), &facts::MenuFacts::of(app), || refused(app)); - CommandGate::new(GateProps { gate, rules, refused }, |props, command| gated(&props.gate, &props.rules, &props.refused, command)) + let fill_history = crate::caf_ui::owns_history_shortcuts(app) && !crate::prefs_ui::recording(app); + CommandGate::new(GateProps { gate, rules, refused, fill_history }, |props, command| { + // The workspace declares these same IDs. Let the runtime route them to its history while the app menu stays blocked. + (props.fill_history && matches!(command.id.as_str(), "edit.undo" | "edit.redo")) || gated(&props.gate, &props.rules, &props.refused, command) + }) } /// What the runtime's gate reads: the runtime asks it again only when these change. @@ -67,6 +71,7 @@ struct GateProps { gate: Gate, rules: ModalRules, refused: Arc>, + fill_history: bool, } /// [`command_gate`]'s test for one command, for callers that hold no gate.