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

@ -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<W2, W3>(self) -> Either3<Self, W2, W3> {
Either3::First(self)
}
fn second3<W1, W3>(self) -> Either3<W1, Self, W3> {
Either3::Second(self)
}
fn third3<W1, W2>(self) -> Either3<W1, W2, Self> {
Either3::Third(self)
}
fn float(self) -> Float<Self> {
Float::new(self)
}

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,
}
}
}