FilmCraft: An open-source, nonlinear video editor written by scratch in pure Rust
Revert shortcut-preset changes in favour of #337's approach
Brandon Thomascc5e3c106549
diff --git a/crates/engine/src/shortcuts.rs b/crates/engine/src/shortcuts.rsindex 90b1766c0..4165cde88 100644--- a/crates/engine/src/shortcuts.rs+++ b/crates/engine/src/shortcuts.rs@@ -24,7 +24,7 @@//! Commands the frontend owns (tools, transport, panels, workspaces) are registered with//! [`Shortcuts::register_external`] so they can be listed, bound and resolved here too.-use std::collections::{BTreeMap, BTreeSet};+use std::collections::BTreeMap;use std::path::{Path, PathBuf};use serde::{Deserialize, Serialize};@@ -593,34 +593,22 @@ impl Shortcuts {_ => return None,};self.apply_table(&mut b, &table);- // Canonical chords can become the same physical key on this platform. Keep the first- // binding, matching resolve(), when generating a built-in preset.- let platform = Platform::current();- let mut seen = BTreeSet::new();- b.retain(|binding| binding.chord().is_none_or(|chord| seen.insert((binding.context().to_string(), chord.effective(platform)))));Some(b)}- /// Apply a preset table: listed commands get the listed keys, and explicit physical keys take- /// precedence over inherited bindings in the same context. Within the table, keep canonical- /// replacement order so folded aliases retain their established first-match winner.+ /// Apply a preset table: listed commands get exactly the listed keys, and a listed key is taken+ /// away from whatever else had it in that context.fn apply_table(&self, b: &mut Vec<Binding>, table: &[Entry]) {let listed: Vec<&str> = table.iter().map(|e| e.0).filter(|c| self.known(c)).collect();b.retain(|x| !listed.contains(&x.command.as_str()));- let platform = Platform::current();- let mut explicit: Vec<Binding> = Vec::new();for (c, k, p) in table {if !self.known(c) {continue;}let nb = Binding::new(c, k, panel_opt(p));- let chord = nb.chord();- let physical = chord.as_ref().map(|chord| chord.effective(platform));- b.retain(|o| !(o.context() == nb.context() && o.chord().map(|chord| chord.effective(platform)) == physical));- explicit.retain(|o| !(o.context() == nb.context() && o.chord() == chord));- explicit.push(nb);+ b.retain(|o| !(o.context() == nb.context() && o.chord() == nb.chord()));+ b.push(nb);}- b.extend(explicit);}fn presets_dir(&self) -> Option<PathBuf> {diff --git a/crates/engine/src/shortcuts_tests.rs b/crates/engine/src/shortcuts_tests.rsindex 1f98b8ecb..d12242333 100644--- a/crates/engine/src/shortcuts_tests.rs+++ b/crates/engine/src/shortcuts_tests.rs@@ -231,133 +231,3 @@ fn list_searches_labels_and_keys() {let v = s.execute("shortcuts.list", json!({"panel": "History", "assigned": true})).unwrap();assert_eq!(v.as_array().unwrap().len(), 2, "{v}");}--#[cfg(target_os = "linux")]-#[test]-fn engine_defaults_keep_the_existing_linux_winners_without_folded_conflicts() {- let s = Session::default();- assert!(s.shortcuts.conflicts(Platform::Linux).is_empty());- for (keys, command) in [("Cmd+9", "multicam.cutToCamera9"), ("Cmd+Shift+M", "markers.addRange"), ("Cmd+T", "trim.toggleType")] {- let chord = Chord::parse(keys).unwrap();- assert_eq!(s.shortcuts.resolve(&chord, None, Platform::Linux).unwrap().command, command, "{keys} keeps its existing first-match action");- let ctrl = keys.replacen("Cmd", "Ctrl", 1);- assert_eq!(s.shortcuts.resolve(&Chord::parse(&ctrl).unwrap(), None, Platform::Linux).unwrap().command, command);- }- assert!(!s.shortcuts.modified);- assert!(!s.shortcuts.can_undo());- assert!(!s.shortcuts.can_redo());-}--#[test]-fn builtin_presets_are_unambiguous_with_engine_and_frontend_commands() {- let platform = Platform::current();- for frontend in [false, true] {- for name in crate::shortcuts::BUILTIN_PRESETS {- let mut s = Session::default();- if frontend {- with_ui_commands(&mut s);- }- s.execute("shortcuts.loadPreset", json!({"name": name})).unwrap();- assert!(s.shortcuts.conflicts(platform).is_empty(), "{name}, frontend={frontend}: {:?}", s.shortcuts.conflicts(platform));- let backspace = Chord::parse("Backspace").unwrap();- assert_eq!(s.shortcuts.resolve(&backspace, Some("Project"), platform).unwrap().command, "project.delete", "panel overrides remain independent");- s.execute("shortcuts.undo", json!({})).unwrap();- assert_eq!(s.shortcuts.preset, crate::shortcuts::DEFAULT_PRESET);- assert!(s.shortcuts.conflicts(platform).is_empty());- s.execute("shortcuts.redo", json!({})).unwrap();- assert_eq!(s.shortcuts.preset, name);- assert!(s.shortcuts.conflicts(platform).is_empty());- }- }-}--#[test]-fn intentional_conflicts_survive_registration_persistence_and_shortcut_undo() {- let dir = temp_dir("intentional-conflicts");- let platform = Platform::current();- let mut s = Session::default();- s.shortcuts.set_dir(&dir);- let defaults = s.shortcuts.bindings.clone();- let result = s.execute("shortcuts.set", json!({"command": "edit.redo", "keys": "M", "keepConflicts": true})).unwrap();- assert_eq!(result["conflicts"].as_array().unwrap().len(), 1);- let edited = s.shortcuts.bindings.clone();- with_ui_commands(&mut s);- assert_eq!(s.shortcuts.bindings, edited, "registering the UI preserves an intentionally modified set");- let mut restored = Session::default();- restored.shortcuts.set_dir(&dir);- with_ui_commands(&mut restored);- assert_eq!(restored.shortcuts.bindings, edited, "a modified built-in set survives restart");- assert!(restored.shortcuts.modified);- assert_eq!(restored.shortcuts.conflicts(platform).len(), 1);- s.execute("shortcuts.undo", json!({})).unwrap();- assert_eq!(s.shortcuts.bindings, defaults);- assert!(!s.shortcuts.modified);- assert!(s.shortcuts.conflicts(platform).is_empty());- s.execute("shortcuts.redo", json!({})).unwrap();- assert_eq!(s.shortcuts.bindings, edited);- assert_eq!(s.shortcuts.conflicts(platform).len(), 1);- s.execute("shortcuts.savePreset", json!({"name": "Kept conflicts"})).unwrap();- assert!(!s.shortcuts.modified);- with_ui_commands(&mut s);- assert_eq!(s.shortcuts.bindings, edited, "an unmodified custom preset keeps its deliberate conflicts");- let mut custom = Session::default();- custom.shortcuts.set_dir(&dir);- with_ui_commands(&mut custom);- assert_eq!(custom.shortcuts.preset, "Kept conflicts");- assert_eq!(custom.shortcuts.bindings, edited);- assert_eq!(custom.shortcuts.conflicts(platform).len(), 1);- custom.execute("shortcuts.loadPreset", json!({"name": crate::shortcuts::DEFAULT_PRESET})).unwrap();- assert!(custom.shortcuts.conflicts(platform).is_empty());- custom.execute("shortcuts.loadPreset", json!({"name": "Kept conflicts"})).unwrap();- assert_eq!(custom.shortcuts.bindings, edited);- assert_eq!(custom.shortcuts.conflicts(platform).len(), 1);- let _ = std::fs::remove_dir_all(&dir);-}--#[cfg(target_os = "linux")]-#[test]-fn an_explicit_final_cut_preset_binding_overrides_inherited_linux_physical_keys() {- for frontend in [false, true] {- let mut session = Session::default();- if frontend {- with_ui_commands(&mut session);- }- session.execute("shortcuts.loadPreset", json!({"name": crate::shortcuts::FCP_PRESET})).unwrap();- for keys in ["Cmd+T", "Ctrl+T"] {- let resolved = session.execute("shortcuts.resolve", json!({"keys": keys})).unwrap();- assert_eq!(- resolved["command"], "sequence.applyVideoTransition",- "the explicit Final Cut Pro table replaces inherited Trim Toggle Type: {resolved}"- );- }- assert!(session.shortcuts.conflicts(Platform::Linux).is_empty());- session.execute("shortcuts.undo", json!({})).unwrap();- let resolved = session.execute("shortcuts.resolve", json!({"keys": "Cmd+T"})).unwrap();- assert_eq!(resolved["command"], "trim.toggleType", "the FilmCraft default keeps its established Linux winner");- session.execute("shortcuts.redo", json!({})).unwrap();- let resolved = session.execute("shortcuts.resolve", json!({"keys": "Ctrl+T"})).unwrap();- assert_eq!(resolved["command"], "sequence.applyVideoTransition");- }-}--#[cfg(target_os = "linux")]-#[test]-fn compatible_presets_keep_the_existing_first_explicit_linux_winners() {- let mut session = Session::default();- // Export is frontend-owned; use the same definition registered by the desktop menu.- session.shortcuts.register_external(vec![CommandInfo::new("mode.export", "Export", &["File", "Export"], Some("Cmd+M"))]);- for (preset, keys, command) in [- (crate::shortcuts::FCP_PRESET, "Cmd+R", "clip.speedDuration"),- (crate::shortcuts::PREMIERE_PRESET, "Cmd+M", "mode.export"),- (crate::shortcuts::PREMIERE_PRESET, "Cmd+Shift+M", "markers.goPrev"),- (crate::shortcuts::PREMIERE_PRESET, "Cmd+T", "trim.toggleType"),- (crate::shortcuts::PREMIERE_PRESET, "Cmd+9", "multicam.cutToCamera9"),- ] {- session.execute("shortcuts.loadPreset", json!({"name": preset})).unwrap();- for spelling in [keys.to_string(), keys.replacen("Cmd", "Ctrl", 1)] {- let resolved = session.execute("shortcuts.resolve", json!({"keys": spelling})).unwrap();- assert_eq!(resolved["command"], command, "{preset}: {spelling} keeps its existing first explicit action: {resolved}");- }- assert!(session.shortcuts.conflicts(Platform::Linux).is_empty());- }-}