From aabf759e21255e17c801bfd7095d9ef165160361 Mon Sep 17 00:00:00 2001 From: Rebornim <250083243+Rebornim@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:24:32 +0000 Subject: [PATCH] Make Ctrl+Shift+Z redo as well as Ctrl+Y --- crates/ui-egui/src/keymap.rs | 73 +++++++++++++++++++----- crates/ui-egui/tests/it/shortcut_keys.rs | 15 +++++ 2 files changed, 74 insertions(+), 14 deletions(-) diff --git a/crates/ui-egui/src/keymap.rs b/crates/ui-egui/src/keymap.rs index cdf46547..7518e3f0 100644 --- a/crates/ui-egui/src/keymap.rs +++ b/crates/ui-egui/src/keymap.rs @@ -189,22 +189,38 @@ pub fn format_shortcut(sc: &KeyboardShortcut) -> String { s } -/// Every bound shortcut as egui's shortcut type. -fn active(keymap: &Keymap) -> Vec { - bindings().iter().filter_map(|b| Some(parse(shortcut(keymap, b)?)?.to_egui_shortcut())).collect() +/// Second default keys as (item, shortcut); menus and the shortcut lists show the first only +const SECOND_SHORTCUTS: [(&str, &str); 1] = [("edit.redo", "Ctrl+Shift+Z")]; + +/// The second default key of item `id`, unless the stored shortcuts change the item or use the key +fn second_shortcut(keymap: &Keymap, id: &str) -> Option { + let (_, text) = SECOND_SHORTCUTS.iter().find(|(item, _)| *item == id)?; + let chord = parse(text)?; + + let replaced_or_taken = keymap.contains_key(id) || keymap.values().any(|stored| parse(stored) == Some(chord)); + (!replaced_or_taken).then_some(chord) } -/// The catalog as craft-keymap sees it, in `bindings()` order: the first one listed wins a shared -/// shortcut. -fn catalog() -> Vec { +/// Every bound shortcut as egui's shortcut type +fn active(keymap: &Keymap) -> Vec { bindings() .iter() - .map(|b| { - let item = CommandDef::new(b.id, b.label, Scope::app()); - match b.default.and_then(parse) { - Some(chord) => item.with_default(craft_keymap::Binding::new(chord)), - None => item, - } + .flat_map(|binding| [shortcut(keymap, binding).and_then(parse), second_shortcut(keymap, binding.id)]) + .flatten() + .map(|chord| chord.to_egui_shortcut()) + .collect() +} + +/// The catalog for craft-keymap in `bindings()` order: the first one listed wins a shared shortcut +fn catalog(keymap: &Keymap) -> Vec { + bindings() + .iter() + .map(|binding| { + let defaults = [binding.default.and_then(parse), second_shortcut(keymap, binding.id)]; + defaults + .into_iter() + .flatten() + .fold(CommandDef::new(binding.id, binding.label, Scope::app()), |item, chord| item.with_default(craft_keymap::Binding::new(chord))) }) .collect() } @@ -229,9 +245,9 @@ fn overrides(keymap: &Keymap) -> Overrides { out } -/// The keymap with `keymap`'s shortcuts for `platform`. +/// The keymap with `keymap`'s shortcuts for `platform` fn resolved(keymap: &Keymap, platform: Platform) -> craft_keymap::Keymap { - craft_keymap::Keymap::new(catalog(), Scopes::new(), platform).with_overrides(overrides(keymap)).with_delete_keys(craft_keymap::DeleteKeys::Apart) + craft_keymap::Keymap::new(catalog(keymap), Scopes::new(), platform).with_overrides(overrides(keymap)).with_delete_keys(craft_keymap::DeleteKeys::Apart) } /// The resolved keymap and the dispatcher that runs it against each frame's keys. @@ -720,12 +736,20 @@ mod tests { fn the_browser_leaves_app_shortcuts_alone_but_keeps_the_clipboard() { let mut keymap = Keymap::new(); keymap.insert("file.close".into(), "Alt+W".into()); + // Ctrl+Z also matches a press with Shift, so without Undo only Redo's own key takes Ctrl+Shift+Z + keymap.insert("edit.undo".into(), String::new()); sync_captured(&keymap); + let ctrl = Modifiers { ctrl: true, command: true, ..Modifiers::NONE }; assert!(captured(ctrl, Key::S) && captured(ctrl, Key::O) && captured(ctrl, Key::D)); + assert!(captured(Modifiers { shift: true, ..ctrl }, Key::Z), "the second Redo key is taken over too"); assert!(captured(Modifiers::NONE, Key::F9) && captured(Modifiers::ALT, Key::W)); assert!(!captured(ctrl, Key::C) && !captured(ctrl, Key::V) && !captured(ctrl, Key::X), "clipboard keys reach the browser"); assert!(!captured(Modifiers::NONE, Key::M) && !captured(Modifiers::NONE, Key::F), "plain keys need no capture"); + + keymap.insert("edit.redo".into(), String::new()); + sync_captured(&keymap); + assert!(!captured(Modifiers { shift: true, ..ctrl }, Key::Z), "clearing Redo gives its second key back"); } #[test] @@ -884,6 +908,27 @@ mod tests { assert_eq!(run(Modifiers::SHIFT, Key::M), None, "Shift+M is not bound"); } + #[test] + fn a_second_default_key_yields_to_the_stored_shortcuts() { + let second_press = press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT); + let run = |keymap: &Keymap| bindings_for(keymap).command_of(&second_press); + let mut keymap = Keymap::new(); + assert_eq!(run(&keymap).as_deref(), Some("edit.redo")); + assert_eq!(bindings_for(&keymap).command_of(&press(Key::Y, Modifiers::COMMAND)).as_deref(), Some("edit.redo")); + assert_eq!(shortcut_of(&keymap, "edit.redo"), Some("Ctrl+Y"), "menus keep showing the first key"); + + // Another item the user put on that key keeps it, without a shared-shortcut warning + keymap.insert("file.save".into(), "Ctrl+Shift+Z".into()); + assert_eq!(run(&keymap).as_deref(), Some("file.save")); + assert!(bindings_for(&keymap).conflict(binding("file.save")).is_none()); + + // Changing or clearing Redo replaces both of its keys + for stored in ["F7", ""] { + keymap = Keymap::from([("edit.redo".into(), stored.into())]); + assert_eq!(run(&keymap), None, "Redo stored as {stored:?}"); + } + } + #[test] fn a_stored_shortcut_replaces_the_default_and_an_empty_one_unbinds() { let mut keymap = Keymap::new(); diff --git a/crates/ui-egui/tests/it/shortcut_keys.rs b/crates/ui-egui/tests/it/shortcut_keys.rs index 80aae4b3..e2053ae8 100644 --- a/crates/ui-egui/tests/it/shortcut_keys.rs +++ b/crates/ui-egui/tests/it/shortcut_keys.rs @@ -56,6 +56,21 @@ fn a_held_key_runs_its_command_again() { assert!(!hidden(&h), "the repeat toggled it back"); } +#[test] +fn ctrl_shift_z_redoes_the_undone_step() { + let mut harness = harness(); + texturecraft_engine::execute(&mut harness.state_mut().session, "layer.add.fill", &serde_json::Value::Null).unwrap(); + let labels_after_add = harness.state().session.history_labels(); + harness.state_mut().session.undo().unwrap(); + harness.run_steps(2); + assert_ne!(harness.state().session.history_labels(), labels_after_add, "the step is undone"); + + harness.event(press(Key::Z, Modifiers::COMMAND | Modifiers::SHIFT, false)); + harness.run_steps(2); + + assert_eq!(harness.state().session.history_labels(), labels_after_add); +} + #[test] fn a_shortcut_stored_in_the_settings_replaces_the_default() { let mut h = harness();