ui-contract: a focused text part takes as its edits only the bindings of Cut, Copy, Paste and the rest that are the platform's text-editing chords: an app's alternate key (F4 for Paste) went to the field too, where production left the text alone

This commit is contained in:
bflatastic
2026-11-23 19:27:29 +07:00
parent 0c633a83cd
commit bbd5eb35d1
3 changed files with 34 additions and 13 deletions
@@ -1040,10 +1040,15 @@ fn build_keymap(
// Delete's keys (Delete, Backspace) stay a focused text part's own deletion, which also deletes one character.
.filter(|(edit, _)| *edit != EditCommand::Delete)
.flat_map(|(edit, command)| {
// Only the platform's own text-editing chords: an app's alternate key (F4 for Paste) is a command key,
// which the typing rule decides on, as a text field elsewhere on the platform ignores it.
keymap
.bindings(command)
.into_iter()
.filter(|binding| binding.trigger.press == PressKind::Single)
.filter(|binding| {
binding.trigger.press == PressKind::Single
&& craft_keymap::text_editing_chords(platform).contains(&binding.trigger.chord.effective(platform))
})
.map(move |binding| (edit, binding.trigger.chord))
})
.collect();
+9 -11
View File
@@ -27,7 +27,7 @@ struct Strip<'a> {
press_captured: bool,
claims: &'a [Chord],
edits: &'a dyn Fn(EditCommand) -> bool,
/// The chords bound to the edit commands.
/// The chords bound to the edit commands that are the platform's text-editing chords.
edit_chords: &'a [(EditCommand, Chord)],
/// The chords bound to the window's keys (`commands::window_shortcuts`).
window_shortcuts: &'a [(WindowShortcut, Chord)],
@@ -98,16 +98,14 @@ fn clip_chord(command: EditCommand, mods: Mods) -> (Key, KeyInput) {
(key, KeyInput::new(chord_mods).with_typed(key))
}
impl Strip<'_> {
/// Whether a clipboard event of `command` with `mods` held is the focused editor's edit: the platform sends it for
/// the edit's own chord, and for Cmd with Shift or Alt too (Cmd+Shift+X), which is another chord, the app's, unless
/// the editor lists that chord for the edit.
fn clip_is_edit(&self, command: EditCommand, mods: Mods) -> bool {
let (_, input) = clip_chord(command, mods);
!((mods.cmd || mods.ctrl) && (mods.shift || mods.alt))
|| self.edit_chords.iter().any(|(edit, chord)| *edit == command && self.layout.matches(chord, &input, self.os))
}
/// Whether a clipboard event with `mods` held is the focused editor's edit: the platform sends it for the edit's own
/// chord, and for Cmd with Shift or Alt too (Cmd+Shift+X), which is no text-editing chord but the app's (a binding of it
/// to the edit still reaches the editor, through the command).
fn clip_is_edit(mods: Mods) -> bool {
!((mods.cmd || mods.ctrl) && (mods.shift || mods.alt))
}
impl Strip<'_> {
/// Whether the text the platform sends with the press `input` stands for is a shortcut's own and belongs to no
/// field: Alt is held and the keymap binds the press to a command that is enabled, so it fires (S-L177). A platform
/// sends its key's text with an Alt shortcut (Alt+Q on Linux). A bound Alt chord whose command is disabled does
@@ -201,7 +199,7 @@ impl Strip<'_> {
batch.egui_texts += 1;
true
}
KeyEvent::Clip { command, text, mods } if (self.edits)(command) && self.clip_is_edit(command, mods) => {
KeyEvent::Clip { command, text, mods } if (self.edits)(command) && clip_is_edit(mods) => {
batch.clips.push((command, batch.place));
if text.is_some() {
batch.paste = text;
@@ -5,7 +5,7 @@
use craft_behavior::{EditCommand, Input};
use craft_keymap::{Chord, Key as ChordKey, Mods, Overrides};
use craft_ui::runtime::commands::{ChromeCommand, CommandInput, CommandSet as _};
use craft_ui::runtime::commands::{AppCatalog, Binding, ChromeCommand, CommandDef, CommandInput, CommandSet as _, EditCommands, Scope, TypingRule};
use craft_ui::runtime::focus::{FocusTarget, KeyUse, request};
use craft_ui::runtime::frame::take_output;
use craft_ui::testing::focus::{Stop, intents_of, key_release, named_id, register_stop, widget};
@@ -130,3 +130,21 @@ fn a_cut_with_shift_held_is_the_apps_chord() {
assert!(!ran(&mut frames, &mut show, &field, vec![Event::ModifiersChanged(Modifiers::COMMAND), Event::Cut]), "Cmd+X runs no command");
assert_eq!(field.borrow().edits, [EditCommand::Cut], "a plain Cut is the field's");
}
/// An app's alternate key for Cut (F2) is no edit of a focused field: under a typing rule that fires nothing while
/// typing it leaves the field alone, while the platform's Cmd+X still cuts. Guards against a key stage that gave a
/// field every binding of an edit command, the app's function-key aliases included.
#[trial]
fn an_apps_alternate_edit_key_is_no_field_edit() {
let field = RefCell::new(Field::default());
let alternate = CommandDef::new(EditCommands::Cut, "Cut", Scope::app()).with_default(Binding::parse("F2").expect("the trial's chord parses"));
let catalog = AppCatalog::new(vec![alternate], 1).expect("the trial's catalog is valid").typing_rule(TypingRule::AllWait);
let mut frames = Frames::new();
frames.commands(CommandInput::new().catalog(catalog));
let mut show = window(&field);
settle(&mut frames, &mut show, true);
ran(&mut frames, &mut show, &field, vec![key_press(Key::F2, Modifiers::NONE), key_release(Key::F2, Modifiers::NONE)]);
assert!(field.borrow().edits.is_empty(), "F2 cut nothing in the field: {:?}", field.borrow().edits);
ran(&mut frames, &mut show, &field, vec![key_press(Key::X, Modifiers::COMMAND), key_release(Key::X, Modifiers::COMMAND)]);
assert_eq!(field.borrow().edits, [EditCommand::Cut], "the platform's Cmd+X is the field's Cut");
}