Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Shortcuts: custom bindings on PageUp/PageDown (and other multi-word keys) fire again (#6827)

00xJ05faf499d5c03
diff --git a/crates/engine/src/prefs.rs b/crates/engine/src/prefs.rs
index 77acd2e34..b33c06fea 100644
--- a/crates/engine/src/prefs.rs
+++ b/crates/engine/src/prefs.rs
@@ -1466,7 +1466,15 @@ pub fn normalize_shortcut(s: &str) -> Option<String> {
Some(s)
}
+/// Key names spelled with more than one capital (egui's `Key::name`): Title case would turn
+/// `PageUp` into `Pageup`, which no key matches, so a binding saved that way never fired (#6827).
+const MULTI_WORD_KEYS: [&str; 8] =
+ ["PageUp", "PageDown", "OpenBracket", "CloseBracket", "OpenCurlyBracket", "CloseCurlyBracket", "BrowserBack", "IntlBackslash"];
+
fn capitalize(s: &str) -> String {
+ if let Some(k) = MULTI_WORD_KEYS.iter().find(|k| k.eq_ignore_ascii_case(s)) {
+ return (*k).to_string();
+ }
let mut c = s.chars();
match c.next() {
Some(f) if f.is_ascii_alphabetic() => {
diff --git a/crates/engine/src/prefs/tests.rs b/crates/engine/src/prefs/tests.rs
index 55e41949b..b0cb787d8 100644
--- a/crates/engine/src/prefs/tests.rs
+++ b/crates/engine/src/prefs/tests.rs
@@ -343,6 +343,11 @@ fn shortcuts_normalise_and_detect_conflicts() {
assert_eq!(normalize_shortcut("Cmd++").as_deref(), Some("Cmd++"));
assert_eq!(normalize_shortcut("Cmd+Shift"), None);
assert_eq!(normalize_shortcut("Cmd+A+B"), None);
+ // #6827: multi-word key names keep egui's spelling (Title case made `Pageup`, matching no key).
+ assert_eq!(normalize_shortcut("shift+pageup").as_deref(), Some("Shift+PageUp"));
+ assert_eq!(normalize_shortcut("Ctrl+Alt+PAGEDOWN").as_deref(), Some("Ctrl+Alt+PageDown"));
+ assert_eq!(normalize_shortcut("Cmd+openbracket").as_deref(), Some("Cmd+OpenBracket"));
+ assert_eq!(normalize_shortcut("home").as_deref(), Some("Home"));
let c = conflicts([("a", "Cmd+J"), ("b", "cmd+j"), ("c", "Cmd+K")]);
assert_eq!(c, vec![("Cmd+J".to_string(), vec!["a".to_string(), "b".to_string()])]);
}
diff --git a/crates/ui-egui/src/layer_shortcut_tests.rs b/crates/ui-egui/src/layer_shortcut_tests.rs
index 96ab2520d..fc9dd9357 100644
--- a/crates/ui-egui/src/layer_shortcut_tests.rs
+++ b/crates/ui-egui/src/layer_shortcut_tests.rs
@@ -188,3 +188,23 @@ fn command_click_on_a_thumbnail_loads_transparency_except_on_the_background() {
click(&mut h, shape, Modifiers::COMMAND);
assert_eq!(bounds(&h), Some(photocraft_geom::Rect::new(100, 50, 220, 150)), "a hidden layer's transparency loads too");
}
+
+/// #6827: a custom binding on PageUp/PageDown fires (it was saved as `Pageup`, which matched no
+/// key), and one saved that way by an earlier version still parses.
+#[test]
+fn custom_page_up_and_page_down_bindings_fire() {
+ for (binding, key, modifiers) in [
+ ("Shift+PageUp", Key::PageUp, Modifiers::SHIFT),
+ ("Cmd+Shift+PageUp", Key::PageUp, Modifiers::COMMAND | Modifiers::SHIFT),
+ ("Cmd+Alt+PageDown", Key::PageDown, Modifiers::COMMAND | Modifiers::ALT),
+ ] {
+ let mut app = app();
+ app.run("edit.fill", json!({"contents": "black"})).unwrap();
+ app.run("edit.keyboardShortcuts", json!({"set": {"edit.transform.flipHorizontal": binding}, "removeConflicts": true})).unwrap();
+ let before = app.session.journal.len();
+ press(&mut app, key, modifiers);
+ let ran: Vec<&str> = app.session.journal[before..].iter().map(|(id, _)| id.as_str()).collect();
+ assert_eq!(ran, ["edit.transform.flipHorizontal"], "{binding}");
+ }
+ assert_eq!(crate::shortcuts::parse("Shift+Pageup"), Some(egui::KeyboardShortcut::new(Modifiers::SHIFT, Key::PageUp)));
+}
diff --git a/crates/ui-egui/src/shortcuts.rs b/crates/ui-egui/src/shortcuts.rs
index 1ad8d6d92..81e64d7e0 100644
--- a/crates/ui-egui/src/shortcuts.rs
+++ b/crates/ui-egui/src/shortcuts.rs
@@ -38,7 +38,8 @@ pub fn parse(s: &str) -> Option<KeyboardShortcut> {
";" => Some(Key::Semicolon),
"'" => Some(Key::Quote),
"]" => Some(Key::CloseBracket),
- name => Key::from_name(name),
+ // Case-insensitive too: bindings saved as `Shift+Pageup` before #6827 still work.
+ name => Key::from_name(name).or_else(|| Key::ALL.iter().find(|k| k.name().eq_ignore_ascii_case(name)).copied()),
}?;
Some(KeyboardShortcut::new(mods, key))
}