Add WidgetExt and AsyncWidgetExt traits

This commit is contained in:
Joscha 2023-02-16 14:43:48 +01:00
parent dbafc40700
commit bcc07dc9ba
2 changed files with 30 additions and 4 deletions

View file

@ -2,18 +2,19 @@ use std::convert::Infallible;
use crossterm::event::Event; use crossterm::event::Event;
use crossterm::style::{ContentStyle, Stylize}; use crossterm::style::{ContentStyle, Stylize};
use toss::widgets::{Border, BorderLook, Text}; use toss::widgets::{BorderLook, Text};
use toss::{Styled, Terminal, Widget}; use toss::{Styled, Terminal, Widget, WidgetExt};
fn widget() -> impl Widget<Infallible> { fn widget() -> impl Widget<Infallible> {
Border::new(Text::new( Text::new(
Styled::new("Hello world!", ContentStyle::default().green()) Styled::new("Hello world!", ContentStyle::default().green())
.then_plain("\n") .then_plain("\n")
.then( .then(
"Press any key to exit", "Press any key to exit",
ContentStyle::default().on_dark_blue(), ContentStyle::default().on_dark_blue(),
), ),
)) )
.border()
.look(BorderLook::LINE_DOUBLE) .look(BorderLook::LINE_DOUBLE)
.style(ContentStyle::default().dark_red()) .style(ContentStyle::default().dark_red())
} }

View file

@ -1,5 +1,6 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::widgets::{Border, Padding};
use crate::{Frame, Size}; use crate::{Frame, Size};
// TODO Feature-gate these traits // TODO Feature-gate these traits
@ -26,3 +27,27 @@ pub trait AsyncWidget<E> {
async fn draw(self, frame: &mut Frame) -> Result<(), E>; async fn draw(self, frame: &mut Frame) -> Result<(), E>;
} }
pub trait WidgetExt: Sized {
fn border(self) -> Border<Self> {
Border::new(self)
}
fn padding(self) -> Padding<Self> {
Padding::new(self)
}
}
impl<W> WidgetExt for W {}
pub trait AsyncWidgetExt: Sized {
fn border(self) -> Border<Self> {
Border::new(self)
}
fn padding(self) -> Padding<Self> {
Padding::new(self)
}
}
impl<W> AsyncWidgetExt for W {}