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);
}
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 toss::frame::{Frame, Pos, Size};
use super::Widget;
use super::{BoxedWidget, Widget};
pub struct Background {
inner: Box<dyn Widget + Send>,
inner: BoxedWidget,
style: ContentStyle,
}
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 {
inner: Box::new(inner),
inner: inner.into(),
style,
}
}

View file

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

View file

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