From 5c8ecd5f0a1983e01a42f2f608ebf9c5a7a11a7b Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 23 Apr 2024 17:43:51 +0200 Subject: [PATCH] lib: Add default impl for callback builders --- src/ui/widget.rs | 47 +++++++++++--------------------------- src/ui/widget/container.rs | 34 +-------------------------- src/ui/widget/input.rs | 12 +--------- src/ui/widget/text.rs | 12 +--------- 4 files changed, 16 insertions(+), 89 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index db176644..7cb26ce9 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -43,14 +43,23 @@ pub trait View { Self: Sized; /// Should set the optional custom event handler. - fn on_event(self, callback: EventCallback) -> Self + fn on_event(mut self, callback: EventCallback) -> Self where - Self: Sized; + Self: Sized, + { + self.base_mut().on_event = Some(callback); + self + } /// Should set the optional update handler. - fn on_update(self, callback: UpdateCallback) -> Self + fn on_update(mut self, callback: UpdateCallback) -> Self where - Self: Sized; + Self: Sized, + + { + self.base_mut().on_update = Some(callback); + self + } /// Returns a boxed `View` fn to_boxed(self) -> Box @@ -193,16 +202,6 @@ where &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = WindowProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); @@ -327,16 +326,6 @@ impl View for Shortcuts { &mut self.base } - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - fn handle_key_event(&mut self, _key: Key) {} fn update(&mut self, state: &S) { @@ -571,16 +560,6 @@ where &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = TableProps::<'_, R>::from_callback(self.base.on_update, state) .unwrap_or(self.props.clone()); diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 3446b31e..a11932f7 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -11,9 +11,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row}; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::style; -use super::{ - BaseView, BoxedWidget, Column, EventCallback, Properties, UpdateCallback, View, Widget, -}; +use super::{BaseView, BoxedWidget, Column, Properties, View, Widget}; #[derive(Clone, Debug)] pub struct HeaderProps<'a> { @@ -95,16 +93,6 @@ impl<'a: 'static, S, A> View for Header<'a, S, A> { &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = self .base @@ -262,16 +250,6 @@ impl<'a: 'static, S, A> View for Footer<'a, S, A> { &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = self .base @@ -434,16 +412,6 @@ where &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = ContainerProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index d2cbbcbe..322ec64d 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -9,7 +9,7 @@ use ratatui::prelude::{Backend, Rect}; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget}; +use super::{BaseView, Properties, View, Widget}; #[derive(Clone)] pub struct TextFieldProps { @@ -146,16 +146,6 @@ impl View for TextField { &mut self.base } - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - fn update(&mut self, state: &S) { if let Some(on_update) = self.base.on_update { if let Some(props) = (on_update)(state).downcast_ref::() { diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index d90ef5e6..5fe0dcef 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -8,7 +8,7 @@ use ratatui::backend::Backend; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::text::Text; -use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget}; +use super::{BaseView, Properties, View, Widget}; #[derive(Clone)] pub struct ParagraphProps<'a> { @@ -165,16 +165,6 @@ impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { &mut self.base } - fn on_event(mut self, callback: EventCallback) -> Self { - self.base.on_event = Some(callback); - self - } - - fn on_update(mut self, callback: UpdateCallback) -> Self { - self.base.on_update = Some(callback); - self - } - fn update(&mut self, state: &S) { self.props = ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());