Add Either widget
This commit is contained in:
parent
8834bb6d9d
commit
95a01d5fc8
3 changed files with 70 additions and 1 deletions
|
|
@ -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<W>(self) -> Either<Self, W> {
|
||||
Either::First(self)
|
||||
}
|
||||
|
||||
fn second<W>(self) -> Either<W, Self> {
|
||||
Either::Second(self)
|
||||
}
|
||||
|
||||
fn float(self) -> Float<Self> {
|
||||
Float::new(self)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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::*;
|
||||
|
|
|
|||
59
src/widgets/either.rs
Normal file
59
src/widgets/either.rs
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::{AsyncWidget, Frame, Size, Widget};
|
||||
|
||||
pub enum Either<I1, I2> {
|
||||
First(I1),
|
||||
Second(I2),
|
||||
}
|
||||
|
||||
impl<E, I1, I2> Widget<E> for Either<I1, I2>
|
||||
where
|
||||
I1: Widget<E>,
|
||||
I2: Widget<E>,
|
||||
{
|
||||
fn size(
|
||||
&self,
|
||||
frame: &mut Frame,
|
||||
max_width: Option<u16>,
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
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<E, I1, I2> AsyncWidget<E> for Either<I1, I2>
|
||||
where
|
||||
I1: AsyncWidget<E> + Send + Sync,
|
||||
I2: 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(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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue