Skip to main content

FocusManager

Struct FocusManager 

Source
pub struct FocusManager { /* private fields */ }
Expand description

Manages the focus tree.

The focus tree is a separate, sparser, tree from the widget tree that maintains the hierarchical relationship between focusable widgets in the widget tree.

The focus manager is responsible for tracking which FocusNode has the primary input focus (the primary_focus), holding the FocusScopeNode that is the root of the focus tree (the root_scope), and what the current highlight_mode is. It also distributes KeyEvents to the nodes in the focus tree.

The singleton FocusManager instance is held by the WidgetsBinding as WidgetsBinding::focus_manager, and can be conveniently accessed using the FocusManager::instance accessor.

To find the FocusNode for a given BuildContext, use Focus::of. To find the FocusScopeNode for a given BuildContext, use FocusScope::of.

If you would like notification whenever the primary_focus changes, register a listener with add_listener.

The highlight_mode describes how focus highlights should be displayed on components in the UI. The highlight mode changes are notified separately via add_highlight_mode_listener and removed with remove_highlight_mode_listener.

Implementations§

Source§

impl FocusManager

Source

pub fn new(app: &mut App) -> Ref<FocusManager>

Creates an object that manages the focus tree.

This constructor is rarely called directly. To access the FocusManager, consider using the instance accessor instead (which gets it from the WidgetsBinding singleton).

This newly constructed focus manager does not have the necessary event handlers registered to allow it to manage focus. To register those event handlers, callers must call register_global_handlers.

Source

