Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

A command's other shortcuts run while the macOS menu bar holds its first

aionggc6444a047175
diff --git a/apps/designcraft/src/native_menu.rs b/apps/designcraft/src/native_menu.rs
index b589b5ecf..e127776ba 100644
--- a/apps/designcraft/src/native_menu.rs
+++ b/apps/designcraft/src/native_menu.rs
@@ -122,7 +122,11 @@ impl NativeMenu {
app.native_menu = true;
let keys: Vec<(String, Option<String>)> =
items.values().filter(|(_, p, _)| p.is_null()).map(|(id, _, _)| (id.clone(), shortcut(app, id))).collect();
- app.native_shortcuts = keys.iter().filter(|(_, sc)| sc.as_deref().and_then(accel).is_some()).map(|(id, _)| id.clone()).collect();
+ let recording = app.shortcut_recording();
+ app.native_shortcuts = keys
+ .iter()
+ .filter_map(|(id, sc)| sc.as_ref().filter(|sc| !recording && accel(sc).is_some()).map(|sc| (id.clone(), sc.clone())))
+ .collect();
let recent = recent.map(|submenu| RecentMenu { submenu, shown: Vec::new(), ids: Vec::new(), separator: None });
Self {
_menu: menu,
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index bef0ef0aa..3a9fd991a 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -801,8 +801,9 @@ pub struct DesignApp {
pub(crate) pending_window_commands: Vec<egui::ViewportCommand>,
/// The host installed a native menu bar (macOS): don't draw menus in the window.
pub native_menu: bool,
- /// Shortcuts the native menu handles (skip them in the egui shortcut handler).
- pub native_shortcuts: std::collections::HashSet<String>,
+ /// The commands the native menu bar gives a key equivalent, with that shortcut: the egui
+ /// shortcut handler leaves that key to the bar and runs the command's other bindings itself.
+ pub native_shortcuts: std::collections::HashMap<String, String>,
/// Every command's shortcuts, with the user's changes (`ui.shortcuts`) applied.
pub keymap: craft_keymap::Keymap,
pub(crate) key_dispatcher: craft_keymap::egui::Dispatcher,
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 740e181f1..c883a7ff8 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -3233,14 +3233,16 @@ fn take_keys(app: &mut DesignApp, input: &mut egui::InputState, typing: bool) ->
let active = craft_keymap::Active { panel: None, contexts, typing };
let native = &app.native_shortcuts;
// Presses the dispatcher leaves alone: Clear (the active tool deletes) and the keys the
- // native menu's accelerators already handled. The native menu bar carries the shortcuts in
- // effect, the user's included, and an accelerator fires only for its shortcut exactly: the
- // Plus key standing in for = never reaches it.
+ // native menu's accelerators already handled. An item holds one key equivalent, its
+ // command's first shortcut in effect, and fires only for it exactly: the Plus key standing
+ // in for = never reaches it, and a command's other bindings run here.
// Copy, Cut and Paste stay too (the dispatcher's default): the canvas and text tools
// handle them, and Paste carries the text.
let accept = |hit: &craft_keymap::egui::Hit| {
let c = &hit.command;
- let leave = hit.exact && hit.cycle.is_empty() && (c.id == "edit.clear" || (c.params.is_null() && native.contains(&c.id)));
+ let on_bar =
+ || c.params.is_null() && native.get(&c.id).and_then(|sc| craft_keymap::Binding::parse(sc).ok()).is_some_and(|b| b.trigger == hit.trigger);
+ let leave = hit.exact && hit.cycle.is_empty() && (c.id == "edit.clear" || on_bar());
!leave
};
let hits = app.key_dispatcher.dispatch_filtered(input, &app.keymap, &active, accept);
@@ -4982,7 +4984,7 @@ mod tests {
}
// With a native menu (macOS) its accelerator takes Cmd+= itself, but never sees the Plus key
// standing in for = (#781).
- app.native_shortcuts.insert("view.zoomIn".to_string());
+ app.native_shortcuts.insert("view.zoomIn".to_string(), shortcut_of(&app, "view.zoomIn").unwrap());
for (k, m, zooms_in) in [
(egui::Key::Plus, egui::Modifiers::COMMAND, true),
(egui::Key::Plus, egui::Modifiers::COMMAND | egui::Modifiers::SHIFT, true),
@@ -4997,16 +4999,23 @@ mod tests {
// A shortcut the user set runs here until the native menu bar carries it as its key
// equivalent; then only the bar runs it, once.
run_ui(&mut app, "window.setShortcut", &json!({"id": "view.zoomOut", "shortcut": "Cmd+9"})).unwrap().unwrap();
- for (native, zooms_out) in [(false, true), (true, false)] {
- if native {
- app.native_shortcuts.insert("view.zoomOut".to_string());
- }
+ let zooms_out = |app: &mut crate::DesignApp, k: egui::Key| {
let z0 = app.view().unwrap().zoom;
- frame(&mut app, key(egui::Key::Num9, egui::Modifiers::COMMAND));
- frame(&mut app, vec![]);
- let z = app.view().unwrap().zoom;
- assert_eq!(z < z0, zooms_out, "native {native}: {z0} -> {z}");
- }
+ frame(app, key(k, egui::Modifiers::COMMAND));
+ frame(app, vec![]);
+ app.view().unwrap().zoom < z0
+ };
+ assert!(zooms_out(&mut app, egui::Key::Num9), "no native bar: the app runs it");
+ app.native_shortcuts.insert("view.zoomOut".to_string(), "Cmd+9".to_string());
+ assert!(!zooms_out(&mut app, egui::Key::Num9), "the bar's key equivalent runs only there");
+ // A second binding of a command whose first the bar holds runs here.
+ let cmd = craft_keymap::CommandRef::new("view.zoomOut");
+ let bindings = ["Cmd+F11", "Cmd+9"].map(|s| craft_keymap::Binding::parse(s).unwrap()).to_vec();
+ app.keymap.set_bindings(&cmd, bindings, craft_keymap::Policy::Steal).unwrap();
+ app.ui.shortcuts = app.keymap.overrides().clone();
+ app.native_shortcuts.insert("view.zoomOut".to_string(), "Cmd+F11".to_string());
+ assert!(!zooms_out(&mut app, egui::Key::F11), "the bar runs its own key");
+ assert!(zooms_out(&mut app, egui::Key::Num9), "the second binding runs in the app");
}
#[test]