Skip to main content

Component

Trait Component 

Source
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§

Source

fn render(&self, width: u16) -> Result<Rendered, RenderError>

Render this component into lines of text at the given width.

The returned Rendered must satisfy the invariant that every line’s visible width is ≤ width.

Provided Methods§

Source

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.

Source

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.

Source

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.

Source

fn as_focusable(&self) -> Option<&dyn Focusable>

Cast this component to a Focusable reference, if supported.

Source

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".

Implementors§