Crates.io | rat-cursor |
lib.rs | rat-cursor |
version | 0.25.1 |
source | src |
created_at | 2024-09-12 06:36:57.3683 |
updated_at | 2024-10-18 02:47:56.321364 |
description | ratatui trait to communicate the cursor position across widgets |
homepage | |
repository | https://github.com/thscharler/rat-cursor |
max_upload_size | |
id | 1372517 |
size | 4,369 |
This crate is a part of rat-salsa.
This crate defines just the trait HasScreenCursor for use in other crates. This aims to overcome the shortcomings of ratatui to handle cursor positioning by widgets.
In the long run I hope there will be a solution within ratatui which will make this obsolete, but for now ...
pub trait HasScreenCursor {
fn screen_cursor(&self) -> Option<(u16, u16)>;
}
This trait is implemented for the widget-state struct.
It's implemented for the state struct because the widget might need to run the full layout process to know the cursor position. Which would approximately double the rendering process.
Instead of setting the cursor position during rendering somehow, the rendering process stores the cursor position in the state struct, where it can be retrieved later on.
The trait returns a screen-position, but only if it actually needs the cursor to be displayed:
A container widget can cascade down to its components.
fn screen_cursor(&self) -> Option<(u16, u16)> {
self.widget1.screen_cursor()
.or(self.widget2.screen_cursor())
.or(self.widget3.screen_cursor())
}