From 2dee39c03ce4f4d52187c99e4f93d062155f65d0 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 17 Feb 2023 12:13:56 +0100 Subject: [PATCH] Add Cursor widget --- src/widgets.rs | 2 ++ src/widgets/cursor.rs | 67 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/widgets/cursor.rs diff --git a/src/widgets.rs b/src/widgets.rs index 37f4701..81d4dab 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,5 +1,6 @@ mod background; mod border; +mod cursor; mod empty; mod float; mod padding; @@ -7,6 +8,7 @@ mod text; pub use background::*; pub use border::*; +pub use cursor::*; pub use empty::*; pub use float::*; pub use padding::*; diff --git a/src/widgets/cursor.rs b/src/widgets/cursor.rs new file mode 100644 index 0000000..feeb045 --- /dev/null +++ b/src/widgets/cursor.rs @@ -0,0 +1,67 @@ +use async_trait::async_trait; + +use crate::{AsyncWidget, Frame, Pos, Size, Widget}; + +pub struct Cursor { + inner: I, + at: Pos, +} + +impl Cursor { + pub fn new(inner: I) -> Self { + Self { + inner, + at: Pos::ZERO, + } + } + + pub fn at(mut self, pos: Pos) -> Self { + self.at = pos; + self + } + + pub fn at_xy(self, x: i32, y: i32) -> Self { + self.at(Pos::new(x, y)) + } +} + +impl Widget for Cursor +where + I: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.inner.size(frame, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.inner.draw(frame)?; + frame.show_cursor(self.at); + Ok(()) + } +} + +#[async_trait] +impl AsyncWidget for Cursor +where + I: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + self.inner.size(frame, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.inner.draw(frame).await?; + frame.show_cursor(self.at); + Ok(()) + } +}