Trait requestty_ui::widgets::Widget
source · [−]pub trait Widget {
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
height
function. - Then, the
render
function 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_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
Render to a given backend.
The widget is responsible for updating the layout to reflect the space that it has used.
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.