diff --git a/src/lib.rs b/src/lib.rs index 43027b7e..d32b0f72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,9 +105,9 @@ impl PageStack { } /// A multi-producer, single-consumer message channel. -pub struct Channel { - pub tx: UnboundedSender, - pub rx: UnboundedReceiver, +pub struct Channel { + pub tx: UnboundedSender, + pub rx: UnboundedReceiver, } impl Default for Channel { @@ -120,16 +120,16 @@ impl Default for Channel { /// Initialize a `Store` with the `State` given and a `Frontend` with the `Widget` given, /// and run their main loops concurrently. Connect them to the `Channel` and also to /// an interrupt broadcast channel also initialized in this function. -pub async fn run(channel: Channel, state: S, root: W) -> Result> +pub async fn run(channel: Channel, state: S, root: W) -> Result> where - S: State + Clone + Debug + Send + Sync + 'static, - W: Widget, + S: State + Clone + Debug + Send + Sync + 'static, + W: Widget, P: Clone + Debug + Send + Sync + 'static, { let (terminator, mut interrupt_rx) = task::create_termination(); - let (store, state_rx) = store::Store::::new(); - let frontend = Frontend::::new(channel.tx.clone()); + let (store, state_rx) = store::Store::::new(); + let frontend = Frontend::::new(channel.tx.clone()); tokio::try_join!( store.main_loop(state, terminator, channel.rx, interrupt_rx.resubscribe()), diff --git a/src/store.rs b/src/store.rs index 0b80b697..36d56540 100644 --- a/src/store.rs +++ b/src/store.rs @@ -17,11 +17,11 @@ pub trait State

where P: Clone + Debug + Send + Sync, { - type Action; + type Message; /// Handle a user-defined application message and return an `Exit` object /// in case the received message requested the application to also quit. - fn handle_action(&mut self, action: Self::Action) -> Option>; + fn update(&mut self, message: Self::Message) -> Option>; /// Handle recurring tick. fn tick(&self); @@ -29,16 +29,16 @@ where /// The `Store` updates the applications' state concurrently. It handles /// messages coming from the frontend and updates the state accordingly. -pub struct Store +pub struct Store where S: State

+ Clone + Send + Sync, P: Clone + Debug + Send + Sync, { state_tx: UnboundedSender, - _phantom: PhantomData<(A, P)>, + _phantom: PhantomData<(M, P)>, } -impl Store +impl Store where S: State

+ Clone + Send + Sync, P: Clone + Debug + Send + Sync, @@ -56,9 +56,9 @@ where } } -impl Store +impl Store where - S: State + Clone + Debug + Send + Sync + 'static, + S: State + Clone + Debug + Send + Sync + 'static, P: Clone + Debug + Send + Sync + 'static, { /// By calling `main_loop`, the store will wait for new messages coming @@ -69,7 +69,7 @@ where self, mut state: S, mut terminator: Terminator

