Skip to main content

Component

Trait Component 

Source
pub trait Component {
    type Output;

    // Required methods
    fn handle(
        &mut self,
        event: &Event,
        context: &Context<'_>,
    ) -> Flow<Self::Output>;
    fn render(&self, context: &Context<'_>) -> View;

    // Provided methods
    fn start(&mut self, context: &Context<'_>) -> Flow<Self::Output> { ... }
    fn tick(&self) -> Option<Duration> { ... }
    fn mouse(&self) -> bool { ... }
    fn keymap(&self) -> Keymap { ... }
    fn focusable(&self) -> bool { ... }
    fn focus_step(&mut self, forward: bool) -> bool { ... }
    fn focus_enter(&mut self, forward: bool) -> bool { ... }
    fn default_value(&self) -> Option<Self::Output> { ... }
    fn prompt(
        &mut self,
        io: &mut dyn LineIo,
    ) -> Result<Option<Self::Output>, NotInteractive> { ... }
}
Expand description

An interactive component: a state machine that handles one event at a time and renders its state. The same component runs under run and in an EventLoop, and in the headless driver for tests.

Required Associated Types§

Required Methods§

Source

fn handle(&mut self, event: &Event, context: &Context<'_>) -> Flow<Self::Output>

Handle one event. Ctrl+C never arrives here: the event loop ends the component with Outcome::Interrupted.

Source

fn render(&self, context: &Context<'_>) -> View

Render the current state.

Provided Methods§

Source

fn start(&mut self, context: &Context<'_>) -> Flow<Self::Output>

Called once, before the component is first painted, with the terminal’s size: for work that needs it, such as rendering content at the width. Returning Done or Cancel finishes the component without waiting for an event. The default does nothing.

Source

fn tick(&self) -> Option<Duration>

How often the component wants Event::Tick, for a spinner or a countdown. None (the default): never.

Source

fn mouse(&self) -> bool

Whether the component wants mouse events (#476): clicks, drags and the wheel. Off by default, since reporting the mouse takes text selection away from the terminal; a component turns it on when its caller opts in, and run then enables mouse reporting for the session.

Source

fn keymap(&self) -> Keymap

The bindings this component has right now, for a help overlay, a shortcut list or status-bar hints to read (see keymap). A container’s include its focused child’s, before its own. The default declares none.

Source

fn focusable(&self) -> bool

Whether the component takes focus in a container: Tab stops on it and keys go to it. The default: yes. A label, or a component that has finished, says no.

Source

fn focus_step(&mut self, forward: bool) -> bool

Move focus to the next (forward) or previous focus stop inside this component, for Tab and Shift+Tab. false when there is none after the current one, so the container moves on to its next child. The default, for a component that is one stop: false.

Source

fn focus_enter(&mut self, forward: bool) -> bool

Take focus from outside: at the first stop inside when forward, at the last otherwise. false when nothing in it can take focus. The default: whether the component is focusable.

Source

fn default_value(&self) -> Option<Self::Output>

The value to return when the terminal is not interactive and the policy asks for defaults. None: there is none.

Source

fn prompt( &mut self, io: &mut dyn LineIo, ) -> Result<Option<Self::Output>, NotInteractive>

Ask for the value line by line, for when the terminal is not interactive and the policy asks for a prompt. The default says the component has no line-based form.

Ok(None) means the user backed out (Outcome::Cancelled). When input ends before an answer, the built-in components answer with their default, and without one return NotInteractive::Ended, which degrade reports as NotInteractive::NoDefault: end of input is no answer, not a refusal.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<C: Component + ?Sized> Component for &mut C

A component borrowed mutably is a component, so a caller can mount one and still read its state after the loop.

Source§

type Output = <C as Component>::Output

Source§

fn start(&mut self, context: &Context<'_>) -> Flow<C::Output>

Source§

fn handle(&mut self, event: &Event, context: &Context<'_>) -> Flow<C::Output>

Source§

fn render(&self, context: &Context<'_>) -> View

Source§

fn tick(&self) -> Option<Duration>

Source§

fn mouse(&self) -> bool

Source§

fn keymap(&self) -> Keymap

Source§

fn focusable(&self) -> bool

Source§

fn focus_step(&mut self, forward: bool) -> bool

Source§

fn focus_enter(&mut self, forward: bool) -> bool

Source§

fn default_value(&self) -> Option<C::Output>

Source§

fn prompt( &mut self, io: &mut dyn LineIo, ) -> Result<Option<C::Output>, NotInteractive>

Source§

impl<C: Component + ?Sized> Component for Box<C>

A boxed component is a component: how containers hold children of different types with one output (compose::Child).

Source§

type Output = <C as Component>::Output

Source§

fn start(&mut self, context: &Context<'_>) -> Flow<C::Output>

Source§

fn handle(&mut self, event: &Event, context: &Context<'_>) -> Flow<C::Output>

Source§

fn render(&self, context: &Context<'_>) -> View

Source§

fn tick(&self) -> Option<Duration>

Source§

fn mouse(&self) -> bool

Source§

fn keymap(&self) -> Keymap

Source§

fn focusable(&self) -> bool

Source§

fn focus_step(&mut self, forward: bool) -> bool

Source§

fn focus_enter(&mut self, forward: bool) -> bool

Source§

fn default_value(&self) -> Option<C::Output>

Source§

fn prompt( &mut self, io: &mut dyn LineIo, ) -> Result<Option<C::Output>, NotInteractive>

Implementors§

Source§

impl Component for AssetPicker

Source§

impl Component for ColorPicker

Source§

impl Component for Confirm

Source§

impl Component for DatePicker

Source§

impl Component for DateTimePicker

Source§

impl Component for FilePicker

Source§

impl Component for Form

Source§

impl Component for Help

Source§

impl Component for Input

Source§

impl Component for LogView

Source§

impl Component for Menu

Source§

impl Component for Pager

Source§

impl Component for Paginator

Source§

impl Component for Palette

Source§

impl Component for PluginView

Source§

impl Component for Shortcuts

Source§

impl Component for TextArea

Source§

impl Component for ThemePicker

Source§

impl Component for TimePicker

Source§

impl Component for Viewport

As a component: a pager over its lines with a status line. Enter finishes with the offset reached; q and Escape cancel. Its keys come from its keymap, so they can be rebound.

Source§

impl<'a, M: 'a> Component for Overlays<'a, M>

Source§

impl<'a, M> Component for Column<'a, M>

Source§

impl<'a, M> Component for Row<'a, M>

Source§

impl<C: Component, M> Component for Map<'_, C, M>

Source§

impl<F: Fn(&Context<'_>) -> View, M> Component for Painted<F, M>

Source§

impl<M> Component for Breadcrumbs<'_, M>

Source§

impl<M> Component for Label<M>

Source§

impl<M> Component for Layers<'_, M>

Source§

impl<M> Component for Split<'_, M>

Source§

impl<M> Component for Stack<'_, M>

Source§

impl<M> Component for StatusBar<M>

Source§

impl<M> Component for Tabs<'_, M>

Source§

impl<T: Clone> Component for ApprovalQueue<T>

Source§

impl<T: Clone> Component for Jump<T>

Source§

impl<T: Clone> Component for MultiSelect<T>

Source§

impl<T: Clone> Component for Reorder<T>

Source§

impl<T: Clone> Component for Select<T>

Source§

impl<T: Clone> Component for TableSelect<T>

Source§

impl<T: Clone> Component for TreeSelect<T>