diff --git a/src/widget.rs b/src/widget.rs index 85a5eb9..d54cebd 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::widgets::{Background, Border, Float, Padding, Layer}; +use crate::widgets::{Background, Border, Either, Float, Layer, Padding}; use crate::{Frame, Size}; // TODO Feature-gate these traits @@ -37,6 +37,14 @@ pub trait WidgetExt: Sized { Border::new(self) } + fn first(self) -> Either { + Either::First(self) + } + + fn second(self) -> Either { + Either::Second(self) + } + fn float(self) -> Float { Float::new(self) } diff --git a/src/widgets.rs b/src/widgets.rs index fe1346d..abb6b8b 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -1,6 +1,7 @@ mod background; mod border; mod cursor; +mod either; mod empty; mod float; mod layer; @@ -10,6 +11,7 @@ mod text; pub use background::*; pub use border::*; pub use cursor::*; +pub use either::*; pub use empty::*; pub use float::*; pub use layer::*; diff --git a/src/widgets/either.rs b/src/widgets/either.rs new file mode 100644 index 0000000..facb2d9 --- /dev/null +++ b/src/widgets/either.rs @@ -0,0 +1,59 @@ +use async_trait::async_trait; + +use crate::{AsyncWidget, Frame, Size, Widget}; + +pub enum Either { + First(I1), + Second(I2), +} + +impl Widget for Either +where + I1: Widget, + I2: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + match self { + Self::First(l) => l.size(frame, max_width, max_height), + Self::Second(r) => r.size(frame, max_width, max_height), + } + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + Self::First(l) => l.draw(frame), + Self::Second(r) => r.draw(frame), + } + } +} + +#[async_trait] +impl AsyncWidget for Either +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + match self { + Self::First(l) => l.size(frame, max_width, max_height).await, + Self::Second(r) => r.size(frame, max_width, max_height).await, + } + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + Self::First(l) => l.draw(frame).await, + Self::Second(r) => r.draw(frame).await, + } + } +}