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
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<Props>);
/// 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

View File

@@ -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<String>); // 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<OnSubmitCb>,
}
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<String>) {
println!("Val: {:?}", val);
}
}