From 904f5c16fa1cb3d94cc76e8c44dcac5006c4c3e2 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 16 Feb 2023 09:27:18 +0100 Subject: [PATCH] Add Widget and AsyncWidget traits --- Cargo.toml | 1 + src/lib.rs | 2 ++ src/widget.rs | 28 ++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+) create mode 100644 src/widget.rs diff --git a/Cargo.toml b/Cargo.toml index c2572b2..b1d625c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +async-trait = "0.1.64" crossterm = "0.26.0" unicode-linebreak = "0.1.4" unicode-segmentation = "1.10.1" diff --git a/src/lib.rs b/src/lib.rs index 4daa5ec..8b4f086 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,7 @@ mod buffer; mod frame; mod styled; mod terminal; +mod widget; mod widthdb; mod wrap; @@ -20,4 +21,5 @@ pub use buffer::{Pos, Size}; pub use frame::*; pub use styled::*; pub use terminal::*; +pub use widget::*; pub use widthdb::*; diff --git a/src/widget.rs b/src/widget.rs new file mode 100644 index 0000000..36fb1e7 --- /dev/null +++ b/src/widget.rs @@ -0,0 +1,28 @@ +use async_trait::async_trait; + +use crate::{Frame, Size}; + +// TODO Feature-gate these traits + +pub trait Widget { + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result; + + fn draw(self, frame: &mut Frame) -> Result<(), E>; +} + +#[async_trait] +pub trait AsyncWidget { + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result; + + async fn draw(self, frame: &mut Frame) -> Result<(), E>; +}