pub trait HasFocus {
// Required methods
fn build(&self, builder: &mut FocusBuilder);
fn focus(&self) -> FocusFlag;
fn area(&self) -> Rect;
// Provided methods
fn build_nav(&self, navigable: Navigation, builder: &mut FocusBuilder) { ... }
fn id(&self) -> usize { ... }
fn area_z(&self) -> u16 { ... }
fn navigable(&self) -> Navigation { ... }
fn is_focused(&self) -> bool { ... }
fn lost_focus(&self) -> bool { ... }
fn gained_focus(&self) -> bool { ... }
fn is_obscured(&self) -> bool { ... }
}Expand description
Trait for a widget that takes part of focus handling.
When used for a simple widget implement
- build()
- focus()
- area()
and optionally
- area_z() and navigable()
use rat_focus::ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
impl HasFocus for MyWidgetState {
fn build(&self, builder: &mut FocusBuilder) {
builder.leaf_widget(self);
}
fn focus(&self) -> FocusFlag {
self.focus.clone()
}
fn area(&self) -> Rect {
self.area
}
}When used for a container widget implement
- build()
use rat_focus::ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }
impl HasFocus for SomeWidgetState {
fn build(&self, builder: &mut FocusBuilder) {
let tag = builder.start(self);
builder.widget(&self.component_a);
builder.widget(&self.component_b);
builder.end(tag);
}
fn focus(&self) -> FocusFlag {
self.focus.clone()
}
fn area(&self) -> Rect {
self.area
}
}Creates a container with an identity.
Or
use rat_focus::ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};
struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }
impl HasFocus for SomeWidgetState {
fn build(&self, builder: &mut FocusBuilder) {
builder.widget(&self.component_a);
builder.widget(&self.component_b);
}
fn focus(&self) -> FocusFlag {
unimplemented!("not in use")
}
fn area(&self) -> Rect {
unimplemented!("not in use")
}
}for an anonymous container.
focus(), area() and area_z() are only used for the first case. navigable() is ignored for containers, leave it at the default.
Required Methods§
Sourcefn build(&self, builder: &mut FocusBuilder)
fn build(&self, builder: &mut FocusBuilder)
Build the focus-structure for the container/widget.
Provided Methods§
Build the focus-structure for the container/widget.
This function is called when the default navigation is overridden by calling FocusBuilder::widget_navigate. You only need to implement this function, if you have a container-widget, that wants to react to an alternate navigation.
For regular widgets this will be called too, but the overridden flag will be used by Focus, regardless of what you do. It’s only useful to get a notification of an alternate navigation.
It defaults to calling build. If you don’t have very specific requirements, you need not concern with this; just implement HasFocus::build.
Sourcefn area_z(&self) -> u16
fn area_z(&self) -> u16
Z-value for the area.
When testing for mouse interactions the z-value is taken into account.
Declares how the widget interacts with focus.
Default is Navigation::Regular.
Sourcefn is_focused(&self) -> bool
fn is_focused(&self) -> bool
Does this widget have the focus or if the flag is used for a container, does any of widgets inside the container have the focus.
This flag is set by Focus::handle.
See match_focus!.
Sourcefn lost_focus(&self) -> bool
fn lost_focus(&self) -> bool
This widget just lost the focus.
This flag is set by Focus::handle if there is a focus transfer,
and will be reset by the next call to Focus::handle.
See on_lost!.
Sourcefn gained_focus(&self) -> bool
fn gained_focus(&self) -> bool
This widget just gained the focus.
This flag is set by Focus::handle if there is a focus transfer,
and will be reset by the next call to Focus::handle.
See on_gained!.
Sourcefn is_obscured(&self) -> bool
fn is_obscured(&self) -> bool
This widget is not beneath the mouse-cursor.
Focus runs a hit-test with the current mouse position for all the widgets. A widget is not obscured if the widget area matches and there is no other matching widget/container with a higher z-index.
This flag is set when handling a mouse-event.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".