A press nothing takes leaves the app's document canvas focused

An unclaimed press surrendered the focus of a canvas declared with
host_canvas at the press (apply_press), and egui's click-away rule took
a focus the app itself had given the canvas at the release. The press
rule now keeps a declared canvas focused, and the begin hook takes the
focus on it off egui's rule, whoever gave it. A canvas the app no longer
declares follows egui's rule again.
This commit is contained in:
aiongg
2026-11-25 14:37:41 +00:00
parent d1bdf8e5da
commit d6aed656f2
6 changed files with 138 additions and 6 deletions
@@ -62,6 +62,12 @@ pub(crate) fn focus_with_filter(ctx: &Context, id: Id, filter: KeyFilter) {
});
}
/// Keeps egui from taking the focus of `id`, if it holds it, on a press or click elsewhere, until the focus moves or a
/// request gives it again.
pub(crate) fn never_surrender(ctx: &Context, id: Id) {
ctx.memory_mut(|memory| memory.set_focus_surrender(id, SurrenderFocusOn::Never));
}
/// Leaves the focus of the app's own widget `id`, if it holds it, to egui's rule, the app's `surrender_focus_on`, with
/// the key filter it has.
pub(crate) fn leave_to_egui(ctx: &Context, id: Id) {
+25
View File
@@ -200,6 +200,8 @@ pub(crate) struct State {
/// The widget the runtime gave egui's focus, under its own press rule, until the end hook settles it
/// (`settle_given`): the focus of an app's own widget is left to egui's rule there.
given: Option<Id>,
/// The app's document canvas whose focus is off egui's press rule (`hold_canvas_focus`).
held_canvas: Option<Id>,
/// The canvases the running pass declared with `host_canvas`, which the end hook looks up among egui's widgets.
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.
@@ -334,6 +336,11 @@ fn take_app_widgets(ctx: &Context, except: Option<Id>) {
/// 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.
///
/// A press that nothing takes leaves the canvas focused, whoever gave it the focus and whatever the app's
/// `surrender_focus_on`: a press on the app's own widgets that take no focus or on empty space moves no focus. The canvas
/// loses the focus to a component or widget that takes it, Tab, F6, a dialog, the app's own request or surrender, and
/// when it is no longer drawn.
///
/// Call it in each pass the canvas shows, before or after the canvas is drawn, with the id of the focusable, enabled
/// 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
@@ -1102,6 +1109,7 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool, dialog_tab: TabReach)
dialog_tab,
frame: crate::runtime::egui::pass::frame_nr(ctx, viewport(ctx)),
};
let focused = input.focused;
let (acts, announcements, host_focus) = with(ctx, |state| {
state.awaited = None;
let host_focus = state.record.host_focus;
@@ -1113,12 +1121,29 @@ pub(crate) fn begin(ctx: &Context, tab_all_controls: bool, dialog_tab: TabReach)
if !host_focus || !acts.is_empty() {
egui_focus::clear_directional_focus(ctx);
}
hold_canvas_focus(ctx, focused);
apply(ctx, acts);
for text in announcements {
crate::runtime::frame::announce(ctx, text);
}
}
/// Takes the focus `focused` off egui's press rule while it is on the app's document canvas, as the last pass declared
/// it: whoever gave that focus, only `State::apply_press` decides what a press does to it. A canvas the app no longer
/// declares is one of the app's own widgets, whose focus is egui's again.
fn hold_canvas_focus(ctx: &Context, focused: Option<Id>) {
let (held, left) = with(ctx, |state| {
let held = focused.filter(|id| nav::declared_canvas(&state.last_stops, *id));
let before = std::mem::replace(&mut state.held_canvas, held);
(held, before.filter(|id| held.is_none() && focused == Some(*id)))
});
if let Some(id) = held {
egui_focus::never_surrender(ctx, id);
} else if let Some(id) = left {
egui_focus::leave_to_egui(ctx, id);
}
}
/// Releases the text widget `widget` at the end of this pass, after the enclosing scopes have had its Escape: a scope
/// deciding whether to close still finds the focused stop. No key is replayed and no other widget loses focus.
pub(crate) fn surrender_after_scopes(ctx: &Context, widget: Id) {
@@ -734,6 +734,11 @@ impl State {
}
}
/// Whether `id` is the app's document canvas, as the pass that registered `stops` declared it (`host_canvas`).
pub(super) fn declared_canvas(stops: &[StopRec], id: Id) -> bool {
stops.iter().any(|rec| rec.stop.host && !rec.stop.automatic() && rec.stop.root == id)
}
/// Focus on the egui widget of `stop`, with the key filter its keys ask for.
pub(super) fn focus_act(stop: &Stop) -> Act {
let roving = stop.group.is_some();
+11 -5
View File
@@ -1,6 +1,7 @@
//! Focus on press (spec 8.3): the press is classified at the begin hook from last pass's shapes, and the
//! classified focus move applies at the end hook.
use super::nav::declared_canvas;
use super::{Act, State, StopKey};
use crate::runtime::egui::focus as egui_focus;
use crate::runtime::{pointer, sampler};
@@ -148,10 +149,10 @@ impl State {
self.stops.iter().chain(&self.last_stops).find(|rec| rec.stop.press_only && rec.stop.root == widget).cloned()
}
/// The press's focus move at the end hook; `focused` is egui's focus now. The press moves the framework's focus
/// only: when `focused` is an app's own widget (`on_app_widget`), egui's own surrender rule, by the app's
/// `surrender_focus_on`, decides whether a press elsewhere takes it; a press a component takes still moves focus
/// to the component.
/// The press's focus move at the end hook; `focused` is egui's focus now. A press a component takes moves focus to
/// the component, whatever has it. A press nothing takes ends the focus of a component only: it leaves the app's
/// document canvas focused (`declared_canvas`), and when `focused` is an app's own widget (`on_app_widget`),
/// egui's own surrender rule, by the app's `surrender_focus_on`, decides whether a press elsewhere takes it.
pub(super) fn apply_press(&mut self, focused: Option<Id>, on_app_widget: bool) -> Vec<Act> {
let Some(plan) = self.press.plan.take() else { return Vec::new() };
match plan.kind {
@@ -166,7 +167,12 @@ impl State {
None if self.in_trapping_scope() => Vec::new(),
None => focused.map(Act::Surrender).into_iter().collect(),
},
PressFocus::Unclaimed if focused == self.press.focus_at_begin && !on_app_widget && !self.in_trapping_scope() => {
PressFocus::Unclaimed
if focused == self.press.focus_at_begin
&& !on_app_widget
&& !focused.is_some_and(|id| declared_canvas(&self.stops, id))
&& !self.in_trapping_scope() =>
{
focused.map(Act::Surrender).into_iter().collect()
}
_ => Vec::new(),
@@ -932,6 +932,96 @@ fn focus_given_to_a_canvas_before_its_declaration_is_the_canvas_s() {
assert_eq!(egui_focused(&frames.ctx), Some(canvas), "egui keeps the focus on the canvas over its Escape");
}
/// A window of the app's own document canvas, declared while `declared` holds, beside the app's field of `host_window`, a
/// button and an area of the app's own that take no focus, and the component `probe`, which takes it.
struct CanvasWindow {
declared: Rc<Cell<bool>>,
host: Rc<RefCell<Host>>,
/// egui's focus in each pass, at its start and after the canvas was drawn.
in_pass: Rc<RefCell<Vec<Option<Id>>>>,
}
impl CanvasWindow {
const BUTTON: Rect = Rect { min: Pos2::new(300.0, 100.0), max: Pos2::new(380.0, 130.0) };
const AREA: Rect = Rect { min: Pos2::new(300.0, 200.0), max: Pos2::new(380.0, 230.0) };
const PROBE: Rect = Rect { min: Pos2::new(300.0, 300.0), max: Pos2::new(380.0, 330.0) };
const CANVAS: Rect = Rect { min: Pos2::new(100.0, 400.0), max: Pos2::new(180.0, 430.0) };
const EMPTY: Pos2 = Pos2::new(500.0, 450.0);
fn new() -> (Self, impl FnMut(&mut Ui)) {
let (host, mut base) = host_window();
let window = Self { declared: Rc::new(Cell::new(true)), host, in_pass: Rc::default() };
let (declared, in_pass) = (Rc::clone(&window.declared), Rc::clone(&window.in_pass));
let show = move |ui: &mut Ui| {
in_pass.borrow_mut().push(egui_focused(ui.ctx()));
base(ui);
fixture::host_button(ui, named_id("app button"), Self::BUTTON);
fixture::host_unfocusable_area(ui, named_id("app area"), Self::AREA);
sensing::show(ui, &[ProbeRoot::new("probe", Self::PROBE, Sense::CLICK | Sense::TAKE_FOCUS)]);
register_stop(ui.ctx(), Stop::new(ProbeRoot::id("probe"), KeyUse::of(Input::Pointer)));
fixture::host_button(ui, Self::canvas(), Self::CANVAS);
if declared.get() {
host_canvas(ui.ctx(), Self::canvas(), KeyUse::CANVAS);
}
in_pass.borrow_mut().push(egui_focused(ui.ctx()));
};
(window, show)
}
fn canvas() -> Id {
named_id("host canvas")
}
/// Gives the canvas the focus, by the app's own request or the framework's.
fn focus(frames: &mut Frames, show: &mut impl FnMut(&mut Ui), app: bool) {
if app {
fixture::host_focus(&frames.ctx, Self::canvas());
} else {
request(&frames.ctx, FocusTarget::Id(Self::canvas()));
}
frames.idle(2, &mut *show);
assert_eq!(egui_focused(&frames.ctx), Some(Self::canvas()));
}
}
/// A click that nothing takes leaves the app's document canvas focused in every pass, whoever gave it the focus: by the
/// framework's press rule and by egui's. A canvas the app no longer declares follows egui's rule.
#[trial]
fn a_click_nothing_takes_leaves_the_app_s_canvas_focused() {
for app in [true, false] {
let (window, mut show) = CanvasWindow::new();
let mut frames = Frames::new();
frames.idle(2, &mut show);
CanvasWindow::focus(&mut frames, &mut show, app);
window.in_pass.take();
for at in [CanvasWindow::BUTTON.center(), CanvasWindow::AREA.center(), CanvasWindow::EMPTY] {
click_focus(&mut frames, &mut show, at);
let passes = window.in_pass.take();
assert!(!passes.is_empty() && passes.iter().all(|focused| *focused == Some(CanvasWindow::canvas())), "app {app}, {at:?}: {passes:?}");
}
window.declared.set(false);
frames.idle(2, &mut show);
let (pressed, clicked) = click_focus(&mut frames, &mut show, CanvasWindow::EMPTY);
assert_eq!((pressed, clicked), (Some(CanvasWindow::canvas()), None), "app {app}: an app's plain widget loses it at the click");
}
}
/// A press on a component that takes focus, or on the app's own field, takes it from the app's document canvas.
#[trial]
fn a_press_that_takes_focus_takes_it_from_the_app_s_canvas() {
for app in [true, false] {
let (window, mut show) = CanvasWindow::new();
let mut frames = Frames::new();
frames.idle(2, &mut show);
CanvasWindow::focus(&mut frames, &mut show, app);
let probe = Some(ProbeRoot::id("probe"));
assert_eq!(click_focus(&mut frames, &mut show, CanvasWindow::PROBE.center()), (probe, probe), "app {app}: the component");
CanvasWindow::focus(&mut frames, &mut show, app);
let at = window.host.borrow().rect.center();
assert_eq!(click_focus(&mut frames, &mut show, at).1, Some(field()), "app {app}: the app's field");
}
}
/// Where Tab reaches only text fields (the Mac's default), it reaches the app's own text field and skips the app's
/// own buttons, as it skips the framework's.
#[trial]
+1 -1
View File
@@ -160,7 +160,7 @@ An app with its own eframe `App` calls the hooks in `craft::ui::shell::embed`. `
| 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, Escape, the plain Enter and egui's clipboard events stay in egui's input for it, and the runtime routes nothing for them; Tab and Shift+Tab are the window's focus moves (Tab row) unless the widget keeps Tab or the popup is open. While egui focus is on such a widget, the arrows 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. |
| Focus surrender | The runtime moves its own focus on press (8.3): a press a framework component takes moves focus to it; a press no component takes drops the framework's focus. egui's click-away never takes the framework's focus. egui's surrender option (`InputOptions::surrender_focus_on`) stays the app's and governs the focus of the app's own widgets, however they got it (the app's code, a click, Tab, F6, a framework request), as in egui alone: a press no component takes leaves that focus to egui's rule, except that a press that gives the focus, and its release, never take it back: the next press or click elsewhere does. Escape releases it too, unless the widget's egui key filter keeps Escape. |
| Focus surrender | The runtime moves its own focus on press (8.3): a press a framework component takes moves focus to it; a press no component takes drops the framework's focus, and leaves the app's document canvas (`host_canvas`) focused, whoever gave it the focus and whatever egui's option says. egui's click-away never takes the framework's focus. egui's surrender option (`InputOptions::surrender_focus_on`) stays the app's and governs the focus of the app's own widgets, however they got it (the app's code, a click, Tab, F6, a framework request), as in egui alone: a press no component takes leaves that focus to egui's rule, except that a press that gives the focus, and its release, never take it back: the next press or click elsewhere does. Escape releases it too, unless the widget's egui key filter keeps Escape. |
| Clicks | egui's click options (`InputOptions::max_click_dist`, `max_click_duration`) stay the app's, for its own widgets. A framework component clicks and drags by the framework's press lifecycle: a release within the slop (`HitSizes::slop`), whatever its duration. Read a component's click with `contract::clicked(&response)` or its output's `clicked()`; egui's `Response::clicked()` on a component's response also needs a click by egui's options. |
| Zoom keys | egui's keyboard zoom is off and nothing takes its place: the interface scale is `RootEnv::scale`, and an app whose ⌘= and ⌘- changed it binds them itself. |
| Quit | egui's quit shortcuts are cleared. ⌘Q (Ctrl+Q on Windows and Linux) is the default of the command `QuitCommand::Quit` (id `window.quit`), which the runtime runs from any source: it asks the root window to close through the close path below, so the app's close decision asks about unsaved work. It fires with a modal open; the close path then refuses under an app-modal layer. Its default is bound and yielded as F6 is; an app whose own shortcut table reads its quit key from egui unbinds it there. |