Widget

Trait Widget 

Source
pub trait Widget {
    // Required methods
    fn render<B: Backend>(
        &mut self,
        layout: &mut Layout,
        backend: &mut B,
    ) -> Result<()>;
    fn height(&mut self, layout: &mut Layout) -> u16;
    fn cursor_pos(&mut self, layout: Layout) -> (u16, u16);
    fn handle_key(&mut self, key: KeyEvent) -> bool;
}
Expand description

A trait to represent renderable objects.

There are 2 purposes of a widget.

  1. Rendering to the screen.
  2. Handling input events.

§Render Cycle

Rendering happens in a 3 step process.

  1. First, the height is calculated with the height function.
  2. Then, the render function is called which is where the actual drawing happens. The cursor should end at the position reflected by the layout.
  3. Finally, the cursor position which the user needs should see is calculated with the cursor_pos function.

While it is not a guarantee that the terminal will be in raw mode, it is highly recommended that those implementing the render cycle call render while in raw mode.

Required Methods§

Source

fn render<B: Backend>( &mut self, layout: &mut Layout, backend: &mut B, ) -> Result<()>

Render to a given backend.

The widget is responsible for updating the layout to reflect the space that it has used.

Source

fn height(&mut self, layout: &mut Layout) -> u16

The number of rows of the terminal the widget will take when rendered.

The widget is responsible for updating the layout to reflect the space that it will use.

Source

fn cursor_pos(&mut self, layout: Layout) -> (u16, u16)

The position of the cursor to be placed at after render. The returned value should be in the form of (x, y), with (0, 0) being the top left of the screen.

For example, if you want the cursor to be at the first character that could be printed, cursor_pos would be (layout.offset_x + layout.line_offset, layout.offset_y). Also see Layout::offset_cursor.

Source

fn handle_key(&mut self, key: KeyEvent) -> bool

Handle a key input. It should return whether key was handled.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<F> Widget for CharInput<F>
where F: Fn(char) -> Option<char>,

Source§

impl<F> Widget for StringInput<F>
where F: Fn(char) -> Option<char>,

Source§

impl<L: List> Widget for Select<L>

Source§

impl<M: AsRef<str>, H: AsRef<str>> Widget for Prompt<M, H>

Source§

impl<S: AsRef<str>> Widget for Text<S>

Source§

impl<T: Deref<Target = str> + ?Sized> Widget for T