FocusManager

Struct FocusManager 

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

Manages keyboard focus across multiple components.

FocusManager is generic over an ID type, which is typically a user-defined enum representing the focusable elements in your application. It tracks which element is currently focused and provides methods for navigation.

§Type Parameters

  • Id: The type used to identify focusable elements. Must implement Clone and PartialEq.
  • focus_next() moves focus forward, wrapping from last to first
  • focus_prev() moves focus backward, wrapping from first to last
  • When unfocused, focus_next() focuses the first item, focus_prev() focuses the last

Implementations§

Source§

impl<Id: Clone + PartialEq> FocusManager<Id>

Source

pub fn new(order: Vec<Id>) -> Self

Creates a new focus manager with the given focus order.

The manager starts with no element focused. Use with_initial_focus to start with the first element focused.

§Example
use envision::component::FocusManager;

let focus: FocusManager<&str> = FocusManager::new(vec!["a", "b", "c"]);
assert_eq!(focus.focused(), None);
Source

pub fn with_initial_focus(order: Vec<Id>) -> Self

Creates a new focus manager with the first element focused.

If the order is empty, no element will be focused.

§Example
use envision::component::FocusManager;

let focus = FocusManager::with_initial_focus(vec!["a", "b", "c"]);
assert_eq!(focus.focused(), Some(&"a"));
Source

pub fn focused(&self) -> Option<&Id>

Returns the currently focused ID, if any.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::new(vec!["a", "b"]);
assert_eq!(focus.focused(), None);

focus.focus(&"a");
assert_eq!(focus.focused(), Some(&"a"));
Source

pub fn is_focused(&self, id: &Id) -> bool

Returns true if the given ID is currently focused.

§Example
use envision::component::FocusManager;

let focus = FocusManager::with_initial_focus(vec!["a", "b"]);
assert!(focus.is_focused(&"a"));
assert!(!focus.is_focused(&"b"));
Source

pub fn focus(&mut self, id: &Id) -> bool

Focuses a specific ID.

Returns true if the ID was found and focused, false if the ID is not in the focus order.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::new(vec!["a", "b", "c"]);

assert!(focus.focus(&"b"));
assert_eq!(focus.focused(), Some(&"b"));

assert!(!focus.focus(&"unknown"));
assert_eq!(focus.focused(), Some(&"b")); // Unchanged
Source

pub fn focus_next(&mut self) -> Option<&Id>

Moves focus to the next item in the order.

If no item is currently focused, focuses the first item. Wraps from the last item to the first.

Returns the newly focused ID, or None if the order is empty.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::with_initial_focus(vec!["a", "b", "c"]);

assert_eq!(focus.focus_next(), Some(&"b"));
assert_eq!(focus.focus_next(), Some(&"c"));
assert_eq!(focus.focus_next(), Some(&"a")); // Wraps
Source

pub fn focus_prev(&mut self) -> Option<&Id>

Moves focus to the previous item in the order.

If no item is currently focused, focuses the last item. Wraps from the first item to the last.

Returns the newly focused ID, or None if the order is empty.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::with_initial_focus(vec!["a", "b", "c"]);

assert_eq!(focus.focus_prev(), Some(&"c")); // Wraps from first to last
assert_eq!(focus.focus_prev(), Some(&"b"));
assert_eq!(focus.focus_prev(), Some(&"a"));
Source

pub fn blur(&mut self)

Removes focus entirely.

After calling this, focused() will return None.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::with_initial_focus(vec!["a", "b"]);
assert!(focus.focused().is_some());

focus.blur();
assert!(focus.focused().is_none());
Source

pub fn focus_first(&mut self) -> Option<&Id>

Focuses the first item in the order.

Returns the focused ID, or None if the order is empty.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::new(vec!["a", "b", "c"]);
focus.focus(&"c");

assert_eq!(focus.focus_first(), Some(&"a"));
assert_eq!(focus.focused(), Some(&"a"));
Source

pub fn focus_last(&mut self) -> Option<&Id>

Focuses the last item in the order.

Returns the focused ID, or None if the order is empty.

§Example
use envision::component::FocusManager;

let mut focus = FocusManager::new(vec!["a", "b", "c"]);

assert_eq!(focus.focus_last(), Some(&"c"));
assert_eq!(focus.focused(), Some(&"c"));
Source

pub fn order(&self) -> &[Id]

Returns the focus order.

§Example
use envision::component::FocusManager;

let focus = FocusManager::new(vec!["a", "b", "c"]);
assert_eq!(focus.order(), &["a", "b", "c"]);
Source

pub fn is_empty(&self) -> bool

Returns true if the focus order is empty.

§Example
use envision::component::FocusManager;

let empty: FocusManager<&str> = FocusManager::default();
assert!(empty.is_empty());

let non_empty = FocusManager::new(vec!["a"]);
assert!(!non_empty.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of items in the focus order.

§Example
use envision::component::FocusManager;

let focus = FocusManager::new(vec!["a", "b", "c"]);
assert_eq!(focus.len(), 3);

Trait Implementations§

Source§

impl<Id: Clone> Clone for FocusManager<Id>

Source§

fn clone(&self) -> FocusManager<Id>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Id: Debug> Debug for FocusManager<Id>

Source§

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

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

impl<Id> Default for FocusManager<Id>

Source§

fn default() -> Self

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

Auto Trait Implementations§

§

impl<Id> Freeze for FocusManager<Id>

§

impl<Id> RefUnwindSafe for FocusManager<Id>
where Id: RefUnwindSafe,

§

impl<Id> Send for FocusManager<Id>
where Id: Send,

§

impl<Id> Sync for FocusManager<Id>
where Id: Sync,

§

impl<Id> Unpin for FocusManager<Id>
where Id: Unpin,

§

impl<Id> UnwindSafe for FocusManager<Id>
where Id: UnwindSafe,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.