pub struct FocusRing { /* private fields */ }Expand description
Tracks which child in a list of focusable items is currently focused.
A FocusRing is a simple index tracker with wrap-around cycling. Parent
components own it and use it to:
- Track which child is focused
- Handle Tab/BackTab navigation
- Query focus state for rendering (e.g.
ring.is_focused(i))
§Example
use tui::FocusRing;
let mut ring = FocusRing::new(3);
assert_eq!(ring.focused(), 0);
ring.focus_next();
assert_eq!(ring.focused(), 1);
ring.focus_next();
ring.focus_next();
assert_eq!(ring.focused(), 0); // wraps aroundImplementations§
Source§impl FocusRing
impl FocusRing
Sourcepub fn new(len: usize) -> Self
pub fn new(len: usize) -> Self
Create a new FocusRing with wrapping enabled.
Focus starts at index 0. If len is 0, all navigation is a no-op.
Sourcepub fn without_wrap(self) -> Self
pub fn without_wrap(self) -> Self
Disable wrap-around: focus_next at the last item and focus_prev at
the first item will not cycle.
Sourcepub fn is_focused(&self, index: usize) -> bool
pub fn is_focused(&self, index: usize) -> bool
Returns true if index is the currently focused index.
Sourcepub fn set_len(&mut self, len: usize)
pub fn set_len(&mut self, len: usize)
Update the number of focusable items. Clamps focused if it would be
out of bounds.
Sourcepub fn focus(&mut self, index: usize) -> bool
pub fn focus(&mut self, index: usize) -> bool
Programmatically set focus to index. Returns false if index is
out of bounds (focus unchanged).
Sourcepub fn focus_next(&mut self) -> bool
pub fn focus_next(&mut self) -> bool
Move focus to the next item. Returns true if focus changed.
Sourcepub fn focus_prev(&mut self) -> bool
pub fn focus_prev(&mut self) -> bool
Move focus to the previous item. Returns true if focus changed.
Sourcepub fn handle_key(&mut self, key_event: KeyEvent) -> FocusOutcome
pub fn handle_key(&mut self, key_event: KeyEvent) -> FocusOutcome
Handle Tab (next) and BackTab (previous) key events.
Returns FocusOutcome::FocusChanged if focus moved,
FocusOutcome::Unchanged if a focus key was pressed but focus didn’t
move, or FocusOutcome::Ignored for all other keys.
Auto Trait Implementations§
impl Freeze for FocusRing
impl RefUnwindSafe for FocusRing
impl Send for FocusRing
impl Sync for FocusRing
impl Unpin for FocusRing
impl UnsafeUnpin for FocusRing
impl UnwindSafe for FocusRing
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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