Skip to main content

Focus

Struct Focus 

Source
pub struct Focus {
    pub key: Option<KeyRef>,
    pub parent_node: Option<FocusNodeRef>,
    pub child: WidgetRef,
    pub focus_node: Option<FocusNodeRef>,
    pub autofocus: bool,
    pub on_focus_change: Option<ValueChanged<bool>>,
    pub include_semantics: bool,
    /* private fields */
}
Expand description

A widget that manages a FocusNode to allow keyboard focus to be given to this widget and its descendants.

When the focus is gained or lost, on_focus_change is called.

For key events, on_key_event is called if AnyFocusNode::has_focus is true for this widget’s focus_node, unless a focused descendant’s on_key_event callback returned KeyEventResult::Handled when called.

This widget does not provide any visual indication that the focus has changed. Any desired visual changes should be made when on_focus_change is called.

To access the FocusNode of the nearest ancestor Focus widget and establish a relationship that will rebuild the widget when the focus changes, use the Focus::of and FocusScope::of static methods.

Managing a FocusNode means managing its lifecycle, listening for changes in focus, and re-parenting it when needed to keep the focus hierarchy in sync with the widget hierarchy. This widget does all of those things for you.

If the Focus::new constructor is used, then this widget will manage any given focus_node by overwriting the appropriate values of the node with the values of this widget whenever the Focus widget is updated.

If Focus::with_external_focus_node is used instead, then the values returned by on_key_event, skip_traversal, can_request_focus, and descendants_are_focusable will be the values in the external focus node, and the external focus node’s values will not be overwritten when the widget is updated.

To collect a sub-tree of nodes into an exclusive group that restricts focus traversal to the group, use a FocusScope.

Fields§

§key: Option<KeyRef>§parent_node: Option<FocusNodeRef>

The optional parent node to use when reparenting the focus_node for this Focus widget.

If parent_node is None, then Focus::maybe_of is used to find the parent in the widget tree, which is typically what is desired, since it is easier to reason about the focus tree if it mirrors the shape of the widget tree.

Set this property if the focus tree needs to have a different shape than the widget tree. This is typically in cases where a dialog is in an Overlay (or another part of the widget tree), and focus should behave as if the widgets in the overlay are descendants of the given parent_node for purposes of focus.

Defaults to None.

§child: WidgetRef

The child widget of this Focus.

§focus_node: Option<FocusNodeRef>

An optional focus node to use as the focus node for this widget.

If one is not supplied, then one will be automatically allocated, owned, and managed by this widget. The widget will be focusable even if a focus_node is not supplied. If supplied, the given node will be hosted by this widget, but not owned. See FocusNode for more information on what being hosted and/or owned implies.

A non-null focus_node must be supplied if using the Focus::with_external_focus_node constructor.

§autofocus: bool

True if this widget will be selected as the initial focus when no other node in its scope is currently focused.

Ideally, there is only one widget with autofocus set in each FocusScope. If there is more than one widget with autofocus set, then the first one added to the tree will get focus.

Defaults to false.

§on_focus_change: Option<ValueChanged<bool>>

Handler called when the focus changes.

Called with true if this widget’s node gains focus, and false if it loses focus.

§include_semantics: bool

Include semantics information in this widget.

Defaults to true.

Implementations§

Source§

impl Focus

Source

pub fn new<K>(child: impl IntoWidget<K>) -> Focus

Creates a widget that manages a FocusNode.

Source

pub fn with_external_focus_node<K>( child: impl IntoWidget<K>, focus_node: FocusNodeRef, ) -> Focus

Creates a Focus widget that uses the given focus_node as the source of truth for attributes on the node, rather than the attributes of this widget.

Source

pub fn key(self, key: KeyRef) -> Focus

Dart Focus(key:).

Source

pub fn parent_node(self, parent_node: FocusNodeRef) -> Focus

Dart Focus(parentNode:).

Source

pub fn focus_node(self, focus_node: FocusNodeRef) -> Focus

Dart Focus(focusNode:).

Source

pub fn autofocus(self, autofocus: bool) -> Focus

Dart Focus(autofocus:).

Source

