Route Undo and Redo to the Content-Aware Fill workspace

This commit is contained in:
yoriswind committed 2026-11-30 13:12:25 -08:00
1 parent 1553cd3c9e
commit 1d96e361a5
4 files changed
+58 -1

No files matched your search

+4
View File
@@ -51,6 +51,10 @@ const CANCELLED: &str = "cancelled";
/// The hosted workspace as the dialog host stores it.
pub(crate) type Hosted = HostedWorkspace<ContentAwareFillWorkspace>;
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.
@@ -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<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default());
@@ -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);
}
+6 -1
View File
@@ -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<Vec<CommandRef>>,
fill_history: bool,
}
/// [`command_gate`]'s test for one command, for callers that hold no gate.