pub fn debug_fill_properties( self: Handle<Self>, app: &mut App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s FocusManager.debugFillProperties: the pending update, the primary focus and the node marked for it next. primaryFocusCreator is the element’s debug creator chain, deferred with debugCreator.

Source

pub fn debug_describe_children( self: Handle<Self>, app: &mut App, ) -> Vec<Rc<dyn DiagnosticsNode>>

Dart’s FocusManager.debugDescribeChildren: the root scope.

Source

pub fn to_string_short(self: Handle<Self>, _app: &App) -> String

Dart’s FocusManager.toStringShort.

Source

pub fn to_string_deep(self: Handle<Self>, app: &mut App) -> String

Dart’s FocusManager.toStringDeep: the manager and its focus tree, at Dart’s default level (debug) and width.

Source

pub fn to_diagnostics_tree_node( self: Handle<Self>, app: &mut App, name: Option<String>, ) -> DiagnosticableTreeNode

Dart’s FocusManager.toDiagnosticsNode(name:), from DiagnosticableTree.

Source

pub fn listen_to_application_lifecycle_changes_if_supported( self: Handle<Self>, app: &mut App, )

Enables this FocusManager to listen to changes of the application lifecycle if it does not already have an application lifecycle listener active, and the app isn’t running on a native mobile platform.

Typically, the application lifecycle listener for this FocusManager is set up at construction, but sometimes it is necessary to manually initialize it when the platform did not name the relevant target at the time of construction. This can happen in a test that changes the platform’s target after the binding has built its FocusManager; the test then calls this method, so the FocusManager can accurately listen to application lifecycle changes, if supported.

Source

pub fn register_global_handlers(self: Handle<Self>, app: &mut App)

Registers global input event handlers that are needed to manage focus.

This calls HardwareKeyboard::add_handler on the shared instance of HardwareKeyboard and adds a route to the global entry in the gesture routing table. As such, only one FocusManager instance should register its global handlers.

When this focus manager is no longer needed, calling dispose on it will unregister these handlers.

Source

pub fn dispose(self: Handle<Self>, app: &mut App)

Discards any resources used by this object.

Source

pub fn instance(app: &mut App) -> Handle<FocusManager>

Provides convenient access to the current FocusManager singleton from the WidgetsBinding instance.

Source

pub fn highlight_strategy( self: Handle<Self>, app: &App, ) -> FocusHighlightStrategy

The strategy by which highlight_mode is determined.

If set to FocusHighlightStrategy::Automatic, then the highlight mode will change depending upon the interaction mode used last.

Defaults to FocusHighlightStrategy::Automatic.

Source

pub fn set_highlight_strategy( self: Handle<Self>, app: &mut App, value: FocusHighlightStrategy, )

Source

pub fn highlight_mode(self: Handle<Self>, app: &mut App) -> FocusHighlightMode

Indicates the current interaction mode for focus highlights.

The value returned depends upon the highlight_strategy used, and possibly the most recent interaction mode that the user used.

If highlight_mode returns FocusHighlightMode::Touch, then widgets should not draw their focus highlight unless they perform text entry.

If it returns FocusHighlightMode::Traditional, then widgets should draw their focus highlight whenever they are focused.

Source

pub fn add_highlight_mode_listener( self: Handle<Self>, app: &mut App, listener: HighlightModeListener, )

Register a closure to be called when the FocusManager notifies its listeners that the value of highlight_mode has changed.

Source

pub fn remove_highlight_mode_listener( self: Handle<Self>, app: &mut App, listener: &HighlightModeListener, )

Remove a previously registered closure from the list of closures that the FocusManager notifies.

Source

pub fn add_early_key_event_handler( self: Handle<Self>, app: &mut App, handler: OnKeyEventCallback, )

Adds a key event handler to a set of handlers that are called before any key event handlers in the focus tree are called.

All of the handlers in the set will be called for every key event the FocusManager receives. If any one of the handlers returns KeyEventResult::Handled or KeyEventResult::SkipRemainingHandlers, then none of the handlers in the focus tree will be called.

Source

pub fn remove_early_key_event_handler( self: Handle<Self>, app: &mut App, handler: &OnKeyEventCallback, )

Removes a key handler added by calling add_early_key_event_handler.

Source

pub fn add_late_key_event_handler( self: Handle<Self>, app: &mut App, handler: OnKeyEventCallback, )

Adds a key event handler to a set of handlers that are called if none of the key event handlers in the focus tree handle the event.

If the event reaches the root of the focus tree without being handled, then all of the handlers in the set will be called. If any of them returns KeyEventResult::Handled or KeyEventResult::SkipRemainingHandlers, then event propagation to the platform will be stopped.

Source

pub fn remove_late_key_event_handler( self: Handle<Self>, app: &mut App, handler: &OnKeyEventCallback, )

Removes a key handler added by calling add_late_key_event_handler.

Source

pub fn root_scope(self: Handle<Self>, app: &App) -> &Ref<FocusScopeNode>

The root FocusScopeNode in the focus tree.

This field is rarely used directly. To find the nearest FocusScopeNode for a given FocusNode, call AnyFocusNode::nearest_scope.

Source

pub fn primary_focus(self: Handle<Self>, app: &App) -> Option<AnyFocusNode>

The node that currently has the primary focus.

Source

pub fn apply_focus_changes_if_needed(self: Handle<Self>, app: &mut App)

Applies any pending focus changes and notifies listeners that the focus has changed.

Must not be called during the build phase. This method is meant to be called in a post-frame callback or microtask when the pending focus changes need to be resolved before something else occurs.

It can’t be called during the build phase because not all listeners are safe to be called with an update during a build.

Typically, this is called automatically by the FocusManager, but sometimes it is necessary to ensure that no focus changes are pending before executing an action.

It is safe to call this if no focus changes are pending.

Trait Implementations§

Source§

impl ChangeNotifier for FocusManager

Source§

fn change_notifier_data(&self) -> &ChangeNotifierData

Source§

fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData

Source§

fn will_add_listener(self: Handle<Self>, app: &mut App)

Runs before the listener is stored. Dart overrides of addListener that work before super.addListener go here.
Source§

fn did_remove_listener(self: Handle<Self>, app: &mut App)

Runs after the listener is removed. Dart overrides of removeListener that work after super.removeListener go here.

Auto Trait Implementations§

§

impl !RefUnwindSafe for FocusManager

§

impl !Send for FocusManager

§

impl !Sync for FocusManager

§

impl !UnwindSafe for FocusManager

§

impl Forget for FocusManager

§

impl Freeze for FocusManager
where *const u8 is !null: Freeze, PhantomData<Option<Listener>>: Freeze, *const RcInner<Cell<usize>> is !null: Freeze, PhantomData<RcInner<Cell<usize>>>: Freeze, NonZero<u32>: Freeze, PhantomData<fn() -> HighlightModeManager>: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze, PhantomData<fn() -> FocusScopeNode>: Freeze, &'static FocusNodeVTable: Freeze, PhantomData<Retained<AnyFocusNode>>: Freeze, PhantomData<Autofocus>: Freeze, *const RcInner<dyn WidgetsBindingObserver> is !null: Freeze, PhantomData<RcInner<dyn WidgetsBindingObserver>>: Freeze,

§

impl Move for FocusManager

§

impl Unpin for FocusManager
where *const u8 is !null: Unpin, Rc<dyn Fn(&mut App)>: Unpin, NonZero<u32>: Unpin, *const (): Unpin, Rc<Cell<usize>>: Unpin, Rc<Keep>: Unpin, &'static FocusNodeVTable: Unpin, Rc<dyn WidgetsBindingObserver>: Unpin,

§

impl UnsafeUnpin for FocusManager
where *const u8 is !null: UnsafeUnpin, PhantomData<Option<Listener>>: UnsafeUnpin, *const RcInner<Cell<usize>> is !null: UnsafeUnpin, PhantomData<RcInner<Cell<usize>>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, PhantomData<fn() -> HighlightModeManager>: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: UnsafeUnpin, PhantomData<fn() -> FocusScopeNode>: UnsafeUnpin, &'static FocusNodeVTable: UnsafeUnpin, PhantomData<Retained<AnyFocusNode>>: UnsafeUnpin, PhantomData<Autofocus>: UnsafeUnpin, *const RcInner<dyn WidgetsBindingObserver> is !null: UnsafeUnpin, PhantomData<RcInner<dyn WidgetsBindingObserver>>: UnsafeUnpin,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ListenableObject for T
where T: ChangeNotifier,

Source§

fn add_listener(self: Handle<T>, app: &mut App, listener: Listener)

Register a closure to be called when the object notifies its listeners.
Source§

fn remove_listener(self: Handle<T>, app: &mut App, listener: &Listener)

Remove a previously registered closure from the list of closures that the object notifies.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>