pub trait Component {
// Required method
fn render(&self, width: u16) -> Result<Rendered, RenderError>;
// Provided methods
fn render_rect(&self, rect: Rect) -> Result<Rendered, RenderError> { ... }
fn handle_input(&mut self, _event: &Event) -> InputResult { ... }
fn wants_key_release(&self) -> bool { ... }
fn as_focusable(&self) -> Option<&dyn Focusable> { ... }
fn as_focusable_mut(&mut self) -> Option<&mut dyn Focusable> { ... }
}Expand description
A UI element that can be rendered and respond to input.
All visible elements in a TUI application implement this trait. The
framework calls render on every frame and
handle_input when the focused component should
process an event.
Components that can receive focus should also implement Focusable.
Required Methods§
Provided Methods§
Sourcefn render_rect(&self, rect: Rect) -> Result<Rendered, RenderError>
fn render_rect(&self, rect: Rect) -> Result<Rendered, RenderError>
Render this component into a specific rectangular area.
The default implementation delegates to render
with the rect’s width, ignoring height bounds. Components that want
to be layout-aware (e.g. clip to height, scroll, center vertically)
should override this.
Sourcefn handle_input(&mut self, _event: &Event) -> InputResult
fn handle_input(&mut self, _event: &Event) -> InputResult
Handle an input event (key press, resize, mouse, etc.).
The default implementation ignores all events. Override this to add interactivity.
Sourcefn wants_key_release(&self) -> bool
fn wants_key_release(&self) -> bool
Returns true if this component wants to receive
KeyEventKind::Release events in addition to Press / Repeat.
Most components should leave this as false.
Sourcefn as_focusable(&self) -> Option<&dyn Focusable>
fn as_focusable(&self) -> Option<&dyn Focusable>
Cast this component to a Focusable reference, if supported.
Sourcefn as_focusable_mut(&mut self) -> Option<&mut dyn Focusable>
fn as_focusable_mut(&mut self) -> Option<&mut dyn Focusable>
Cast this component to a mutable Focusable reference, if supported.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".