Skip to main content

Focusable

Trait Focusable 

Source
pub trait Focusable: 'static {
    // Required method
    fn focus_handle(&self, cx: &App) -> FocusHandle;
}
Expand description

A view that owns a FocusHandle and can be focused as a whole.

Implement it on views that should receive keyboard focus. Context::focus_view then moves focus to the view’s handle, so callers don’t need to keep the handle themselves.

§Examples

struct Search {
    focus_handle: FocusHandle,
}

impl Focusable for Search {
    fn focus_handle(&self, _cx: &App) -> FocusHandle {
        self.focus_handle.clone()
    }
}
impl Render for Search {
    fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
        div().track_focus(&self.focus_handle)
    }
}

window.update(|search, window, cx| window.focus(&search.focus_handle(cx), cx));
window.update(|search, window, _| assert!(search.focus_handle.is_focused(window)));

Required Methods§

Source

fn focus_handle(&self, cx: &App) -> FocusHandle

Returns the focus handle for this view.

Return a clone of a handle you stored, not a new one. A new handle has a new focus id, so focus moved to it would not match the element you render.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§