From b57763e68845f12832562d26bb7247af2f985541 Mon Sep 17 00:00:00 2001 From: veeso Date: Wed, 3 Mar 2021 15:47:25 +0100 Subject: [PATCH] Msg instead of callbacks --- src/ui/layout/mod.rs | 40 ++++++++++++++++++++++++++++++---------- src/ui/layout/props.rs | 35 ++++------------------------------- 2 files changed, 34 insertions(+), 41 deletions(-) diff --git a/src/ui/layout/mod.rs b/src/ui/layout/mod.rs index 422e1bd..62ca049 100644 --- a/src/ui/layout/mod.rs +++ b/src/ui/layout/mod.rs @@ -24,26 +24,44 @@ */ // Modules +pub mod components; pub mod props; // locals +use crate::ui::activities::Activity; use props::{Props, PropsBuilder}; // ext use crossterm::event::Event as InputEvent; use tui::widgets::Widget; +// -- Msg + +/// ## Msg +/// +/// Msg is an enum returned by an `Update` or an `On`. +/// Yep, I took inspiration from Elm. +pub enum Msg { + OnSubmit(Payload), + None, +} + +/// ## Payload +/// +/// Payload describes the payload for a `Msg` +pub enum Payload { + Text(String), + Number(isize), + Unumber(usize), + None +} + // -- States /// ## States /// /// States is a trait which defines the behaviours for the states model for the different component. /// A state contains internal values for each component. -pub(crate) trait States { - /// ### update - /// - /// Create a new state from current one and new - fn update(&self, new: dyn States) -> dyn States; -} +pub(crate) trait States {} // -- Component @@ -62,8 +80,9 @@ pub trait Component { /// /// Update component properties /// Properties should first be retrieved through `get_props` which creates a builder from - /// existing properties and then edited before calling update - fn update(&mut self, props: Option); + /// existing properties and then edited before calling update. + /// Returns a Msg to the view + fn update(&mut self, props: Props) -> Msg; /// ### get_props /// @@ -74,8 +93,9 @@ pub trait Component { /// ### on /// - /// Handle input event and update internal states - fn on(&mut self, ev: InputEvent); + /// Handle input event and update internal states. + /// Returns a Msg to the view + fn on(&mut self, ev: InputEvent) -> Msg; // -- events diff --git a/src/ui/layout/props.rs b/src/ui/layout/props.rs index c4dd5fc..e7d66ff 100644 --- a/src/ui/layout/props.rs +++ b/src/ui/layout/props.rs @@ -23,14 +23,9 @@ * */ -// locals -use super::super::activities::Activity; // ext use tui::style::Color; -// Callback types -pub type OnSubmitCb = fn(&mut dyn Activity, Option); // Activity, Value - // -- Props /// ## Props @@ -46,8 +41,6 @@ pub struct Props { pub italic: bool, // Italic pub underlined: bool, // Underlined pub texts: TextParts, // text parts - // Callbacks - pub on_submit: Option, } impl Default for Props { @@ -61,8 +54,6 @@ impl Default for Props { italic: false, underlined: false, texts: TextParts::default(), - // Callbacks - on_submit: None, } } } @@ -80,8 +71,10 @@ impl PropsBuilder { /// ### from_props /// /// Create a props builder from existing properties - pub fn from_props(props: Props) -> Self { - PropsBuilder { props: Some(props) } + pub fn from_props(props: &Props) -> Self { + PropsBuilder { + props: Some(props.clone()), + } } /// ### build @@ -160,16 +153,6 @@ impl PropsBuilder { } self } - - /// ### on_submit - /// - /// Set on_submit callback for component - pub fn on_submit(&mut self, on_submit: OnSubmitCb) -> &mut Self { - if let Some(props) = self.props.as_mut() { - props.on_submit = Some(on_submit); - } - self - } } impl Default for PropsBuilder { @@ -223,7 +206,6 @@ mod tests { assert_eq!(props.bold, false); assert_eq!(props.italic, false); assert_eq!(props.underlined, false); - assert!(props.on_submit.is_none()); assert!(props.texts.title.is_none()); assert!(props.texts.body.is_none()); } @@ -237,7 +219,6 @@ mod tests { .bold() .italic() .underlined() - .on_submit(on_submit_cb) .with_texts(TextParts::new( Some(String::from("hello")), Some(vec![String::from("hey")]), @@ -269,7 +250,6 @@ mod tests { .bold() .italic() .underlined() - .on_submit(on_submit_cb) .with_texts(TextParts::new( Some(String::from("hello")), Some(vec![String::from("hey")]), @@ -287,7 +267,6 @@ mod tests { .bold() .italic() .underlined() - .on_submit(on_submit_cb) .with_texts(TextParts::new( Some(String::from("hello")), Some(vec![String::from("hey")]), @@ -321,10 +300,4 @@ mod tests { assert!(parts.title.is_none()); assert!(parts.body.is_none()); } - - // Callbacks - - fn on_submit_cb(_activity: &mut dyn Activity, val: Option) { - println!("Val: {:?}", val); - } }