, - mut action_rx: UnboundedReceiver, + mut message_rx: UnboundedReceiver, mut interrupt_rx: broadcast::Receiver>, ) -> anyhow::Result> { // Send the initial state once @@ -79,10 +79,10 @@ where let result = loop { tokio::select! { - // Handle the actions coming from the frontend + // Handle the messages coming from the frontend // and process them to do async operations - Some(action) = action_rx.recv() => { - if let Some(exit) = state.handle_action(action) { + Some(message) = message_rx.recv() => { + if let Some(exit) = state.update(message) { let interrupted = Interrupted::User { payload: exit.value }; let _ = terminator.terminate(interrupted.clone()); diff --git a/src/ui.rs b/src/ui.rs index e7940daf..843eae30 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -29,16 +29,14 @@ const INLINE_HEIGHT: usize = 20; /// /// Once created and run with `main_loop`, the `Frontend` will wait for new messages /// being sent on either the terminal event, the state or the interrupt message channel. -pub struct Frontend { - action_tx: mpsc::UnboundedSender, +pub struct Frontend { + tx: mpsc::UnboundedSender, } -impl Frontend { +impl Frontend { /// Create a new `Frontend` storing the sending end of a message channel. - pub fn new(action_tx: mpsc::UnboundedSender) -> Self { - Self { - action_tx: action_tx.clone(), - } + pub fn new(tx: mpsc::UnboundedSender) -> Self { + Self { tx: tx.clone() } } /// By calling `main_loop`, the `Frontend` will wait for new messages being sent @@ -63,7 +61,7 @@ impl Frontend { ) -> anyhow::Result> where S: State

, - W: Widget, + W: Widget, P: Clone + Send + Sync + Debug, { let mut ticker = tokio::time::interval(RENDERING_TICK_RATE); @@ -79,7 +77,7 @@ impl Frontend { root.update(&state); root } - None => W::new(&state, self.action_tx.clone()), + None => W::new(&state, self.tx.clone()), } }; @@ -110,12 +108,4 @@ impl Frontend { result } - - // pub fn action_tx(&self) -> UnboundedSender { - // self.action_tx.clone() - // } - - // pub fn action_rx(&self) -> UnboundedReceiver { - // self.action_rx.clone() - // } } diff --git a/src/ui/widget.rs b/src/ui/widget.rs index 77f00d34..b33f4417 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -14,35 +14,35 @@ use termion::event::Key; use ratatui::prelude::*; use ratatui::widgets::Cell; -pub type BoxedWidget = Box>; +pub type BoxedWidget = Box>; pub type UpdateCallback = fn(&S) -> Box; pub type EventCallback = fn(&mut dyn Any); /// A `WidgetBase` provides common functionality to a `Widget`. It's used to store /// event and update callbacks as well sending messages to the UI's message channel. -pub struct WidgetBase { +pub struct WidgetBase { /// Message sender - pub action_tx: UnboundedSender, + pub tx: UnboundedSender, /// Custom update handler pub on_update: Option>, /// Additional custom event handler pub on_event: Option, } -impl WidgetBase { +impl WidgetBase { /// Create a new `WidgetBase` with no callbacks set. - pub fn new(action_tx: UnboundedSender) -> Self { + pub fn new(tx: UnboundedSender) -> Self { Self { - action_tx: action_tx.clone(), + tx: tx.clone(), on_update: None, on_event: None, } } /// Send a message to the internal channel. - pub fn send(&self, action: A) -> Result<(), SendError> { - self.action_tx.send(action) + pub fn send(&self, message: M) -> Result<(), SendError> { + self.tx.send(message) } } @@ -87,11 +87,11 @@ impl From for RenderProps { /// This is the trait that you should implement to define a custom `Widget`. pub trait Widget { type State; - type Action; + type Message; /// Should return a new view with props build from state (if type is known) and a /// message sender set. - fn new(state: &Self::State, action_tx: UnboundedSender) -> Self + fn new(state: &Self::State, tx: UnboundedSender) -> Self where Self: Sized; @@ -119,14 +119,14 @@ pub trait Widget { fn render(&self, frame: &mut Frame, props: RenderProps); /// Return a reference to this widgets' base. - fn base(&self) -> &WidgetBase; + fn base(&self) -> &WidgetBase; /// Return a mutable reference to this widgets' base. - fn base_mut(&mut self) -> &mut WidgetBase; + fn base_mut(&mut self) -> &mut WidgetBase; /// Send a message to the widgets' base channel. - fn send(&self, action: Self::Action) -> Result<(), SendError> { - self.base().send(action) + fn send(&self, message: Self::Message) -> Result<(), SendError> { + self.base().send(message) } /// Should set the optional custom event handler. diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index fb24c838..4eb11fb0 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -67,11 +67,11 @@ impl<'a> Default for HeaderProps<'a> { impl<'a: 'static> Properties for HeaderProps<'a> {} impl<'a: 'static> BoxedAny for HeaderProps<'a> {} -pub struct Header<'a: 'static, S, A> { +pub struct Header<'a: 'static, S, M> { /// Internal props props: HeaderProps<'a>, /// Internal base - base: WidgetBase, + base: WidgetBase, } impl<'a, S, A> Header<'a, S, A> { @@ -87,13 +87,13 @@ impl<'a, S, A> Header<'a, S, A> { } } -impl<'a: 'static, S, A> Widget for Header<'a, S, A> { - type Action = A; +impl<'a: 'static, S, M> Widget for Header<'a, S, M> { + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: HeaderProps::default(), } } @@ -166,11 +166,11 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { frame.render_widget(header, header_layout[0]); } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } @@ -208,14 +208,14 @@ impl<'a> Default for FooterProps<'a> { impl<'a: 'static> Properties for FooterProps<'a> {} impl<'a: 'static> BoxedAny for FooterProps<'a> {} -pub struct Footer<'a, S, A> { +pub struct Footer<'a, S, M> { /// Internal props props: FooterProps<'a>, /// Internal base - base: WidgetBase, + base: WidgetBase, } -impl<'a, S, A> Footer<'a, S, A> { +impl<'a, S, M> Footer<'a, S, M> { pub fn columns(mut self, columns: Vec>) -> Self { self.props.columns = columns; self @@ -250,13 +250,13 @@ impl<'a, S, A> Footer<'a, S, A> { } } -impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { - type Action = A; +impl<'a: 'static, S, M> Widget for Footer<'a, S, M> { + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: FooterProps::default(), } } @@ -305,11 +305,11 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } @@ -329,46 +329,46 @@ impl ContainerProps { impl Properties for ContainerProps {} impl BoxedAny for ContainerProps {} -pub struct Container { +pub struct Container { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal props props: ContainerProps, /// Container header - header: Option>, + header: Option>, /// Content widget - content: Option>, + content: Option>, /// Container footer - footer: Option>, + footer: Option>, } -impl Container { - pub fn header(mut self, header: BoxedWidget) -> Self { +impl Container { + pub fn header(mut self, header: BoxedWidget) -> Self { self.header = Some(header); self } - pub fn content(mut self, content: BoxedWidget) -> Self { + pub fn content(mut self, content: BoxedWidget) -> Self { self.content = Some(content); self } - pub fn footer(mut self, footer: BoxedWidget) -> Self { + pub fn footer(mut self, footer: BoxedWidget) -> Self { self.footer = Some(footer); self } } -impl Widget for Container { - type Action = A; +impl Widget for Container { + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self + fn new(_state: &S, tx: UnboundedSender) -> Self where Self: Sized, { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: ContainerProps::default(), header: None, content: None, @@ -446,11 +446,11 @@ impl Widget for Container { } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } @@ -479,19 +479,19 @@ impl SectionGroupProps { impl Properties for SectionGroupProps {} impl BoxedAny for SectionGroupProps {} -pub struct SectionGroup { +pub struct SectionGroup { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal table properties props: SectionGroupProps, /// All sections - sections: Vec>, + sections: Vec>, /// Internal selection and offset state state: SectionGroupState, } -impl SectionGroup { - pub fn section(mut self, section: BoxedWidget) -> Self { +impl SectionGroup { + pub fn section(mut self, section: BoxedWidget) -> Self { self.sections.push(section); self } @@ -515,13 +515,17 @@ impl SectionGroup { } } -impl Widget for SectionGroup { +impl Widget for SectionGroup +where + S: 'static, + M: 'static, +{ type State = S; - type Action = A; + type Message = M; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: SectionGroupProps::default(), sections: vec![], state: SectionGroupState { focus: Some(0) }, @@ -579,11 +583,11 @@ impl Widget for SectionGroup { } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index a9aa880a..d3352be1 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -56,16 +56,16 @@ struct TextFieldState { impl BoxedAny for TextFieldState {} -pub struct TextField { +pub struct TextField { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal props props: TextFieldProps, /// Internal state state: TextFieldState, } -impl TextField { +impl TextField { pub fn text(&self) -> Option<&String> { self.state.text.as_ref() } @@ -131,13 +131,17 @@ impl TextField { } } -impl Widget for TextField { - type Action = A; +impl Widget for TextField +where + S: 'static, + M: 'static, +{ + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: TextFieldProps::default(), state: TextFieldState { text: None, @@ -234,13 +238,13 @@ impl Widget for TextField { } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } -impl BoxedAny for TextField {} +impl BoxedAny for TextField {} diff --git a/src/ui/widget/list.rs b/src/ui/widget/list.rs index cc335e4e..6fb1c108 100644 --- a/src/ui/widget/list.rs +++ b/src/ui/widget/list.rs @@ -88,19 +88,19 @@ impl<'a: 'static, R, const W: usize> BoxedAny for TableProps<'a, R, W> where R: impl BoxedAny for TableState {} -pub struct Table<'a, S, A, R, const W: usize> +pub struct Table<'a, S, M, R, const W: usize> where R: ToRow, { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal table properties props: TableProps<'a, R, W>, /// Internal selection and offset state state: TableState, } -impl<'a, S, A, R, const W: usize> Table<'a, S, A, R, W> +impl<'a, S, M, R, const W: usize> Table<'a, S, M, R, W> where R: ToRow, { @@ -159,16 +159,16 @@ where } } -impl<'a: 'static, S: 'static, A: 'static, R, const W: usize> Widget for Table<'a, S, A, R, W> +impl<'a: 'static, S: 'a, M: 'a, R, const W: usize> Widget for Table<'a, S, M, R, W> where R: ToRow + Clone + 'static, { - type Action = A; + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: TableProps::default(), state: TableState::default().with_selected(Some(0)), } @@ -273,11 +273,11 @@ where } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index 1db0b97f..488d026d 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -53,16 +53,16 @@ struct ParagraphState { impl BoxedAny for ParagraphState {} -pub struct Paragraph<'a, S, A> { +pub struct Paragraph<'a, S, M> { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal props props: ParagraphProps<'a>, /// Internal state state: ParagraphState, } -impl<'a, S, A> Paragraph<'a, S, A> { +impl<'a, S, M> Paragraph<'a, S, M> { pub fn scroll(&self) -> (u16, u16) { (self.state.offset as u16, 0) } @@ -136,16 +136,21 @@ impl<'a, S, A> Paragraph<'a, S, A> { } } -impl<'a: 'static, S: 'static, A: 'static> Widget for Paragraph<'a, S, A> { - type Action = A; +impl<'a, S, M> Widget for Paragraph<'a, S, M> +where + 'a: 'static, + S: 'static, + M: 'static, +{ + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self + fn new(_state: &S, tx: UnboundedSender) -> Self where Self: Sized, { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: ParagraphProps::default(), state: ParagraphState { offset: 0, @@ -200,11 +205,11 @@ impl<'a: 'static, S: 'static, A: 'static> Widget for Paragraph<'a, S, A> { frame.render_widget(content, content_area); } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } diff --git a/src/ui/widget/window.rs b/src/ui/widget/window.rs index 5472d670..52f466d3 100644 --- a/src/ui/widget/window.rs +++ b/src/ui/widget/window.rs @@ -35,38 +35,39 @@ impl Default for WindowProps { impl Properties for WindowProps {} impl BoxedAny for WindowProps {} -pub struct Window { +pub struct Window { /// Internal base - base: WidgetBase, + base: WidgetBase, /// Internal properties props: WindowProps, /// All pages known - pages: HashMap>, + pages: HashMap>, } -impl Window +impl Window where Id: Clone + Hash + Eq + PartialEq, { - pub fn page(mut self, id: Id, page: BoxedWidget) -> Self { + pub fn page(mut self, id: Id, page: BoxedWidget) -> Self { self.pages.insert(id, page); self } } -impl<'a: 'static, S, A, Id> Widget for Window +impl<'a, S, M, Id> Widget for Window where - Id: Clone + Hash + Eq + PartialEq + 'a, + 'a: 'static, + Id: Clone + Hash + Eq + PartialEq + 'static, { - type Action = A; + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self + fn new(_state: &S, tx: UnboundedSender) -> Self where Self: Sized, { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: WindowProps::default(), pages: HashMap::new(), } @@ -113,11 +114,11 @@ where } } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } } @@ -155,14 +156,14 @@ impl Default for ShortcutsProps { impl Properties for ShortcutsProps {} impl BoxedAny for ShortcutsProps {} -pub struct Shortcuts { +pub struct Shortcuts { /// Internal properties props: ShortcutsProps, /// Internal base - base: WidgetBase, + base: WidgetBase, } -impl Shortcuts { +impl Shortcuts { pub fn divider(mut self, divider: char) -> Self { self.props.divider = divider; self @@ -179,13 +180,13 @@ impl Shortcuts { } } -impl Widget for Shortcuts { - type Action = A; +impl Widget for Shortcuts { + type Message = M; type State = S; - fn new(_state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, tx: UnboundedSender) -> Self { Self { - base: WidgetBase::new(action_tx.clone()), + base: WidgetBase::new(tx.clone()), props: ShortcutsProps::default(), } } @@ -235,11 +236,11 @@ impl Widget for Shortcuts { frame.render_widget(table, props.area); } - fn base(&self) -> &WidgetBase { + fn base(&self) -> &WidgetBase { &self.base } - fn base_mut(&mut self) -> &mut WidgetBase { + fn base_mut(&mut self) -> &mut WidgetBase { &mut self.base } }