diff --git a/CHANGELOG.md b/CHANGELOG.md index d029ed5..0b8b087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ Procedure when bumping the version number: ### Added - `Frame::set_title` +- `WidgetExt::title` +- `widgets::title` ## v0.2.0 - 2023-08-31 diff --git a/src/widget.rs b/src/widget.rs index 58ce562..8aec3d2 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -2,7 +2,7 @@ use async_trait::async_trait; use crate::widgets::{ Background, Border, Boxed, BoxedAsync, BoxedSendSync, Desync, Either2, Either3, Float, - JoinSegment, Layer2, Padding, Resize, + JoinSegment, Layer2, Padding, Resize, Title, }; use crate::{Frame, Size, WidthDb}; @@ -107,6 +107,10 @@ pub trait WidgetExt: Sized { fn resize(self) -> Resize { Resize::new(self) } + + fn title(self, title: S) -> Title { + Title::new(self, title) + } } // It would be nice if this could be restricted to types implementing Widget. diff --git a/src/widgets.rs b/src/widgets.rs index 7c3fc55..28b44d8 100644 --- a/src/widgets.rs +++ b/src/widgets.rs @@ -13,6 +13,7 @@ pub mod padding; pub mod predrawn; pub mod resize; pub mod text; +pub mod title; pub use background::*; pub use border::*; @@ -29,3 +30,4 @@ pub use padding::*; pub use predrawn::*; pub use resize::*; pub use text::*; +pub use title::*; diff --git a/src/widgets/title.rs b/src/widgets/title.rs new file mode 100644 index 0000000..c0dc0d4 --- /dev/null +++ b/src/widgets/title.rs @@ -0,0 +1,59 @@ +use async_trait::async_trait; + +use crate::{AsyncWidget, Frame, Size, Widget, WidthDb}; + +#[derive(Debug, Clone)] +pub struct Title { + pub inner: I, + pub title: String, +} + +impl Title { + pub fn new(inner: I, title: S) -> Self { + Self { + inner, + title: title.to_string(), + } + } +} + +impl Widget for Title +where + I: Widget, +{ + fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.inner.size(widthdb, max_width, max_height) + } + + fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.inner.draw(frame)?; + frame.set_title(Some(self.title)); + Ok(()) + } +} + +#[async_trait] +impl AsyncWidget for Title +where + I: AsyncWidget + Send + Sync, +{ + async fn size( + &self, + widthdb: &mut WidthDb, + max_width: Option, + max_height: Option, + ) -> Result { + self.inner.size(widthdb, max_width, max_height).await + } + + async fn draw(self, frame: &mut Frame) -> Result<(), E> { + self.inner.draw(frame).await?; + frame.set_title(Some(self.title)); + Ok(()) + } +}