diff --git a/src/ui/widget.rs b/src/ui/widget.rs index cba06c30..cd17bdef 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -144,16 +144,9 @@ pub trait Properties { Box::new(self) } - fn from_boxed_any(any: Box) -> Option - where - Self: Sized + Clone + 'static, - { - any.downcast_ref::().cloned() - } - fn from_callback(callback: Option>, state: &S) -> Option where - Self: Sized + Clone + 'static, + Self: Sized + Clone + 'static + BoxedAny, { callback .map(|callback| (callback)(state)) @@ -161,18 +154,19 @@ pub trait Properties { } } -pub trait WidgetState { - fn to_boxed_any(self) -> Box - where - Self: Sized + Clone + 'static, - { - Box::new(self) - } - +/// Provide default implementations for conversions to and from `Box`. +pub trait BoxedAny { fn from_boxed_any(any: Box) -> Option where Self: Sized + Clone + 'static, { any.downcast_ref::().cloned() } -} + + fn to_boxed_any(self) -> Box + where + Self: Sized + Clone + 'static, + { + Box::new(self) + } +} \ No newline at end of file diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index e63d2d9a..ffc0d4b7 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -10,7 +10,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row}; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::style; -use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget, WidgetState}; +use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget}; #[derive(Clone, Debug)] pub struct Column<'a> { @@ -65,6 +65,7 @@ impl<'a> Default for HeaderProps<'a> { } impl<'a: 'static> Properties for HeaderProps<'a> {} +impl<'a: 'static> BoxedAny for HeaderProps<'a> {} pub struct Header<'a: 'static, S, A> { /// Internal props @@ -205,6 +206,7 @@ impl<'a> Default for FooterProps<'a> { } impl<'a: 'static> Properties for FooterProps<'a> {} +impl<'a: 'static> BoxedAny for FooterProps<'a> {} pub struct Footer<'a, S, A> { /// Internal props @@ -325,6 +327,7 @@ impl ContainerProps { } impl Properties for ContainerProps {} +impl BoxedAny for ContainerProps {} pub struct Container { /// Internal base @@ -459,7 +462,7 @@ pub struct SectionGroupState { focus: Option, } -impl WidgetState for SectionGroupState {} +impl BoxedAny for SectionGroupState {} #[derive(Clone, Default)] pub struct SectionGroupProps { @@ -475,6 +478,7 @@ impl SectionGroupProps { } impl Properties for SectionGroupProps {} +impl BoxedAny for SectionGroupProps {} pub struct SectionGroup { /// Internal base diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 08a62c66..0cfe7eaa 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -6,7 +6,7 @@ use ratatui::layout::{Constraint, Layout}; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{BaseView, Properties, RenderProps, Widget, WidgetState}; +use super::{BaseView, BoxedAny, Properties, RenderProps, Widget}; #[derive(Clone)] pub struct TextFieldProps { @@ -54,7 +54,7 @@ pub struct TextFieldState { pub cursor_position: usize, } -impl WidgetState for TextFieldState {} +impl BoxedAny for TextFieldState {} pub struct TextField { /// Internal base diff --git a/src/ui/widget/list.rs b/src/ui/widget/list.rs index 822ac77e..e763e866 100644 --- a/src/ui/widget/list.rs +++ b/src/ui/widget/list.rs @@ -13,7 +13,8 @@ use ratatui::widgets::TableState; use crate::ui::theme::style; use crate::ui::{layout, span}; -use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget, WidgetState}; +use super::BoxedAny; +use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget}; #[derive(Clone, Debug)] pub struct TableProps<'a, R, const W: usize> @@ -83,7 +84,9 @@ where } impl<'a: 'static, R, const W: usize> Properties for TableProps<'a, R, W> where R: ToRow + 'static {} -impl WidgetState for TableState {} +impl<'a: 'static, R, const W: usize> BoxedAny for TableProps<'a, R, W> where R: ToRow + 'static {} + +impl BoxedAny for TableState {} pub struct Table<'a, S, A, R, const W: usize> where diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index 2c13b0e1..02a54e35 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -5,7 +5,7 @@ use termion::event::Key; use ratatui::layout::{Constraint, Layout}; use ratatui::text::Text; -use super::{BaseView, Properties, RenderProps, Widget, WidgetState}; +use super::{BaseView, BoxedAny, Properties, RenderProps, Widget}; #[derive(Clone)] pub struct ParagraphProps<'a> { @@ -41,6 +41,7 @@ impl<'a> Default for ParagraphProps<'a> { } impl<'a: 'static> Properties for ParagraphProps<'a> {} +impl<'a: 'static> BoxedAny for ParagraphProps<'a> {} #[derive(Clone)] pub struct ParagraphState { @@ -50,7 +51,7 @@ pub struct ParagraphState { pub progress: usize, } -impl WidgetState for ParagraphState {} +impl BoxedAny for ParagraphState {} pub struct Paragraph<'a, S, A> { /// Internal base diff --git a/src/ui/widget/window.rs b/src/ui/widget/window.rs index b5dccd6d..44c9dc81 100644 --- a/src/ui/widget/window.rs +++ b/src/ui/widget/window.rs @@ -12,7 +12,7 @@ use ratatui::widgets::Row; use crate::ui::theme::style; -use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget}; +use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget}; #[derive(Clone)] pub struct WindowProps { @@ -33,6 +33,7 @@ impl Default for WindowProps { } impl Properties for WindowProps {} +impl BoxedAny for WindowProps {} pub struct Window { /// Internal base @@ -152,6 +153,7 @@ impl Default for ShortcutsProps { } impl Properties for ShortcutsProps {} +impl BoxedAny for ShortcutsProps {} pub struct Shortcuts { /// Internal properties