pub struct Focus { /* private fields */ }
Expand description
A focus handle that represents a focusable component.
Each Focus
instance has a unique identifier and can be used to manage
focus state for a specific component. The focus system ensures that only
one component can have focus at a time across the entire application.
§Examples
use tessera_ui::Focus;
// Create a focus instance for a component
let button_focus = Focus::new();
// Request focus for this component
button_focus.request_focus();
// Check if this component currently has focus
if button_focus.is_focused() {
println!("Button is focused!");
}
// Focus is automatically cleared when the instance is dropped
drop(button_focus);
Implementations§
Source§impl Focus
impl Focus
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new focus instance with a unique identifier.
Each focus instance is assigned a unique UUID that distinguishes it from all other focus instances in the application. This ensures that focus operations are applied to the correct component.
§Examples
use tessera_ui::Focus;
let focus1 = Focus::new();
let focus2 = Focus::new();
// There can only be one focused component at a time
focus1.request_focus();
assert!(focus1.is_focused());
assert!(!focus2.is_focused());
focus2.request_focus();
assert!(!focus1.is_focused());
assert!(focus2.is_focused());
Sourcepub fn is_focused(&self) -> bool
pub fn is_focused(&self) -> bool
Checks if this focus instance currently has focus.
Returns true
if this specific focus instance is the currently
active focus in the global focus state, false
otherwise.
§Examples
use tessera_ui::Focus;
let focus = Focus::new();
// Initially, no focus is active
assert!(!focus.is_focused());
// After requesting focus, this instance should be focused
focus.request_focus();
assert!(focus.is_focused());
§Thread Safety
This method is thread-safe and can be called from any thread. It acquires a read lock on the global focus state.
Sourcepub fn request_focus(&self)
pub fn request_focus(&self)
Requests focus for this component.
This method sets the global focus state to this focus instance, potentially removing focus from any previously focused component. Only one component can have focus at a time.
§Examples
use tessera_ui::Focus;
let focus1 = Focus::new();
let focus2 = Focus::new();
focus1.request_focus();
assert!(focus1.is_focused());
assert!(!focus2.is_focused());
// Requesting focus for focus2 removes it from focus1
focus2.request_focus();
assert!(!focus1.is_focused());
assert!(focus2.is_focused());
§Thread Safety
This method is thread-safe and can be called from any thread. It acquires a write lock on the global focus state.
Sourcepub fn unfocus(&self)
pub fn unfocus(&self)
Clears focus if this instance currently has it.
This method removes focus from the global state, but only if this specific focus instance is the one that currently has focus. If another component has focus, this method has no effect.
§Examples
use tessera_ui::Focus;
let focus1 = Focus::new();
let focus2 = Focus::new();
focus1.request_focus();
assert!(focus1.is_focused());
// Clear focus from focus1
focus1.unfocus();
assert!(!focus1.is_focused());
// If focus2 has focus, focus1.unfocus() won't affect it
focus2.request_focus();
focus1.unfocus(); // No effect
assert!(focus2.is_focused());
§Thread Safety
This method is thread-safe and can be called from any thread. It acquires a write lock on the global focus state.
Trait Implementations§
Source§impl Default for Focus
impl Default for Focus
Source§fn default() -> Self
fn default() -> Self
Creates a new focus instance with a unique identifier.
This is equivalent to calling Focus::new()
.
Source§impl Drop for Focus
impl Drop for Focus
Source§fn drop(&mut self)
fn drop(&mut self)
Automatically clears focus when the Focus
instance is dropped.
This ensures that focus is properly cleaned up when a component is destroyed or goes out of scope. If this focus instance currently has focus, it will be cleared from the global focus state.
§Examples
use tessera_ui::Focus;
{
let focus = Focus::new();
focus.request_focus();
assert!(focus.is_focused());
} // focus is dropped here, automatically clearing focus
// Focus is now cleared globally
let another_focus = Focus::new();
assert!(!another_focus.is_focused());
§Thread Safety
This method is thread-safe and will properly handle cleanup even if called from different threads.
Auto Trait Implementations§
impl Freeze for Focus
impl RefUnwindSafe for Focus
impl Send for Focus
impl Sync for Focus
impl Unpin for Focus
impl UnwindSafe for Focus
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
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.§impl<T> DowncastSync for T
impl<T> DowncastSync for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian()
.