From bcc07dc9bab08a260cbd841b612fdcef1706fa45 Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 16 Feb 2023 14:43:48 +0100 Subject: [PATCH] Add WidgetExt and AsyncWidgetExt traits --- examples/hello_world_widgets.rs | 9 +++++---- src/widget.rs | 25 +++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/examples/hello_world_widgets.rs b/examples/hello_world_widgets.rs index 507feea..99393ec 100644 --- a/examples/hello_world_widgets.rs +++ b/examples/hello_world_widgets.rs @@ -2,18 +2,19 @@ use std::convert::Infallible; use crossterm::event::Event; use crossterm::style::{ContentStyle, Stylize}; -use toss::widgets::{Border, BorderLook, Text}; -use toss::{Styled, Terminal, Widget}; +use toss::widgets::{BorderLook, Text}; +use toss::{Styled, Terminal, Widget, WidgetExt}; fn widget() -> impl Widget { - Border::new(Text::new( + Text::new( Styled::new("Hello world!", ContentStyle::default().green()) .then_plain("\n") .then( "Press any key to exit", ContentStyle::default().on_dark_blue(), ), - )) + ) + .border() .look(BorderLook::LINE_DOUBLE) .style(ContentStyle::default().dark_red()) } diff --git a/src/widget.rs b/src/widget.rs index 36fb1e7..4ac3710 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -1,5 +1,6 @@ use async_trait::async_trait; +use crate::widgets::{Border, Padding}; use crate::{Frame, Size}; // TODO Feature-gate these traits @@ -26,3 +27,27 @@ pub trait AsyncWidget { async fn draw(self, frame: &mut Frame) -> Result<(), E>; } + +pub trait WidgetExt: Sized { + fn border(self) -> Border { + Border::new(self) + } + + fn padding(self) -> Padding { + Padding::new(self) + } +} + +impl WidgetExt for W {} + +pub trait AsyncWidgetExt: Sized { + fn border(self) -> Border { + Border::new(self) + } + + fn padding(self) -> Padding { + Padding::new(self) + } +} + +impl AsyncWidgetExt for W {}