Add Either3 widget

This commit is contained in:
Joscha 2023-02-18 18:30:53 +01:00
parent 15e30dfdb2
commit 828bba464a
2 changed files with 85 additions and 9 deletions

View file

@ -20,15 +20,15 @@ where
max_height: Option<u16>,
) -> Result<Size, E> {
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<u16>,
) -> Result<Size, E> {
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<I1, I2, I3> {
First(I1),
Second(I2),
Third(I3),
}
impl<E, I1, I2, I3> Widget<E> for Either3<I1, I2, I3>
where
I1: Widget<E>,
I2: Widget<E>,
I3: Widget<E>,
{
fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<E, I1, I2, I3> AsyncWidget<E> for Either3<I1, I2, I3>
where
I1: AsyncWidget<E> + Send + Sync,
I2: AsyncWidget<E> + Send + Sync,
I3: AsyncWidget<E> + Send + Sync,
{
async fn size(
&self,
frame: &mut Frame,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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,
}
}
}