Add BoxedWidget type alias

This commit is contained in:
Joscha 2022-07-20 22:25:27 +02:00
parent 54ed495491
commit 3e6b214e81
4 changed files with 26 additions and 18 deletions

View file

@ -14,3 +14,11 @@ pub trait Widget {
async fn render(self: Box<Self>, frame: &mut Frame); async fn render(self: Box<Self>, frame: &mut Frame);
} }
pub type BoxedWidget = Box<dyn Widget + Send>;
impl<W: 'static + Widget + Send> From<W> for BoxedWidget {
fn from(widget: W) -> Self {
Box::new(widget)
}
}

View file

@ -2,17 +2,17 @@ use async_trait::async_trait;
use crossterm::style::ContentStyle; use crossterm::style::ContentStyle;
use toss::frame::{Frame, Pos, Size}; use toss::frame::{Frame, Pos, Size};
use super::Widget; use super::{BoxedWidget, Widget};
pub struct Background { pub struct Background {
inner: Box<dyn Widget + Send>, inner: BoxedWidget,
style: ContentStyle, style: ContentStyle,
} }
impl Background { impl Background {
pub fn new<W: 'static + Widget + Send>(inner: W, style: ContentStyle) -> Self { pub fn new<W: Into<BoxedWidget>>(inner: W, style: ContentStyle) -> Self {
Self { Self {
inner: Box::new(inner), inner: inner.into(),
style, style,
} }
} }

View file

@ -1,17 +1,17 @@
use async_trait::async_trait; use async_trait::async_trait;
use toss::frame::{Frame, Pos, Size}; use toss::frame::{Frame, Pos, Size};
use super::Widget; use super::{BoxedWidget, Widget};
pub struct Segment { pub struct Segment {
widget: Box<dyn Widget + Send>, widget: BoxedWidget,
expanding: bool, expanding: bool,
} }
impl Segment { impl Segment {
pub fn new<W: 'static + Widget + Send>(widget: W) -> Self { pub fn new<W: Into<BoxedWidget>>(widget: W) -> Self {
Self { Self {
widget: Box::new(widget), widget: widget.into(),
expanding: false, expanding: false,
} }
} }

View file

@ -4,7 +4,7 @@ use async_trait::async_trait;
use parking_lot::Mutex; use parking_lot::Mutex;
use toss::frame::{Frame, Pos, Size}; use toss::frame::{Frame, Pos, Size};
use super::Widget; use super::{BoxedWidget, Widget};
/////////// ///////////
// State // // State //
@ -199,12 +199,12 @@ impl<Id: Clone> ListState<Id> {
enum Row<Id> { enum Row<Id> {
Unselectable { Unselectable {
normal: Box<dyn Widget + Send>, normal: BoxedWidget,
}, },
Selectable { Selectable {
id: Id, id: Id,
normal: Box<dyn Widget + Send>, normal: BoxedWidget,
selected: Box<dyn Widget + Send>, selected: BoxedWidget,
}, },
} }
@ -257,21 +257,21 @@ impl<Id> List<Id> {
self.rows.is_empty() self.rows.is_empty()
} }
pub fn add_unsel<W: 'static + Widget + Send>(&mut self, normal: W) { pub fn add_unsel<W: Into<BoxedWidget>>(&mut self, normal: W) {
self.rows.push(Row::Unselectable { self.rows.push(Row::Unselectable {
normal: Box::new(normal), normal: normal.into(),
}); });
} }
pub fn add_sel<W1, W2>(&mut self, id: Id, normal: W1, selected: W2) pub fn add_sel<W1, W2>(&mut self, id: Id, normal: W1, selected: W2)
where where
W1: 'static + Widget + Send, W1: Into<BoxedWidget>,
W2: 'static + Widget + Send, W2: Into<BoxedWidget>,
{ {
self.rows.push(Row::Selectable { self.rows.push(Row::Selectable {
id, id,
normal: Box::new(normal), normal: normal.into(),
selected: Box::new(selected), selected: selected.into(),
}); });
} }
} }