From 2c88e8f9a3a608a3a9dd24263a3cb83514ca5424 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 14 May 2024 23:40:28 +0200 Subject: [PATCH] lib: Improve event callback --- src/ui/widget.rs | 20 +++++++++++++++++--- src/ui/widget/container.rs | 7 ++----- src/ui/widget/input.rs | 15 +++++++++------ src/ui/widget/list.rs | 11 ++++++----- src/ui/widget/text.rs | 9 +++------ 5 files changed, 37 insertions(+), 25 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index 116b7ccb..798810c3 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -6,6 +6,7 @@ pub mod window; use std::any::Any; +use tokio::sync::mpsc::error::SendError; use tokio::sync::mpsc::UnboundedSender; use termion::event::Key; @@ -16,7 +17,7 @@ use ratatui::widgets::Cell; pub type BoxedWidget = Box>; pub type UpdateCallback = fn(&S) -> Box; -pub type EventCallback = fn(Box, UnboundedSender); +pub type EventCallback = fn(&mut dyn Any); /// A `View`s common fields. pub struct BaseView { @@ -25,7 +26,13 @@ pub struct BaseView { /// Custom update handler pub on_update: Option>, /// Additional custom event handler - pub on_event: Option>, + pub on_event: Option, +} + +impl BaseView { + pub fn send(&self, action: A) -> Result<(), SendError> { + self.action_tx.send(action) + } } /// General properties that specify how a `Widget` is rendered. @@ -104,7 +111,7 @@ pub trait Widget { fn base_mut(&mut self) -> &mut BaseView; /// Should set the optional custom event handler. - fn on_event(mut self, callback: EventCallback) -> Self + fn on_event(mut self, callback: EventCallback) -> Self where Self: Sized, { @@ -128,6 +135,13 @@ pub trait Widget { { Box::new(self) } + + fn downcast_ref(any: &dyn Any) -> Option<&Self> + where + Self: Sized + 'static, + { + any.downcast_ref::() + } } /// Needs to be implemented for items that are supposed to be rendered in tables. diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index ffc0d4b7..e8aaac30 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -516,7 +516,7 @@ impl SectionGroup { } } -impl Widget for SectionGroup { +impl Widget for SectionGroup { type State = S; type Action = A; @@ -555,10 +555,7 @@ impl Widget for SectionGroup { } if let Some(on_event) = self.base.on_event { - (on_event)( - self.state.clone().to_boxed_any(), - self.base.action_tx.clone(), - ); + (on_event)(self); } } diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 0cfe7eaa..3f2f8f1d 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -49,7 +49,7 @@ impl Default for TextFieldProps { impl Properties for TextFieldProps {} #[derive(Clone)] -pub struct TextFieldState { +struct TextFieldState { pub text: Option, pub cursor_position: usize, } @@ -66,6 +66,10 @@ pub struct TextField { } impl TextField { + pub fn text(&self) -> Option<&String> { + self.state.text.as_ref() + } + fn move_cursor_left(&mut self) { let cursor_moved_left = self.state.cursor_position.saturating_sub(1); self.state.cursor_position = self.clamp_cursor(cursor_moved_left); @@ -127,7 +131,7 @@ impl TextField { } } -impl Widget for TextField { +impl Widget for TextField { type Action = A; type State = S; @@ -168,10 +172,7 @@ impl Widget for TextField { } if let Some(on_event) = self.base.on_event { - (on_event)( - self.state.clone().to_boxed_any(), - self.base.action_tx.clone(), - ); + (on_event)(self); } } @@ -241,3 +242,5 @@ impl Widget for TextField { &mut self.base } } + +impl BoxedAny for TextField {} diff --git a/src/ui/widget/list.rs b/src/ui/widget/list.rs index e763e866..ab3c3c19 100644 --- a/src/ui/widget/list.rs +++ b/src/ui/widget/list.rs @@ -104,6 +104,10 @@ impl<'a, S, A, R, const W: usize> Table<'a, S, A, R, W> where R: ToRow, { + pub fn selected(&self) -> Option { + self.state.selected() + } + fn prev(&mut self) -> Option { let selected = self .state @@ -155,7 +159,7 @@ where } } -impl<'a: 'static, S, A, R, const W: usize> Widget for Table<'a, S, A, R, W> +impl<'a: 'static, S: 'static, A: 'static, R, const W: usize> Widget for Table<'a, S, A, R, W> where R: ToRow + Clone + 'static, { @@ -200,10 +204,7 @@ where self.props.selected = self.state.selected(); if let Some(on_event) = self.base.on_event { - (on_event)( - self.state.clone().to_boxed_any(), - self.base.action_tx.clone(), - ); + (on_event)(self); } } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index 02a54e35..011a5e5a 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -44,7 +44,7 @@ impl<'a: 'static> Properties for ParagraphProps<'a> {} impl<'a: 'static> BoxedAny for ParagraphProps<'a> {} #[derive(Clone)] -pub struct ParagraphState { +struct ParagraphState { /// Internal offset pub offset: usize, /// Internal progress @@ -136,7 +136,7 @@ impl<'a, S, A> Paragraph<'a, S, A> { } } -impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { +impl<'a: 'static, S: 'static, A: 'static> Widget for Paragraph<'a, S, A> { type Action = A; type State = S; @@ -185,10 +185,7 @@ impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { } if let Some(on_event) = self.base.on_event { - (on_event)( - self.state.clone().to_boxed_any(), - self.base.action_tx.clone(), - ); + (on_event)(self); } }