| Crates.io | weavetui_derive |
| lib.rs | weavetui_derive |
| version | 0.1.3 |
| created_at | 2025-08-31 14:33:26.321932+00 |
| updated_at | 2025-09-15 12:25:04.889433+00 |
| description | Procedural macro for weavetui components. |
| homepage | https://github.com/mzyui/weavetui |
| repository | https://github.com/mzyui/weavetui |
| max_upload_size | |
| id | 1818604 |
| size | 45,316 |
weavetui_derive is the procedural macro companion for the weavetui Text User Interface (TUI) framework. It provides the powerful #[component] attribute macro, significantly reducing boilerplate code and streamlining the development of weavetui components.
#[component] attribute automatically implements weavetui_core::Component and weavetui_core::ComponentAccessor for your structs. This includes injecting a _ctx field (of type weavetui_core::ComponentContext) to manage internal component state like children, area, active status, action sender, and theme manager.children = [...], fostering a clear and hierarchical UI structure. The macro handles the creation and initialization of these children.weavetui_core: Seamlessly integrates with the core traits and types defined in weavetui_core, providing a cohesive and powerful development experience.To use the #[component] macro in your weavetui project, add weavetui_derive as a dependency in your Cargo.toml:
[dependencies]
weavetui_derive = { version = "0.1.1" } # Or specify a path/git dependency for development
Apply the #[component] attribute to your struct definitions. The macro will automatically generate the necessary trait implementations for weavetui_core::Component and weavetui_core::ComponentAccessor.
When you use the #[component] attribute, a pub _ctx: weavetui_core::ComponentContext field is automatically added to your struct (if not already present). This _ctx field encapsulates essential component state:
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.use weavetui_derive::component;
#[component(default)]
struct MySimpleComponent {
// Your component's custom fields
}
// MySimpleComponent now implements weavetui_core::Component and weavetui_core::ComponentAccessor
// and has a `_ctx` field for internal management.
You can declare child components directly within the #[component] attribute using the children = [...] syntax. The macro will automatically initialize these children within the _ctx.children map.
use weavetui_derive::component;
use weavetui_core::Component; // This import might not be strictly necessary for the example, but good for context
#[component(default)]
struct HeaderComponent;
#[component(default)]
struct FooterComponent;
#[component(default)]
struct ButtonComponent;
#[component(default, children(
"header" => HeaderComponent,
"footer" => FooterComponent,
"button_area" => ButtonComponent,
))]
struct ParentComponent {
title: String,
}
// ParentComponent will have its `_ctx.children` field initialized with instances
// of HeaderComponent, FooterComponent, and ButtonComponent.
A practical example of its usage can be found in the counter_app.rs example within the main weavetui repository.
We welcome contributions to weavetui_derive! Please refer to the main weavetui project's CONTRIBUTING.md for detailed guidelines on how to get involved, report issues, and submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for details.