ui-contract, ui-canvas: KeyUse::command_keys and Canvas::editor_command_keys: a canvas editor that has the keys leaves chords with the platform's command modifier and function keys to the app's commands under any typing rule, its text editing chords kept; off by default: under TypingRule::AllWait a type tool's editor ran none of the app's Cmd chords or function keys

This commit is contained in:
bflatastic
2026-11-24 00:58:13 +07:00
parent 83b922e633
commit 0726c0f87d
6 changed files with 75 additions and 4 deletions
+10 -1
View File
@@ -223,6 +223,11 @@ pub struct Canvas<'a> {
/// and Redo fall through to the app command's state. Ignored without [`editor`](Self::editor).
#[prop]
inactive_edits: EditSet,
/// While the editor part has the keys, chords with the platform's command modifier and function keys still run the
/// app's commands, whatever its typing rule ([`KeyUse::command_keys`]): a type tool whose app runs its commands
/// while typing. The editor keeps its text editing chords. Ignored without [`editor`](Self::editor).
#[prop]
editor_command_keys: bool,
/// The editor part's inputs of this pass, taken once in `update`: `tool_pass` is built from a shared `Cx`.
editor_input: Arc<[EditorInput]>,
/// The rulers' units and zero point, published to the body in its [`CanvasSpace`].
@@ -315,6 +320,7 @@ impl<'a> Canvas<'a> {
keys_to_body: false,
editor: None,
inactive_edits: EditSet::NONE,
editor_command_keys: false,
editor_input: Arc::from([]),
pasteboard: PasteboardColor::Default,
precise_cursors: false,
@@ -838,7 +844,10 @@ impl Component for Canvas<'_> {
/// nudges. It still marks its window as showing a document.
fn keys(&self) -> KeyUse {
match self.editor {
Some(_) => KeyUse::of(Input::Editor).with_chrome_key(true).with_moves().with_inactive(self.inactive_edits),
Some(_) => {
let keys = KeyUse::of(Input::Editor).with_chrome_key(true).with_moves().with_inactive(self.inactive_edits);
if self.editor_command_keys { keys.with_command_keys() } else { keys }
}
None => Self::KEYS,
}
}
+41 -1
View File
@@ -8,7 +8,9 @@ use craft_ui::canvas::{Canvas, tool_pass};
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
use craft_ui::primitives::{EditorPurpose, EditorState};
use craft_ui::runtime::access::Role;
use craft_ui::runtime::commands::{CommandRef, CommandState, EditCommands, resolve};
use craft_ui::runtime::commands::{
AppCatalog, Binding, CommandDef, CommandInput, CommandRef, CommandState, EditCommands, Platform, Scope, TypingRule, resolve,
};
use craft_ui::runtime::focus::{self, EditSet};
use craft_ui::runtime::frame;
use craft_ui::testing::focus::egui_focused;
@@ -213,3 +215,41 @@ fn inactive_edits_disable_cut_and_let_undo_and_redo_fall_through_to_app_state()
fixture.settle();
assert!(resolve(fixture.ctx(), EditCommands::Cut).enabled, "selecting text in the TextField enables Cut independently");
}
/// Under a typing rule that fires nothing while typing (`AllWait`), a canvas editor that lets command keys through
/// (`Canvas::editor_command_keys`) leaves a Cmd chord and a function key to the app's commands while it has the keys,
/// and a plain key bound to a command stays its typing; without it none of them fires. Guards a type tool whose app ran
/// its commands while typing.
#[trial]
fn a_canvas_editor_that_lets_command_keys_through_runs_them_while_typing() {
const ALIGN: &str = "canvasTrial.alignRight";
const ACTION: &str = "canvasTrial.action";
const TOOL: &str = "canvasTrial.tool";
for through in [false, true] {
let mut fixture = Fixture::new(move |ui| {
let room = ui.available_rect_before_wrap();
let view = CanvasView::new(room, Point::new(PAGE.width / 2.0, PAGE.height / 2.0), 1.0);
let _ = canvas(view).editor_command_keys(through).show_at(ui, room, |_, _| {});
});
fixture.os(Platform::Linux);
let def =
|id: &str, chord: &str| CommandDef::new(id, id, Scope::app()).with_default(Binding::parse(chord).expect("the trial's chord parses"));
let catalog = AppCatalog::new(vec![def(ALIGN, "Cmd+Shift+R"), def(ACTION, "F7"), def(TOOL, "P")], 1)
.expect("the trial's catalog is valid")
.typing_rule(TypingRule::AllWait);
fixture.commands(CommandInput::new().catalog(catalog));
fixture.room(ROOM);
fixture.settle();
fixture.click_at((ROOM / 2.0).to_pos2());
fixture.settle();
frame::take_output(fixture.ctx());
fixture.key(Key::R, Modifiers::COMMAND | Modifiers::SHIFT);
fixture.key(Key::F7, Modifiers::NONE);
fixture.key(Key::P, Modifiers::NONE);
fixture.settle();
let fired: Vec<String> =
frame::take_output(fixture.ctx()).fired.into_iter().map(|fired| fired.command.id).filter(|id| id.starts_with("canvasTrial.")).collect();
let expected: Vec<String> = if through { vec![ALIGN.to_owned(), ACTION.to_owned()] } else { Vec::new() };
assert_eq!(fired, expected, "command keys let through: {through}");
}
}
@@ -2080,7 +2080,14 @@ pub(crate) fn begin(ctx: &Context) {
let (_, instance) = with(ctx, |board| board.target_of(ctx, command, &board.here));
resolve_in(ctx, command, instance, Reach::Focus, None, Reads::default()).0.enabled
};
let typing = shortcuts.typing && !shortcut.canvas;
// A text component that lets command keys through leaves their presses to the app's commands.
let command_key = shortcuts.command_keys
&& shortcuts
.layout
.candidates(&shortcut.input)
.first()
.is_some_and(|chord| TypingRule::CommandKeysOnly.fires(chord, keys.keymap.platform()));
let typing = shortcuts.typing && !shortcut.canvas && !command_key;
let hold = key
.filter(|_| !typing && !modal_at_start)
.and_then(|key| keys.hold(&shortcuts.layout, &shortcut.input, typing).map(|hold| (key, hold)));
@@ -312,6 +312,10 @@ pub struct KeyUse {
/// The component is a text field whose draft a press on a part declared `holds_drafts`, or on a popup layer above
/// it, leaves unapplied; a lone Alt in the field only alerts and a window deactivation does not commit the draft.
pub holds_drafts: bool,
/// While the component types, chords with the platform's command modifier and function keys still run the app's
/// commands whatever the app's typing rule, as under `TypingRule::CommandKeysOnly`; the text editing chords stay
/// the component's. For a canvas tool's text editor whose app runs its commands while typing.
pub command_keys: bool,
}
/// Which Tab stops a dialog's Tab order visits.
@@ -348,6 +352,7 @@ impl KeyUse {
session: false,
type_ahead: false,
holds_drafts: false,
command_keys: false,
};
/// A divider between panes: it takes the arrows as moves of its value, but it is no document and no canvas tool, so
/// it marks no window as showing a document and takes no nudges.
@@ -389,6 +394,7 @@ impl KeyUse {
session: false,
type_ahead: false,
holds_drafts: false,
command_keys: false,
}
}
@@ -398,6 +404,12 @@ impl KeyUse {
self
}
/// The same use letting command chords and function keys run the app's commands while it types (`command_keys`).
pub const fn with_command_keys(mut self) -> Self {
self.command_keys = true;
self
}
/// The same use taking typed letters as `Intent::Text`.
pub const fn with_type_ahead(mut self) -> Self {
self.type_ahead = true;
+2
View File
@@ -133,6 +133,8 @@ pub(crate) struct Shortcuts {
pub(crate) keys: Vec<Shortcut>,
/// A text field owns the keys: plain keys wait for it.
pub(crate) typing: bool,
/// The text component that owns the keys leaves command chords and function keys to the app (`KeyUse::command_keys`).
pub(crate) command_keys: bool,
/// A dialog owns the keys: commands that do not fire in dialogs wait.
pub(crate) dialog: bool,
/// The layout that reads the presses.
+2 -1
View File
@@ -252,6 +252,7 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
let dialog = owner == KeyOwner::Class(OwnerClass::Dialog);
// The focused component keeps its own keys inside a dialog: a text field its typing, a widget its activation.
let typing = owner != KeyOwner::Blocked && own == Some(OwnerClass::Text);
let command_keys = typing && target.is_some_and(|rec| rec.stop.keys.command_keys);
let held: Vec<bool> = batch
.presses
.iter()
@@ -275,7 +276,7 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
.collect();
let keys = batch.shortcuts.iter().zip(&kept).filter(|(_, stays)| **stays).map(|(shortcut, _)| *shortcut).collect();
let mods = remap_mods(&batch.mods, &kept);
(Shortcuts { keys, typing, dialog, layout: Layout::default(), mods, focus_lost: batch.focus_lost }, held)
(Shortcuts { keys, typing, command_keys, dialog, layout: Layout::default(), mods, focus_lost: batch.focus_lost }, held)
}
/// Whether the focused stop `target`, whose keys `owner` owns, holds `press`: the press is its own and reaches neither