rat_cursor/
lib.rs

1#![doc = include_str!("../readme.md")]
2
3/// Trait for accessing the screen-cursor.
4///
5/// In ratatui the screen-cursor can't be set during rendering, instead
6/// it must be set with the Frame at some point.
7///
8/// This trait provides a method to get the screen cursor (if any)
9/// for a widget.
10pub trait HasScreenCursor {
11    /// This returns the cursor position if
12    /// - The cursor is displayed at all, and not scrolled off-screen.
13    /// - The widget has some kind of input focus
14    /// - other reasons
15    fn screen_cursor(&self) -> Option<(u16, u16)>;
16}
17
18/// Returns the screen_cursor for the first widget that returns one.
19#[inline(always)]
20pub fn screen_cursor<const N: usize>(list: [&dyn HasScreenCursor; N]) -> Option<(u16, u16)> {
21    for v in list {
22        if let Some(v) = v.screen_cursor() {
23            return Some(v);
24        }
25    }
26    None
27}
28
29/// Create the implementation of HasScreenCursor for the
30/// given list of struct members.
31#[macro_export]
32macro_rules! impl_screen_cursor {
33    ($($n:ident),* for $ty:ty) => {
34        impl $crate::HasScreenCursor for $ty {
35            fn screen_cursor(&self) -> Option<(u16, u16)> {
36                use $crate::screen_cursor;
37                screen_cursor([
38                    $(&self.$n),*
39                ])
40            }
41        }
42    };
43}