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
impl FocusManager
Sourcepub fn new(app: &mut App) -> Ref<FocusManager>
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.
Sourcepub fn debug_fill_properties(
self: Handle<Self>,
app: &mut App,
properties: &mut DiagnosticPropertiesBuilder,
)
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.
Sourcepub fn debug_describe_children(
self: Handle<Self>,
app: &mut App,
) -> Vec<Rc<dyn DiagnosticsNode>>
pub fn debug_describe_children( self: Handle<Self>, app: &mut App, ) -> Vec<Rc<dyn DiagnosticsNode>>
Dart’s FocusManager.debugDescribeChildren: the root scope.
Sourcepub fn to_string_short(self: Handle<Self>, _app: &App) -> String
pub fn to_string_short(self: Handle<Self>, _app: &App) -> String
Dart’s FocusManager.toStringShort.
Sourcepub fn to_string_deep(self: Handle<Self>, app: &mut App) -> String
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.
Sourcepub fn to_diagnostics_tree_node(
self: Handle<Self>,
app: &mut App,
name: Option<String>,
) -> DiagnosticableTreeNode
pub fn to_diagnostics_tree_node( self: Handle<Self>, app: &mut App, name: Option<String>, ) -> DiagnosticableTreeNode
Dart’s FocusManager.toDiagnosticsNode(name:), from DiagnosticableTree.
Sourcepub fn listen_to_application_lifecycle_changes_if_supported(
self: Handle<Self>,
app: &mut App,
)
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.
Sourcepub fn register_global_handlers(self: Handle<Self>, app: &mut App)
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.
Sourcepub fn instance(app: &mut App) -> Handle<FocusManager>
pub fn instance(app: &mut App) -> Handle<FocusManager>
Provides convenient access to the current FocusManager singleton from the
WidgetsBinding instance.
Sourcepub fn highlight_strategy(
self: Handle<Self>,
app: &App,
) -> FocusHighlightStrategy
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.
Sourcepub fn set_highlight_strategy(
self: Handle<Self>,
app: &mut App,
value: FocusHighlightStrategy,
)
pub fn set_highlight_strategy( self: Handle<Self>, app: &mut App, value: FocusHighlightStrategy, )
See highlight_strategy.
Sourcepub fn highlight_mode(self: Handle<Self>, app: &mut App) -> FocusHighlightMode
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.
Sourcepub fn add_highlight_mode_listener(
self: Handle<Self>,
app: &mut App,
listener: HighlightModeListener,
)
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.
Sourcepub fn remove_highlight_mode_listener(
self: Handle<Self>,
app: &mut App,
listener: &HighlightModeListener,
)
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.
Sourcepub fn add_early_key_event_handler(
self: Handle<Self>,
app: &mut App,
handler: OnKeyEventCallback,
)
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.
Sourcepub fn remove_early_key_event_handler(
self: Handle<Self>,
app: &mut App,
handler: &OnKeyEventCallback,
)
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.
Sourcepub fn add_late_key_event_handler(
self: Handle<Self>,
app: &mut App,
handler: OnKeyEventCallback,
)
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.
Sourcepub fn remove_late_key_event_handler(
self: Handle<Self>,
app: &mut App,
handler: &OnKeyEventCallback,
)
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.
Sourcepub fn root_scope(self: Handle<Self>, app: &App) -> &Ref<FocusScopeNode>
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.
Sourcepub fn primary_focus(self: Handle<Self>, app: &App) -> Option<AnyFocusNode>
pub fn primary_focus(self: Handle<Self>, app: &App) -> Option<AnyFocusNode>
The node that currently has the primary focus.
Sourcepub fn apply_focus_changes_if_needed(self: Handle<Self>, app: &mut App)
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
impl ChangeNotifier for FocusManager
fn change_notifier_data(&self) -> &ChangeNotifierData
fn change_notifier_data_mut(&mut self) -> &mut ChangeNotifierData
Source§fn will_add_listener(self: Handle<Self>, app: &mut App)
fn will_add_listener(self: Handle<Self>, app: &mut App)
addListener that
work before super.addListener go here.Source§fn did_remove_listener(self: Handle<Self>, app: &mut App)
fn did_remove_listener(self: Handle<Self>, app: &mut App)
removeListener that
work after super.removeListener go here.