Runtime: with nothing focused, the clipboard events and edit chords of the canvas's edit set stay in egui for it

This commit is contained in:
aiongg
2026-11-12 14:32:49 +00:00
parent b55b820460
commit 38fb06debd
4 changed files with 61 additions and 22 deletions
+5 -3
View File
@@ -193,9 +193,11 @@ pub(crate) fn register_stop(ctx: &Context, stop: Stop) {
/// `RootEnv::tab_toggles_chrome` a bare Tab with nothing focused toggles its panels, as with a framework canvas. Focus
/// reaches the canvas by Tab, F6 or `request`; while it has focus, its keys route as `keys` says (the arrows as
/// nudges, Escape as its `Intent::Cancel`). `keys` is `KeyUse::CANVAS`, or a variant such as
/// `KeyUse::CANVAS.with_keep(KeepKeys::Tab)` while the canvas edits text in place and Tab is its own: with the canvas
/// focused or nothing focused, a bare Tab then stays in egui's input for the app and toggles nothing. Call it each
/// pass the canvas shows.
/// `KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(..)` while the canvas edits text in place and Tab and the
/// clipboard are its own. With nothing focused the canvas is the key target: a bare Tab it keeps, and the clipboard
/// events and edit chords of its edit set, stay in egui's input for the app, and nothing toggles or fires with them.
/// With the canvas focused, Tab it keeps stays in egui too, and its edit set arrives as `Intent::Edit`
/// (`keys::intents_of`), as for any focused stop. Call it each pass the canvas shows.
pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) {
register_stop(ctx, Stop::new(id, keys.with_chrome_key(true)));
}
+16 -15
View File
@@ -35,9 +35,10 @@ struct Strip<'a> {
/// An app's own egui widget or popup has the focus (`FocusedRecord::foreign`): Tab, Escape and the clipboard
/// events are its own.
foreign: bool,
/// Nothing has the focus and the window's canvas keeps Tab (`KeepKeys::Tab`): the canvas is the key target, as
/// the chrome gate takes it, and reads a bare Tab from egui.
canvas_keeps_tab: bool,
/// The keys of the window's canvas while nothing has the focus, neither a framework stop nor an app's own widget
/// or popup: the canvas is then the key target, as the chrome gate takes it, and reads from egui the keys its
/// `KeyUse` keeps (Tab with `KeepKeys::Tab`) and the clipboard events and edit chords of its edit set.
canvas: Option<KeyUse>,
/// egui holds no focus and the window registered no focus stop in its last pass: a framework focus move has
/// nowhere to go, so a bare Tab is egui's. Focus on a component's widget without a stop is focus: egui would take
/// a Tab from it as a move to the next widget it shows, in a later pass.
@@ -102,6 +103,7 @@ impl Strip<'_> {
false
}
KeyEvent::Clip { .. } if self.foreign => true,
KeyEvent::Clip { command, .. } if self.canvas.is_some_and(|canvas| canvas.edits.contains(command)) => true,
KeyEvent::Clip { command: EditCommand::Copy | EditCommand::Cut, .. } if self.label_selection => true,
KeyEvent::Clip { command, .. } => {
// The platform sends the clipboard event in place of the chord: an unlisted one is the chord. The
@@ -169,6 +171,10 @@ impl Strip<'_> {
let escape = key == Key::Escape && !mods.cmd && !mods.ctrl && !(self.records && !mods.is_none());
let mnemonic = self.menu_bar && self.os != Os::Mac && mods == Mods::ALT && key.is_letter();
let edit = self.edit_chords.iter().find(|(command, chord)| (self.edits)(*command) && self.layout.matches(chord, input, self.os));
let canvas_edit = self.canvas.is_some_and(|canvas| {
self.edit_chords.iter().any(|(command, chord)| canvas.edits.contains(*command) && self.layout.matches(chord, input, self.os))
});
let canvas_tab = bare_tab && self.canvas.is_some_and(|canvas| canvas.keep == focus::KeepKeys::Tab);
// The chrome keys are Tab's, which the bare-Tab arm and `take_nav` read.
let window_shortcut = self
.window_shortcuts
@@ -188,9 +194,9 @@ impl Strip<'_> {
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: None });
}
false
} else if (self.foreign && (bare_tab || escape)) || (self.canvas_keeps_tab && bare_tab) {
// The app's own focused widget or popup, or the canvas that keeps Tab with nothing focused, reads the key
// from egui; the runtime routes nothing for it, so no shortcut toggles the chrome with it either.
} else if (self.foreign && (bare_tab || escape)) || canvas_tab || canvas_edit {
// The app's own focused widget or popup, or the canvas with nothing focused, reads the key from egui; the
// runtime routes nothing for it, so no shortcut toggles the chrome or fires an edit command with it either.
true
} else if bare_tab && self.no_stops {
// egui moves its own focus with it, from nothing focused into the app's own widgets (`focus::begin`).
@@ -272,15 +278,10 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool {
let keys = record.keys;
let menu_bar = record.menu_bar && keys.is_none_or(|keys| !matches!(keys.owner, OwnerClass::Text | OwnerClass::Recorder));
let before = input.events.len();
let edit_chords = if keys.is_some_and(|keys| EditCommands::ALL.iter().any(|command| command.edit().is_some_and(|edit| keys.edits.contains(edit))))
{
commands::edit_chords(ctx)
} else {
Arc::default()
};
let canvas = focus::canvas_keys(ctx, viewport).filter(|_| keys.is_none() && !record.foreign);
let lists_edits = |keys: &KeyUse| EditCommands::ALL.iter().any(|command| command.edit().is_some_and(|edit| keys.edits.contains(edit)));
let edit_chords = if keys.as_ref().or(canvas.as_ref()).is_some_and(lists_edits) { commands::edit_chords(ctx) } else { Arc::default() };
let window_shortcuts = commands::window_shortcuts(ctx);
let canvas_keeps_tab =
keys.is_none() && !record.foreign && focus::canvas_keys(ctx, viewport).is_some_and(|canvas| canvas.keep == focus::KeepKeys::Tab);
let no_stops = keys.is_none() && !record.foreign && focus::tab_from_nothing(ctx, viewport);
let latin = input.latin_layout_chars.clone();
let (batch, visible) = with(ctx, viewport, |stage| {
@@ -307,7 +308,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool {
menu_bar,
canvas_space: hole.as_ref().is_some_and(|hole| hole.space || hole.space_pressed),
foreign: record.foreign,
canvas_keeps_tab,
canvas,
no_stops,
label_selection: record.label_selection,
};
@@ -4,7 +4,7 @@ use crate::runtime::commands::{CommandInput, CommandSet as _, EditCommands, NavC
use crate::runtime::drag::{self, DragPreview};
use crate::runtime::egui::fixture;
use crate::runtime::env::RootEnv;
use crate::runtime::focus::{FocusTarget, KeepKeys, KeyUse, host_canvas, request};
use crate::runtime::focus::{EditSet, FocusTarget, KeepKeys, KeyUse, host_canvas, request};
use crate::runtime::frame::{ChromeToggle, take_output};
use crate::runtime::keys::with_window_intents;
use crate::testing::focus::{Stop, egui_focused, named_id, register_stop, widget};
@@ -12,7 +12,7 @@ use crate::testing::input::{key_press, key_reached, slop};
use crate::testing::pointer::{self, moved, press, release};
use crate::testing::sensing::{self, ProbeRoot};
use crate::testing::{Frames, trial};
use craft_behavior::{Input, Intent, Sense};
use craft_behavior::{EditCommand, Input, Intent, Sense};
use egui::{Event, Id, ImeEvent, Key, Modifiers, Pos2, Rect, Ui, ViewportId};
use std::cell::RefCell;
use std::rc::Rc;
@@ -433,6 +433,42 @@ fn a_host_canvas_marks_the_window_and_tab_toggles_its_panels() {
);
}
/// What `event` did with nothing focused in a window whose host declares its canvas with `keys`: whether the event
/// reached egui's input, and the ids of the commands it fired.
fn clipboard_with_canvas(keys: KeyUse, event: Event) -> (bool, Vec<String>) {
let canvas = named_id("host canvas");
let events = Rc::new(RefCell::new(Vec::new()));
let seen = Rc::clone(&events);
let (_, mut base) = host_window();
let mut show = move |ui: &mut Ui| {
seen.borrow_mut().extend(ui.input(|input| input.events.clone()));
base(ui);
widget(ui, canvas);
host_canvas(ui.ctx(), canvas, keys);
};
let mut frames = Frames::new();
frames.frame(Vec::new(), &mut show);
take_output(&frames.ctx);
events.borrow_mut().clear();
frames.frame(vec![event.clone()], &mut show);
frames.frame(Vec::new(), &mut show);
let fired = take_output(&frames.ctx).fired.into_iter().map(|fired| fired.command.id).collect();
(events.borrow().contains(&event), fired)
}
#[trial]
fn a_host_canvas_reads_the_clipboard_of_its_edit_set_with_nothing_focused() {
let edits = EditSet::NONE.with(EditCommand::Cut).with(EditCommand::Copy).with(EditCommand::Paste);
let typing = KeyUse::CANVAS.with_edits(edits);
let paste = Event::Paste("x".to_owned());
assert_eq!(clipboard_with_canvas(typing, paste.clone()), (true, Vec::new()), "the canvas's paste stays in egui and fires nothing");
assert_eq!(clipboard_with_canvas(typing, Event::Cut), (true, Vec::new()), "and its cut");
assert!(!clipboard_with_canvas(KeyUse::CANVAS, paste).0, "a canvas without the command: the paste is the app's chord");
let chord = key_press(Key::V, Modifiers::COMMAND);
assert_eq!(clipboard_with_canvas(typing, chord.clone()), (true, Vec::new()), "the canvas's chord stays in egui and fires nothing");
assert!(!clipboard_with_canvas(KeyUse::CANVAS, chord).1.is_empty(), "a canvas without the command: the chord fires the app's Paste");
}
/// What an F6 press did under `overrides`: whether it reached egui, and the ids of the commands it fired.
fn f6_under(overrides: Option<craft_keymap::Overrides>) -> (bool, Vec<String>) {
let (host, mut show) = host_window();
+2 -2
View File
@@ -149,11 +149,11 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
| What | Effect on the app's own egui code |
| --- | --- |
| Tab | Bare Tab moves framework focus and never reaches egui, unless the focused framework stop keeps Tab or an app's own widget has the focus (below), or nothing has the focus in a window that registered no focus stop: egui then moves its own focus into the app's widgets. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus framework controls the platform's rule skips. With `RootEnv::tab_toggles_chrome`, Tab toggles the chrome in a window that shows a document canvas, with nothing focused or the canvas focused, and arrives in `FrameOutput::chrome_toggles`. The canvas is a framework canvas, or the app's own egui canvas declared each pass with `ui::runtime::focus::host_canvas`. A canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) is the Tab's target with nothing focused, as when it is focused: the press stays in egui's input for the app and toggles nothing. |
| Tab | Bare Tab moves framework focus and never reaches egui, unless the focused framework stop keeps Tab or an app's own widget has the focus (below), or nothing has the focus in a window that registered no focus stop: egui then moves its own focus into the app's widgets. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus framework controls the platform's rule skips. With `RootEnv::tab_toggles_chrome`, Tab toggles the chrome in a window that shows a document canvas, with nothing focused or the canvas focused, and arrives in `FrameOutput::chrome_toggles`. The canvas is a framework canvas, or the app's own egui canvas declared each pass with `ui::runtime::focus::host_canvas`. With nothing focused, the canvas is the key target: a canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input, as when it is focused, and nothing toggles. |
| F6 | F6 and Shift+F6 move between panes and never reach egui. They are the defaults of the commands `window.nextPane` and `window.previousPane` (`NavCommand`): a default chord that another command binds is that command's, and the overrides move or unbind them (`CommandInput::overrides`). An app whose own shortcut table keeps F6 unbinds them there. |
| Escape | Plain Escape (Alt and Shift may be held) goes to the focused framework stop and its scopes; with no framework stop focused, it goes to the window as `Intent::Cancel`. It reaches egui only while an app's own widget has the focus (below). An Escape the runtime takes outside a captured framework press also ends egui's own drag in that pass, as egui's Escape does. |
| F10, Alt+letter | Taken while a framework menu bar is registered: F10 (`window.focusMenuBar`, bound and yielded as F6 is) focuses the bar, Alt+letter opens a menu (not on macOS). |
| Edit chords | Taken while a framework editor is focused. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). While egui holds a label text selection, copy and cut stay egui's, and egui copies the selection. |
| Edit chords | Taken while a framework editor is focused. With nothing focused, the edit chords and egui's copy, cut and paste events of the commands in the window canvas's edit set (`KeyUse::edits`, `host_canvas`) stay in egui's input for it, and no command fires with them. Otherwise egui's copy, cut and paste events become the ⌘C/X/V chord for the app's commands, unless an app's own widget has the focus (below). While egui holds a label text selection, copy and cut stay egui's, and egui copies the selection. |
| IME | Taken while a framework editor owns the IME: its events and `PlatformOutput.ime` are the runtime's. Otherwise they stay egui's, for the app's own `TextEdit` or IME code. |
| An app's own focused widget | While egui focus is on a widget no framework component made, or an egui popup is open, and no framework press or drag is captured, Tab, Shift+Tab, Escape and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them. While egui focus is on such a widget, the arrows and Tab move egui focus among the app's widgets as egui does; while a framework stop has the focus, egui's arrow moves are off. |
| Releases | The release of every key the runtime took is taken too. |