diff --git a/src/ui/widgets.rs b/src/ui/widgets.rs index c9c1288..75fc727 100644 --- a/src/ui/widgets.rs +++ b/src/ui/widgets.rs @@ -14,3 +14,11 @@ pub trait Widget { async fn render(self: Box, frame: &mut Frame); } + +pub type BoxedWidget = Box; + +impl From for BoxedWidget { + fn from(widget: W) -> Self { + Box::new(widget) + } +} diff --git a/src/ui/widgets/background.rs b/src/ui/widgets/background.rs index dad8fd6..bcc03f4 100644 --- a/src/ui/widgets/background.rs +++ b/src/ui/widgets/background.rs @@ -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, + inner: BoxedWidget, style: ContentStyle, } impl Background { - pub fn new(inner: W, style: ContentStyle) -> Self { + pub fn new>(inner: W, style: ContentStyle) -> Self { Self { - inner: Box::new(inner), + inner: inner.into(), style, } } diff --git a/src/ui/widgets/join.rs b/src/ui/widgets/join.rs index 4b13260..9b6c05c 100644 --- a/src/ui/widgets/join.rs +++ b/src/ui/widgets/join.rs @@ -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, + widget: BoxedWidget, expanding: bool, } impl Segment { - pub fn new(widget: W) -> Self { + pub fn new>(widget: W) -> Self { Self { - widget: Box::new(widget), + widget: widget.into(), expanding: false, } } diff --git a/src/ui/widgets/list.rs b/src/ui/widgets/list.rs index 71c9f78..5b67a13 100644 --- a/src/ui/widgets/list.rs +++ b/src/ui/widgets/list.rs @@ -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 ListState { enum Row { Unselectable { - normal: Box, + normal: BoxedWidget, }, Selectable { id: Id, - normal: Box, - selected: Box, + normal: BoxedWidget, + selected: BoxedWidget, }, } @@ -257,21 +257,21 @@ impl List { self.rows.is_empty() } - pub fn add_unsel(&mut self, normal: W) { + pub fn add_unsel>(&mut self, normal: W) { self.rows.push(Row::Unselectable { - normal: Box::new(normal), + normal: normal.into(), }); } pub fn add_sel(&mut self, id: Id, normal: W1, selected: W2) where - W1: 'static + Widget + Send, - W2: 'static + Widget + Send, + W1: Into, + W2: Into, { self.rows.push(Row::Selectable { id, - normal: Box::new(normal), - selected: Box::new(selected), + normal: normal.into(), + selected: selected.into(), }); } }