Skip to main content

Module focus

Module focus 

Source
Expand description

Focus scopes and the focus chain (ADR 0007).

A scope is a named region of the interface (a panel, a dialog, a list) that sets context keys while the focus is inside it. Scopes nest the way they are drawn; each pass records them, and the focus chain is the scopes containing the focused widget (or, when nothing has the keyboard focus, the place last clicked), outermost first. The when_context built from it gives each scope’s keys the scope’s depth, so key resolution prefers the binding nearest the focus.

In hover mode (focus_mode), the place the pointer moved to stands for the place last clicked, and a widget focused in another scope lets go of the focus.

The chain is computed from the previous pass’s scopes (egui decides focus between passes), so it is ready before this pass’s widgets read the keyboard. Each window has its own scopes and chain (egui keeps a focus per window): in a detached panel’s window, when_context says what has the keyboard there.

A focused widget belongs to the scopes it was drawn in, wherever it lies: a row scrolled out of its panel is still that panel’s. The place a pointer pressed has no widget to ask, so it goes by the scopes’ rectangles.

take_moved says when the focus went somewhere else, once per change, for what keeps a notion of its own of where the user is (the workbench’s focused panel); prefer names the widget a scope’s focus should land on when something hands the scope the keyboard.

A region is a scope F6 goes to, see regions.

§A focus you can see, from the keyboard only

egui-pop rings the focus by the web’s :focus-visible rule: each window keeps one flag, turned on by a navigation key (Tab and Shift+Tab, the arrows, Home, End, Page Up, Page Down) or a focus moved without the pointer (F6, a shortcut, a screen reader), and off by any press of the pointer. Every focus ring egui-pop draws (the plugin’s, and the focus look of its widgets, views, grids, panels and the kit’s focus state) shows only while it is on (ring_shown, shows_ring); text inputs show their focus however it came. Where the keyboard goes, and the focus AccessKit reports, never change with it.

§Escape, in order

With the focus plugin installed, one press of Escape goes to the first of these that takes it:

  1. a focused text input lets go of the focus, which stays in the input’s surface (its scope still heads the focus chain); nothing else sees that press (unless the input lets Escape through, see escape_passes); an input that cancels on Escape (a slider’s typed entry) learns of that press through escape_left;
  2. a gesture in progress: egui aborts a drag, and the press is the drag’s alone (a slider’s, a knob’s, a scrub’s, a viewport’s): the plugin takes it out of the input, so no host around the gesture (a dialog, the Settings window, a popover) dismisses on it; the widget puts its value back, reading the press through escape_cancelled or cancel_pressed, and a value store gesture is rolled back (binding). Only the next Escape reaches the host;
  3. the top overlay closes, if it closes on Escape (popovers, menus, dialogs: “Escape dismisses”), see overlay::begin_pass;
  4. the key bindings of Escape, the innermost focused surface’s first, then the outer surfaces’, then the global ones (ADR 0007);
  5. what is left reaches egui’s widgets.

So in a text field of a panel, the first Escape leaves the field and the second reaches the panel’s own Escape (cancelling a gesture, closing the panel’s overlay).

While a text input owns the keyboard (text_input_owns_keyboard: a text field has the focus, or an open input claimed it with claim_keyboard), the key dispatcher leaves Escape and Enter to it.

use egui_pop_core::focus;

fn panel(ui: &mut egui::Ui) {
    focus::scope(ui, "list", &[("listMode", "grid")], |ui| {
        let _ = ui.button("Item");
    });
}

Structs§

Region
What makes a scope a region of the window, for region.
ScopeRecord
One scope as drawn in a pass.

Enums§

Moved
How the place the user works in changed, from take_moved.

Functions§

