mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 05:37:04 +00:00
host_canvas: a declared canvas no widget draws is a finding
egui drops the focus of an id no interactive widget of the pass has, so Tab lost the focus at such a canvas and started over at the first stop. The end hook looks each declared canvas up among the pass's widgets and reports `canvas-without-widget` when it is not there; debug builds log it once.
This commit is contained in:
@@ -33,6 +33,12 @@ pub(crate) fn candidates_since(ctx: &Context, seen: usize) -> Vec<egui::FocusCan
|
||||
ctx.memory(|memory| memory.focus_candidates().get(seen..).map(<[_]>::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))
|
||||
}
|
||||
|
||||
/// Whether the widget egui holds key focus on keeps Tab by its event filter (`Memory::focus_lock_filter`).
|
||||
pub(crate) fn focused_keeps_tab(ctx: &Context) -> bool {
|
||||
ctx.memory(|memory| memory.focus_lock_filter()).is_some_and(|filter| filter.tab)
|
||||
|
||||
@@ -17,8 +17,11 @@ pub(crate) use registration::{Group, Stop};
|
||||
use crate::runtime::egui::focus as egui_focus;
|
||||
use crate::runtime::egui::memory::FastMap;
|
||||
use crate::runtime::frame::PerViewport;
|
||||
use crate::runtime::inspect;
|
||||
use crate::style::log_once;
|
||||
use craft_behavior::{DocKey, OwnerClass, PartRef};
|
||||
use egui::{Context, Id, IdSet, ViewportId};
|
||||
use smallvec::SmallVec;
|
||||
use std::fmt;
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
@@ -194,6 +197,9 @@ pub(crate) struct State {
|
||||
/// How many of egui's focus candidates of the running pass (`Memory::focus_candidates`) `take_app_widgets` has
|
||||
/// read.
|
||||
pub(super) candidates_seen: usize,
|
||||
/// The canvases the running pass declared with `host_canvas`, which the end hook takes and looks up among egui's
|
||||
/// widgets.
|
||||
pub(super) host_canvases: SmallVec<[Id; 1]>,
|
||||
/// A text widget that gave its Escape on to the enclosing scopes and lets focus go at the end of the pass.
|
||||
surrender_after_scopes: Option<Id>,
|
||||
/// A text widget in a scope that gives focus back to its opener, held one more pass: the scope the Escape closes is
|
||||
@@ -318,9 +324,17 @@ fn take_app_widgets(ctx: &Context, except: Option<Id>) {
|
||||
/// them; Page Up and Down, Home and End arrive as its `Intent::Move`, as while it has the focus. A bare Tab with nothing
|
||||
/// focused is never the canvas's, even when it keeps Tab: it moves focus into the window or toggles the panels, so the
|
||||
/// 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
|
||||
/// each pass the canvas shows.
|
||||
/// 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.
|
||||
pub fn host_canvas(ctx: &Context, id: Id, keys: KeyUse) {
|
||||
if !crate::runtime::measure::active(ctx) {
|
||||
with(ctx, |state| state.host_canvases.push(id));
|
||||
}
|
||||
register_stop(ctx, Stop::new(id, keys.with_chrome_key(true)).host());
|
||||
}
|
||||
|
||||
@@ -1102,11 +1116,29 @@ 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.
|
||||
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;
|
||||
}
|
||||
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");
|
||||
});
|
||||
}
|
||||
inspect::finding(ctx, inspect::Finding { rule: "canvas-without-widget", component: "host_canvas", subject: format!("{id:?}") });
|
||||
}
|
||||
}
|
||||
|
||||
/// The end hook: applies the press's focus move, returns focus to the opener of a scope that went away,
|
||||
/// moves it off a stop that was not registered, and records what the next input hook reads.
|
||||
pub(crate) fn end(ctx: &Context) {
|
||||
let viewport = viewport(ctx);
|
||||
take_app_widgets(ctx, None);
|
||||
report_undrawn_canvases(ctx);
|
||||
// A field held a pass for the scope it sits in lets go now, unless that scope is going away with focus in the field:
|
||||
// nav::end below then gives the focus back to the scope's opener from where the field holds it.
|
||||
if let Some(widget) = with(ctx, |state| state.surrender_deferred.take())
|
||||
|
||||
@@ -12,6 +12,7 @@ use crate::runtime::frame::{ChromeToggle, take_output};
|
||||
use crate::runtime::ime;
|
||||
use crate::runtime::keys::with_window_intents;
|
||||
use crate::style::{ComponentStyle, StyleCx};
|
||||
use crate::testing::contract::findings;
|
||||
use crate::testing::focus::{Stop, egui_focused, key_release, named_id, pop_scope, push_scope, register_stop, widget};
|
||||
use crate::testing::input::{key_press, key_reached, slop};
|
||||
use crate::testing::pointer::{self, moved, press, release};
|
||||
@@ -998,6 +999,36 @@ 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() {
|
||||
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() {
|
||||
host_canvas(ui.ctx(), canvas, KeyUse::CANVAS);
|
||||
}
|
||||
if draws.get() {
|
||||
widget(ui, canvas);
|
||||
}
|
||||
};
|
||||
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:?}"
|
||||
);
|
||||
}
|
||||
|
||||
/// 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>) {
|
||||
|
||||
@@ -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). 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 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`, 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:
|
||||
|
||||
Reference in New Issue
Block a user