From 4bdb8b850a36599aacf778a8331d07ba67652fa1 Mon Sep 17 00:00:00 2001 From: aiongg Date: Wed, 25 Nov 2026 13:10:39 +0000 Subject: [PATCH] host_canvas: a canvas widget that takes no focus is the same finding A widget drawn with the bare Sense::DRAG or Sense::CLICK is not focusable, and egui takes the focus from it when it is drawn, so Tab is lost there too. The end hook reads the sense of the canvas's widget and names it in the finding. --- .../ui-contract/src/runtime/egui/fixture.rs | 6 ++ crates/ui-contract/src/runtime/egui/focus.rs | 8 +-- crates/ui-contract/src/runtime/focus.rs | 31 ++++----- .../src/testing/host_key_trials.rs | 65 +++++++++++++------ docs/craft-framework.md | 2 +- 5 files changed, 71 insertions(+), 41 deletions(-) diff --git a/crates/ui-contract/src/runtime/egui/fixture.rs b/crates/ui-contract/src/runtime/egui/fixture.rs index c7a8eca3e6..e2434526e6 100644 --- a/crates/ui-contract/src/runtime/egui/fixture.rs +++ b/crates/ui-contract/src/runtime/egui/fixture.rs @@ -618,6 +618,12 @@ pub(crate) fn host_drag_area(ui: &Ui, id: egui::Id, rect: Rect) -> (bool, bool) (response.dragged(), response.drag_stopped()) } +/// A host's own egui widget `id` at `rect` that senses drags and takes no focus. +#[cfg(feature = "suite")] +pub(crate) fn host_unfocusable_area(ui: &Ui, id: egui::Id, rect: Rect) { + ui.interact(rect, id, egui::Sense::DRAG); +} + /// A host's own selectable egui label of `text`; returns its rect. #[cfg(feature = "suite")] pub(crate) fn host_label(ui: &mut Ui, text: &str) -> Rect { diff --git a/crates/ui-contract/src/runtime/egui/focus.rs b/crates/ui-contract/src/runtime/egui/focus.rs index 6e845caa13..01c16aca8c 100644 --- a/crates/ui-contract/src/runtime/egui/focus.rs +++ b/crates/ui-contract/src/runtime/egui/focus.rs @@ -33,10 +33,10 @@ pub(crate) fn candidates_since(ctx: &Context, seen: usize) -> Vec::to_vec).unwrap_or_default()) } -/// Whether egui keeps the focus of `id` at the end of the running pass: an interactive widget drawn so far has the id -/// (`PassState::used_ids`, which egui's end pass checks the focused id against). -pub(crate) fn keeps_focus_of(ctx: &Context, id: Id) -> bool { - ctx.viewport(|viewport| viewport.this_pass.used_ids.contains_key(&id)) +/// What the widget `id` drawn so far in the running pass senses; `None` when no widget has the id. egui takes the focus +/// from a widget that is not focusable when it is drawn, and from an id no interactive widget has at the pass's end. +pub(crate) fn drawn_sense(ctx: &Context, id: Id) -> Option { + ctx.viewport(|viewport| viewport.this_pass.widgets.get(id).map(|widget| widget.sense)) } /// Whether the widget egui holds key focus on keeps Tab by its event filter (`Memory::focus_lock_filter`). diff --git a/crates/ui-contract/src/runtime/focus.rs b/crates/ui-contract/src/runtime/focus.rs index b3275feedd..10ed5a645e 100644 --- a/crates/ui-contract/src/runtime/focus.rs +++ b/crates/ui-contract/src/runtime/focus.rs @@ -326,11 +326,12 @@ fn take_app_widgets(ctx: &Context, except: Option) { /// canvas cannot trap keyboard navigation. With the canvas focused, the same keys stay in egui, a Tab it keeps and /// Escape too, as for any focused widget of the app's own; Escape with nothing focused is the window's cancel. /// -/// Call it in each pass the canvas shows, before or after the canvas is drawn, with the id of the interactive egui widget -/// the app draws for it in every such pass (`ui.interact(rect, id, Sense::click())`). egui drops the focus of an id that -/// no such widget of the pass has, so Tab would lose the focus at the canvas and start over at the window's first stop. -/// A pass that declares a canvas and draws no widget with its id reports the finding `canvas-without-widget`, and debug -/// builds log it once. A pass that hides the canvas makes no call. +/// Call it in each pass the canvas shows, before or after the canvas is drawn, with the id of the focusable egui widget +/// the app draws for it in every such pass (`Sense::click()`, `Sense::drag()`; the bare flags `Sense::CLICK` and +/// `Sense::DRAG` are not focusable). egui drops the focus of an id that no such widget of the pass has, so Tab would +/// lose the focus at the canvas and start over at the window's first stop. A pass that declares a canvas and draws no +/// such widget reports the finding `canvas-without-widget`, and debug builds log it once. A pass that hides the canvas +/// makes no call. pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) { if !crate::runtime::measure::active(ctx) { with(ctx, |state| state.host_canvases.push(id)); @@ -1116,20 +1117,20 @@ pub(crate) fn surrender_after_scopes(ctx: &Context, widget: Id) { with(ctx, |state| state.surrender_after_scopes = Some(widget)); } -/// Reports each canvas the running pass declared (`host_canvas`) whose id no interactive egui widget of the pass has. -/// Read at the pass's end, since a canvas may be declared before it is drawn. A canvas hidden for one pass and still -/// declared counts: egui drops its focus at the end of that pass. +/// Reports each canvas the running pass declared (`host_canvas`) whose id no focusable egui widget of the pass has. Read +/// at the pass's end, since a canvas may be declared before it is drawn. A canvas hidden for one pass and still declared +/// counts: egui drops its focus at the end of that pass. fn report_undrawn_canvases(ctx: &Context) { for id in with(ctx, |state| std::mem::take(&mut state.host_canvases)) { - if egui_focus::keeps_focus_of(ctx, id) { - continue; - } + let subject = match egui_focus::drawn_sense(ctx, id) { + Some(sense) if sense.is_focusable() => continue, + Some(sense) => format!("{id:?}: its widget senses {sense:?}, which takes no focus"), + None => format!("{id:?}: no egui widget with the id was drawn"), + }; if cfg!(debug_assertions) { - log_once(format!("canvas:{id:?}"), || { - log::warn!("`host_canvas` declared the canvas {id:?}, but no interactive egui widget with that id was drawn in the pass"); - }); + log_once(format!("canvas:{subject}"), || log::warn!("`host_canvas` declared the canvas {subject}")); } - inspect::finding(ctx, inspect::Finding { rule: "canvas-without-widget", component: "host_canvas", subject: format!("{id:?}") }); + inspect::finding(ctx, inspect::Finding { rule: "canvas-without-widget", component: "host_canvas", subject }); } } diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index 1ad0c30260..d717b889e5 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -999,34 +999,57 @@ fn a_host_canvas_marks_the_window_and_tab_toggles_its_panels() { ); } -/// Guards the finding that tells an app its canvas stop takes Tab and loses the focus: `host_canvas` named an id that no -/// egui widget of the pass has. A canvas declared before it is drawn, and one neither declared nor drawn, give none. -#[trial] -fn a_host_canvas_no_widget_draws_is_a_finding() { +/// How an app's window shows the canvas of `host_canvas_findings`. +#[derive(Clone, Copy, Debug)] +enum Shown { + /// Neither declared nor drawn. + Hidden, + /// Declared, and drawn with a focusable widget that senses clicks or drags. + Click, + Drag, + /// Declared, and drawn with a widget that senses drags and is not focusable. + Unfocusable, + /// Declared and not drawn. + Undrawn, +} + +/// Whether the canvas holds egui focus after a Tab from nothing in a window that shows it as `shown`, declared before it +/// is drawn, and the subjects of the findings on it. +fn host_canvas_findings(shown: Shown) -> (bool, Vec) { let canvas = named_id("host canvas"); - let (declared, drawn) = (Rc::new(Cell::new(true)), Rc::new(Cell::new(true))); - let (declares, draws) = (Rc::clone(&declared), Rc::clone(&drawn)); let mut show = move |ui: &mut Ui| { - if declares.get() { + if !matches!(shown, Shown::Hidden) { host_canvas(ui.ctx(), canvas, KeyUse::CANVAS); } - if draws.get() { - widget(ui, canvas); + let rect = Rect::from_min_size(Pos2::new(100.0, 300.0), egui::vec2(80.0, 30.0)); + match shown { + Shown::Hidden | Shown::Undrawn => {} + Shown::Click => _ = fixture::host_button(ui, canvas, rect), + Shown::Drag => _ = fixture::host_drag_area(ui, canvas, rect), + Shown::Unfocusable => fixture::host_unfocusable_area(ui, canvas, rect), } }; let mut frames = Frames::new(); - frames.idle(3, &mut show); - declared.set(false); - drawn.set(false); - frames.idle(1, &mut show); - assert!(findings(&frames.ctx).is_empty(), "a drawn canvas and a hidden one: {:?}", findings(&frames.ctx)); - declared.set(true); - frames.idle(1, &mut show); - let found = findings(&frames.ctx); - assert!( - matches!(found.as_slice(), [only] if only.rule == "canvas-without-widget" && only.subject == format!("{canvas:?}")), - "the declared canvas no widget draws: {found:?}" - ); + frames.idle(2, &mut show); + press_key(&mut frames, &mut show, Key::Tab); + frames.idle(2, &mut show); + let found = findings(&frames.ctx).into_iter().filter(|finding| finding.rule == "canvas-without-widget"); + (egui_focused(&frames.ctx) == Some(canvas), found.map(|finding| finding.subject).collect()) +} + +/// Guards the finding that tells an app its canvas stop takes Tab and loses the focus: `host_canvas` named an id that no +/// focusable egui widget of the pass has. +#[trial] +fn a_host_canvas_no_focusable_widget_draws_is_a_finding() { + for shown in [Shown::Click, Shown::Drag] { + assert_eq!(host_canvas_findings(shown), (true, Vec::new()), "{shown:?}: Tab stays at a canvas with a focusable widget"); + } + assert_eq!(host_canvas_findings(Shown::Hidden).1, Vec::::new(), "a hidden canvas makes no call"); + for (shown, says) in [(Shown::Undrawn, "no egui widget"), (Shown::Unfocusable, "Sense")] { + let (focused, found) = host_canvas_findings(shown); + assert!(!focused, "{shown:?}: egui drops the focus Tab gave the canvas"); + assert!(matches!(found.as_slice(), [only] if only.contains(says)), "{shown:?}: {found:?}"); + } } /// What `event` did with nothing focused in a window whose host declares its canvas with `keys`: whether the event diff --git a/docs/craft-framework.md b/docs/craft-framework.md index 9d7a00314d..8064f47d80 100644 --- a/docs/craft-framework.md +++ b/docs/craft-framework.md @@ -144,7 +144,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. ` | Each frame | `raw_input_hook` | `embed::before_input(ctx, \|output\| { ...; input })`: the closure receives the `FrameOutput` of the frame before (fired commands, announcements, chrome toggles) and returns the `FrameInput` for this frame (`RootEnv`, command states, the `Host`). eframe runs the hook for the main window only: an app that drops the window system's input there (an agent-driven session) sets `FrameInput::window_input(WindowInput::Dropped)` too, and the runtime drops it in every other OS window it hosts, before injected input joins. macOS Control+Click is the runtime's in every window; the app does not map it. | | Each frame | `logic`, after the app's own logic | `embed::logic(ctx, \|\| decision)`: the root window's close path. The closure runs only when the root window was asked to close, and returns `CloseDecision::Close` or `Hold`. | | Each pass | `ui`, before the app's UI | `embed::before_ui(ctx, frame)`: hands the runtime the root's native window (eframe's `Frame`, or any `raw_window_handle::HasWindowHandle`) on Windows, where the runtime answers the window's hit test (resize edges, the title bar's move area, snap layouts on the maximize button), and fits the root window to its monitor once. Native only; it needs no eframe feature of the facade. | -| Each pass | `ui`, where the app draws its own document canvas | Optional: `ui::runtime::focus::host_canvas(ctx, id, KeyUse::CANVAS)` declares it as the window's canvas (Tab row below). `id` is the id of the interactive egui widget the app draws for the canvas in that pass: egui drops the focus of an id no widget has, so Tab would lose the focus there, and the pass reports the finding `canvas-without-widget`. Every other focusable egui widget the app draws is a Tab stop with no call: the window's one Tab order holds it among the framework's stops, in the order the pass draws them. | +| Each pass | `ui`, where the app draws its own document canvas | Optional: `ui::runtime::focus::host_canvas(ctx, id, KeyUse::CANVAS)` declares it as the window's canvas (Tab row below). `id` is the id of the focusable egui widget the app draws for the canvas in that pass (`Sense::click()`, `Sense::drag()`): egui drops the focus of any other id, so Tab would lose the focus there, and the pass reports the finding `canvas-without-widget`. Every other focusable egui widget the app draws is a Tab stop with no call: the window's one Tab order holds it among the framework's stops, in the order the pass draws them. | | Each pass | `ui`, last | Optional: `ui::runtime::keys::with_window_intents(ctx, rtl, \|intents\| ...)` reads the keys no component took (below). | `install` takes over these parts of the context for the whole window, not only for framework components: