diff --git a/src/widget.rs b/src/widget.rs index 97e5ee2..2fc3eb9 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; -use crate::widgets::{Background, Border, Either, Float, Layer, Padding}; +use crate::widgets::{Background, Border, Either, Either3, Float, Layer, Padding}; use crate::{Frame, Size}; // TODO Feature-gate these traits @@ -45,6 +45,18 @@ pub trait WidgetExt: Sized { Either::Second(self) } + fn first3(self) -> Either3 { + Either3::First(self) + } + + fn second3(self) -> Either3 { + Either3::Second(self) + } + + fn third3(self) -> Either3 { + Either3::Third(self) + } + fn float(self) -> Float { Float::new(self) } diff --git a/src/widgets/either.rs b/src/widgets/either.rs index 9092d4f..8c4f79a 100644 --- a/src/widgets/either.rs +++ b/src/widgets/either.rs @@ -20,15 +20,15 @@ where 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), + Self::First(w) => w.size(frame, max_width, max_height), + Self::Second(w) => w.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), + Self::First(w) => w.draw(frame), + Self::Second(w) => w.draw(frame), } } } @@ -46,15 +46,79 @@ where 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, + Self::First(w) => w.size(frame, max_width, max_height).await, + Self::Second(w) => w.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, + Self::First(w) => w.draw(frame).await, + Self::Second(w) => w.draw(frame).await, + } + } +} + +#[derive(Debug, Clone, Copy)] +pub enum Either3 { + First(I1), + Second(I2), + Third(I3), +} + +impl Widget for Either3 +where + I1: Widget, + I2: Widget, + I3: Widget, +{ + fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + match self { + Self::First(w) => w.size(frame, max_width, max_height), + Self::Second(w) => w.size(frame, max_width, max_height), + Self::Third(w) => w.size(frame, max_width, max_height), + } + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + Self::First(w) => w.draw(frame), + Self::Second(w) => w.draw(frame), + Self::Third(w) => w.draw(frame), + } + } +} + +#[async_trait] +impl AsyncWidget for Either3 +where + I1: AsyncWidget + Send + Sync, + I2: AsyncWidget + Send + Sync, + I3: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + frame: &mut Frame, + max_width: Option, + max_height: Option, + ) -> Result { + match self { + Self::First(w) => w.size(frame, max_width, max_height).await, + Self::Second(w) => w.size(frame, max_width, max_height).await, + Self::Third(w) => w.size(frame, max_width, max_height).await, + } + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + match self { + Self::First(w) => w.draw(frame).await, + Self::Second(w) => w.draw(frame).await, + Self::Third(w) => w.draw(frame).await, } } }