pub trait Component: ComponentAccessor + Downcast {
Show 13 methods
// Required method
fn draw(&mut self, f: &mut Frame<'_>, area: Rect);
// Provided methods
fn init(&mut self, area: Rect) { ... }
fn keybindings(&self) -> KeyBindings { ... }
fn handle_key_events(&mut self, key: KeyEvent) -> Option<Action> { ... }
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Option<Action> { ... }
fn handle_tick_event(&mut self) -> Option<Action> { ... }
fn handle_frame_event(&mut self) -> Option<Action> { ... }
fn handle_paste_event(&mut self, message: &str) -> Option<Action> { ... }
fn update(&mut self, action: &Action) { ... }
fn on_event(&mut self, message: &str) { ... }
fn child_mut(&mut self, name: &str) -> Option<&mut Box<dyn Component>> { ... }
fn child(&mut self, name: &str) -> Option<&Box<dyn Component>> { ... }
fn on_active_changed(&mut self, active: bool) { ... }
}Expand description
The main trait for all UI components in the weavetui framework.
This trait defines the core functionality of a component, including event handling
(handle_key_events, handle_mouse_events, etc.) and rendering (draw).
Implementors must also implement ComponentAccessor and Downcast.
Required Methods§
Provided Methods§
Sourcefn init(&mut self, area: Rect)
fn init(&mut self, area: Rect)
Initialize the component with a specified area if necessary. Usefull for components that need to performe some initialization before the first render.
§Arguments
area- Rectangular area where the component will be rendered the first time.
Sourcefn keybindings(&self) -> KeyBindings
fn keybindings(&self) -> KeyBindings
Returns the KeyBindings for this component.
KeyBindings are used to display keybinding hints to the user.
Sourcefn handle_key_events(&mut self, key: KeyEvent) -> Option<Action>
fn handle_key_events(&mut self, key: KeyEvent) -> Option<Action>
Sourcefn handle_mouse_events(&mut self, mouse: MouseEvent) -> Option<Action>
fn handle_mouse_events(&mut self, mouse: MouseEvent) -> Option<Action>
Sourcefn handle_tick_event(&mut self) -> Option<Action>
fn handle_tick_event(&mut self) -> Option<Action>
Handle Tick events and produce actions if necessary.
§Returns
Option<Action>- An action to be processed or none.
Sourcefn handle_frame_event(&mut self) -> Option<Action>
fn handle_frame_event(&mut self) -> Option<Action>
Handle frame events and produce actions if necessary.
§Returns
Option<Action>- An action to be processed or none.
Sourcefn handle_paste_event(&mut self, message: &str) -> Option<Action>
fn handle_paste_event(&mut self, message: &str) -> Option<Action>
Sourcefn update(&mut self, action: &Action)
fn update(&mut self, action: &Action)
Update the state of the component based on a received action.
§Arguments
action- An action that may modify the state of the component.
Sourcefn on_event(&mut self, message: &str)
fn on_event(&mut self, message: &str)
Receive a custom event, probably from another component.
§Arguments
message- A string message to be processed.
Sourcefn child_mut(&mut self, name: &str) -> Option<&mut Box<dyn Component>>
fn child_mut(&mut self, name: &str) -> Option<&mut Box<dyn Component>>
Get a child component by name as a mutable reference.
The method will return the child as a mutable reference to a Box<dyn Component>, which
means that the caller will have to downcast it to the desired type if necessary.
let child = self.child_mut("child_name").unwrap();
if let Some(downcasted_child) = child.downcast_mut::<MyComponent>() {
// do something with the downcasted child
}§Arguments
name- The name of the child component.
§Returns
Option<&mut Box<dyn Component>>- A mutable reference to the child component or none.
Sourcefn child(&mut self, name: &str) -> Option<&Box<dyn Component>>
fn child(&mut self, name: &str) -> Option<&Box<dyn Component>>
Get a child component by name as an immutable reference.
The method will return the child as a reference to a Box<dyn Component>, which means that
the caller will have to downcast it to the desired type if necessary.
let child = self.child("child_name").unwrap();
if let Some(downcasted_child) = child.downcast_ref::<MyComponent>() {
// do something with the downcasted child
}… or just use the [child_downcast] utility functions.
§Arguments
name- The name of the child component.
§Returns
Option<&Box<dyn Component>>- A reference to the child component or none.
Sourcefn on_active_changed(&mut self, active: bool)
fn on_active_changed(&mut self, active: bool)
Notify the component that its active state has changed.
Whenever the active state of a component changes, the component will be notified through this method. This is useful for components that need to perform some action when they are activated or deactivated.
§Arguments
active- The new active state of the component.
Implementations§
Source§impl dyn Component
impl dyn Component
Sourcepub fn is<__T>(&self) -> boolwhere
__T: Component,
pub fn is<__T>(&self) -> boolwhere
__T: Component,
Returns true if the trait object wraps an object of type __T.
Sourcepub fn downcast<__T>(
self: Box<dyn Component>,
) -> Result<Box<__T>, Box<dyn Component>>where
__T: Component,
pub fn downcast<__T>(
self: Box<dyn Component>,
) -> Result<Box<__T>, Box<dyn Component>>where
__T: Component,
Returns a boxed object from a boxed trait object if the underlying object is of type
__T. Returns the original boxed trait if it isn’t.
Sourcepub fn downcast_rc<__T>(
self: Rc<dyn Component>,
) -> Result<Rc<__T>, Rc<dyn Component>>where
__T: Component,
pub fn downcast_rc<__T>(
self: Rc<dyn Component>,
) -> Result<Rc<__T>, Rc<dyn Component>>where
__T: Component,
Returns an Rc-ed object from an Rc-ed trait object if the underlying object is of
type __T. Returns the original Rc-ed trait if it isn’t.
Sourcepub fn downcast_ref<__T>(&self) -> Option<&__T>where
__T: Component,
pub fn downcast_ref<__T>(&self) -> Option<&__T>where
__T: Component,
Returns a reference to the object within the trait object if it is of type __T, or
None if it isn’t.
Sourcepub fn downcast_mut<__T>(&mut self) -> Option<&mut __T>where
__T: Component,
pub fn downcast_mut<__T>(&mut self) -> Option<&mut __T>where
__T: Component,
Returns a mutable reference to the object within the trait object if it is of type
__T, or None if it isn’t.