Skip to main content

FocusHandle

Struct FocusHandle 

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

A way to put keyboard focus on one widget. Compose’s FocusRequester.

Make one with use_focus_handle, give it to a widget with .focus_handle(&handle), and call FocusHandle::request from a handler or an effect. Cloning is cheap: every clone is the same handle. The handle follows its widget’s node across re-renders, and when the widget is replaced by a new node, it follows the new one.

A drawer that takes focus when it opens and gives it back to the control that opened it when Escape closes it:

ⓘ
use compose_rust::runtime::*;
use compose_rust::ui::*;

#[Composable]
fn Screen() {
    let open = remember(|| mutable_state_of(false));
    let opener = use_focus_handle();
    let drawer = use_focus_handle();
    Column().content(|| {
        Button("Menu")
            .focus_handle(&opener)
            .on_click(move || open.set(true));
        if open.get() {
            let back = opener.clone();
            Column()
                .focus_handle(&drawer)
                .on_key_down(move |key| {
                    if key.text() == "Escape" {
                        key.consume();
                        open.set(false);
                        back.request();
                    }
                })
                .content(|| {
                    Text("Drawer");
                });
        }
    });
    // The drawer's node exists once the composition that opened it is applied, so
    // the request runs as an effect of that composition rather than in the click.
    let take = drawer.clone();
    let is_open = open.get();
    launched_effect(is_open, move || async move {
        if is_open {
            take.request();
        }
    });
}

A request made while the handle’s node is not on screen does nothing and is not kept for later, which is why the drawer above asks from an effect.

Implementations§

Source§

impl FocusHandle

Source

pub fn new() -> Self

A handle bound to nothing yet.

Source

pub fn request(&self)

Asks for keyboard focus on the node this handle is bound to. The next batch carries one RequestFocus for it, however many times this is called before then.

A handle that is bound to no node on screen, because its widget has not been composed yet or has left, asks for nothing.

Source

pub fn is_bound(&self) -> bool

Whether the handle is bound to a node. A node that has since left the screen still counts until another widget takes the handle.

Trait Implementations§

Source§

impl Clone for FocusHandle

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FocusHandle

Source§

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

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

impl Default for FocusHandle

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for FocusHandle

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.