From 80fef4400162a47d312f690da8d9c89dd464eba1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 17 Nov 2026 08:54:37 +0000 Subject: [PATCH] Typing scopes: a command set marked typing is a keymap context while an instance is around the focus, so a text component's own keys work while it types; the text editor's formatting keys are one --- crates/text/src/ui/editor/commands.rs | 9 +++++---- .../ui-contract/src/runtime/commands/board.rs | 18 ++++++++++++------ .../ui-contract/src/runtime/commands/mod.rs | 5 +++++ .../src/runtime/commands/registry.rs | 19 ++++++++++++++++++- crates/ui-macros/src/command_set.rs | 11 ++++++++++- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/crates/text/src/ui/editor/commands.rs b/crates/text/src/ui/editor/commands.rs index 9928732c3..7791f91f0 100644 --- a/crates/text/src/ui/editor/commands.rs +++ b/crates/text/src/ui/editor/commands.rs @@ -1,7 +1,8 @@ //! The editor's own commands: Photoshop's type formatting keys, the commit and cancel chords and the Character panel -//! toggle. They sit in the `textEditor` scope, so they reach the editor before an app's shortcut on the same chord -//! (Cmd+T is Free Transform elsewhere) while the editor holds the keys. The editor reads them with -//! `take_command`, so the keymap rebinds them like any command; the editor reads no raw keys. +//! toggle. They sit in the `textEditor` scope, a typing scope: while the editor holds the keys they reach it before an +//! app's shortcut on the same chord (Cmd+T is Free Transform elsewhere), and the keymap's typing rule, which keeps Alt +//! chords with a text field's text, lets them through (Alt+arrows kern and step leading, baseline and tracking). The +//! editor reads them with `take_command`, so the keymap rebinds them like any command; the editor reads no raw keys. use craft_behavior::{Granularity, MoveMods, NavMove}; @@ -17,7 +18,7 @@ const FAR: f32 = 5.0; /// The commands of a type session. Each runs on the editor that holds the keyboard; the host decides what a /// formatting command means for its text. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, craft_ui::contract::CommandSet)] -#[command(prefix = "textEditor", scope = "textEditor")] +#[command(prefix = "textEditor", scope = "textEditor", typing)] #[non_exhaustive] pub enum TextEditorCommand { /// Commit the session and keep the text. diff --git a/crates/ui-contract/src/runtime/commands/board.rs b/crates/ui-contract/src/runtime/commands/board.rs index 87706aa83..9844a352b 100644 --- a/crates/ui-contract/src/runtime/commands/board.rs +++ b/crates/ui-contract/src/runtime/commands/board.rs @@ -242,7 +242,9 @@ impl Board { /// The keymap and the scopes a key matches in while `chain` (outermost first) has the keys. fn key_scopes(&self, chain: &[Id]) -> Option { let keymap = self.keymap.as_ref().map(|cache| Arc::clone(&cache.keymap))?; - Some(KeyScopes { keymap, scopes: self.scopes_around(chain), held: self.held_scopes() }) + let scopes = self.scopes_around(chain); + let typing = scopes.iter().copied().filter(|scope| self.registry.is_typing_scope(scope)).collect(); + Some(KeyScopes { keymap, scopes, held: self.held_scopes(), typing }) } /// The scopes whose keys work now: those of the momentary commands whose key is held. @@ -421,16 +423,19 @@ struct KeyScopes { scopes: Vec<&'static str>, /// The held scopes ([`Board::held_scopes`]): the keymap's contexts, whose keys come before every scope's. held: Vec<&'static str>, + /// The typing scopes among `scopes` (`CommandSet::TYPING`): contexts of the keymap too, active while typing. + typing: Vec<&'static str>, } impl KeyScopes { /// What a press of `input` runs, read through `layout`, with `typing` when a text field has the keys. With /// `matcher` and the press's time the press counts toward a double press; without, it is matched on its own. A - /// held scope's keys stay with the text while typing. + /// held scope's keys stay with the text while typing; a typing scope's keys are the text component's own. fn press(&self, layout: &Layout, input: &KeyInput, typing: bool, matcher: Option<(&mut Matcher, f64)>) -> Option { let (panel, outer) = self.scopes.split_first().map_or((None, &[][..]), |(panel, outer)| (Some(*panel), outer)); - let held: &[&str] = if typing { &[] } else { &self.held }; - let active = Active { panel, contexts: held, typing }; + let held: &[&'static str] = if typing { &[] } else { &self.held }; + let contexts: Vec<&str> = self.typing.iter().chain(held).copied().collect(); + let active = Active { panel, contexts: &contexts, typing }; match matcher { Some((matcher, now)) => matcher.press(layout, &self.keymap, &Press { input, repeat: false, now, active: &active, outer }), None => Matcher::single(layout, &self.keymap, input, &active, outer), @@ -490,10 +495,11 @@ pub fn keymap_builds() -> u64 { fn build_keymap(registry: ®istry::Registry, app: Option<&AppCatalog>, platform: Platform, overrides: Option<&Arc>) -> BuiltKeymap { #[cfg(feature = "testing")] KEYMAP_BUILDS.with(|builds| builds.set(builds.get() + 1)); - // A held scope is a context of the keymap: its keys work while it is active, before any panel's. + // A held scope and a typing scope are contexts of the keymap: their keys work while they are active, before any + // panel's, and a typing scope's while typing too. let mut scopes = Scopes::new(); for def in registry.defs() { - if registry.is_held_scope(def.scope.as_str()) { + if registry.is_held_scope(def.scope.as_str()) || registry.is_typing_scope(def.scope.as_str()) { scopes = scopes.with_context(def.scope.as_str()); } else if !def.scope.is_app() { scopes = scopes.with_panel(def.scope.as_str()); diff --git a/crates/ui-contract/src/runtime/commands/mod.rs b/crates/ui-contract/src/runtime/commands/mod.rs index 9fd0d69e2..e7f5ca7af 100644 --- a/crates/ui-contract/src/runtime/commands/mod.rs +++ b/crates/ui-contract/src/runtime/commands/mod.rs @@ -83,6 +83,11 @@ pub trait CommandSet: Copy + Send + Sync + 'static { /// The scope the set's commands belong to. const SCOPE: CommandScope; + /// The set's keys are those of a text component, meant for typing (`#[command(typing)]`): its scope is a keymap + /// context, active while an instance of it is around the focus, so its keys come before every other scope's and + /// work while the component types, as a type tool's formatting keys do. + const TYPING: bool = false; + /// The command of this set that `command` names. fn from_ref(command: &CommandRef) -> Option; diff --git a/crates/ui-contract/src/runtime/commands/registry.rs b/crates/ui-contract/src/runtime/commands/registry.rs index 57c5e707d..115552e22 100644 --- a/crates/ui-contract/src/runtime/commands/registry.rs +++ b/crates/ui-contract/src/runtime/commands/registry.rs @@ -23,6 +23,8 @@ pub struct SetEntry { pub edits: fn() -> Vec<(&'static str, EditCommand)>, /// The momentary commands of the set whose held key makes a scope's keys work. pub holds: fn() -> Vec<(&'static str, CommandScope)>, + /// The set's scope when its keys are meant for typing (`CommandSet::TYPING`). + pub typing: Option, /// The defaults of one platform, which replace the command's other defaults there. pub platform_defaults: fn() -> Result, CommandDefError>, /// A story's set (`#[command(story)]`): loaded only by an install that shows stories. @@ -37,9 +39,10 @@ impl SetEntry { no_default_on: fn() -> Vec<(&'static str, Platform)>, edits: fn() -> Vec<(&'static str, EditCommand)>, holds: fn() -> Vec<(&'static str, CommandScope)>, + typing: Option, platform_defaults: fn() -> Result, CommandDefError>, ) -> Self { - SetEntry { name, defs, no_default_on, edits, holds, platform_defaults, story: false } + SetEntry { name, defs, no_default_on, edits, holds, typing, platform_defaults, story: false } } /// The entry as a story's set. @@ -79,6 +82,11 @@ macro_rules! __register_command_set { <$ty as $crate::runtime::commands::CommandSet>::no_default_on, <$ty as $crate::runtime::commands::CommandSet>::edits, <$ty as $crate::runtime::commands::CommandSet>::holds, + if <$ty as $crate::runtime::commands::CommandSet>::TYPING { + ::core::option::Option::Some(<$ty as $crate::runtime::commands::CommandSet>::SCOPE) + } else { + ::core::option::Option::None + }, <$ty as $crate::runtime::commands::CommandSet>::platform_defaults, )) } @@ -168,6 +176,8 @@ pub(crate) struct Registry { platform_defaults: Vec<(String, Platform, Binding)>, /// The scope each momentary command's held key makes work, by the command's id. holds: HashMap, + /// The scopes whose keys are meant for typing (`CommandSet::TYPING`). + typing: HashSet<&'static str>, /// Whether the stories' sets are loaded. stories: bool, } @@ -183,6 +193,7 @@ impl Registry { registry.no_default.extend((entry.no_default_on)().into_iter().map(|(id, platform)| (id.to_owned(), platform))); registry.edits.extend((entry.edits)().into_iter().map(|(id, edit)| (id.to_owned(), edit))); registry.holds.extend((entry.holds)().into_iter().map(|(id, scope)| (id.to_owned(), scope.name()))); + registry.typing.extend(entry.typing.map(CommandScope::name)); registry .platform_defaults .extend((entry.platform_defaults)()?.into_iter().map(|(id, platform, binding)| (id.to_owned(), platform, binding))); @@ -237,6 +248,12 @@ impl Registry { self.holds.values().any(|held| *held == scope) } + /// Whether `scope` is a typing scope (`CommandSet::TYPING`): its keys work while an instance of it is around the + /// focus, typing included. + pub(crate) fn is_typing_scope(&self, scope: &str) -> bool { + self.typing.contains(scope) + } + pub(crate) fn defs(&self) -> &[CommandDef] { &self.defs } diff --git a/crates/ui-macros/src/command_set.rs b/crates/ui-macros/src/command_set.rs index 2983a2f58..50553c1ed 100644 --- a/crates/ui-macros/src/command_set.rs +++ b/crates/ui-macros/src/command_set.rs @@ -1,8 +1,9 @@ -//! `#[derive(CommandSet)]` with `#[command(prefix = "..", scope = "..", story)]` on the enum and +//! `#[derive(CommandSet)]` with `#[command(prefix = "..", scope = "..", typing, story)]` on the enum and //! `#[command(label = "..", icon = "..", kind = "..", group = "..", modal = "..", edit = "..", holds = "..", default = "..", path = "..", alias = "..")]` //! on variants. //! `edit = ".."` links the command to the `EditCommand` a focused component takes it as. //! `holds = ".."` on a `kind = "momentary"` command names the scope whose keys work while the command's key is held. +//! `typing` on the enum makes the set's scope a typing scope: a text component's own keys, which work while it types. //! `default(mac = "..")` (or `windows`, `linux`) is a default of one platform: it replaces the unqualified defaults there. use crate::util::ui_crate; @@ -14,6 +15,7 @@ use syn::{Attribute, Data, DeriveInput, Fields, LitStr, Variant}; struct SetAttrs { prefix: Option, scope: Option, + typing: bool, story: bool, } @@ -216,6 +218,8 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { set.prefix = Some(meta.value()?.parse()?); } else if meta.path.is_ident("scope") { set.scope = Some(meta.value()?.parse()?); + } else if meta.path.is_ident("typing") { + set.typing = true; } else if meta.path.is_ident("story") { set.story = true; } else { @@ -229,6 +233,10 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { if let Some(scope) = &set.scope { check_scope(scope)?; } + if set.typing && set.scope.is_none() { + return Err(syn::Error::new_spanned(ident, "`typing` belongs to a set with its own `scope`")); + } + let typing = set.typing; let scope = match &set.scope { Some(scope) => quote!(#k::runtime::commands::CommandScope::new(#scope)), None => quote!(#k::runtime::commands::CommandScope::APP), @@ -286,6 +294,7 @@ pub(crate) fn derive(input: TokenStream) -> syn::Result { Ok(quote! { impl #k::runtime::commands::CommandSet for #ident { const SCOPE: #k::runtime::commands::CommandScope = #scope; + const TYPING: bool = #typing; fn from_ref(command: &#k::runtime::commands::CommandRef) -> ::core::option::Option { match command.id.as_str() {