flywheel/widget/traits.rs
1//! Widget trait and common widget implementations.
2//!
3//! This module defines the core `Widget` trait that all UI components implement,
4//! along with commonly used widgets like `TextInput` and `StatusBar`.
5
6use crate::buffer::Buffer;
7use crate::layout::Rect;
8use crate::actor::InputEvent;
9
10/// A UI component that can be rendered to a buffer and handle input.
11///
12/// All widgets implement this trait, allowing them to be composed into
13/// complex layouts and handled uniformly by the rendering system.
14pub trait Widget {
15 /// Get the current bounds of this widget.
16 fn bounds(&self) -> Rect;
17
18 /// Set the bounds of this widget.
19 ///
20 /// Called when the layout changes (e.g., terminal resize).
21 fn set_bounds(&mut self, bounds: Rect);
22
23 /// Render this widget to the given buffer.
24 ///
25 /// The widget should only write to cells within its bounds.
26 fn render(&self, buffer: &mut Buffer);
27
28 /// Handle an input event.
29 ///
30 /// Returns `true` if the event was consumed by this widget,
31 /// `false` if it should propagate to other widgets.
32 fn handle_input(&mut self, event: &InputEvent) -> bool;
33
34 /// Check if this widget needs to be redrawn.
35 fn needs_redraw(&self) -> bool;
36
37 /// Clear the redraw flag after rendering.
38 fn clear_redraw(&mut self);
39}