Add Boxed and BoxedAsync widgets

This commit is contained in:
Joscha 2023-04-08 16:00:04 +02:00
parent 007493f136
commit 7c6e651f88
3 changed files with 134 additions and 1 deletions

View file

@ -1,7 +1,8 @@
use async_trait::async_trait;
use crate::widgets::{
Background, Border, Either2, Either3, Float, JoinSegment, Layer2, Padding, Resize,
Background, Border, Boxed, BoxedAsync, Either2, Either3, Float, JoinSegment, Layer2, Padding,
Resize,
};
use crate::{Frame, Size, WidthDb};
@ -39,6 +40,20 @@ pub trait WidgetExt: Sized {
Border::new(self)
}
fn boxed<'a, E>(self) -> Boxed<'a, E>
where
Self: Widget<E> + 'a,
{
Boxed::new(self)
}
fn boxed_async<'a, E>(self) -> BoxedAsync<'a, E>
where
Self: AsyncWidget<E> + Send + Sync + 'a,
{
BoxedAsync::new(self)
}
fn first2<W2>(self) -> Either2<Self, W2> {
Either2::First(self)
}

View file

@ -1,5 +1,6 @@
pub mod background;
pub mod border;
pub mod boxed;
pub mod cursor;
pub mod editor;
pub mod either;
@ -14,6 +15,7 @@ pub mod text;
pub use background::*;
pub use border::*;
pub use boxed::*;
pub use cursor::*;
pub use editor::*;
pub use either::*;

116
src/widgets/boxed.rs Normal file
View file

@ -0,0 +1,116 @@
use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Size, Widget, WidthDb};
pub struct Boxed<'a, E>(Box<dyn WidgetWrapper<E> + 'a>);
impl<'a, E> Boxed<'a, E> {
pub fn new<I>(inner: I) -> Self
where
I: Widget<E> + 'a,
{
Self(Box::new(inner))
}
}
trait WidgetWrapper<E> {
fn wrap_size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E>;
fn wrap_draw(self: Box<Self>, frame: &mut Frame) -> Result<(), E>;
}
impl<E, W> WidgetWrapper<E> for W
where
W: Widget<E>,
{
fn wrap_size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.size(widthdb, max_width, max_height)
}
fn wrap_draw(self: Box<Self>, frame: &mut Frame) -> Result<(), E> {
(*self).draw(frame)
}
}
impl<E> Widget<E> for Boxed<'_, E> {
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.wrap_size(widthdb, max_width, max_height)
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.wrap_draw(frame)
}
}
pub struct BoxedAsync<'a, E>(Box<dyn AsyncWidgetWrapper<E> + Send + Sync + 'a>);
impl<'a, E> BoxedAsync<'a, E> {
pub fn new<I>(inner: I) -> Self
where
I: AsyncWidget<E> + Send + Sync + 'a,
{
Self(Box::new(inner))
}
}
#[async_trait]
trait AsyncWidgetWrapper<E> {
async fn wrap_size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E>;
async fn wrap_draw(self: Box<Self>, frame: &mut Frame) -> Result<(), E>;
}
#[async_trait]
impl<E, W> AsyncWidgetWrapper<E> for W
where
W: AsyncWidget<E> + Send + Sync,
{
async fn wrap_size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.size(widthdb, max_width, max_height).await
}
async fn wrap_draw(self: Box<Self>, frame: &mut Frame) -> Result<(), E> {
(*self).draw(frame).await
}
}
#[async_trait]
impl<E> AsyncWidget<E> for BoxedAsync<'_, E> {
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
self.0.wrap_size(widthdb, max_width, max_height).await
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.0.wrap_draw(frame).await
}
}