pub struct Stateful<S>where
S: StateTransitions,{ /* private fields */ }Expand description
A stateful element with user-defined state type
The state type S must implement StateTransitions to define how
events cause state changes. Use the on_state callback to apply
visual changes based on state using pattern matching.
§Example
use blinc_layout::prelude::*;
let button = Stateful::new(ButtonState::Idle)
.w(100.0).h(40.0)
.on_state(|state, div| match state {
ButtonState::Idle => { *div = div.swap().bg(Color::BLUE); }
ButtonState::Hovered => { *div = div.swap().bg(Color::CYAN); }
ButtonState::Pressed => { *div = div.swap().bg(Color::BLUE).scale(0.97); }
ButtonState::Disabled => { *div = div.swap().bg(Color::GRAY); }
});Implementations§
Source§impl<S> Stateful<S>where
S: StateTransitions,
impl<S> Stateful<S>where
S: StateTransitions,
Create a stateful element with externally-provided shared state
Use this when you need state to persist across rebuilds.
The shared state can come from WindowedContext::use_stateful_state()
or be created manually.
§Example
// State persists across rebuilds
let state = ctx.use_stateful_state("my_button", ButtonState::Idle);
button()
.with_state(state)
.on_state(|state, div| { ... })Get a clone of the shared state handle
This can be stored externally for state persistence across rebuilds.
Sourcepub fn default_state(self, state: S) -> Stateful<S>
pub fn default_state(self, state: S) -> Stateful<S>
Sourcepub fn inner_render_props(&self) -> RenderProps
pub fn inner_render_props(&self) -> RenderProps
Get the render props of the inner Div
This allows accessing the accumulated layout properties.
Sourcepub fn inner_layout_style(&self) -> Option<Style>
pub fn inner_layout_style(&self) -> Option<Style>
Get a clone of the inner Div’s layout style
This allows capturing the final taffy Style after all builder methods have been applied.
Sourcepub fn apply_callback(&self)
pub fn apply_callback(&self)
Apply the state callback to update the inner div
This is useful when you need to manually trigger a callback application, for example in a custom ElementBuilder::build() implementation.
Sourcepub fn on_state<F>(self, callback: F) -> Stateful<S>
pub fn on_state<F>(self, callback: F) -> Stateful<S>
Set the state change callback
The callback receives the current state for pattern matching and a mutable reference to a Div for applying visual changes. The callback is immediately applied to set the initial visual state, and event handlers are automatically registered to trigger state transitions.
§Example
.on_state(|state, div| match state {
ButtonState::Idle => { *div = div.swap().bg(Color::BLUE); }
ButtonState::Hovered => { *div = div.swap().bg(Color::CYAN); }
// ...
})Sourcepub fn deps(self, signals: &[SignalId]) -> Stateful<S>
pub fn deps(self, signals: &[SignalId]) -> Stateful<S>
Set signal dependencies for this stateful element
When any of the specified signals change, the on_state callback
will be re-run to update the element’s visual props.
§Example
let direction = ctx.use_state_keyed("direction", || Direction::Vertical);
stateful(button_state)
.deps(&[direction.signal_id()])
.on_state(move |state, div| {
// Read direction here - will be current on each refresh
let label = match direction.get() { ... };
div.set_child(span(label));
})Sourcepub fn ensure_state_handlers_registered(&self)
pub fn ensure_state_handlers_registered(&self)
Ensure state transition handlers are registered (idempotent)
This is public so that wrappers like Button can call it to ensure the hover/press state handlers are registered even when not using on_state().
Sourcepub fn dispatch_state(&self, new_state: S) -> bool
pub fn dispatch_state(&self, new_state: S) -> bool
Dispatch a new state
Updates the current state and applies the callback if the state changed. Uses incremental prop/subtree updates instead of full tree rebuild. Returns true if the state changed.
Sourcepub fn handle_event(&self, event: u32) -> bool
pub fn handle_event(&self, event: u32) -> bool
Handle an event and potentially transition state
Returns true if the state changed.
pub fn id(self, id: &str) -> Stateful<S>
Sourcepub fn padding(self, len: Length) -> Stateful<S>
pub fn padding(self, len: Length) -> Stateful<S>
Set padding using a semantic Length value (builder pattern)
Sourcepub fn padding_x(self, len: Length) -> Stateful<S>
pub fn padding_x(self, len: Length) -> Stateful<S>
Set horizontal padding using a semantic Length value (builder pattern)
Sourcepub fn padding_y(self, len: Length) -> Stateful<S>
pub fn padding_y(self, len: Length) -> Stateful<S>
Set vertical padding using a semantic Length value (builder pattern)
Sourcepub fn items_start(self) -> Stateful<S>
pub fn items_start(self) -> Stateful<S>
Align items to start (builder pattern)
Sourcepub fn items_center(self) -> Stateful<S>
pub fn items_center(self) -> Stateful<S>
Center items (builder pattern)
Sourcepub fn justify_start(self) -> Stateful<S>
pub fn justify_start(self) -> Stateful<S>
Justify content start (builder pattern)
Sourcepub fn justify_center(self) -> Stateful<S>
pub fn justify_center(self) -> Stateful<S>
Center justify (builder pattern)
Sourcepub fn justify_end(self) -> Stateful<S>
pub fn justify_end(self) -> Stateful<S>
Justify content end (builder pattern)
Sourcepub fn justify_between(self) -> Stateful<S>
pub fn justify_between(self) -> Stateful<S>
Space between (builder pattern)
Sourcepub fn border(self, width: f32, color: Color) -> Stateful<S>
pub fn border(self, width: f32, color: Color) -> Stateful<S>
Set border with color and width (builder pattern)
Sourcepub fn border_color(self, color: Color) -> Stateful<S>
pub fn border_color(self, color: Color) -> Stateful<S>
Set border color only (builder pattern)
Sourcepub fn border_width(self, width: f32) -> Stateful<S>
pub fn border_width(self, width: f32) -> Stateful<S>
Set border width only (builder pattern)
Sourcepub fn flex_shrink(self) -> Stateful<S>
pub fn flex_shrink(self) -> Stateful<S>
Set flex shrink (builder pattern)
Sourcepub fn flex_shrink_0(self) -> Stateful<S>
pub fn flex_shrink_0(self) -> Stateful<S>
Set flex shrink to 0 (no shrinking) (builder pattern)
Sourcepub fn overflow_clip(self) -> Stateful<S>
pub fn overflow_clip(self) -> Stateful<S>
Set overflow to clip (clips children to container bounds)
Sourcepub fn cursor(self, cursor: CursorStyle) -> Stateful<S>
pub fn cursor(self, cursor: CursorStyle) -> Stateful<S>
Set cursor style (builder pattern)
Sourcepub fn cursor_pointer(self) -> Stateful<S>
pub fn cursor_pointer(self) -> Stateful<S>
Set cursor to pointer (hand) - convenience for clickable elements
Sourcepub fn cursor_text(self) -> Stateful<S>
pub fn cursor_text(self) -> Stateful<S>
Set cursor to text (I-beam) - for text input areas
Sourcepub fn child(self, child: impl ElementBuilder + 'static) -> Stateful<S>
pub fn child(self, child: impl ElementBuilder + 'static) -> Stateful<S>
Add child (builder pattern)
Sourcepub fn on_mouse_down<F>(self, handler: F) -> Stateful<S>
pub fn on_mouse_down<F>(self, handler: F) -> Stateful<S>
Register a mouse down handler (builder pattern)
Sourcepub fn on_mouse_up<F>(self, handler: F) -> Stateful<S>
pub fn on_mouse_up<F>(self, handler: F) -> Stateful<S>
Register a mouse up handler (builder pattern)
Sourcepub fn on_hover_enter<F>(self, handler: F) -> Stateful<S>
pub fn on_hover_enter<F>(self, handler: F) -> Stateful<S>
Register a hover enter handler (builder pattern)
Sourcepub fn on_hover_leave<F>(self, handler: F) -> Stateful<S>
pub fn on_hover_leave<F>(self, handler: F) -> Stateful<S>
Register a hover leave handler (builder pattern)
Sourcepub fn on_unmount<F>(self, handler: F) -> Stateful<S>
pub fn on_unmount<F>(self, handler: F) -> Stateful<S>
Register an unmount handler (builder pattern)
Sourcepub fn on_key_down<F>(self, handler: F) -> Stateful<S>
pub fn on_key_down<F>(self, handler: F) -> Stateful<S>
Register a key down handler (builder pattern)
Sourcepub fn on_key_up<F>(self, handler: F) -> Stateful<S>
pub fn on_key_up<F>(self, handler: F) -> Stateful<S>
Register a key up handler (builder pattern)
Sourcepub fn on_scroll<F>(self, handler: F) -> Stateful<S>
pub fn on_scroll<F>(self, handler: F) -> Stateful<S>
Register a scroll handler (builder pattern)
Sourcepub fn on_mouse_move<F>(self, handler: F) -> Stateful<S>
pub fn on_mouse_move<F>(self, handler: F) -> Stateful<S>
Register a mouse move handler (builder pattern)
Sourcepub fn on_drag<F>(self, handler: F) -> Stateful<S>
pub fn on_drag<F>(self, handler: F) -> Stateful<S>
Register a drag handler (builder pattern)
Drag events are emitted when the mouse moves while a button is pressed.
Use EventContext::local_x/y to get the current position during drag.
Sourcepub fn on_drag_end<F>(self, handler: F) -> Stateful<S>
pub fn on_drag_end<F>(self, handler: F) -> Stateful<S>
Register a drag end handler (builder pattern)
Called when the mouse button is released after a drag operation.
Sourcepub fn on_resize<F>(self, handler: F) -> Stateful<S>
pub fn on_resize<F>(self, handler: F) -> Stateful<S>
Register a resize handler (builder pattern)
Sourcepub fn on_event<F>(self, event_type: u32, handler: F) -> Stateful<S>
pub fn on_event<F>(self, event_type: u32, handler: F) -> Stateful<S>
Register a handler for a specific event type (builder pattern)
Sourcepub fn on_layout<F>(self, callback: F) -> Stateful<S>
pub fn on_layout<F>(self, callback: F) -> Stateful<S>
Set a layout callback that fires synchronously after each layout computation
Unlike on_ready which fires once with a delay, on_layout fires immediately
and synchronously every time the element’s bounds are computed. This is useful
for position-dependent operations like dropdown positioning.
§Example
let trigger_bounds: State<(f32, f32, f32, f32)> = ...;
Stateful::new(())
.w(200.0)
.h(40.0)
.on_layout(move |bounds| {
trigger_bounds.set((bounds.x, bounds.y, bounds.width, bounds.height));
})Sourcepub fn bounds_storage(&self) -> Arc<Mutex<Option<ElementBounds>>>
pub fn bounds_storage(&self) -> Arc<Mutex<Option<ElementBounds>>>
Get the layout bounds storage for reading current bounds
Returns a shared reference to the storage that is updated after each layout. Use this to read the current bounds in event handlers.
Sourcepub fn bind(self, element_ref: &ElementRef<Stateful<S>>) -> BoundStateful<S>
pub fn bind(self, element_ref: &ElementRef<Stateful<S>>) -> BoundStateful<S>
Bind this element to an ElementRef for external access
Returns a BoundStateful that continues the fluent API chain while
also making the element accessible via the ref.
§Example
let button_ref = ElementRef::<Button>::new();
let ui = div()
.child(
button()
.bind(&button_ref) // Binds and continues chain
.on_state(|state, div| { ... })
);
// Later, access via the ref
button_ref.with_mut(|btn| {
btn.dispatch_state(ButtonState::Pressed);
});Trait Implementations§
Source§impl<S> ElementBuilder for Stateful<S>where
S: StateTransitions,
impl<S> ElementBuilder for Stateful<S>where
S: StateTransitions,
Source§fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId
fn build(&self, tree: &mut LayoutTree) -> LayoutNodeId
Source§fn render_props(&self) -> RenderProps
fn render_props(&self) -> RenderProps
Source§fn children_builders(&self) -> &[Box<dyn ElementBuilder>]
fn children_builders(&self) -> &[Box<dyn ElementBuilder>]
Source§fn element_type_id(&self) -> ElementTypeId
fn element_type_id(&self) -> ElementTypeId
Source§fn element_classes(&self) -> &[String]
fn element_classes(&self) -> &[String]
Source§fn event_handlers(&self) -> Option<&EventHandlers>
fn event_handlers(&self) -> Option<&EventHandlers>
Source§fn layout_bounds_storage(&self) -> Option<Arc<Mutex<Option<ElementBounds>>>>
fn layout_bounds_storage(&self) -> Option<Arc<Mutex<Option<ElementBounds>>>>
Source§fn layout_bounds_callback(
&self,
) -> Option<Arc<dyn Fn(ElementBounds) + Send + Sync>>
fn layout_bounds_callback( &self, ) -> Option<Arc<dyn Fn(ElementBounds) + Send + Sync>>
Source§fn layout_animation_config(&self) -> Option<LayoutAnimationConfig>
fn layout_animation_config(&self) -> Option<LayoutAnimationConfig>
Source§fn visual_animation_config(&self) -> Option<VisualAnimationConfig>
fn visual_animation_config(&self) -> Option<VisualAnimationConfig>
Source§fn scroll_physics(&self) -> Option<Arc<Mutex<ScrollPhysics>>>
fn scroll_physics(&self) -> Option<Arc<Mutex<ScrollPhysics>>>
Source§fn text_render_info(&self) -> Option<TextRenderInfo>
fn text_render_info(&self) -> Option<TextRenderInfo>
Source§fn styled_text_render_info(&self) -> Option<StyledTextRenderInfo>
fn styled_text_render_info(&self) -> Option<StyledTextRenderInfo>
Source§fn svg_render_info(&self) -> Option<SvgRenderInfo>
fn svg_render_info(&self) -> Option<SvgRenderInfo>
Source§fn image_render_info(&self) -> Option<ImageRenderInfo>
fn image_render_info(&self) -> Option<ImageRenderInfo>
Source§fn canvas_render_info(
&self,
) -> Option<Rc<dyn Fn(&mut dyn DrawContext, CanvasBounds)>>
fn canvas_render_info( &self, ) -> Option<Rc<dyn Fn(&mut dyn DrawContext, CanvasBounds)>>
Source§fn scroll_info(&self) -> Option<ScrollRenderInfo>
fn scroll_info(&self) -> Option<ScrollRenderInfo>
Source§fn motion_animation_for_child(
&self,
_child_index: usize,
) -> Option<MotionAnimation>
fn motion_animation_for_child( &self, _child_index: usize, ) -> Option<MotionAnimation>
Source§fn motion_bindings(&self) -> Option<MotionBindings>
fn motion_bindings(&self) -> Option<MotionBindings>
Source§fn motion_stable_id(&self) -> Option<&str>
fn motion_stable_id(&self) -> Option<&str>
Source§fn motion_should_replay(&self) -> bool
fn motion_should_replay(&self) -> bool
Source§fn motion_is_suspended(&self) -> bool
fn motion_is_suspended(&self) -> bool
Source§fn motion_is_exiting(&self) -> bool
fn motion_is_exiting(&self) -> bool
Use query_motion(key).exit() to explicitly trigger motion exit
Source§fn semantic_type_name(&self) -> Option<&'static str>
fn semantic_type_name(&self) -> Option<&'static str>
Source§fn set_auto_id(&mut self, _id: String) -> bool
fn set_auto_id(&mut self, _id: String) -> bool
Source§fn children_builders_mut(&mut self) -> &mut [Box<dyn ElementBuilder>]
fn children_builders_mut(&mut self) -> &mut [Box<dyn ElementBuilder>]
Source§fn bound_scroll_ref(&self) -> Option<&ScrollRef>
fn bound_scroll_ref(&self) -> Option<&ScrollRef>
Source§fn motion_on_ready_callback(
&self,
) -> Option<Arc<dyn Fn(ElementBounds) + Send + Sync>>
fn motion_on_ready_callback( &self, ) -> Option<Arc<dyn Fn(ElementBounds) + Send + Sync>>
Auto Trait Implementations§
impl<S> !Freeze for Stateful<S>
impl<S> !RefUnwindSafe for Stateful<S>
impl<S> !Send for Stateful<S>
impl<S> !Sync for Stateful<S>
impl<S> Unpin for Stateful<S>
impl<S> UnsafeUnpin for Stateful<S>
impl<S> !UnwindSafe for Stateful<S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.