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.
- Rendering to the screen.
- Handling input events.
§Render Cycle
Rendering happens in a 3 step process.
- First, the height is calculated with the
heightfunction. - Then, the
renderfunction is called which is where the actual drawing happens. The cursor should end at the position reflected by the layout. - Finally, the cursor position which the user needs should see is calculated with the
cursor_posfunction.
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§
Sourcefn render<B: Backend>(
&mut self,
layout: &mut Layout,
backend: &mut B,
) -> Result<()>
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.
Sourcefn height(&mut self, layout: &mut Layout) -> u16
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.
Sourcefn cursor_pos(&mut self, layout: Layout) -> (u16, u16)
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.
Sourcefn handle_key(&mut self, key: KeyEvent) -> bool
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.