From fbea3a0574ae56acdf6ec04e0d2f9f8e45c7372c Mon Sep 17 00:00:00 2001 From: aiongg Date: Thu, 12 Nov 2026 08:09:59 +0000 Subject: [PATCH] Runtime: with nothing focused, a canvas that keeps Tab gets the bare Tab from egui and nothing toggles --- crates/ui-contract/src/runtime/focus.rs | 11 +++++++++-- crates/ui-contract/src/runtime/keys/input.rs | 13 ++++++++++--- crates/ui-contract/src/testing/host_key_trials.rs | 13 ++++++++++++- docs/craft-framework.md | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index f0d0e2dda..986136ecf 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -191,12 +191,19 @@ 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. Call it each pass -/// the canvas shows. +/// `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. pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) { register_stop(ctx, Stop::new(id, keys.with_chrome_key(true))); } +/// The keys of the document canvas `viewport` registered in its last pass (`KeyUse::chrome_key`), framework or the +/// app's own. Read before the begin hook rotates the registrations, by the input hook and the begin hook's Tab moves. +pub(crate) fn canvas_keys(ctx: &Context, viewport: ViewportId) -> Option { + with_viewport(ctx, viewport, |state| state.stops.iter().find(|rec| rec.stop.keys.chrome_key).map(|rec| rec.stop.keys)) +} + /// How many stops the running pass has registered so far. pub(crate) fn stop_count(ctx: &Context) -> usize { with(ctx, |state| state.stops.len()) diff --git a/crates/ui-contract/src/runtime/keys/input.rs b/crates/ui-contract/src/runtime/keys/input.rs index 0e7134439..3bb9e74ab 100644 --- a/crates/ui-contract/src/runtime/keys/input.rs +++ b/crates/ui-contract/src/runtime/keys/input.rs @@ -35,6 +35,9 @@ 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, /// egui holds a label text selection, which a copy or cut event copies. label_selection: bool, } @@ -181,8 +184,9 @@ impl Strip<'_> { batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: None }); } false - } else if self.foreign && (bare_tab || escape) { - // The app's own focused widget or popup reads the key from egui; the runtime routes nothing for it. + } 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. true } else if bare_tab && !self.keeps_tab { batch.tabs.push(TabPress { input: *input, repeat }); @@ -267,6 +271,8 @@ pub(crate) fn input_hook(ctx: &Context, input: &mut RawInput) -> bool { 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 latin = input.latin_layout_chars.clone(); let (batch, visible) = with(ctx, viewport, |stage| { let mut claims = record.claims.clone(); @@ -292,6 +298,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, label_selection: record.label_selection, }; let mut batch = Batch::default(); @@ -358,7 +365,7 @@ pub(crate) fn take_nav(ctx: &Context) -> NavKeys { let viewport = crate::runtime::egui::pass::current_viewport(ctx); let enabled = Env::root_for(ctx, viewport).is_some_and(|env| env.tab_toggles_chrome); let focused = focus::focused_rec(ctx); - let canvas = focus::with(ctx, |state| state.stops.iter().any(|rec| rec.stop.keys.chrome_key)); + let canvas = focus::canvas_keys(ctx, viewport).is_some(); let layer = overlay::key_layer(ctx, &focused.as_ref().map(|rec| focus::scope_chain(ctx, rec)).unwrap_or_default()); let gate = tab_toggles_chrome(enabled, canvas, layer, focused.as_ref().map(|rec| &rec.stop.keys)); for press in tabs { diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 50544f604..5ca38d797 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -308,8 +308,14 @@ fn tab_with_canvas(keys: Option, focused: bool) -> (Vec<(ViewportId, Chr } take_output(&frames.ctx); seen(&host); + let before = egui_focused(&frames.ctx); press_key(&mut frames, &mut show, Key::Tab); - (take_output(&frames.ctx).chrome_toggles, seen(&host).0.contains(&Key::Tab)) + let reached = seen(&host).0.contains(&Key::Tab); + if reached { + frames.frame(Vec::new(), &mut show); + assert_eq!(egui_focused(&frames.ctx), before, "a Tab the canvas keeps moves no egui focus"); + } + (take_output(&frames.ctx).chrome_toggles, reached) } #[trial] @@ -322,6 +328,11 @@ fn a_host_canvas_marks_the_window_and_tab_toggles_its_panels() { (Vec::new(), true), "a focused canvas that keeps Tab gets it from egui" ); + assert_eq!( + tab_with_canvas(Some(KeyUse::CANVAS.with_keep(KeepKeys::Tab)), false), + (Vec::new(), true), + "with nothing focused, a canvas that keeps Tab gets it from egui and nothing toggles" + ); } /// What an F6 press did under `overrides`: whether it reached egui, and the ids of the commands it fired. diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 1d9251e21..1b1d1ddac 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -149,7 +149,7 @@ 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). 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`. | +| 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). 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. | | 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). |