pub fn on_focus_change( self, on_focus_change: impl Fn(&mut App, bool) + 'static, ) -> Focus

Dart Focus(onFocusChange:).

Source

pub fn on_key_event(self, on_key_event: FocusOnKeyEventCallback) -> Focus

Dart Focus(onKeyEvent:).

Source

pub fn can_request_focus(self, can_request_focus: bool) -> Focus

Dart Focus(canRequestFocus:).

Source

pub fn skip_traversal(self, skip_traversal: bool) -> Focus

Dart Focus(skipTraversal:).

Source

pub fn descendants_are_focusable(self, descendants_are_focusable: bool) -> Focus

Dart Focus(descendantsAreFocusable:).

Source

pub fn descendants_are_traversable( self, descendants_are_traversable: bool, ) -> Focus

Dart Focus(descendantsAreTraversable:).

Source

pub fn include_semantics(self, include_semantics: bool) -> Focus

Dart Focus(includeSemantics:).

Source

pub fn debug_label(self, debug_label: impl Into<String>) -> Focus

Dart Focus(debugLabel:).

Source

pub fn get_on_key_event(&self, app: &App) -> Option<FocusOnKeyEventCallback>

A handler for keys that are pressed when this object or one of its children has focus.

Key events are first given to the FocusNode that has primary focus, and if its on_key_event returns KeyEventResult::Ignored, then they are given to each ancestor node up the focus hierarchy in turn. If an event reaches the root of the hierarchy, it is discarded.

Source

pub fn get_can_request_focus(&self, app: &App) -> bool

If true, this widget may request the primary focus.

Defaults to true. Set to false if you want the FocusNode this widget manages to do nothing when AnyFocusNode::request_focus is called on it. Does not affect the children of this node, and AnyFocusNode::has_focus can still return true if this node is the ancestor of the primary focus.

Source

pub fn get_skip_traversal(&self, app: &mut App) -> bool

Sets the AnyFocusNode::skip_traversal flag on the focus node so that it won’t be visited by the FocusTraversalPolicy.

Source

pub fn get_descendants_are_focusable(&self, app: &App) -> bool

If false, will make this widget’s descendants unfocusable.

Defaults to true. Does not affect focusability of this node (just its descendants): for that, use AnyFocusNode::can_request_focus.

Source

pub fn get_descendants_are_traversable(&self, app: &App) -> bool

If false, will make this widget’s descendants untraversable.

Defaults to true. Does not affect traversability of this node (just its descendants): for that, use AnyFocusNode::skip_traversal.

Source

pub fn get_debug_label(&self, app: &App) -> Option<String>

A debug label for this widget.

Source

pub fn of( app: &mut App, context: BuildContext, scope_ok: bool, create_dependency: bool, ) -> AnyFocusNode

Returns the focus node of the Focus that most tightly encloses the given BuildContext.

If no Focus node is found before reaching the nearest FocusScope widget, or there is no Focus widget in the context, then this method panics.

If create_dependency is true, calling this function creates a dependency that will rebuild the given context when the focus node gains or loses focus.

Source

pub fn maybe_of( app: &mut App, context: BuildContext, scope_ok: bool, create_dependency: bool, ) -> Option<AnyFocusNode>

Returns the focus node of the Focus that most tightly encloses the given BuildContext.

If no Focus node is found before reaching the nearest FocusScope widget, or there is no Focus widget in scope, then this method returns None.

If create_dependency is true, calling this function creates a dependency that will rebuild the given context when the focus node gains or loses focus.

Source

pub fn is_at(app: &mut App, context: BuildContext) -> bool

Returns true if the nearest enclosing Focus widget’s node is focused.

A convenience method to allow build methods to write Focus::is_at(app, context) to get whether or not the nearest Focus above them in the widget hierarchy currently has the input focus.

Returns false if no Focus widget is found before reaching the nearest FocusScope, or if the root of the focus tree is reached without finding a Focus widget.

Calling this function creates a dependency that will rebuild the given context when the focus changes.

Trait Implementations§

Source§

impl Debug for Focus

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StatefulWidget for Focus

Source§

type State = FocusState

The state this widget creates.
Source§

fn key(&self) -> Option<&KeyRef>

Source§

fn debug_fill_properties( &self, app: &App, properties: &mut DiagnosticPropertiesBuilder, )

Dart’s debugFillProperties: the properties a dump shows for this widget, in Dart’s order. Dart’s Widget base only sets the dense tree style, which the framework does.
Source§

fn create_state(&self) -> FocusState

Creates the mutable state for this widget at a given location in the tree. Read more
Source§

fn debug_check_constructor(&self)

Source§

fn as_preferred_size_widget(&self) -> Option<&dyn PreferredSizeWidget>

Source§

fn type_name(&self) -> &'static str

See Widget::type_name. A stateful form another widget kind becomes (an animated or status transition widget) names the widget it wraps.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Focus

§

impl !Send for Focus

§

impl !Sync for Focus

§

impl !UnwindSafe for Focus

§

impl Forget for Focus

§

impl Freeze for Focus
where *const RcInner<dyn Key> is !null: Freeze, PhantomData<RcInner<dyn Key>>: Freeze, NonZero<u32>: Freeze, &'static FocusNodeVTable: Freeze, *const RcInner<Keep> is !null: Freeze, PhantomData<RcInner<Keep>>: Freeze, *const RcInner<dyn Widget> is !null: Freeze, PhantomData<RcInner<dyn Widget>>: Freeze, *const RcInner<dyn Fn(&mut App, bool)> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, bool)>>: Freeze, *const RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult> is !null: Freeze, PhantomData<RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>>: Freeze, *const u8 is !null: Freeze, PhantomData<u8>: Freeze,

§

impl Move for Focus

§

impl Unpin for Focus
where Rc<dyn Key>: Unpin, NonZero<u32>: Unpin, &'static FocusNodeVTable: Unpin, Rc<Keep>: Unpin, Rc<dyn Widget>: Unpin, Rc<dyn Fn(&mut App, bool)>: Unpin, Rc<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>: Unpin, *const u8 is !null: Unpin,

§

impl UnsafeUnpin for Focus
where *const RcInner<dyn Key> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Key>>: UnsafeUnpin, NonZeroU32Inner: UnsafeUnpin, &'static FocusNodeVTable: UnsafeUnpin, *const RcInner<Keep> is !null: UnsafeUnpin, PhantomData<RcInner<Keep>>: UnsafeUnpin, *const RcInner<dyn Widget> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Widget>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, bool)> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, bool)>>: UnsafeUnpin, *const RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult> is !null: UnsafeUnpin, PhantomData<RcInner<dyn Fn(&mut App, AnyFocusNode, &KeyEvent) -> KeyEventResult>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<u8>: 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<W> IntoWidget<StatefulKind> for W
where W: StatefulWidget,

Source§

fn into_widget(self) -> Rc<dyn Widget>

The widget as a tree node, made where this is called: its creation_location.
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>