From f327d8670e04e2a7819904422876ab80ebb86764 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 25 Apr 2024 13:57:31 +0200 Subject: [PATCH] lib: Improve widget state conversions --- src/ui/widget.rs | 24 ++++++++++++++++++++++-- src/ui/widget/container.rs | 12 ++---------- src/ui/widget/input.rs | 11 +++++++++-- src/ui/widget/text.rs | 10 ++++++++-- 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index c663ccf6..5cebc16e 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -21,7 +21,7 @@ use super::{layout, span}; pub type BoxedWidget = Box>; pub type UpdateCallback = fn(&S) -> Box; -pub type EventCallback = fn(&dyn Any, UnboundedSender); +pub type EventCallback = fn(Box, UnboundedSender); /// A `View`s common fields. pub struct BaseView { @@ -130,6 +130,22 @@ pub trait Properties { } } +pub trait WidgetState { + fn to_boxed_any(self) -> Box + where + Self: Sized + Clone + 'static, + { + Box::new(self) + } + + fn from_boxed_any(any: Box) -> Option + where + Self: Sized + Clone + 'static, + { + any.downcast_ref::().cloned() + } +} + #[derive(Clone)] pub struct WindowProps { current_page: Option, @@ -457,6 +473,7 @@ where } impl<'a: 'static, R> Properties for TableProps<'a, R> where R: ToRow + 'static {} +impl WidgetState for TableState {} pub struct Table<'a, S, A, R> where @@ -570,7 +587,10 @@ where self.props.selected = self.state.selected(); if let Some(on_event) = self.base.on_event { - (on_event)(&self.state, self.base.action_tx.clone()); + (on_event)( + self.state.clone().to_boxed_any(), + self.base.action_tx.clone(), + ); } } diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index a6552b0c..ee40a4f0 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -92,11 +92,7 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { } } - fn handle_event(&mut self, _key: Key) { - if let Some(on_event) = self.base.on_event { - (on_event)(&self.props, self.base.action_tx.clone()); - } - } + fn handle_event(&mut self, _key: Key) {} fn update(&mut self, state: &S) { self.props = self @@ -269,11 +265,7 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { } } - fn handle_event(&mut self, _key: Key) { - if let Some(on_event) = self.base.on_event { - (on_event)(&self.props, self.base.action_tx.clone()); - } - } + fn handle_event(&mut self, _key: Key) {} fn update(&mut self, state: &S) { self.props = self diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 733754ae..ac83b78c 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -9,7 +9,7 @@ use ratatui::prelude::Rect; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{BaseView, Properties, Widget}; +use super::{BaseView, Properties, Widget, WidgetState}; #[derive(Clone)] pub struct TextFieldProps { @@ -50,11 +50,15 @@ impl Default for TextFieldProps { } impl Properties for TextFieldProps {} + +#[derive(Clone)] pub struct TextFieldState { pub text: Option, pub cursor_position: usize, } +impl WidgetState for TextFieldState {} + pub struct TextField { /// Internal base base: BaseView, @@ -167,7 +171,10 @@ impl Widget for TextField { } if let Some(on_event) = self.base.on_event { - (on_event)(&self.state, self.base.action_tx.clone()); + (on_event)( + self.state.clone().to_boxed_any(), + self.base.action_tx.clone(), + ); } } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index a2fe59b4..61f689c8 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -7,7 +7,7 @@ use termion::event::Key; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::text::Text; -use super::{BaseView, Properties, Widget}; +use super::{BaseView, Properties, Widget, WidgetState}; #[derive(Clone)] pub struct ParagraphProps<'a> { @@ -51,6 +51,7 @@ impl<'a> Default for ParagraphProps<'a> { impl<'a: 'static> Properties for ParagraphProps<'a> {} +#[derive(Clone)] pub struct ParagraphState { /// Internal offset pub offset: usize, @@ -58,6 +59,8 @@ pub struct ParagraphState { pub progress: usize, } +impl WidgetState for ParagraphState {} + pub struct Paragraph<'a, S, A> { /// Internal base base: BaseView, @@ -190,7 +193,10 @@ impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { } if let Some(on_event) = self.base.on_event { - (on_event)(&self.state, self.base.action_tx.clone()); + (on_event)( + self.state.clone().to_boxed_any(), + self.base.action_tx.clone(), + ); } }