diff --git a/crates/ui-egui/src/prefs/prefs_ui.rs b/crates/ui-egui/src/prefs/prefs_ui.rs index e88daaeb48..d3a674bc80 100644 --- a/crates/ui-egui/src/prefs/prefs_ui.rs +++ b/crates/ui-egui/src/prefs/prefs_ui.rs @@ -85,6 +85,8 @@ mod recovery_tests; mod shortcut_capture_tests; #[cfg(test)] mod shortcut_set_tests; +#[cfg(test)] +mod shortcut_undo_tests; #[cfg(test)] mod tests; diff --git a/crates/ui-egui/src/prefs/prefs_ui/shortcut_undo_tests.rs b/crates/ui-egui/src/prefs/prefs_ui/shortcut_undo_tests.rs new file mode 100644 index 0000000000..1fecb5b937 --- /dev/null +++ b/crates/ui-egui/src/prefs/prefs_ui/shortcut_undo_tests.rs @@ -0,0 +1,67 @@ +//! #14314: Edit › Keyboard Shortcuts has one Undo button. It steps the last committed key change back, then reads +//! Redo and steps it forward again. How the button follows recording and conflicts is craft-menu's own (its +//! `undo_toggle` trials); this drives PhotoCraft's dialog through it. + +use craft::keymap::{Binding, CommandRef}; +use egui::accesskit::Role; +use egui::{Key, Modifiers}; +use egui_kittest::Harness; +use egui_kittest::kittest::{NodeT, Queryable}; + +use super::shortcut_capture_tests::{harness, tap}; +use crate::PhotocraftApp; + +/// Whether each footer button named `name` can be clicked. +fn buttons(h: &Harness<'_, Box>, name: &str) -> Vec { + h.query_all_by_role(Role::Button) + .filter(|node| node.accesskit_node().label().as_deref() == Some(name)) + .map(|node| !node.accesskit_node().is_disabled()) + .collect() +} + +/// Clicks the one button named `name`. +fn click(h: &mut Harness<'_, Box>, name: &str) { + let button = h + .query_all_by_role(Role::Button) + .find(|node| node.accesskit_node().label().as_deref() == Some(name)) + .unwrap_or_else(|| panic!("a button named {name}")); + button.click(); + h.run_steps(2); +} + +fn copy_keys(app: &PhotocraftApp) -> Vec { + app.prefs_rt.shortcuts.as_ref().expect("the dialog is open").keymap.bindings(&CommandRef::new("edit.copy")) +} + +#[test] +fn one_undo_button_steps_the_last_key_change_and_turns_into_redo() { + let mut h = harness(); + crate::prefs_ui::open_shortcuts(h.state_mut(), 0); + h.run_steps(2); + assert_eq!(buttons(&h, "Undo"), [false], "one Undo button, disabled when the dialog opens"); + assert!(buttons(&h, "Redo").is_empty(), "no Redo button beside it"); + + // Record Shift+F11, a key no command has, over Copy's key in the detail pane: a committed change. + let before = copy_keys(h.state()); + h.state_mut().prefs_rt.shortcuts.as_mut().expect("the dialog is open").state.select(&CommandRef::new("edit.copy")); + h.run_steps(2); + let replace = h + .query_all_by_label_contains(" for Copy") + .find(|node| node.accesskit_node().label().is_some_and(|label| label.starts_with("Replace ") && label.ends_with(" for Copy"))) + .expect("the detail pane offers to replace Copy's key"); + replace.click(); + h.run_steps(2); + tap(&mut h, Key::F11, Modifiers::SHIFT); + let key = Binding::parse("Shift+F11").expect("a key"); + assert!(copy_keys(h.state()).contains(&key), "Shift+F11 replaced Copy's key: {:?}", copy_keys(h.state())); + assert_eq!(buttons(&h, "Undo"), [true], "the change can be undone"); + + click(&mut h, "Undo"); + assert_eq!(copy_keys(h.state()), before, "Undo gives Copy its key back"); + assert!(buttons(&h, "Undo").is_empty(), "the button no longer reads Undo"); + assert_eq!(buttons(&h, "Redo"), [true], "it reads Redo"); + + click(&mut h, "Redo"); + assert!(copy_keys(h.state()).contains(&key), "Redo puts Shift+F11 back"); + assert_eq!(buttons(&h, "Undo"), [true], "and the button reads Undo again"); +} diff --git a/crates/ui-egui/src/prefs/prefs_ui/shortcuts.rs b/crates/ui-egui/src/prefs/prefs_ui/shortcuts.rs index d5c1c10faf..8b3ca59b42 100644 --- a/crates/ui-egui/src/prefs/prefs_ui/shortcuts.rs +++ b/crates/ui-egui/src/prefs/prefs_ui/shortcuts.rs @@ -14,7 +14,9 @@ use std::collections::{BTreeMap, BTreeSet}; use craft::keymap::import::kys; use craft::keymap::{Builtins, CommandRef, ImportOptions, Keymap, NoneDefined, Overrides, Profiles, Scopes, reserved}; use craft::menu::Row; -use craft::menu::shortcuts::{Area, ConflictMode, DialogState, DialogTab, Format, Options, Outcome, ShortcutWords, ShortcutsDialog, kys_error_text}; +use craft::menu::shortcuts::{ + Area, ConflictMode, DialogState, DialogTab, Format, Options, Outcome, ShortcutWords, ShortcutsDialog, UndoButtons, kys_error_text, +}; use craft::ui::choice::Checkbox; use craft::ui::contract::{Bind, Entry, Props as _, Show as _, ShowContainer as _}; use craft::ui::display::Text; @@ -175,7 +177,7 @@ pub(crate) fn queue_import(app: &mut PhotocraftApp, text: String) { } /// The dialog's options: Photoshop's Shortcuts For areas, the conflict bar, `.kys` files in and -/// out, and the summary. +/// out, the summary, and one Undo button that steps the last change and turns into Redo (#14314). fn options() -> Options { let kys = Format::new(KYS_FORMAT, "Photoshop Keyboard Shortcuts (.kys)", &["kys", "psp"]); Options::default().with(|o| { @@ -185,6 +187,7 @@ fn options() -> Options { o.import_formats = vec![kys]; o.export_formats = vec![kys]; o.summary = true; + o.undo = UndoButtons::Toggle; }) }