#[component]Expand description
Implements the weavetui_core::Component and weavetui_core::ComponentAccessor traits
for a struct, turning it into a weavetui component.
This macro simplifies component creation by:
- Automatically injecting necessary fields:
children,_area,_active, and_action_tx. - Generating a
Defaultimplementation that initializes children. - Providing default implementations for the
ComponentAccessortrait.
§Attributes
-
#[component(children = [child_name => ChildType, ...])]: Defines child components. The macro will add achildrenfield of typeBTreeMap<String, Box<dyn Component>>and initialize it with the specified children in theDefaultimplementation.child_name: A string literal representing the key for the child.ChildType: The type of the child component, which must implementDefault.
-
#[component(default)]: Generates a defaultdrawmethod implementation for theComponenttrait. This is useful for placeholder components or for quickly visualizing the component’s area. The defaultdrawmethod renders a bordered block with the component’s name and dimensions.
§Injected Fields
When you use the #[component] attribute, the following fields are automatically added to your struct if they are not already present:
pub children: BTreeMap<String, Box<dyn Component>>: A map to hold child components, allowing for nested UI structures._area: Option<ratatui::layout::Rect>: Stores the rendering area assigned to the component by its parent._active: bool: A flag indicating whether the component is currently active and should respond to events._action_tx: Option<UnboundedSender<Action>>: A channel sender for dispatching actions to the application’s central event loop._theme_manager: weavetui_core::theme::ThemeManager: Manages the theme and styles for the component and its children.
§Example
use weavetui::prelude::*; // Includes necessary traits and macros
use ratatui::prelude::*;
#[component(default)]
pub struct Child;
#[component(children = [ "child" => Child ])]
#[derive(Default)]
pub struct MyComponent {
pub counter: i32,
}
impl Component for MyComponent {
fn draw(&mut self, f: &mut Frame, area: Rect) {
// Custom draw logic here
}
}
fn main() {
let my_component = MyComponent::default();
assert_eq!(my_component.children.len(), 1);
assert!(my_component.children.contains_key("child"));
}