#[non_exhaustive]pub enum FocusOrigin {
Keyboard,
Pointer(PointerKind),
Programmatic,
Accessibility,
Restored,
}Expand description
How focus was acquired.
Read by :focus-visible — a focus ring belongs to keyboard and assistive
navigation, not to a click — and by anything that has to treat a finger
differently from a mouse. The pointer arm carries the device that delivered
the focus, because “a pointer focused this” is not one behaviour: a mouse
focuses on press, a finger and a pen focus on release, and only a release
that lands back on the same focusable counts.
#[non_exhaustive]: matches need a wildcard arm. Most call sites want
is_pointer rather than a match at all.
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Keyboard
Focus gained via Tab/Shift-Tab keyboard navigation, or by any other keystroke a widget routes into focus.
Pointer(PointerKind)
Focus gained by pointing at the widget, with the device that did it.
Programmatic
Focus set programmatically by the application. Carries no input
modality of its own: a scripted focus leaves the focus ring exactly as
the user’s last real interaction left it, which is what
:focus-visible does for element.focus().
Accessibility
Focus moved by assistive technology — a screen reader’s
Action::Focus, or an automation client
standing in for one. Reveals the focus ring: the user is navigating,
they are simply not doing it with a key.
Restored
Focus put back by the tree after a rebuild destroyed the widget that held it: the rebuilt subtree’s successor takes the focus its predecessor had. Nobody moved focus, so a widget must treat this as continuity, not arrival.
The case it exists for is a text field whose own value rebuilds an
ancestor, as a filtering search box does: every keystroke recreates
the field, and a field that selected its whole text on what it took
for a fresh keyboard focus made the next keystroke replace everything
typed so far, leaving only the last character visible. Declares no
modality, like Programmatic: the ring stays
wherever the rebuilt widget’s predecessor had it.
Implementations§
Source§impl FocusOrigin
impl FocusOrigin
Sourcepub const POINTER: Self
pub const POINTER: Self
Focus arrived by pointer, from a site that cannot know which device delivered it.
A control deriving its own origin from hover or from the tree’s
input-modality signal — rather than from the
FocusGained it was handed —
knows only that the keyboard was not involved. It says so with
PointerKind::Unknown instead of naming a device it never saw, so a
consumer reading pointer_kind is never told a
finger was a mouse.
Sourcepub const fn is_pointer(self) -> bool
pub const fn is_pointer(self) -> bool
Whether focus arrived by pointing at the widget.
The predicate that replaced == FocusOrigin::Pointer: a pointer origin
now names its device, so equality against a bare variant no longer
compiles and equality against one device would silently exclude the
others.
Sourcepub const fn pointer_kind(self) -> Option<PointerKind>
pub const fn pointer_kind(self) -> Option<PointerKind>
The device that delivered a pointer focus, or None for every other
origin. PointerKind::Unknown for a widget-side derivation — see
POINTER.
Sourcepub const fn focus_visible(self) -> Option<bool>
pub const fn focus_visible(self) -> Option<bool>
Whether this origin reveals the focus ring — the :focus-visible
question, answered in one place so the tree’s modality signal and any
widget consulting the origin cannot disagree.
Keyboard and assistive navigation reveal it; a pointer hides it.
Programmatic answers neither: a scripted focus
declares no modality, so the tree leaves the signal where the last real
interaction put it.