#[tessera]
Expand description
The #[tessera]
attribute macro transforms a regular Rust function into a Tessera UI component.
This macro performs several key transformations:
- Registers the function as a node in the Tessera component tree
- Injects
measure
,state_handler
andon_minimize
functions into the component scope - Manages component tree lifecycle (push/pop operations)
- Provides error safety by wrapping the original function body
§Parameters
_attr
: Attribute arguments (currently unused)item
: The function to be transformed into a component
§Generated Code
The macro generates code that:
- Accesses the Tessera runtime to manage the component tree
- Creates a new component node with the function name
- Provides closures for
measure
andstate_handler
functionality - Executes the original function body within a safe closure
- Cleans up the component tree after execution
§Example
use tessera_ui_macros::tessera;
#[tessera]
fn button_component(label: String) {
// The macro provides access to these functions:
measure(Box::new(|_| {
// Custom layout logic
use tessera_ui::{ComputedData, Px};
Ok(ComputedData {
width: Px(100),
height: Px(50),
})
}));
state_handler(Box::new(|_| {
// Event handling logic
}));
on_minimize(Box::new(|minimized| {
if minimized {
println!("Window minimized!");
} else {
println!("Window restored!");
}
}));
}
§Error Handling
The macro wraps the original function body in a closure to prevent early returns from breaking the component tree structure. This ensures that the component tree is always properly cleaned up, even if the component function returns early.