Focus: a Tab with no stop to move to and nothing focused stays in egui's input for the app's own binding

This commit is contained in:
aiongg
2026-11-25 01:15:31 +00:00
parent 6a02da62d4
commit e46f5180a0
4 changed files with 47 additions and 4 deletions
+8
View File
@@ -345,6 +345,14 @@ pub(crate) fn is_canvas_member(ctx: &Context, rec: &StopRec) -> bool {
})
}
/// Whether a bare Tab in `viewport` has no stop to move the focus to: the window's last pass registered no stop (the
/// app's focusable egui widgets are stops too, `take_app_widgets`) and nothing held the focus at its end. Such a Tab
/// stays in egui's input for the app's own binding. Read by the input hook, before the begin hook rotates the
/// registrations.
pub(crate) fn tab_has_no_stop(ctx: &Context, viewport: ViewportId) -> bool {
with_viewport(ctx, viewport, |state| state.stops.is_empty() && state.focus_now.is_none())
}
/// How many stops the running pass has registered so far.
pub(crate) fn stop_count(ctx: &Context) -> usize {
take_app_widgets(ctx, None);
+5 -3
View File
@@ -45,8 +45,9 @@ struct Strip<'a> {
/// An app's own egui widget or popup has the focus (`FocusedRecord::foreign`): Escape, the plain Enter and the
/// clipboard events are its own, and no window intent comes of them.
foreign: bool,
/// A bare Tab is egui's (`FocusedRecord::tab_in_egui`): an app's own popup is open, or the app's own focused widget
/// keeps Tab by its egui key filter.
/// A bare Tab is egui's: an app's own popup is open, or the app's own focused widget keeps Tab by its egui key
/// filter (`FocusedRecord::tab_in_egui`), or the window has no stop to move the focus to and nothing focused
/// (`focus::tab_has_no_stop`), so the Tab is the app's own binding's.
tab_in_egui: 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
@@ -665,6 +666,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
let bound = |chord: &Chord| commands::runs_now(ctx, viewport, chord);
let press_captured = crate::runtime::pointer::press_captured_in(ctx, viewport) || crate::runtime::drag::running(ctx);
let repeats = Env::root_for(ctx, viewport).is_some_and(|env| env.shortcuts_repeat);
let tab_in_egui = record.tab_in_egui || focus::tab_has_no_stop(ctx, viewport);
let (batch, visible, later) = with(ctx, viewport, |stage| {
let claims = stage_claims(&record, target_canvas.as_ref(), stage);
let edits = |command| !record.host && keys.is_some_and(|keys| keys.edits.contains(command));
@@ -686,7 +688,7 @@ pub(crate) fn input_hook(ctx: &Context, input: &RawInput, events: &mut Vec<Arriv
holds_drafts: keys.is_some_and(|keys| keys.holds_drafts),
canvas_space: hole.as_ref().is_some_and(|hole| hole.space || hole.space_pressed),
foreign: record.foreign,
tab_in_egui: record.tab_in_egui,
tab_in_egui,
host: record.host,
egui_edits: keys.filter(|_| record.host).or(canvas).map_or(EditSet::NONE, |keys| keys.edits),
label_selection: record.label_selection,
@@ -707,6 +707,39 @@ fn an_open_app_popup_keeps_tab() {
assert_eq!(egui_focused(&frames.ctx), Some(named_id("button")), "the framework moved no focus");
}
/// A Tab in a window with no stop and nothing focused moves no focus, so it stays in egui's input for the app's own
/// binding (EffectCraft's Tab for the timeline's mini flowchart), from the window's first pass on. Once the app draws a
/// focusable widget, that widget is a stop and the Tab is the framework's move to it.
#[trial]
fn an_unowned_tab_with_no_stops_reaches_the_app() {
let button = named_id("app button");
let (reached, draw) = (Rc::new(Cell::new(0)), Rc::new(Cell::new(false)));
let mut show = {
let (reached, draw) = (Rc::clone(&reached), Rc::clone(&draw));
move |ui: &mut Ui| {
if key_reached(ui.ctx(), Key::Tab) {
reached.set(reached.get() + 1);
}
if draw.get() {
fixture::host_button(ui, button, Rect::from_min_size(Pos2::new(40.0, 40.0), egui::vec2(80.0, 30.0)));
}
}
};
let mut frames = Frames::new();
press_key(&mut frames, &mut show, Key::Tab);
assert_eq!(reached.take(), 1, "the window's first pass has no stop: its Tab is the app's");
frames.idle(2, &mut show);
frames.frame(vec![key_press(Key::Tab, Modifiers::SHIFT), key_release(Key::Tab, Modifiers::SHIFT)], &mut show);
assert_eq!(reached.take(), 1, "with nothing drawn, Shift+Tab is the app's too");
assert_eq!(egui_focused(&frames.ctx), None);
draw.set(true);
frames.idle(2, &mut show);
press_key(&mut frames, &mut show, Key::Tab);
frames.frame(Vec::new(), &mut show);
assert_eq!(reached.take(), 0, "the app's button is a stop: the Tab is the framework's move");
assert_eq!(egui_focused(&frames.ctx), Some(button), "and the Tab moved focus to the button");
}
#[trial]
fn escape_ends_an_egui_drag_on_a_host_widget() {
let area = named_id("host drag area");
+1 -1
View File
@@ -151,7 +151,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 focus over the window's one Tab order and never reaches egui, unless the focused framework stop keeps Tab, the app's own focused widget keeps it by its egui key filter (a multi-line `TextEdit` with `lock_focus`, a code editor), or an egui popup is open (below). The order holds the framework's stops and every focusable egui widget the app draws (egui's focus candidates of the pass), in the order the pass registers and draws them, each inside the framework scope it was drawn in; a `TextEdit` is a text stop. So the app's own menu bar, buttons and fields are reached where it draws them, with no registration. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus controls the platform's rule skips. 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`, which takes the canvas's keys at the canvas's place in the order. A canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input only while it has the focus; with nothing focused, Tab moves focus into the window or toggles the chrome, so the canvas cannot trap keyboard navigation. A focused canvas whose editor part keeps Tab reads Shift+Tab as `Intent::BackTab` (`EditorInput::BackTab`); no other stop gets it as an intent, and a text field keeps Shift+Tab as a focus move. |
| Tab | Bare Tab moves focus over the window's one Tab order and never reaches egui, unless the focused framework stop keeps Tab, the app's own focused widget keeps it by its egui key filter (a multi-line `TextEdit` with `lock_focus`, a code editor), or an egui popup is open (below). The order holds the framework's stops and every focusable egui widget the app draws (egui's focus candidates of the pass), in the order the pass registers and draws them, each inside the framework scope it was drawn in; a `TextEdit` is a text stop. So the app's own menu bar, buttons and fields are reached where it draws them, with no registration. A window with stops but no Tab entry (on the Mac, where Tab reaches only text fields by default) keeps the Tab from egui, which would otherwise focus controls the platform's rule skips. A window that registered no stop, with nothing focused, has nowhere to move the focus: its Tab and Shift+Tab stay in egui's input for the app's own binding. 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`, which takes the canvas's keys at the canvas's place in the order. A canvas that keeps Tab (`KeyUse::CANVAS.with_keep(KeepKeys::Tab)`) gets the press in egui's input only while it has the focus; with nothing focused, Tab moves focus into the window or toggles the chrome, so the canvas cannot trap keyboard navigation. A focused canvas whose editor part keeps Tab reads Shift+Tab as `Intent::BackTab` (`EditorInput::BackTab`); no other stop gets it as an intent, and a text field keeps Shift+Tab as a focus move. |
| F6 | F6 and Shift+F6 move between panes and never reach egui. They are the defaults of the commands `NavCommand::NextPane` and `NavCommand::PreviousPane` (ids `window.next_pane` and `window.previous_pane`; name them by `NavCommand::NextPane.id()` in code): 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`. A held Escape cancels once: its repeats raise no `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 (`NavCommand::FocusMenuBar`, id `window.focus_menu_bar`, bound and yielded as F6 is) focuses the bar, Alt+letter opens a menu (not on macOS) unless the keymap binds it to an enabled command, which runs instead. |