FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
control: Ctrl+ chords through ui.key fire shortcuts off macOS (#245)
wstock32764392f1ce
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rsindex e491e36ae..0555a99f7 100644--- a/crates/ui-egui/src/control.rs+++ b/crates/ui-egui/src/control.rs@@ -62,10 +62,23 @@ fn err(e: impl std::fmt::Display) -> Outcome {Outcome::Done(json!({"ok": false, "error": e.to_string()}))}+/// Modifier flags from `{modifiers: {ctrl, command, shift, alt}}` (or the same keys at the top+/// level), as the OS would report them pressed.fn modifiers(p: &Value) -> egui::Modifiers {let m = p.get("modifiers").unwrap_or(p);let b = |k: &str| m.get(k).and_then(Value::as_bool).unwrap_or(false);- egui::Modifiers { alt: b("alt"), ctrl: b("ctrl"), shift: b("shift"), mac_cmd: cfg!(target_os = "macos") && b("command"), command: b("command") }+ as_pressed(egui::Modifiers { alt: b("alt"), ctrl: b("ctrl"), shift: b("shift"), mac_cmd: cfg!(target_os = "macos") && b("command"), command: b("command") })+}++/// `m` as egui-winit reports a physical press: off macOS Control is the primary modifier, so+/// `ctrl` and `command` are one key and a press of either carries both. A synthetic `Ctrl+Z` then+/// matches a `Cmd+Z` binding exactly as the real key does (#245).+fn as_pressed(mut m: egui::Modifiers) -> egui::Modifiers {+ if !cfg!(target_os = "macos") && (m.ctrl || m.command) {+ m.ctrl = true;+ m.command = true;+ }+ m}/// Resolve a point from `{id}` (element centre) or `{x, y}`.@@ -332,9 +345,8 @@ pub fn handle(app: &mut FilmcraftApp, ctx: &egui::Context, req: &ControlRequest)}"ui.key" => {let Some(name) = s("key") else { return err("missing `key`") };- let Some((mut m, key)) = crate::menus::parse_shortcut(name) else { return err(format!("unknown key `{name}`")) };- let extra = modifiers(p);- m |= extra;+ let Some((m, key)) = crate::menus::parse_shortcut(name) else { return err(format!("unknown key `{name}`")) };+ let m = as_pressed(m | modifiers(p));app.synthetic.push(egui::Event::Key { key, physical_key: None, pressed: true, repeat: false, modifiers: m });app.synthetic.push(egui::Event::Key { key, physical_key: None, pressed: false, repeat: false, modifiers: m });Outcome::AfterInputdiff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rsindex a02ecbf9b..2e55572c4 100644--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@ -502,16 +502,22 @@ pub fn shortcut_text(s: &str) -> String {Chord::parse(s).map(|c| c.display(Platform::current())).unwrap_or_else(|_| s.to_string())}-/// Parse "Cmd+Shift+K" into modifiers + key.+/// Parse "Cmd+Shift+K" into the modifiers a chord requires + its key. Off macOS the Control key+/// is the primary modifier, so `Ctrl` and `Cmd` are one key and both mean `command` (the engine's+/// `Chord::effective` says the same): a physical Ctrl press carries `command` there, and egui's+/// matching asks only for what the pattern names, so a `Ctrl+…` chord matches a `Cmd+…` binding+/// and the other way round (#245). On a Mac they stay two keys.pub fn parse_shortcut(s: &str) -> Option<(egui::Modifiers, egui::Key)> {let mut m = egui::Modifiers::NONE;let mut key = None;let parts: Vec<&str> = if s == "+" { vec!["+"] } else { s.split('+').collect() };+ let mac = cfg!(target_os = "macos");for p in parts {match p {"Cmd" => m.command = true,"Shift" => m.shift = true,"Alt" => m.alt = true,+ "Ctrl" if !mac => m.command = true,"Ctrl" => m.ctrl = true,k => {key = match k {@@ -638,3 +644,33 @@ fn menu_entry(ui: &mut egui::Ui, it: &MenuItem) -> bool {}r.clicked()}++#[cfg(test)]+mod parse_shortcut_tests {+ use super::parse_shortcut;++ /// Off macOS `Ctrl` and `Cmd` are the same key, so either spelling parses to the modifiers a+ /// physical Ctrl press carries and matches a binding written the other way (#245).+ #[test]+ fn ctrl_and_cmd_are_one_key_off_macos() {+ let (ctrl, k) = parse_shortcut("Ctrl+Z").unwrap();+ let (cmd, k2) = parse_shortcut("Cmd+Z").unwrap();+ assert_eq!((k, k2), (egui::Key::Z, egui::Key::Z));+ if cfg!(target_os = "macos") {+ assert_eq!((ctrl.ctrl, ctrl.command), (true, false));+ assert_eq!((cmd.ctrl, cmd.command), (false, true));+ assert!(!ctrl.matches_logically(cmd) && !cmd.matches_logically(ctrl), "two different keys on a Mac");+ } else {+ assert_eq!((ctrl.ctrl, ctrl.command), (false, true));+ assert_eq!(ctrl, cmd, "one key off a Mac");+ // what egui-winit reports for the physical key, and a bare `command` as tests send it+ let physical = egui::Modifiers { ctrl: true, command: true, ..Default::default() };+ assert!(physical.matches_logically(cmd) && physical.matches_logically(ctrl));+ assert!(egui::Modifiers::COMMAND.matches_logically(ctrl));+ }+ assert!(!parse_shortcut("Z").unwrap().0.matches_logically(cmd), "a bare key never stands in for the chord");+ let (m, k) = parse_shortcut("Cmd+Shift+K").unwrap();+ assert!(m.command && m.shift && !m.alt && k == egui::Key::K);+ assert_eq!(parse_shortcut("+").map(|(_, k)| k), Some(egui::Key::Plus));+ }+}diff --git a/crates/ui-egui/tests/keyboard_ui.rs b/crates/ui-egui/tests/keyboard_ui.rsindex 67020b7fb..dfc4324c8 100644--- a/crates/ui-egui/tests/keyboard_ui.rs+++ b/crates/ui-egui/tests/keyboard_ui.rs@@ -179,3 +179,44 @@ fn new_keys_work_in_the_app() {d.key("Cmd+Shift+0");assert_eq!(d.app().ui.program.zoom, None);}++/// Off macOS the menus show `Ctrl+…`, so that is what agents send: a `Ctrl` chord through+/// `ui.key` (and `ctrl` in `ui.click` modifiers) must act like the physical key, which egui-winit+/// reports as `ctrl` + `command`. Bindings are written `Cmd+…`, so this failed to match (#245).+#[cfg(not(target_os = "macos"))]+#[test]+fn ctrl_chords_fire_shortcuts_off_macos() {+ let mut d = Driver::new();+ let seq = d.exec("sequence.inspect", json!({}));+ let second = &seq["video"][0]["items"].as_array().unwrap()[1];+ let (clip, start) = (second["clip"].as_u64().unwrap(), second["start"].as_i64().unwrap());+ let start_of = |d: &mut Driver| {+ d.exec("sequence.inspect", json!({}))["video"][0]["items"]+ .as_array()+ .unwrap()+ .iter()+ .find(|i| i["clip"].as_u64() == Some(clip))+ .map(|i| i["start"].as_i64().unwrap())+ .unwrap()+ };+ d.exec("playhead.set", json!({"time": start + 1_000_000_000}));+ d.focus("Timeline");+ d.key("D");+ assert!(d.app().session.state.selection.iter().any(|c| c.0 == clip));+ // Ctrl+Right nudges one frame right, like Cmd+Right; Ctrl+Z undoes it+ let rate = d.app().session.sequence_rate();+ d.key("Ctrl+Right");+ assert_eq!(start_of(&mut d), start + rate.tick_of(1).0, "Ctrl+Right nudges");+ d.key("Ctrl+Z");+ assert_eq!(start_of(&mut d), start, "Ctrl+Z undoes");+ // the same chord spelt with the modifier flag+ d.ok("ui.key", json!({"key": "Right", "ctrl": true}));+ d.frames(3);+ assert_eq!(start_of(&mut d), start + rate.tick_of(1).0, "`ctrl: true` nudges");+ d.ok("ui.key", json!({"key": "Z", "ctrl": true}));+ d.frames(3);+ assert_eq!(start_of(&mut d), start, "`ctrl: true` undoes");+ // and the bare key still does not+ d.key("Z");+ assert_eq!(start_of(&mut d), start);+}diff --git a/docs/control-protocol.md b/docs/control-protocol.mdindex 6419e154a..6b92c3e61 100644--- a/docs/control-protocol.md+++ b/docs/control-protocol.md@@ -34,7 +34,7 @@ Methods (handlers in `crates/ui-egui/src/control.rs`):| `ui.click` / `ui.move` | `{id}` or `{x,y}`, `button`, `count`, `modifiers` | synthetic pointer input || `ui.drag` | `{from, to, steps, modifiers}` | press–move–release || `ui.scroll` | `{id|x,y, dx, dy, modifiers}` | wheel / trackpad |-| `ui.key` / `ui.type` | `{key}` (`Cmd+K`, `Space`…) / `{text}` | keyboard |+| `ui.key` / `ui.type` | `{key}` (`Cmd+K`, `Space`…; off macOS `Ctrl+K` is the same key) / `{text}` | keyboard || `ui.timeline.hit` / `ui.timeline.locate` | `{x,y}` / `{clip, edge?}` | timeline hit-testing || `ui.playback` | `{action: play|stop|toggle, speed?}` | || `ui.screenshot` | `{path?, panel?}` | PNG of the window or one panel; fails after 10 s when no frame is presented (window hidden, display asleep) |