cancel_pressed
Whether a gesture’s cancel key was pressed in this pass: the key itself, or, for Escape, the press the focus plugin took for the drag it cancelled (escape_cancelled). A drag that ends (drag_stopped) asks this to know whether it was cancelled.
chain
The scopes of the focus chain, outermost first.
chain_names
The names in the focus chain, outermost first.
claim_keyboard
Claims the keyboard for an open text input (call it each pass it stays open): while it is claimed, text_input_owns_keyboard is true, so key dispatchers leave Escape and Enter to the input. The claim holds for the pass it is made in and the next one, since a dispatcher runs before the input is drawn. A slider’s typed entry claims it: Escape cancels the entry, Enter commits it, and neither runs a key binding.
escape_cancelled
The widget whose drag Escape cancelled in this pass, if any. With the focus plugin installed, a press of Escape during a drag is the drag’s, before any host’s (see the order): the plugin takes the press out of the input, so a gesture reads it here (or through cancel_pressed) and not from the keys. Asked as often as needed in the pass.
escape_left
Whether Escape just took the focus from this text input (the focus plugin swallows that press, see the order): an input that cancels on Escape reads it here, since the key itself is gone. Answered once: the press is spent by the first call that sees it.
escape_passes
Lets Escape through a text input (call it each pass it is drawn): Escape then goes on to the overlay around it, as for a search field that is the whole of a popup (the command palette), instead of leaving the field first.
focused
The focused widget, if any.
focused_in_window
The widget a window’s last pass left with the focus, for a plugin’s input hook, which should ask this with RawInput::viewport_id and not the memory: for most windows, the memory is still another window’s there. To let go of that focus, wait for the window’s pass to begin (on_begin_pass).
install
Installs the focus plugin on a context (once; later calls do nothing): Escape leaves a text input before anything else sees it, see the order, the keys typed with an AccessKit Focus action reach the field it focuses, a widget the keyboard focuses comes into view in the scroll areas around it, nested ones included (the pointer’s focus leaves them where they are), and the focus keeps its place: Tab after Escape or after the focused widget went away goes on from there, and a widget removed hands the focus to its neighbour.
keep_tab
Makes the popover being drawn keep Tab: past its last stop Tab comes back to its first, Shift+Tab the other way, and Tab from outside it goes in, so that nothing walks the window behind it while it stays open. With no stop in it, it closes as Tab goes on. A focused widget taking Tab itself keeps it.
keys_read_by_focus
The keys the widget holding the focus in this window said it reads itself (reads_keys), this pass or the last; none when nothing has the focus.
own_ring
Says that a widget shows its own focus (call it each pass it is drawn), so that the focus plugin paints no ring around it. A widget painting a kit state from State::from_response has it said for it.
peek_response
A widget’s response from this pass (else the last), read without touching the focus.
prefer
Names the widget the scopes open around it hand the focus to when asked (call it each pass, in the scope, where it is drawn): the canvas under a panel’s toolbar, the search field a “go to the search” command lands in. Without one, a panel given the keyboard holds it itself, and Tab enters its widgets. The first widget to ask in a scope wins.
preferred
The widget named by prefer so far in the scope being drawn.
reads_keys
Says that a widget reads these keys itself while it has the focus (call it each pass it is drawn): pressed without Ctrl or Alt while it is focused, they reach it before a global key binding of theirs, as a text field keeps the keys it types. A list or a grid says so of its arrows, Home, End, Page Up and Page Down, Space and Enter.
region
A scope that F6 goes to: a part of the window (an area, a palette, the status bar), see regions. Don’t draw one inside another.
ring_shown
Whether the focus is visible (the web’s :focus-visible): a navigation key (Tab, Shift+Tab, an arrow, Home, End, Page Up, Page Down) was pressed, or the focus moved without the pointer (F6, a command, a screen reader), since the last press of the pointer. The focus plugin then rings the focused widget, and a widget drawing its own focus ring draws it only while this holds (shows_ring). One flag per window.
scope
Draws add inside a named scope that sets keys (and <name>Focus) while the focus is in it.
scopes
The scopes recorded in the previous pass (for the debug bridge).
shows_ring
Whether a widget draws its focus ring now: it has the focus, and the focus is visible (ring_shown). A widget showing its own focus still calls own_ring whenever it has the focus, ring drawn or not.
take_moved
What moved since the last time this was asked, at most once per change and per window: where the pointer pressed, where a widget took the focus, or that the focus went nowhere. Read before the pass’s drawing, it says what the end of the previous pass settled.
takes_keys
Says that a widget reads Page Up, Page Down, Home or End while focused (call it each pass it is drawn, before the scroll area around it ends), so that the area leaves them to it. A widget sensing drags needs none of this, nor does a text input: a list, a grid or a row of stars does.
text_input_owns_keyboard
Whether a text input owns the keyboard: a text field has the focus, or an open input claimed it (claim_keyboard) this pass or the last. egui-pop’s key dispatcher then leaves Escape and Enter (without Ctrl or Alt) to the input; a host with a dispatcher of its own, or one that reads Escape before the widgets are drawn, should ask this first and do the same.
when_context
The context keys: base (depth 0), each scope of the chain at its depth, then textInputFocus at the deepest depth when a text field has the focus, and widgetFocus whenever a widget has it.