Add Background widget

This commit is contained in:
Joscha 2022-07-12 21:42:09 +02:00
parent 8eda1ad97d
commit d2c4d2b029
2 changed files with 37 additions and 0 deletions

View file

@ -1,3 +1,4 @@
pub mod background;
pub mod list;
pub mod text;

View file

@ -0,0 +1,36 @@
use async_trait::async_trait;
use crossterm::style::ContentStyle;
use toss::frame::{Frame, Pos, Size};
use super::Widget;
pub struct Background {
inner: Box<dyn Widget + Send>,
style: ContentStyle,
}
impl Background {
pub fn new<W: 'static + Widget + Send>(inner: W, style: ContentStyle) -> Self {
Self {
inner: Box::new(inner),
style,
}
}
}
#[async_trait]
impl Widget for Background {
fn size(&self, frame: &mut Frame, max_width: Option<u16>, max_height: Option<u16>) -> Size {
self.inner.size(frame, max_width, max_height)
}
async fn render(self: Box<Self>, frame: &mut Frame, pos: Pos, size: Size) {
for dy in 0..size.height {
for dx in 0..size.width {
frame.write(pos + Pos::new(dx.into(), dy.into()), (" ", self.style));
}
}
self.inner.render(frame, pos, size).await;
}
}