Msg instead of callbacks

This commit is contained in:
veeso
2021-03-03 15:47:25 +01:00
parent 3ccbb325b3
commit b57763e688
2 changed files with 34 additions and 41 deletions

View File

@@ -24,26 +24,44 @@
*/ */
// Modules // Modules
pub mod components;
pub mod props; pub mod props;
// locals // locals
use crate::ui::activities::Activity;
use props::{Props, PropsBuilder}; use props::{Props, PropsBuilder};
// ext // ext
use crossterm::event::Event as InputEvent; use crossterm::event::Event as InputEvent;
use tui::widgets::Widget; 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 /// ## States
/// ///
/// States is a trait which defines the behaviours for the states model for the different component. /// States is a trait which defines the behaviours for the states model for the different component.
/// A state contains internal values for each component. /// A state contains internal values for each component.
pub(crate) trait States { pub(crate) trait States {}
/// ### update
///
/// Create a new state from current one and new
fn update(&self, new: dyn States) -> dyn States;
}
// -- Component // -- Component
@@ -62,8 +80,9 @@ pub trait Component {
/// ///
/// Update component properties /// Update component properties
/// Properties should first be retrieved through `get_props` which creates a builder from /// Properties should first be retrieved through `get_props` which creates a builder from
/// existing properties and then edited before calling update /// existing properties and then edited before calling update.
fn update(&mut self, props: Option<Props>); /// Returns a Msg to the view
fn update(&mut self, props: Props) -> Msg;
/// ### get_props /// ### get_props
/// ///
@@ -74,8 +93,9 @@ pub trait Component {
/// ### on /// ### on
/// ///
/// Handle input event and update internal states /// Handle input event and update internal states.
fn on(&mut self, ev: InputEvent); /// Returns a Msg to the view
fn on(&mut self, ev: InputEvent) -> Msg;
// -- events // -- events

View File

@@ -23,14 +23,9 @@
* *
*/ */
// locals
use super::super::activities::Activity;
// ext // ext
use tui::style::Color; use tui::style::Color;
// Callback types
pub type OnSubmitCb = fn(&mut dyn Activity, Option<String>); // Activity, Value
// -- Props // -- Props
/// ## Props /// ## Props
@@ -46,8 +41,6 @@ pub struct Props {
pub italic: bool, // Italic pub italic: bool, // Italic
pub underlined: bool, // Underlined pub underlined: bool, // Underlined
pub texts: TextParts, // text parts pub texts: TextParts, // text parts
// Callbacks
pub on_submit: Option<OnSubmitCb>,
} }
impl Default for Props { impl Default for Props {
@@ -61,8 +54,6 @@ impl Default for Props {
italic: false, italic: false,
underlined: false, underlined: false,
texts: TextParts::default(), texts: TextParts::default(),
// Callbacks
on_submit: None,
} }
} }
} }
@@ -80,8 +71,10 @@ impl PropsBuilder {
/// ### from_props /// ### from_props
/// ///
/// Create a props builder from existing properties /// Create a props builder from existing properties
pub fn from_props(props: Props) -> Self { pub fn from_props(props: &Props) -> Self {
PropsBuilder { props: Some(props) } PropsBuilder {
props: Some(props.clone()),
}
} }
/// ### build /// ### build
@@ -160,16 +153,6 @@ impl PropsBuilder {
} }
self 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 { impl Default for PropsBuilder {
@@ -223,7 +206,6 @@ mod tests {
assert_eq!(props.bold, false); assert_eq!(props.bold, false);
assert_eq!(props.italic, false); assert_eq!(props.italic, false);
assert_eq!(props.underlined, false); assert_eq!(props.underlined, false);
assert!(props.on_submit.is_none());
assert!(props.texts.title.is_none()); assert!(props.texts.title.is_none());
assert!(props.texts.body.is_none()); assert!(props.texts.body.is_none());
} }
@@ -237,7 +219,6 @@ mod tests {
.bold() .bold()
.italic() .italic()
.underlined() .underlined()
.on_submit(on_submit_cb)
.with_texts(TextParts::new( .with_texts(TextParts::new(
Some(String::from("hello")), Some(String::from("hello")),
Some(vec![String::from("hey")]), Some(vec![String::from("hey")]),
@@ -269,7 +250,6 @@ mod tests {
.bold() .bold()
.italic() .italic()
.underlined() .underlined()
.on_submit(on_submit_cb)
.with_texts(TextParts::new( .with_texts(TextParts::new(
Some(String::from("hello")), Some(String::from("hello")),
Some(vec![String::from("hey")]), Some(vec![String::from("hey")]),
@@ -287,7 +267,6 @@ mod tests {
.bold() .bold()
.italic() .italic()
.underlined() .underlined()
.on_submit(on_submit_cb)
.with_texts(TextParts::new( .with_texts(TextParts::new(
Some(String::from("hello")), Some(String::from("hello")),
Some(vec![String::from("hey")]), Some(vec![String::from("hey")]),
@@ -321,10 +300,4 @@ mod tests {
assert!(parts.title.is_none()); assert!(parts.title.is_none());
assert!(parts.body.is_none()); assert!(parts.body.is_none());
} }
// Callbacks
fn on_submit_cb(_activity: &mut dyn Activity, val: Option<String>) {
println!("Val: {:?}", val);
}
} }