From ef7239a97b28744c6bd90b5c200a1561b78a5acc Mon Sep 17 00:00:00 2001 From: aiongg Date: Tue, 24 Nov 2026 16:34:04 +0000 Subject: [PATCH] A host canvas holding egui focus keeps Escape and its clipboard in egui A focused `Stop::host` (`focus::host_canvas`), whether the framework or the app's own egui request focused it, is the app's own widget for Escape and its edit set: Escape, the clipboard events and the edit chords of its edit set stay in egui's input, as they do for the canvas with nothing focused, instead of arriving as its `Intent::Cancel` and `Intent::Edit`. egui keeps its focus over the Escape, and a menu's edit command is the app's. Tab, the arrows and F6 still follow its `KeyUse`. --- crates/ui-contract/src/runtime/focus.rs | 18 +++--- crates/ui-contract/src/runtime/focus/nav.rs | 4 +- crates/ui-contract/src/runtime/keys/input.rs | 30 ++++++---- crates/ui-contract/src/runtime/keys/route.rs | 4 +- .../src/testing/host_key_trials.rs | 60 +++++++++++++++++++ 5 files changed, 94 insertions(+), 22 deletions(-) diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index 414c51e5a..405187cd9 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -239,15 +239,15 @@ pub(crate) fn register_stop(ctx: &Context, stop: Stop) { /// Registers the app's own egui widget `id`, drawn in the running pass, as its window's document canvas: a focus stop /// with `keys`, marked as the canvas (`KeyUse::chrome_key`). The window then shows a document, so with /// `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).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, the chords it claims -/// (`KeyUse::with_claims`; Alt+numpad code entry leaves a claimed Alt+digit alone), 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; Page Up and Down, -/// Home and End arrive as its `Intent::Move`, as while it has the focus. With the canvas focused, Tab it keeps and the -/// chords it claims stay 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. +/// reaches the canvas by Tab, F6, `request` or the app's own egui focus request; while it has focus, Tab and the arrows +/// route as `keys` says (the arrows as nudges, Tab moving focus on or toggling the panels unless it keeps Tab). `keys` +/// is `KeyUse::CANVAS`, or a variant such as `KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(..)` while Tab and the +/// clipboard are the canvas's own. The canvas reads its keys from egui: with nothing focused it is the key target, and +/// a bare Tab it keeps, the chords it claims (`KeyUse::with_claims`; Alt+numpad code entry leaves a claimed Alt+digit +/// alone), 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; Page Up and Down, Home and End arrive as its `Intent::Move`, as while it has the focus. +/// With the canvas focused, the same keys stay in egui, and Escape too, as for any focused widget of the app's own; +/// Escape with nothing focused is the window's cancel. 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)).host()); } diff --git a/crates/ui-contract/src/runtime/focus/nav.rs b/crates/ui-contract/src/runtime/focus/nav.rs index ab676cceb..58924165b 100644 --- a/crates/ui-contract/src/runtime/focus/nav.rs +++ b/crates/ui-contract/src/runtime/focus/nav.rs @@ -262,7 +262,9 @@ impl State { let stop = &rec.stop; let roving = stop.group.is_some(); let keys = stop.keys; - let escape = keys.claims.iter().any(|chord| chord.key == craft_keymap::Key::Escape); + // An app's own widget (`Stop::host`) reads its Escape from egui, as a stop that claims it does: egui keeps its + // focus on it. + let escape = stop.host || keys.claims.iter().any(|chord| chord.key == craft_keymap::Key::Escape); let filter = egui_focus::filter( keys.keep == super::KeepKeys::Tab, roving || matches!(keys.keep, super::KeepKeys::InlineArrows | super::KeepKeys::BlockArrows), diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 87e8fcace..f1f0c09e1 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -5,7 +5,7 @@ use super::{AltTrack, Batch, EscapePress, LastPress, MenuKey, NavKeys, Press, Sh use crate::runtime::commands::{self, EditCommands, FirePhase, NavCommand, WindowShortcut}; use crate::runtime::egui::input::{self as raw_input, Arrived, KeyEvent, RawInput}; use crate::runtime::env::Env; -use crate::runtime::focus::{self, EndKey, KeyUse}; +use crate::runtime::focus::{self, EditSet, EndKey, KeyUse}; use crate::runtime::frame::{ChromeToggle, request_chrome_toggle}; use crate::runtime::overlay; use crate::runtime::sampler; @@ -26,6 +26,7 @@ struct Strip<'a> { /// A press or a drag session is open: an Escape cancels it whatever the modifiers. press_captured: bool, claims: &'a Claims, + /// The edit commands the focused framework component takes as `Intent::Edit`. edits: &'a dyn Fn(EditCommand) -> bool, /// The chords bound to the edit commands that are the platform's text-editing chords. edit_chords: &'a [(EditCommand, Chord)], @@ -44,10 +45,17 @@ 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, + /// The focused stop is the app's own egui widget (`Stop::host`, `FocusedRecord::host`), which reads its keys from + /// egui: its Escape stays there, as an app's own focused widget's does. Tab and the arrows follow its `KeyUse`, as + /// the framework's navigation reads it. + host: 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. + /// `KeyUse` keeps (Tab with `KeepKeys::Tab`). canvas: Option, + /// The edit set whose clipboard events and edit chords stay in egui's input: the focused stop's when it is the + /// app's own widget (`host`), else the canvas's with nothing focused (`canvas`). + egui_edits: EditSet, /// 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. @@ -214,7 +222,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, .. } if self.egui_edits.contains(command) => true, KeyEvent::Clip { command: EditCommand::Copy | EditCommand::Cut, .. } if self.label_selection => true, // A clipboard event behind an open menu reaches nothing: the menu keeps it, as a platform menu does. The // chord's own key press still runs its command, which closes the menu. @@ -354,9 +362,8 @@ impl Strip<'_> { && key.is_letter() && ((mods == Mods::ALT && !self.shortcut_text(input)) || on_bar); 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 egui_edit = + self.edit_chords.iter().any(|(command, chord)| self.egui_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 @@ -370,7 +377,7 @@ impl Strip<'_> { PressUse::Edit(*command) } else if key == Key::Space && self.canvas_space { PressUse::CanvasSpace - } else if (self.foreign && (bare_tab || escape)) || canvas_tab || canvas_edit { + } else if (self.foreign && (bare_tab || escape)) || (self.host && escape && !self.press_captured) || canvas_tab || egui_edit { PressUse::Egui } else if bare_tab && self.no_stops { PressUse::TabToEgui @@ -430,8 +437,9 @@ impl Strip<'_> { } false } - // 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. + // The app's own focused widget (a host stop included) 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. PressUse::Egui => true, PressUse::TabToEgui => { // egui moves its own focus with it, from nothing focused into the app's own widgets (`focus::begin`). @@ -670,7 +678,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec Shortcuts { } /// Asks the focused component of the running viewport to take `command` as `Intent::Edit` in the next pass. -/// `false` when it does not list the command. A paste asks the platform for the clipboard text, which arrives as +/// `false` when it does not list the command, or is the app's own widget (`Stop::host`), whose edits are the app's. A paste asks the platform for the clipboard text, which arrives as /// the paste event that becomes the intent. Focus is read as it is now: a menu pick returns focus in its pass /// before its command fires, and the command goes where focus returned. pub(crate) fn queue_edit(ctx: &Context, command: EditCommand) -> bool { let viewport = crate::runtime::egui::pass::current_viewport(ctx); - if !focus::focused_rec(ctx).is_some_and(|rec| rec.stop.keys.edits.contains(command)) { + if !focus::focused_rec(ctx).is_some_and(|rec| !rec.stop.host && rec.stop.keys.edits.contains(command)) { return false; } if command == EditCommand::Paste { diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index dff07ee73..b8ac5d394 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -511,6 +511,66 @@ fn a_host_canvas_reads_the_clipboard_of_its_edit_set_with_nothing_focused() { assert!(!clipboard_with_canvas(KeyUse::CANVAS, chord).1.is_empty(), "a canvas without the command: the chord fires the app's Paste"); } +/// What a host canvas with GridCraft's keys saw of a copy and an Escape while it held egui focus: whether the copy +/// reached egui's input with no command fired, the keys that reached egui and the window cancels, and egui's focus +/// after the Escape. `app_focus` gives it egui focus by the app's own request, else by the framework's `request`. +fn focused_host_canvas_keys(app_focus: bool) -> (bool, (Vec, usize), Option) { + const KEYS: KeyUse = KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(EditSet::ALL); + let canvas = named_id("host canvas"); + let events = Rc::new(RefCell::new(Vec::new())); + let seen_events = Rc::clone(&events); + let (host, mut base) = host_window(); + let mut show = move |ui: &mut Ui| { + seen_events.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); + if app_focus { + fixture::host_focus(&frames.ctx, canvas); + } else { + request(&frames.ctx, FocusTarget::Id(canvas)); + } + frames.frame(Vec::new(), &mut show); + assert_eq!(egui_focused(&frames.ctx), Some(canvas), "the canvas holds egui focus"); + take_output(&frames.ctx); + seen(&host); + events.borrow_mut().clear(); + frames.frame(vec![Event::Copy], &mut show); + frames.frame(Vec::new(), &mut show); + let copied = events.borrow().contains(&Event::Copy) && take_output(&frames.ctx).fired.is_empty(); + press_key(&mut frames, &mut show, Key::Escape); + let escape = seen(&host); + frames.frame(Vec::new(), &mut show); + (copied, escape, egui_focused(&frames.ctx)) +} + +/// A host canvas holding egui focus is the app's own focused widget: its clipboard events and Escape stay in egui's +/// input, and no window cancel comes of the Escape. With nothing focused, Escape is the window's cancel. +#[trial] +fn a_host_canvas_with_egui_focus_reads_escape_and_the_clipboard_from_egui() { + let canvas = named_id("host canvas"); + let (copied, escape, focused) = focused_host_canvas_keys(false); + assert!(copied, "the copy of a canvas the framework focused stays in egui and fires nothing"); + assert_eq!(escape, (vec![Key::Escape], 0), "its Escape is egui's, and no window cancel"); + assert_eq!(focused, Some(canvas), "egui keeps the framework's focus on the canvas over its Escape"); + let (copied, escape, _) = focused_host_canvas_keys(true); + assert!(copied, "the copy of a canvas the app focused stays in egui and fires nothing"); + assert_eq!(escape, (vec![Key::Escape], 0), "and its Escape"); + let (host, mut base) = host_window(); + let mut show = move |ui: &mut Ui| { + base(ui); + widget(ui, canvas); + host_canvas(ui.ctx(), canvas, KeyUse::CANVAS.with_keep(KeepKeys::Tab).with_edits(EditSet::ALL)); + }; + let mut frames = Frames::new(); + frames.frame(Vec::new(), &mut show); + press_key(&mut frames, &mut show, Key::Escape); + assert_eq!(seen(&host), (Vec::new(), 1), "with nothing focused, Escape is the window's cancel"); +} + /// A clipboard with a picture and no text sends a paste with no text: it fires the app's Paste as the chord does, and /// `textless_paste` reads it in that pass only. #[trial]