Keyboard Shortcuts: one Undo button that steps the last key change and turns into Redo (#14314)

Main's dialog is craft-libs' shared one; craft-libs#4263 gave it
Options::undo = UndoButtons::Toggle, which PhotoCraft now turns on. The test
records a key over Copy's in the detail pane, then steps it with the one
button: Undo gives the key back and the button reads Redo, Redo puts it back.
This commit is contained in:
David Mann committed 2026-11-29 21:13:01 +01:00
1 parent 9f8344e0a8
commit 61da85d1fa
3 files changed
+74 -2

No files matched your search

+2
View File
@@ -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;
@@ -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<PhotocraftApp>>, name: &str) -> Vec<bool> {
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<PhotocraftApp>>, 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<Binding> {
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");
}
@@ -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;
})
}