Add Layer widget

This commit is contained in:
Joscha 2023-02-17 21:06:12 +01:00
parent 72b44fb3fc
commit 7c3277a822
3 changed files with 76 additions and 1 deletions

View file

@ -1,6 +1,6 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::widgets::{Background, Border, Float, Padding}; use crate::widgets::{Background, Border, Float, Padding, Layer};
use crate::{Frame, Size}; use crate::{Frame, Size};
// TODO Feature-gate these traits // TODO Feature-gate these traits
@ -41,6 +41,14 @@ pub trait WidgetExt: Sized {
Float::new(self) Float::new(self)
} }
fn below<W>(self, top: W) -> Layer<Self, W> {
Layer::new(self, top)
}
fn above<W>(self, bottom: W) -> Layer<W, Self> {
Layer::new(bottom, self)
}
fn padding(self) -> Padding<Self> { fn padding(self) -> Padding<Self> {
Padding::new(self) Padding::new(self)
} }

View file

@ -3,6 +3,7 @@ mod border;
mod cursor; mod cursor;
mod empty; mod empty;
mod float; mod float;
mod layer;
mod padding; mod padding;
mod text; mod text;
@ -11,5 +12,6 @@ pub use border::*;
pub use cursor::*; pub use cursor::*;
pub use empty::*; pub use empty::*;
pub use float::*; pub use float::*;
pub use layer::*;
pub use padding::*; pub use padding::*;
pub use text::*; pub use text::*;

65
src/widgets/layer.rs Normal file
View file

@ -0,0 +1,65 @@
use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Size, Widget};
pub struct Layer<I1, I2> {
bottom: I1,
top: I2,
}
impl<I1, I2> Layer<I1, I2> {
pub fn new(bottom: I1, top: I2) -> Self {
Self { bottom, top }
}
fn size(bottom: Size, top: Size) -> Size {
Size::new(bottom.width.max(top.width), bottom.height.max(top.height))
}
}
impl<E, I1, I2> Widget<E> for Layer<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> {
let bottom = self.bottom.size(frame, max_width, max_height)?;
let top = self.top.size(frame, max_width, max_height)?;
Ok(Self::size(bottom, top))
}
fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.bottom.draw(frame)?;
self.top.draw(frame)?;
Ok(())
}
}
#[async_trait]
impl<E, I1, I2> AsyncWidget<E> for Layer<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> {
let bottom = self.bottom.size(frame, max_width, max_height).await?;
let top = self.top.size(frame, max_width, max_height).await?;
Ok(Self::size(bottom, top))
}
async fn draw(self, frame: &mut Frame) -> Result<(), E> {
self.bottom.draw(frame).await?;
self.top.draw(frame).await?;
Ok(())
}
}