From 501e2cf1fc87c3785a6bb4fc7e18163d2c2711b7 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 20 Oct 2026 15:21:54 +0000 Subject: [PATCH] keymap: while typing, text-editing chords go to the text field unless an active context binds them --- crates/keymap/README.md | 7 ++- crates/keymap/src/egui.rs | 4 ++ crates/keymap/src/keymap.rs | 95 ++++++++++++++++++++++++++++++++--- crates/keymap/src/lib.rs | 2 +- crates/keymap/tests/egui.rs | 17 +++++++ crates/keymap/tests/keymap.rs | 29 +++++++++++ 6 files changed, 146 insertions(+), 8 deletions(-) diff --git a/crates/keymap/README.md b/crates/keymap/README.md index d9d6f225c..15e812dbb 100644 --- a/crates/keymap/README.md +++ b/crates/keymap/README.md @@ -57,7 +57,12 @@ assert_eq!(press("M", &editing), Lookup::None, "plain keys go to the text field" While a text field has the keyboard (`Active::typing`), the keymap's `TypingRule` decides which app and panel bindings wait: plain keys (the default), everything without the command modifier, -or everything. Bindings in an active context always fire, so `Text: Cmd+B` works while typing. +or everything. Under every rule, the platform's text-editing chords (`text_editing_chords`) go +to the field too: undo and redo, select all, and moving and deleting by word, line and text +(Cmd+Z, Alt+Left and Cmd+Backspace on macOS; Ctrl+Z, Ctrl+Y, Ctrl+Left and Ctrl+Home +elsewhere). Other command chords, such as Cmd+S, still fire. Bindings in an active context +always fire, so `Text: Cmd+B` works while typing, and a `Text: Cmd+Z` binding takes undo from +the field. With the `egui` feature, `egui::Dispatcher` does this for a frame's key events (see Features). diff --git a/crates/keymap/src/egui.rs b/crates/keymap/src/egui.rs index a42e7d9d4..fcf113b1c 100644 --- a/crates/keymap/src/egui.rs +++ b/crates/keymap/src/egui.rs @@ -223,6 +223,10 @@ struct LastPress { /// AltGr: the browser on Windows reports AltGr+Q (`@` on a German layout) as Ctrl+Alt+Q, and /// the web build's text agent sends the `@` after it. egui-winit reports AltGr with neither Ctrl /// nor Alt. A Ctrl+Alt press without text is a command chord. +/// +/// Other presses are looked up; the keymap's [`TypingRule`](crate::TypingRule) and +/// [`text_editing_chords`](crate::text_editing_chords) leave plain keys and the text-editing chords +/// (Cmd+Z, word moves) to the field. #[derive(Clone, Debug)] pub struct Dispatcher { last: Option, diff --git a/crates/keymap/src/keymap.rs b/crates/keymap/src/keymap.rs index 4c3f23fc4..1dfcb2395 100644 --- a/crates/keymap/src/keymap.rs +++ b/crates/keymap/src/keymap.rs @@ -7,7 +7,7 @@ use std::fmt; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::chord::{Chord, ParseError, Platform, Trigger}; +use crate::chord::{Chord, Key, Mods, ParseError, Platform, Trigger}; use crate::command::{CommandRef, MAX_KEY}; use crate::scope::{Active, Scope, ScopeKind, Scopes}; @@ -196,8 +196,9 @@ impl Bindable { } } -/// Which chords work while a text field has the keyboard. Bindings in an active context always -/// work: a `Text: Cmd+B` binding is meant for typing. +/// Which chords work while a text field has the keyboard. Under every rule, the +/// [`text_editing_chords`] go to the text field too. Bindings in an active context always work: a +/// `Text: Cmd+B` binding is meant for typing. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum TypingRule { @@ -211,8 +212,11 @@ pub enum TypingRule { } impl TypingRule { - /// Whether `chord` may fire while typing. - pub fn fires(self, chord: &Chord) -> bool { + /// Whether `chord` may fire while typing on `platform`. + pub fn fires(self, chord: &Chord, platform: Platform) -> bool { + if text_editing_chords(platform).contains(&chord.effective(platform)) { + return false; + } match self { TypingRule::PlainKeysWait => !chord.is_plain() || chord.is_function_key(), TypingRule::CommandKeysOnly => chord.has_command() || chord.is_function_key(), @@ -221,6 +225,85 @@ impl TypingRule { } } +const CMD: Mods = Mods::CMD; +const CMD_SHIFT: Mods = Mods { shift: true, ..Mods::CMD }; +const ALT: Mods = Mods::ALT; +const ALT_SHIFT: Mods = Mods { shift: true, ..Mods::ALT }; +const CTRL: Mods = Mods::CTRL; + +// The chords egui's TextEdit acts on that follow the platform's conventions. egui also takes +// Ctrl+H, Ctrl+K, Ctrl+U and Ctrl+W everywhere, but off macOS those are app commands. +const MAC_EDITING: &[Chord] = &[ + // Undo, redo, select all. + Chord::new(CMD, Key::Z), + Chord::new(CMD_SHIFT, Key::Z), + Chord::new(CMD, Key::A), + // Words. + Chord::new(ALT, Key::Left), + Chord::new(ALT, Key::Right), + Chord::new(ALT_SHIFT, Key::Left), + Chord::new(ALT_SHIFT, Key::Right), + Chord::new(ALT, Key::Backspace), + Chord::new(ALT, Key::Delete), + // Line start and end. + Chord::new(CMD, Key::Left), + Chord::new(CMD, Key::Right), + Chord::new(CMD_SHIFT, Key::Left), + Chord::new(CMD_SHIFT, Key::Right), + Chord::new(CMD, Key::Backspace), + Chord::new(CMD, Key::Delete), + // Text start and end. + Chord::new(CMD, Key::Up), + Chord::new(CMD, Key::Down), + Chord::new(CMD_SHIFT, Key::Up), + Chord::new(CMD_SHIFT, Key::Down), + // The Control keys of Cocoa text fields: line start and end, character and line moves, + // deletion. + Chord::new(CTRL, Key::A), + Chord::new(CTRL, Key::E), + Chord::new(CTRL, Key::B), + Chord::new(CTRL, Key::F), + Chord::new(CTRL, Key::P), + Chord::new(CTRL, Key::N), + Chord::new(CTRL, Key::H), + Chord::new(CTRL, Key::K), + Chord::new(CTRL, Key::U), + Chord::new(CTRL, Key::W), +]; + +const PC_EDITING: &[Chord] = &[ + // Undo, redo, select all. + Chord::new(CMD, Key::Z), + Chord::new(CMD, Key::Y), + Chord::new(CMD_SHIFT, Key::Z), + Chord::new(CMD, Key::A), + // Words. + Chord::new(CMD, Key::Left), + Chord::new(CMD, Key::Right), + Chord::new(CMD_SHIFT, Key::Left), + Chord::new(CMD_SHIFT, Key::Right), + Chord::new(CMD, Key::Backspace), + Chord::new(CMD, Key::Delete), + // Text start and end. + Chord::new(CMD, Key::Home), + Chord::new(CMD, Key::End), + Chord::new(CMD_SHIFT, Key::Home), + Chord::new(CMD_SHIFT, Key::End), + Chord::new(CMD, Key::Up), + Chord::new(CMD, Key::Down), + Chord::new(CMD_SHIFT, Key::Up), + Chord::new(CMD_SHIFT, Key::Down), +]; + +/// The chords with modifiers that edit text on `platform`, as effective chords: undo, redo, +/// select all, and moving and deleting by word, line and text. While typing they go to the text +/// field under every [`TypingRule`]; only a binding in an active context takes one. Plain keys +/// (arrows, Home, End, Backspace, Delete, with or without Shift) are not listed, since every rule +/// holds them already. The `egui` dispatcher keeps the clipboard chords for the field itself. +pub fn text_editing_chords(platform: Platform) -> &'static [Chord] { + if platform.is_mac() { MAC_EDITING } else { PC_EDITING } +} + /// What a key press runs. #[derive(Clone, Debug, PartialEq)] pub enum Lookup { @@ -500,7 +583,7 @@ impl Keymap { /// The command `chord` (pressed twice when `double`) runs with `active` focus. pub fn lookup(&self, chord: &Chord, double: bool, active: &Active) -> Lookup { let want = Trigger { chord: chord.effective(self.platform), double }; - let held = active.typing && !self.typing.fires(&want.chord); + let held = active.typing && !self.typing.fires(&want.chord, self.platform); let Some(holders) = self.by_trigger.get(&want) else { return Lookup::None }; // Each command's nearest rank; `holders` is in catalog order. let mut ranks: Vec<(usize, u8)> = Vec::new(); diff --git a/crates/keymap/src/lib.rs b/crates/keymap/src/lib.rs index 9fbcfa2ea..9816ecda5 100644 --- a/crates/keymap/src/lib.rs +++ b/crates/keymap/src/lib.rs @@ -145,6 +145,6 @@ pub use command::{CommandRef, MAX_KEY}; pub use import::{ContextRule, ForeignEntry, ForeignKey, ForeignSet, ImportOptions, ImportPlan, ImportReport, NoneDefined, Start, plan}; pub use keymap::{ AssignError, Assigned, Bindable, Binding, BindingError, Conflict, ConflictKind, DropReason, Holder, Keymap, Kind, Lookup, MAX_BINDING_TEXT, - MAX_BINDINGS, MAX_COMMANDS, MAX_CONFLICTS, Overrides, Policy, Problem, TypingRule, + MAX_BINDINGS, MAX_COMMANDS, MAX_CONFLICTS, Overrides, Policy, Problem, TypingRule, text_editing_chords, }; pub use scope::{Active, MAX_SCOPE_NAME, Scope, ScopeKind, Scopes}; diff --git a/crates/keymap/tests/egui.rs b/crates/keymap/tests/egui.rs index b1fb76307..1e185ba84 100644 --- a/crates/keymap/tests/egui.rs +++ b/crates/keymap/tests/egui.rs @@ -351,6 +351,23 @@ fn typing_blocks_plain_keys_but_not_command_chords() { assert_eq!(left[1], Event::Text("b".into())); } +#[test] +fn undo_goes_to_the_text_field_and_save_still_runs() { + let km = Keymap::new( + vec![ + Bindable::new("edit.undo", "Undo", Scope::app()).with_default(Binding::parse("Cmd+Z").unwrap()), + Bindable::new("file.save", "Save", Scope::app()).with_default(Binding::parse("Cmd+S").unwrap()), + ], + Scopes::new(), + Platform::Windows, + ); + let typing = Active { typing: true, ..Active::NONE }; + let events = vec![press(EguiKey::Z, CTRL), press(EguiKey::S, CTRL)]; + let (ran, left) = run(&mut Dispatcher::new(), &km, &typing, frame(1.0, CTRL, events)); + assert_eq!(ran, ["file.save"]); + assert_eq!(left, [press(EguiKey::Z, CTRL)]); +} + #[test] fn the_focused_panel_wins_and_tool_groups_cycle() { let km = dispatch_keymap(); diff --git a/crates/keymap/tests/keymap.rs b/crates/keymap/tests/keymap.rs index 74ba8d6e4..58a904b0f 100644 --- a/crates/keymap/tests/keymap.rs +++ b/crates/keymap/tests/keymap.rs @@ -97,6 +97,35 @@ fn typing_rules_decide_what_fires_in_a_text_field() { } } +#[test] +fn text_editing_chords_go_to_the_text_field_under_every_rule() { + let catalog = vec![app("edit.undo", &["Cmd+Z"]), app("file.save", &["Cmd+S"]), app("nav.word", &["Alt+Left"]), app("nav.start", &["Cmd+Home"])]; + let typing = Active { typing: true, ..Active::NONE }; + for rule in [TypingRule::PlainKeysWait, TypingRule::CommandKeysOnly] { + for platform in [Platform::Mac, Platform::Windows] { + let km = Keymap::new(catalog.clone(), Scopes::new(), platform).with_typing_rule(rule); + assert_eq!(press(&km, "Cmd+Z", &typing), Lookup::None, "{rule:?} {platform:?}"); + assert_eq!(press(&km, "Cmd+Z", &Active::NONE), run("edit.undo")); + assert_eq!(press(&km, "Cmd+S", &typing), run("file.save"), "{rule:?} {platform:?}"); + } + } + // Word and line keys follow the platform. + let mac = Keymap::new(catalog.clone(), Scopes::new(), Platform::Mac); + let windows = Keymap::new(catalog.clone(), Scopes::new(), Platform::Windows); + assert_eq!(press(&mac, "Alt+Left", &typing), Lookup::None); + assert_eq!(press(&windows, "Alt+Left", &typing), run("nav.word")); + assert_eq!(press(&windows, "Ctrl+Home", &typing), Lookup::None); + assert_eq!(press(&mac, "Cmd+Home", &typing), run("nav.start")); + + // A binding in an active context takes the chord from the field. + let catalog = vec![app("edit.undo", &["Cmd+Z"]), app("text.undo", &["Text: Cmd+Z"])]; + let km = Keymap::new(catalog, scopes(), Platform::Windows); + let editing = Active { contexts: &["Text"], typing: true, ..Active::NONE }; + assert_eq!(press(&km, "Cmd+Z", &editing), run("text.undo")); + assert_eq!(press(&km, "Cmd+Z", &typing), Lookup::None); + assert!(!TypingRule::PlainKeysWait.fires(&chord("Ctrl+Z"), Platform::Linux)); +} + #[test] fn bindings_of_an_active_context_fire_while_typing_under_every_rule() { let catalog = vec![