Add Layer widget
This commit is contained in:
parent
72b44fb3fc
commit
7c3277a822
3 changed files with 76 additions and 1 deletions
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
65
src/widgets/layer.rs
Normal 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(())
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue