Add Title widget

This commit is contained in:
Joscha 2024-01-05 13:33:51 +01:00
parent 2714deeafb
commit b757f1be03
4 changed files with 68 additions and 1 deletions

View file

@ -15,6 +15,8 @@ Procedure when bumping the version number:
### Added
- `Frame::set_title`
- `WidgetExt::title`
- `widgets::title`
## v0.2.0 - 2023-08-31

View file

@ -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<Self> {
Resize::new(self)
}
fn title<S: ToString>(self, title: S) -> Title<Self> {
Title::new(self, title)
}
}
// It would be nice if this could be restricted to types implementing Widget.

View file

@ -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::*;

59
src/widgets/title.rs Normal file
View file

@ -0,0 +1,59 @@
use async_trait::async_trait;
use crate::{AsyncWidget, Frame, Size, Widget, WidthDb};
#[derive(Debug, Clone)]
pub struct Title<I> {
pub inner: I,
pub title: String,
}
impl<I> Title<I> {
pub fn new<S: ToString>(inner: I, title: S) -> Self {
Self {
inner,
title: title.to_string(),
}
}
}
impl<E, I> Widget<E> for Title<I>
where
I: Widget<E>,
{
fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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<E, I> AsyncWidget<E> for Title<I>
where
I: AsyncWidget<E> + Send + Sync,
{
async fn size(
&self,
widthdb: &mut WidthDb,
max_width: Option<u16>,
max_height: Option<u16>,
) -> Result<Size, E> {
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(())
}
}