diff --git a/bin/commands/inbox/select.rs b/bin/commands/inbox/select.rs index 85943776..d1827ee2 100644 --- a/bin/commands/inbox/select.rs +++ b/bin/commands/inbox/select.rs @@ -18,13 +18,14 @@ use tui::cob::inbox::{self}; use tui::store; use tui::store::StateValue; use tui::task::{self, Interrupted}; -use tui::ui::items::NotificationItem; -use tui::ui::items::NotificationItemFilter; +use tui::terminal; +use tui::ui::items::{Filter, NotificationItem, NotificationItemFilter}; use tui::ui::Frontend; use tui::Exit; use ui::ListPage; +use super::common::SelectionMode; use super::common::{Mode, RepositoryMode}; type Selection = tui::Selection; @@ -58,9 +59,15 @@ impl Default for UIState { } } +#[derive(Clone, Debug)] +pub struct NotificationsState { + items: Vec, + selected: Option, +} + #[derive(Clone, Debug)] pub struct State { - notifications: Vec, + notifications: NotificationsState, mode: Mode, project: Project, filter: NotificationItemFilter, @@ -68,6 +75,35 @@ pub struct State { ui: UIState, } +impl State { + pub fn shortcuts(&self) -> Vec<(&str, &str)> { + if self.ui.show_search { + vec![("esc", "cancel"), ("enter", "apply")] + } else if self.ui.show_help { + vec![("?", "close")] + } else { + match self.mode.selection() { + SelectionMode::Id => vec![("enter", "select"), ("/", "search")], + SelectionMode::Operation => vec![ + ("enter", "show"), + ("c", "clear"), + ("/", "search"), + ("?", "help"), + ], + } + } + } + + pub fn notifications(&self) -> Vec { + self.notifications + .items + .iter() + .filter(|patch| self.filter.matches(patch)) + .cloned() + .collect() + } +} + impl TryFrom<&Context> for State { type Error = anyhow::Error; @@ -159,7 +195,10 @@ impl TryFrom<&Context> for State { } Ok(Self { - notifications, + notifications: NotificationsState { + items: notifications, + selected: None, + }, mode: mode.clone(), project, filter, @@ -171,7 +210,7 @@ impl TryFrom<&Context> for State { pub enum Action { Exit { selection: Option }, - Update, + Select { selected: Option }, PageSize(usize), OpenSearch, UpdateSearch { value: String }, @@ -187,6 +226,10 @@ impl store::State for State { fn handle_action(&mut self, action: Action) -> Option> { match action { Action::Exit { selection } => Some(Exit { value: selection }), + Action::Select { selected } => { + self.notifications.selected = selected; + None + } Action::PageSize(size) => { self.ui.page_size = size; None @@ -223,7 +266,6 @@ impl store::State for State { self.ui.show_help = false; None } - Action::Update => None, } } } @@ -241,7 +283,10 @@ impl App { tokio::try_join!( store.main_loop(state, terminator, action_rx, interrupt_rx.resubscribe()), - frontend.main_loop::(state_rx, interrupt_rx.resubscribe()), + frontend.main_loop::, Selection>( + state_rx, + interrupt_rx.resubscribe() + ), )?; if let Ok(reason) = interrupt_rx.recv().await { diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index 5bcf7754..80e1e34d 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::collections::HashMap; use std::str::FromStr; @@ -12,18 +13,24 @@ use ratatui::text::{Line, Span, Text}; use radicle_tui as tui; -use tui::ui::items::{Filter, NotificationItem, NotificationItemFilter, NotificationState}; +use tui::ui::items::{NotificationItem, NotificationItemFilter, NotificationState}; use tui::ui::span; -use tui::ui::widget::container::{Footer, Header}; -use tui::ui::widget::input::TextField; -use tui::ui::widget::text::Paragraph; -use tui::ui::widget::{Column, Render, Shortcuts, Table, View}; +use tui::ui::widget; +use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; +use tui::ui::widget::input::{TextField, TextFieldProps}; +use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{ + Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, + Widget, +}; use tui::Selection; use crate::tui_inbox::common::{InboxOperation, Mode, RepositoryMode, SelectionMode}; use super::{Action, State}; +type BoxedWidget = widget::BoxedWidget; + pub struct ListPageProps { show_search: bool, show_help: bool, @@ -38,22 +45,26 @@ impl From<&State> for ListPageProps { } } -pub struct ListPage<'a> { - /// Action sender - pub action_tx: UnboundedSender, - /// State mapped props +pub struct ListPage { + /// Internal properties props: ListPageProps, - /// Notification widget - notifications: Notifications<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Patches widget + notifications: BoxedWidget, /// Search widget - search: Search, + search: BoxedWidget, /// Help widget - help: Help<'a>, + help: BoxedWidget, /// Shortcut widget - shortcuts: Shortcuts, + shortcuts: BoxedWidget, } -impl<'a> View for ListPage<'a> { +impl<'a: 'static, B: Backend + 'a> View for ListPage { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, @@ -61,51 +72,43 @@ impl<'a> View for ListPage<'a> { Self { action_tx: action_tx.clone(), props: ListPageProps::from(state), - notifications: Notifications::new(state, action_tx.clone()), - search: Search::new(state, action_tx.clone()), - help: Help::new(state, action_tx.clone()), - shortcuts: Shortcuts::new(&(), action_tx.clone()), + notifications: Notifications::new(state, action_tx.clone()).to_boxed(), + search: Search::new(state, action_tx.clone()).to_boxed(), + help: Help::new(state, action_tx.clone()).to_boxed(), + shortcuts: Shortcuts::new(state, action_tx.clone()) + .on_update(|state| { + Box::new(ShortcutsProps::default().shortcuts(&state.shortcuts())) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let shorts = if state.ui.show_search { - vec![("esc", "cancel"), ("enter", "apply")] - } else if state.ui.show_help { - vec![("?", "close")] - } else { - match state.mode.selection() { - SelectionMode::Id => vec![("enter", "select"), ("/", "search")], - SelectionMode::Operation => vec![ - ("enter", "show"), - ("c", "clear"), - ("/", "search"), - ("?", "help"), - ], - } - }; + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let shortcuts = self.shortcuts.move_with_state(&()); - let shortcuts = shortcuts.shortcuts(&shorts); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - ListPage { - notifications: self.notifications.move_with_state(state), - shortcuts, - help: self.help.move_with_state(state), - props: ListPageProps::from(state), - ..self - } + fn update(&mut self, state: &State) { + self.props = ListPageProps::from(state); + + self.notifications.update(state); + self.search.update(state); + self.help.update(state); + self.shortcuts.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { if self.props.show_search { - >::handle_key_event(&mut self.search, key) + self.search.handle_key_event(key); } else if self.props.show_help { - >::handle_key_event(&mut self.help, key) + self.help.handle_key_event(key); } else { match key { Key::Esc | Key::Ctrl('c') => { @@ -118,19 +121,15 @@ impl<'a> View for ListPage<'a> { let _ = self.action_tx.send(Action::OpenHelp); } _ => { - >::handle_key_event( - &mut self.notifications, - key, - ); + self.notifications.handle_key_event(key); } } } - let _ = self.action_tx.send(Action::Update); } } -impl<'a, B: Backend> Render for ListPage<'a> { - fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: ()) { +impl<'a: 'static, B: Backend + 'a> Widget for ListPage { + fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: &dyn Any) { let area = frame.size(); let layout = tui::ui::layout::default_page(area, 0u16, 1u16); @@ -138,30 +137,21 @@ impl<'a, B: Backend> Render for ListPage<'a> { let component_layout = Layout::vertical([Constraint::Min(1), Constraint::Length(2)]) .split(layout.component); - as Render>::render( - &self.notifications, - frame, - component_layout[0], - (), - ); - >::render(&self.search, frame, component_layout[1], ()); + self.notifications.render(frame, component_layout[0], &()); + self.search.render(frame, component_layout[1], &()); } else if self.props.show_help { - as Render>::render(&self.help, frame, layout.component, ()); + self.help.render(frame, layout.component, &()); } else { - as Render>::render( - &self.notifications, - frame, - layout.component, - (), - ); + self.notifications.render(frame, layout.component, &()); } - as Render>::render(&self.shortcuts, frame, layout.shortcuts, ()); + self.shortcuts.render(frame, layout.shortcuts, &()); } } struct NotificationsProps<'a> { notifications: Vec, + selected: Option, mode: Mode, stats: HashMap, columns: Vec>, @@ -178,12 +168,7 @@ impl<'a> From<&State> for NotificationsProps<'a> { let mut seen = 0; let mut unseen = 0; - let notifications: Vec = state - .notifications - .iter() - .filter(|issue| state.filter.matches(issue)) - .cloned() - .collect(); + let notifications = state.notifications(); // Compute statistics for notification in ¬ifications { @@ -198,6 +183,7 @@ impl<'a> From<&State> for NotificationsProps<'a> { Self { notifications, + selected: None, mode: state.mode.clone(), stats, columns: [ @@ -223,18 +209,25 @@ impl<'a> From<&State> for NotificationsProps<'a> { } } -struct Notifications<'a> { - /// Action sender - action_tx: UnboundedSender, - /// State mapped props +struct Notifications<'a, B: Backend> { + /// Internal properties props: NotificationsProps<'a>, - /// Notification table - table: Table<'a, Action, NotificationItem>, - /// Table footer - footer: Footer<'a, Action>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Table widget + table: BoxedWidget, + /// Footer widget w/ context + footer: BoxedWidget, } -impl<'a> View for Notifications<'a> { +impl<'a: 'static, B> View for Notifications<'a, B> +where + B: Backend + 'a, +{ fn new(state: &State, action_tx: UnboundedSender) -> Self { let props = NotificationsProps::from(state); let name = match state.mode.repository() { @@ -246,62 +239,82 @@ impl<'a> View for Notifications<'a> { Self { action_tx: action_tx.clone(), props: NotificationsProps::from(state), - table: Table::new(&(), action_tx.clone()) - .items(props.notifications.clone()) - .columns(props.columns.to_vec()) - .header( - Header::new(&(), action_tx.clone()) - .columns( - [ - Column::new("", Constraint::Length(0)), - Column::new(Text::from(name), Constraint::Fill(1)), - ] - .to_vec(), + table: Box::>::new( + Table::new(state, action_tx.clone()) + .header( + Header::new(state, action_tx.clone()) + .columns( + [ + Column::new("", Constraint::Length(0)), + Column::new(Text::from(name), Constraint::Fill(1)), + ] + .to_vec(), + ) + .cutoff(props.cutoff, props.cutoff_after) + .focus(props.focus) + .to_boxed(), + ) + .on_change(|props, action_tx| { + props + .downcast_ref::>() + .and_then(|props| { + action_tx + .send(Action::Select { + selected: props.selected, + }) + .ok() + }); + }) + .on_update(|state| { + let props = NotificationsProps::from(state); + + Box::>::new( + TableProps::default() + .columns(props.columns) + .items(state.notifications()) + .footer(!state.ui.show_search) + .page_size(state.ui.page_size) + .cutoff(props.cutoff, props.cutoff_after), ) - .cutoff(props.cutoff, props.cutoff_after) - .focus(props.focus), - ) - .footer(!props.show_search) - .cutoff(props.cutoff, props.cutoff_after), - footer: Footer::new(&(), action_tx), + }), + ), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = NotificationsProps::from(state); + + Box::>::new( + FooterProps::default().columns(Self::build_footer(&props, props.selected)), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let notifications: Vec = state - .notifications - .iter() - .filter(|issue| state.filter.matches(issue)) - .cloned() - .collect(); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let props = NotificationsProps::from(state); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let table = self.table.move_with_state(&()); - let table = table - .items(notifications) - .footer(!state.ui.show_search) - .page_size(state.ui.page_size); + fn update(&mut self, state: &State) { + // TODO call mapper here instead? + self.props = NotificationsProps::from(state); - let footer = self.footer.move_with_state(&()); - let footer = footer.columns(Self::build_footer(&props, table.selected())); - - Self { - props, - table, - footer, - ..self - } + self.table.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: Key) { match key { Key::Char('\n') => { - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.notifications.get(selected)) .and_then(|notif| { let selection = match self.props.mode.selection() { @@ -319,8 +332,8 @@ impl<'a> View for Notifications<'a> { }); } Key::Char('c') => { - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.notifications.get(selected)) .and_then(|notif| { self.action_tx @@ -335,16 +348,13 @@ impl<'a> View for Notifications<'a> { }); } _ => { - as View<(), Action>>::handle_key_event( - &mut self.table, - key, - ); + self.table.handle_key_event(key); } } } } -impl<'a> Notifications<'a> { +impl<'a, B: Backend> Notifications<'a, B> { fn build_footer(props: &NotificationsProps<'a>, selected: Option) -> Vec> { let search = Line::from( [ @@ -377,7 +387,7 @@ impl<'a> Notifications<'a> { let progress = selected .map(|selected| { - Table::::progress( + Table::::progress( selected, props.notifications.len(), props.page_size, @@ -423,19 +433,22 @@ impl<'a> Notifications<'a> { } } -impl<'a, B: Backend> Render for Notifications<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, B> Widget for Notifications<'a, B> +where + B: Backend + 'a, +{ + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let header_height = 3_usize; let page_size = if self.props.show_search { - as Render>::render(&self.table, frame, area, ()); + self.table.render(frame, area, &()); (area.height as usize).saturating_sub(header_height) } else { let layout = Layout::vertical([Constraint::Min(1), Constraint::Length(3)]).split(area); - as Render>::render(&self.table, frame, layout[0], ()); - as Render>::render(&self.footer, frame, layout[1], ()); + self.table.render(frame, layout[0], &()); + self.footer.render(frame, layout[1], &()); (area.height as usize).saturating_sub(header_height) }; @@ -446,30 +459,61 @@ impl<'a, B: Backend> Render for Notifications<'a> { } } -pub struct Search { - pub action_tx: UnboundedSender, - pub input: TextField, +pub struct Search { + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Search input field + input: BoxedWidget, } -impl View for Search { +impl View for Search { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .title("Search") - .inline(true); - Self { action_tx, input }.move_with_state(state) + .on_change(|props, action_tx| { + props.downcast_ref::().and_then(|props| { + action_tx + .send(Action::UpdateSearch { + value: props.text.clone(), + }) + .ok() + }); + }) + .on_update(|state| { + Box::::new( + TextFieldProps::default() + .text(&state.search.read().to_string()) + .title("Search") + .inline(true), + ) + }) + .to_boxed(); + Self { + action_tx, + input, + on_update: None, + on_change: None, + } } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let input = self.input.move_with_state(state); - let input = input.text(&state.search.read().to_string()); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - Self { input, ..self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &State) { + self.input.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -481,22 +525,19 @@ impl View for Search { let _ = self.action_tx.send(Action::ApplySearch); } _ => { - as View>::handle_key_event(&mut self.input, key); - let _ = self.action_tx.send(Action::UpdateSearch { - value: self.input.read().to_string(), - }); + self.input.handle_key_event(key); } } } } -impl Render for Search { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl Widget for Search { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let layout = Layout::horizontal(Constraint::from_mins([0])) .horizontal_margin(1) .split(area); - as Render>::render(&self.input, frame, layout[0], ()); + self.input.render(frame, layout[0], &()); } } @@ -645,70 +686,93 @@ impl<'a> From<&State> for HelpProps<'a> { } } -pub struct Help<'a> { - /// Send messages - pub action_tx: UnboundedSender, - /// This widget's render properties - pub props: HelpProps<'a>, +pub struct Help<'a, B: Backend> { + /// Internal properties + props: HelpProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, /// Container header - header: Header<'a, Action>, + header: BoxedWidget, /// Content widget - content: Paragraph<'a, Action>, + content: BoxedWidget, /// Container footer - footer: Footer<'a, Action>, + footer: BoxedWidget, } -impl<'a> View for Help<'a> { +impl<'a, B: Backend> View for Help<'a, B> { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { - let props = HelpProps::from(state); - Self { action_tx: action_tx.clone(), - props: props.clone(), - header: Header::new(&(), action_tx.clone()), - content: Paragraph::new(state, action_tx.clone()), - footer: Footer::new(&(), action_tx), + props: HelpProps::from(state), + header: Header::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + HeaderProps::default() + .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) + .focus(props.focus), + ) + }) + .to_boxed(), + content: Paragraph::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + ParagraphProps::default() + .text(&props.content) + .page_size(props.page_size) + .focus(props.focus), + ) + }) + .to_boxed(), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = HelpProps::from(state); + let progress = span::default(format!("{}%", 0)).dim(); + + Box::>::new( + FooterProps::default() + .columns( + [ + Column::new(Text::raw(""), Constraint::Fill(1)), + Column::new(Text::from(progress), Constraint::Min(4)), + ] + .to_vec(), + ) + .focus(props.focus), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let props = HelpProps::from(state); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let header = self.header.move_with_state(&()); - let header = header - .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) - .focus(props.focus); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let content = self.content.move_with_state(state); - let content = content.text(&props.content).page_size(props.page_size); + fn update(&mut self, state: &State) { + self.props = HelpProps::from(state); - let progress = span::default(format!("{}%", content.progress())).dim(); - - let footer = self.footer.move_with_state(&()); - let footer = footer - .columns( - [ - Column::new(Text::raw(""), Constraint::Fill(1)), - Column::new(Text::from(progress), Constraint::Min(4)), - ] - .to_vec(), - ) - .focus(props.focus); - - Self { - props, - header, - content, - footer, - ..self - } + self.header.update(state); + self.content.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -720,14 +784,14 @@ impl<'a> View for Help<'a> { let _ = self.action_tx.send(Action::CloseHelp); } _ => { - as View<(), _>>::handle_key_event(&mut self.content, key); + self.content.handle_key_event(key); } } } } -impl<'a, B: Backend> Render for Help<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a, B: Backend> Widget for Help<'a, B> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let [header_area, content_area, footer_area] = Layout::vertical([ Constraint::Length(3), Constraint::Min(1), @@ -735,9 +799,9 @@ impl<'a, B: Backend> Render for Help<'a> { ]) .areas(area); - as Render>::render(&self.header, frame, header_area, ()); - as Render>::render(&self.content, frame, content_area, ()); - as Render>::render(&self.footer, frame, footer_area, ()); + self.header.render(frame, header_area, &()); + self.content.render(frame, content_area, &()); + self.footer.render(frame, footer_area, &()); let page_size = content_area.height as usize; if page_size != self.props.page_size { diff --git a/bin/commands/issue/select.rs b/bin/commands/issue/select.rs index 626e6c1f..8f3d7372 100644 --- a/bin/commands/issue/select.rs +++ b/bin/commands/issue/select.rs @@ -16,7 +16,8 @@ use tui::store; use tui::store::StateValue; use tui::task; use tui::task::Interrupted; -use tui::ui::items::{IssueItem, IssueItemFilter}; +use tui::terminal; +use tui::ui::items::{Filter, IssueItem, IssueItemFilter}; use tui::ui::Frontend; use tui::Exit; @@ -54,15 +55,50 @@ impl Default for UIState { } } +#[derive(Clone, Debug)] +pub struct IssuesState { + items: Vec, + selected: Option, +} + #[derive(Clone, Debug)] pub struct State { - issues: Vec, + issues: IssuesState, mode: Mode, filter: IssueItemFilter, search: StateValue, ui: UIState, } +impl State { + pub fn shortcuts(&self) -> Vec<(&str, &str)> { + if self.ui.show_search { + vec![("esc", "cancel"), ("enter", "apply")] + } else if self.ui.show_help { + vec![("?", "close")] + } else { + match self.mode { + Mode::Id => vec![("enter", "select"), ("/", "search")], + Mode::Operation => vec![ + ("enter", "show"), + ("e", "edit"), + ("/", "search"), + ("?", "help"), + ], + } + } + } + + pub fn issues(&self) -> Vec { + self.issues + .items + .iter() + .filter(|issue| self.filter.matches(issue)) + .cloned() + .collect() + } +} + impl TryFrom<&Context> for State { type Error = anyhow::Error; @@ -81,7 +117,10 @@ impl TryFrom<&Context> for State { items.sort_by(|a, b| b.timestamp.cmp(&a.timestamp)); Ok(Self { - issues: items, + issues: IssuesState { + items, + selected: None, + }, mode: context.mode.clone(), filter, search, @@ -92,7 +131,7 @@ impl TryFrom<&Context> for State { pub enum Action { Exit { selection: Option }, - Update, + Select { selected: Option }, PageSize(usize), OpenSearch, UpdateSearch { value: String }, @@ -108,6 +147,10 @@ impl store::State for State { fn handle_action(&mut self, action: Action) -> Option> { match action { Action::Exit { selection } => Some(Exit { value: selection }), + Action::Select { selected } => { + self.issues.selected = selected; + None + } Action::PageSize(size) => { self.ui.page_size = size; None @@ -142,7 +185,6 @@ impl store::State for State { self.ui.show_help = false; None } - Action::Update => None, } } } @@ -160,7 +202,10 @@ impl App { tokio::try_join!( store.main_loop(state, terminator, action_rx, interrupt_rx.resubscribe()), - frontend.main_loop::(state_rx, interrupt_rx.resubscribe()), + frontend.main_loop::, Selection>( + state_rx, + interrupt_rx.resubscribe() + ), )?; if let Ok(reason) = interrupt_rx.recv().await { diff --git a/bin/commands/issue/select/ui.rs b/bin/commands/issue/select/ui.rs index 26fba63c..75fa4ad3 100644 --- a/bin/commands/issue/select/ui.rs +++ b/bin/commands/issue/select/ui.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::collections::HashMap; use std::str::FromStr; use std::vec; @@ -14,12 +15,16 @@ use ratatui::text::{Line, Span, Text}; use radicle_tui as tui; -use tui::ui::items::{Filter, IssueItem, IssueItemFilter}; +use tui::ui::items::{IssueItem, IssueItemFilter}; use tui::ui::span; -use tui::ui::widget::container::{Footer, Header}; -use tui::ui::widget::input::TextField; -use tui::ui::widget::text::Paragraph; -use tui::ui::widget::{Column, Render, Shortcuts, Table, View}; +use tui::ui::widget; +use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; +use tui::ui::widget::input::{TextField, TextFieldProps}; +use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{ + Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, + Widget, +}; use tui::Selection; use crate::tui_issue::common::IssueOperation; @@ -27,6 +32,8 @@ use crate::tui_issue::common::Mode; use super::{Action, State}; +type BoxedWidget = widget::BoxedWidget; + pub struct ListPageProps { show_search: bool, show_help: bool, @@ -41,22 +48,29 @@ impl From<&State> for ListPageProps { } } -pub struct ListPage<'a> { - /// Action sender - pub action_tx: UnboundedSender, - /// State mapped props +pub struct ListPage { + /// Internal properties props: ListPageProps, - /// Notification widget - issues: Issues<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Patches widget + issues: BoxedWidget, /// Search widget - search: Search, + search: BoxedWidget, /// Help widget - help: Help<'a>, + help: BoxedWidget, /// Shortcut widget - shortcuts: Shortcuts, + shortcuts: BoxedWidget, } -impl<'a> View for ListPage<'a> { +impl<'a: 'static, B> View for ListPage +where + B: Backend + 'a, +{ fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, @@ -64,51 +78,43 @@ impl<'a> View for ListPage<'a> { Self { action_tx: action_tx.clone(), props: ListPageProps::from(state), - issues: Issues::new(state, action_tx.clone()), - search: Search::new(state, action_tx.clone()), - help: Help::new(state, action_tx.clone()), - shortcuts: Shortcuts::new(&(), action_tx.clone()), + issues: Issues::new(state, action_tx.clone()).to_boxed(), + search: Search::new(state, action_tx.clone()).to_boxed(), + help: Help::new(state, action_tx.clone()).to_boxed(), + shortcuts: Shortcuts::new(state, action_tx.clone()) + .on_update(|state| { + Box::new(ShortcutsProps::default().shortcuts(&state.shortcuts())) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let shorts = if state.ui.show_search { - vec![("esc", "cancel"), ("enter", "apply")] - } else if state.ui.show_help { - vec![("?", "close")] - } else { - match state.mode { - Mode::Id => vec![("enter", "select"), ("/", "search")], - Mode::Operation => vec![ - ("enter", "show"), - ("e", "edit"), - ("/", "search"), - ("?", "help"), - ], - } - }; + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let shortcuts = self.shortcuts.move_with_state(&()); - let shortcuts = shortcuts.shortcuts(&shorts); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - ListPage { - issues: self.issues.move_with_state(state), - shortcuts, - help: self.help.move_with_state(state), - props: ListPageProps::from(state), - ..self - } + fn update(&mut self, state: &State) { + self.props = ListPageProps::from(state); + + self.issues.update(state); + self.search.update(state); + self.help.update(state); + self.shortcuts.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { if self.props.show_search { - >::handle_key_event(&mut self.search, key) + self.search.handle_key_event(key); } else if self.props.show_help { - >::handle_key_event(&mut self.help, key) + self.help.handle_key_event(key); } else { match key { Key::Esc | Key::Ctrl('c') => { @@ -121,16 +127,18 @@ impl<'a> View for ListPage<'a> { let _ = self.action_tx.send(Action::OpenHelp); } _ => { - >::handle_key_event(&mut self.issues, key); + self.issues.handle_key_event(key); } } } - let _ = self.action_tx.send(Action::Update); } } -impl<'a, B: Backend> Render for ListPage<'a> { - fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: ()) { +impl<'a: 'static, B> Widget for ListPage +where + B: Backend + 'a, +{ + fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: &dyn Any) { let area = frame.size(); let layout = tui::ui::layout::default_page(area, 0u16, 1u16); @@ -138,15 +146,15 @@ impl<'a, B: Backend> Render for ListPage<'a> { let component_layout = Layout::vertical([Constraint::Min(1), Constraint::Length(2)]) .split(layout.component); - as Render>::render(&self.issues, frame, component_layout[0], ()); - >::render(&self.search, frame, component_layout[1], ()); + self.issues.render(frame, component_layout[0], &()); + self.search.render(frame, component_layout[1], &()); } else if self.props.show_help { - as Render>::render(&self.help, frame, layout.component, ()); + self.help.render(frame, layout.component, &()); } else { - as Render>::render(&self.issues, frame, layout.component, ()); + self.issues.render(frame, layout.component, &()); } - as Render>::render(&self.shortcuts, frame, layout.shortcuts, ()); + self.shortcuts.render(frame, layout.shortcuts, &()); } } @@ -154,6 +162,7 @@ impl<'a, B: Backend> Render for ListPage<'a> { struct IssuesProps<'a> { mode: Mode, issues: Vec, + selected: Option, search: String, stats: HashMap, columns: Vec>, @@ -168,12 +177,7 @@ impl<'a> From<&State> for IssuesProps<'a> { fn from(state: &State) -> Self { use radicle::issue::State; - let issues: Vec = state - .issues - .iter() - .filter(|issue| state.filter.matches(issue)) - .cloned() - .collect(); + let issues = state.issues(); let mut open = 0; let mut other = 0; @@ -221,72 +225,95 @@ impl<'a> From<&State> for IssuesProps<'a> { stats, page_size: state.ui.page_size, show_search: state.ui.show_search, + selected: state.issues.selected, } } } -struct Issues<'a> { - /// Action sender - action_tx: UnboundedSender, - /// State mapped props +struct Issues<'a, B> { + /// Internal properties props: IssuesProps<'a>, - /// Notification table - table: Table<'a, Action, IssueItem>, - /// Footer - footer: Footer<'a, Action>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Table widget + table: BoxedWidget, + /// Footer widget w/ context + footer: BoxedWidget, } -impl<'a> View for Issues<'a> { +impl<'a: 'static, B> View for Issues<'a, B> +where + B: Backend + 'a, +{ fn new(state: &State, action_tx: UnboundedSender) -> Self { let props = IssuesProps::from(state); Self { action_tx: action_tx.clone(), props: props.clone(), - table: Table::new(&(), action_tx.clone()) - .items(props.issues.clone()) - .columns(props.columns.to_vec()) - .header( - Header::new(&(), action_tx.clone()) - .columns(props.columns.clone()) - .cutoff(props.cutoff, props.cutoff_after) - .focus(props.focus), - ) - .footer(!props.show_search) - .cutoff(props.cutoff, props.cutoff_after), - footer: Footer::new(&(), action_tx), + table: Box::>::new( + Table::new(state, action_tx.clone()) + .header( + Header::new(state, action_tx.clone()) + .columns(props.columns.clone()) + .cutoff(props.cutoff, props.cutoff_after) + .focus(props.focus) + .to_boxed(), + ) + .on_change(|props, action_tx| { + if let Some(props) = props.downcast_ref::>() { + let _ = action_tx.send(Action::Select { + selected: props.selected, + }); + } + }) + .on_update(|state| { + let props = IssuesProps::from(state); + + Box::>::new( + TableProps::default() + .columns(props.columns) + .items(state.issues()) + .footer(!state.ui.show_search) + .page_size(state.ui.page_size) + .cutoff(props.cutoff, props.cutoff_after), + ) + }), + ), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = IssuesProps::from(state); + + Box::>::new( + FooterProps::default().columns(Self::build_footer(&props, props.selected)), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let issues: Vec = state - .issues - .iter() - .filter(|issue| state.filter.matches(issue)) - .cloned() - .collect(); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let props = IssuesProps::from(state); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let table = self.table.move_with_state(&()); - let table = table - .items(issues) - .footer(!state.ui.show_search) - .page_size(state.ui.page_size); + fn update(&mut self, state: &State) { + // TODO call mapper here instead? + self.props = IssuesProps::from(state); - let footer = self.footer.move_with_state(&()); - let footer = footer.columns(Self::build_footer(&props, table.selected())); - - Self { - props, - table, - footer, - ..self - } + self.table.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: Key) { @@ -297,8 +324,8 @@ impl<'a> View for Issues<'a> { Mode::Id => None, }; - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.issues.get(selected)) .and_then(|issue| { self.action_tx @@ -313,8 +340,8 @@ impl<'a> View for Issues<'a> { }); } Key::Char('e') => { - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.issues.get(selected)) .and_then(|issue| { self.action_tx @@ -329,16 +356,13 @@ impl<'a> View for Issues<'a> { }); } _ => { - as View<(), Action>>::handle_key_event( - &mut self.table, - key, - ); + self.table.handle_key_event(key); } } } } -impl<'a> Issues<'a> { +impl<'a, B: Backend> Issues<'a, B> { fn build_footer(props: &IssuesProps<'a>, selected: Option) -> Vec> { let search = Line::from( [ @@ -387,7 +411,11 @@ impl<'a> Issues<'a> { let progress = selected .map(|selected| { - Table::::progress(selected, props.issues.len(), props.page_size) + Table::::progress( + selected, + props.issues.len(), + props.page_size, + ) }) .unwrap_or_default(); let progress = span::default(format!("{}%", progress)).dim(); @@ -435,19 +463,22 @@ impl<'a> Issues<'a> { } } -impl<'a, B: Backend> Render for Issues<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, B> Widget for Issues<'a, B> +where + B: Backend + 'a, +{ + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let header_height = 3_usize; let page_size = if self.props.show_search { - as Render>::render(&self.table, frame, area, ()); + self.table.render(frame, area, &()); (area.height as usize).saturating_sub(header_height) } else { let layout = Layout::vertical([Constraint::Min(1), Constraint::Length(3)]).split(area); - as Render>::render(&self.table, frame, layout[0], ()); - as Render>::render(&self.footer, frame, layout[1], ()); + self.table.render(frame, layout[0], &()); + self.footer.render(frame, layout[1], &()); (area.height as usize).saturating_sub(header_height) }; @@ -458,30 +489,61 @@ impl<'a, B: Backend> Render for Issues<'a> { } } -pub struct Search { - pub action_tx: UnboundedSender, - pub input: TextField, +pub struct Search { + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Search input field + input: BoxedWidget, } -impl View for Search { +impl View for Search { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .title("Search") - .inline(true); - Self { action_tx, input }.move_with_state(state) + .on_change(|props, action_tx| { + props.downcast_ref::().and_then(|props| { + action_tx + .send(Action::UpdateSearch { + value: props.text.clone(), + }) + .ok() + }); + }) + .on_update(|state| { + Box::::new( + TextFieldProps::default() + .text(&state.search.read().to_string()) + .title("Search") + .inline(true), + ) + }) + .to_boxed(); + Self { + action_tx, + input, + on_update: None, + on_change: None, + } } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let input = self.input.move_with_state(state); - let input = input.text(&state.search.read().to_string()); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - Self { input, ..self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &State) { + self.input.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -493,25 +555,23 @@ impl View for Search { let _ = self.action_tx.send(Action::ApplySearch); } _ => { - as View>::handle_key_event(&mut self.input, key); - let _ = self.action_tx.send(Action::UpdateSearch { - value: self.input.read().to_string(), - }); + self.input.handle_key_event(key); } } } } -impl Render for Search { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl Widget for Search { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let layout = Layout::horizontal(Constraint::from_mins([0])) .horizontal_margin(1) .split(area); - as Render>::render(&self.input, frame, layout[0], ()); + self.input.render(frame, layout[0], &()); } } +#[derive(Clone)] pub struct HelpProps<'a> { content: Text<'a>, focus: bool, @@ -656,70 +716,93 @@ impl<'a> From<&State> for HelpProps<'a> { } } -pub struct Help<'a> { - /// Send messages - pub action_tx: UnboundedSender, - /// This widget's render properties - pub props: HelpProps<'a>, +pub struct Help<'a, B: Backend> { + /// Internal properties + props: HelpProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, /// Container header - header: Header<'a, Action>, + header: BoxedWidget, /// Content widget - content: Paragraph<'a, Action>, + content: BoxedWidget, /// Container footer - footer: Footer<'a, Action>, + footer: BoxedWidget, } -impl<'a> View for Help<'a> { +impl<'a, B: Backend> View for Help<'a, B> { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { - let props = HelpProps::from(state); - Self { action_tx: action_tx.clone(), - props, - header: Header::new(&(), action_tx.clone()), - content: Paragraph::new(state, action_tx.clone()), - footer: Footer::new(&(), action_tx), + props: HelpProps::from(state), + header: Header::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + HeaderProps::default() + .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) + .focus(props.focus), + ) + }) + .to_boxed(), + content: Paragraph::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + ParagraphProps::default() + .text(&props.content) + .page_size(props.page_size) + .focus(props.focus), + ) + }) + .to_boxed(), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = HelpProps::from(state); + let progress = span::default(format!("{}%", 0)).dim(); + + Box::>::new( + FooterProps::default() + .columns( + [ + Column::new(Text::raw(""), Constraint::Fill(1)), + Column::new(Text::from(progress), Constraint::Min(4)), + ] + .to_vec(), + ) + .focus(props.focus), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let props = HelpProps::from(state); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let header = self.header.move_with_state(&()); - let header = header - .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) - .focus(props.focus); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let content = self.content.move_with_state(state); - let content = content.text(&props.content).page_size(props.page_size); + fn update(&mut self, state: &State) { + self.props = HelpProps::from(state); - let progress = span::default(format!("{}%", content.progress())).dim(); - - let footer = self.footer.move_with_state(&()); - let footer = footer - .columns( - [ - Column::new(Text::raw(""), Constraint::Fill(1)), - Column::new(Text::from(progress), Constraint::Min(4)), - ] - .to_vec(), - ) - .focus(props.focus); - - Self { - props, - header, - content, - footer, - ..self - } + self.header.update(state); + self.content.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -731,14 +814,14 @@ impl<'a> View for Help<'a> { let _ = self.action_tx.send(Action::CloseHelp); } _ => { - as View<(), _>>::handle_key_event(&mut self.content, key); + self.content.handle_key_event(key); } } } } -impl<'a, B: Backend> Render for Help<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a, B: Backend> Widget for Help<'a, B> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let [header_area, content_area, footer_area] = Layout::vertical([ Constraint::Length(3), Constraint::Min(1), @@ -746,9 +829,9 @@ impl<'a, B: Backend> Render for Help<'a> { ]) .areas(area); - as Render>::render(&self.header, frame, header_area, ()); - as Render>::render(&self.content, frame, content_area, ()); - as Render>::render(&self.footer, frame, footer_area, ()); + self.header.render(frame, header_area, &()); + self.content.render(frame, content_area, &()); + self.footer.render(frame, footer_area, &()); let page_size = content_area.height as usize; if page_size != self.props.page_size { diff --git a/bin/commands/patch/select.rs b/bin/commands/patch/select.rs index 297ab640..dbd74861 100644 --- a/bin/commands/patch/select.rs +++ b/bin/commands/patch/select.rs @@ -12,11 +12,11 @@ use radicle::Profile; use radicle_tui as tui; use tui::cob::patch; -use tui::cob::patch::Filter; use tui::store; use tui::task; use tui::task::Interrupted; -use tui::ui::items::{PatchItem, PatchItemFilter}; +use tui::terminal; +use tui::ui::items::{Filter, PatchItem, PatchItemFilter}; use tui::ui::Frontend; use tui::Exit; @@ -30,7 +30,7 @@ pub struct Context { pub profile: Profile, pub repository: Repository, pub mode: Mode, - pub filter: Filter, + pub filter: patch::Filter, } pub struct App { @@ -54,15 +54,51 @@ impl Default for UIState { } } +#[derive(Clone, Debug)] +pub struct PatchesState { + items: Vec, + selected: Option, +} + #[derive(Clone, Debug)] pub struct State { - patches: Vec, + patches: PatchesState, mode: Mode, filter: PatchItemFilter, search: store::StateValue, ui: UIState, } +impl State { + pub fn shortcuts(&self) -> Vec<(&str, &str)> { + if self.ui.show_search { + vec![("esc", "cancel"), ("enter", "apply")] + } else if self.ui.show_help { + vec![("?", "close")] + } else { + match self.mode { + Mode::Id => vec![("enter", "select"), ("/", "search")], + Mode::Operation => vec![ + ("enter", "show"), + ("c", "checkout"), + ("d", "diff"), + ("/", "search"), + ("?", "help"), + ], + } + } + } + + pub fn patches(&self) -> Vec { + self.patches + .items + .iter() + .filter(|patch| self.filter.matches(patch)) + .cloned() + .collect() + } +} + impl TryFrom<&Context> for State { type Error = anyhow::Error; @@ -81,7 +117,10 @@ impl TryFrom<&Context> for State { items.sort_by(|a, b| b.timestamp.cmp(&a.timestamp)); Ok(Self { - patches: items, + patches: PatchesState { + items, + selected: None, + }, mode: context.mode.clone(), filter, search, @@ -92,7 +131,7 @@ impl TryFrom<&Context> for State { pub enum Action { Exit { selection: Option }, - Update, + Select { selected: Option }, PageSize(usize), OpenSearch, UpdateSearch { value: String }, @@ -108,6 +147,10 @@ impl store::State for State { fn handle_action(&mut self, action: Action) -> Option> { match action { Action::Exit { selection } => Some(Exit { value: selection }), + Action::Select { selected } => { + self.patches.selected = selected; + None + } Action::PageSize(size) => { self.ui.page_size = size; None @@ -142,7 +185,6 @@ impl store::State for State { self.ui.show_help = false; None } - Action::Update => None, } } } @@ -160,7 +202,10 @@ impl App { tokio::try_join!( store.main_loop(state, terminator, action_rx, interrupt_rx.resubscribe()), - frontend.main_loop::(state_rx, interrupt_rx.resubscribe()), + frontend.main_loop::, Selection>( + state_rx, + interrupt_rx.resubscribe() + ), )?; if let Ok(reason) = interrupt_rx.recv().await { diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index 01149866..54a25302 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::collections::HashMap; use std::str::FromStr; use std::vec; @@ -15,12 +16,16 @@ use radicle::patch::{self, Status}; use radicle_tui as tui; -use tui::ui::items::{Filter, PatchItem, PatchItemFilter}; +use tui::ui::items::{PatchItem, PatchItemFilter}; use tui::ui::span; -use tui::ui::widget::container::{Footer, Header}; -use tui::ui::widget::input::TextField; -use tui::ui::widget::text::Paragraph; -use tui::ui::widget::{Column, Render, Shortcuts, Table, View, Widget}; +use tui::ui::widget; +use tui::ui::widget::container::{Footer, FooterProps, Header, HeaderProps}; +use tui::ui::widget::input::{TextField, TextFieldProps}; +use tui::ui::widget::text::{Paragraph, ParagraphProps}; +use tui::ui::widget::{ + Column, EventCallback, Shortcuts, ShortcutsProps, Table, TableProps, UpdateCallback, View, + Widget, +}; use tui::Selection; use crate::tui_patch::common::Mode; @@ -28,6 +33,8 @@ use crate::tui_patch::common::PatchOperation; use super::{Action, State}; +type BoxedWidget = widget::BoxedWidget; + pub struct ListPageProps { show_search: bool, show_help: bool, @@ -42,25 +49,26 @@ impl From<&State> for ListPageProps { } } -impl<'a, B: Backend> Widget for Patches<'a> {} -impl Widget for Search {} - -pub struct ListPage<'a> { - /// Action sender - pub action_tx: UnboundedSender, - /// State mapped props +pub struct ListPage { + /// Internal properties props: ListPageProps, - /// Notification widget - patches: Patches<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Patches widget + patches: BoxedWidget, /// Search widget - search: Search, + search: BoxedWidget, /// Help widget - help: Help<'a>, + help: BoxedWidget, /// Shortcut widget - shortcuts: Shortcuts, + shortcuts: BoxedWidget, } -impl<'a> View for ListPage<'a> { +impl<'a: 'static, B: Backend + 'a> View for ListPage { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, @@ -68,53 +76,43 @@ impl<'a> View for ListPage<'a> { Self { action_tx: action_tx.clone(), props: ListPageProps::from(state), - patches: Patches::new(state, action_tx.clone()), - search: Search::new(state, action_tx.clone()), - help: Help::new(state, action_tx.clone()), - shortcuts: Shortcuts::new(&(), action_tx), + patches: Patches::new(state, action_tx.clone()).to_boxed(), + search: Search::new(state, action_tx.clone()).to_boxed(), + help: Help::new(state, action_tx.clone()).to_boxed(), + shortcuts: Shortcuts::new(state, action_tx.clone()) + .on_update(|state| { + Box::new(ShortcutsProps::default().shortcuts(&state.shortcuts())) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let shorts = if state.ui.show_search { - vec![("esc", "cancel"), ("enter", "apply")] - } else if state.ui.show_help { - vec![("?", "close")] - } else { - match state.mode { - Mode::Id => vec![("enter", "select"), ("/", "search")], - Mode::Operation => vec![ - ("enter", "show"), - ("c", "checkout"), - ("d", "diff"), - ("/", "search"), - ("?", "help"), - ], - } - }; + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let shortcuts = self.shortcuts.move_with_state(state); - let shortcuts = shortcuts.shortcuts(&shorts); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - ListPage { - patches: self.patches.move_with_state(state), - search: self.search.move_with_state(state), - shortcuts, - help: self.help.move_with_state(state), - props: ListPageProps::from(state), - ..self - } + fn update(&mut self, state: &State) { + self.props = ListPageProps::from(state); + + self.patches.update(state); + self.search.update(state); + self.help.update(state); + self.shortcuts.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { if self.props.show_search { - >::handle_key_event(&mut self.search, key) + self.search.handle_key_event(key); } else if self.props.show_help { - >::handle_key_event(&mut self.help, key); + self.help.handle_key_event(key); } else { match key { Key::Esc | Key::Ctrl('c') => { @@ -127,16 +125,15 @@ impl<'a> View for ListPage<'a> { let _ = self.action_tx.send(Action::OpenHelp); } _ => { - >::handle_key_event(&mut self.patches, key); + self.patches.handle_key_event(key); } } } - let _ = self.action_tx.send(Action::Update); } } -impl<'a, B: Backend> Render for ListPage<'a> { - fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: ()) { +impl<'a: 'static, B: Backend + 'a> Widget for ListPage { + fn render(&self, frame: &mut ratatui::Frame, _area: Rect, __props: &dyn Any) { let area = frame.size(); let layout = tui::ui::layout::default_page(area, 0u16, 1u16); @@ -144,15 +141,15 @@ impl<'a, B: Backend> Render for ListPage<'a> { let component_layout = Layout::vertical([Constraint::Min(1), Constraint::Length(2)]) .split(layout.component); - as Render>::render(&self.patches, frame, component_layout[0], ()); - >::render(&self.search, frame, component_layout[1], ()); + self.patches.render(frame, component_layout[0], &()); + self.search.render(frame, component_layout[1], &()); } else if self.props.show_help { - as Render>::render(&self.help, frame, layout.component, ()); + self.help.render(frame, layout.component, &()); } else { - as Render>::render(&self.patches, frame, layout.component, ()); + self.patches.render(frame, layout.component, &()); } - as Render>::render(&self.shortcuts, frame, layout.shortcuts, ()); + self.shortcuts.render(frame, layout.shortcuts, &()); } } @@ -160,6 +157,7 @@ impl<'a, B: Backend> Render for ListPage<'a> { struct PatchesProps<'a> { mode: Mode, patches: Vec, + selected: Option, search: String, stats: HashMap, columns: Vec>, @@ -177,12 +175,7 @@ impl<'a> From<&State> for PatchesProps<'a> { let mut archived = 0; let mut merged = 0; - let patches: Vec = state - .patches - .iter() - .filter(|patch| state.filter.matches(patch)) - .cloned() - .collect(); + let patches = state.patches(); for patch in &patches { match patch.state { @@ -225,70 +218,99 @@ impl<'a> From<&State> for PatchesProps<'a> { stats, page_size: state.ui.page_size, show_search: state.ui.show_search, + selected: state.patches.selected, } } } -struct Patches<'a> { - /// Action sender - action_tx: UnboundedSender, - /// State mapped props +struct Patches<'a, B> { + /// Internal properties props: PatchesProps<'a>, - /// Notification table - table: Table<'a, Action, PatchItem>, - /// Table footer - footer: Footer<'a, Action>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Table widget + table: BoxedWidget, + /// Footer widget w/ context + footer: BoxedWidget, } -impl<'a> View for Patches<'a> { +impl<'a: 'static, B> View for Patches<'a, B> +where + B: Backend + 'a, +{ fn new(state: &State, action_tx: UnboundedSender) -> Self { let props = PatchesProps::from(state); Self { action_tx: action_tx.clone(), props: props.clone(), - table: Table::new(&(), action_tx.clone()) - .items(props.patches.clone()) - .columns(props.columns.to_vec()) - .header( - Header::new(&(), action_tx.clone()) - .columns(props.columns.clone()) - .cutoff(props.cutoff, props.cutoff_after) - .focus(props.focus), - ) - .footer(!props.show_search) - .cutoff(props.cutoff, props.cutoff_after), - footer: Footer::new(&(), action_tx), + table: Box::>::new( + Table::new(state, action_tx.clone()) + .header( + Header::new(state, action_tx.clone()) + .columns(props.columns.clone()) + .cutoff(props.cutoff, props.cutoff_after) + .focus(props.focus) + .to_boxed(), + ) + .on_change(|props, action_tx| { + props + .downcast_ref::>() + .and_then(|props| { + action_tx + .send(Action::Select { + selected: props.selected, + }) + .ok() + }); + }) + .on_update(|state| { + let props = PatchesProps::from(state); + + Box::>::new( + TableProps::default() + .columns(props.columns) + .items(state.patches()) + .footer(!state.ui.show_search) + .page_size(state.ui.page_size) + .cutoff(props.cutoff, props.cutoff_after), + ) + }), + ), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = PatchesProps::from(state); + + Box::>::new( + FooterProps::default().columns(Self::build_footer(&props, props.selected)), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self { - let patches: Vec = state - .patches - .iter() - .filter(|patch| state.filter.matches(patch)) - .cloned() - .collect(); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let props = PatchesProps::from(state); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let table = self - .table - .move_with_state(&()) - .items(patches) - .footer(!state.ui.show_search) - .page_size(state.ui.page_size); + fn update(&mut self, state: &State) { + // TODO call mapper here instead? + self.props = PatchesProps::from(state); - let footer = self.footer.move_with_state(&()); - let footer = footer.columns(Self::build_footer(&props, table.selected())); - - Self { - props, - table, - footer, - ..self - } + self.table.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: Key) { @@ -299,8 +321,8 @@ impl<'a> View for Patches<'a> { Mode::Id => None, }; - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.action_tx @@ -315,8 +337,8 @@ impl<'a> View for Patches<'a> { }); } Key::Char('c') => { - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.action_tx @@ -331,8 +353,8 @@ impl<'a> View for Patches<'a> { }); } Key::Char('d') => { - self.table - .selected() + self.props + .selected .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.action_tx @@ -347,16 +369,13 @@ impl<'a> View for Patches<'a> { }); } _ => { - as View<(), Action>>::handle_key_event( - &mut self.table, - key, - ); + self.table.handle_key_event(key); } } } } -impl<'a> Patches<'a> { +impl<'a, B: Backend> Patches<'a, B> { fn build_footer(props: &PatchesProps<'a>, selected: Option) -> Vec> { let filter = PatchItemFilter::from_str(&props.search).unwrap_or_default(); @@ -418,7 +437,11 @@ impl<'a> Patches<'a> { let progress = selected .map(|selected| { - Table::::progress(selected, props.patches.len(), props.page_size) + Table::::progress( + selected, + props.patches.len(), + props.page_size, + ) }) .unwrap_or_default(); let progress = span::default(format!("{}%", progress)).dim(); @@ -468,19 +491,22 @@ impl<'a> Patches<'a> { } } -impl<'a, B: Backend> Render for Patches<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, B> Widget for Patches<'a, B> +where + B: Backend + 'a, +{ + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let header_height = 3_usize; let page_size = if self.props.show_search { - as Render>::render(&self.table, frame, area, ()); + self.table.render(frame, area, &()); (area.height as usize).saturating_sub(header_height) } else { let layout = Layout::vertical([Constraint::Min(1), Constraint::Length(3)]).split(area); - as Render>::render(&self.table, frame, layout[0], ()); - as Render>::render(&self.footer, frame, layout[1], ()); + self.table.render(frame, layout[0], &()); + self.footer.render(frame, layout[1], &()); (area.height as usize).saturating_sub(header_height) }; @@ -491,30 +517,61 @@ impl<'a, B: Backend> Render for Patches<'a> { } } -pub struct Search { - pub action_tx: UnboundedSender, - pub input: TextField, +pub struct Search { + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Search input field + input: BoxedWidget, } -impl View for Search { +impl View for Search { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .title("Search") - .inline(true); - Self { action_tx, input }.move_with_state(state) + .on_change(|props, action_tx| { + props.downcast_ref::().and_then(|props| { + action_tx + .send(Action::UpdateSearch { + value: props.text.clone(), + }) + .ok() + }); + }) + .on_update(|state| { + Box::::new( + TextFieldProps::default() + .text(&state.search.read().to_string()) + .title("Search") + .inline(true), + ) + }) + .to_boxed(); + Self { + action_tx, + input, + on_update: None, + on_change: None, + } } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let input = self.input.move_with_state(state); - let input = input.text(&state.search.read().to_string()); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - Self { input, ..self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &State) { + self.input.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -526,22 +583,19 @@ impl View for Search { let _ = self.action_tx.send(Action::ApplySearch); } _ => { - as View>::handle_key_event(&mut self.input, key); - let _ = self.action_tx.send(Action::UpdateSearch { - value: self.input.read().to_string(), - }); + self.input.handle_key_event(key); } } } } -impl Render for Search { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl Widget for Search { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let layout = Layout::horizontal(Constraint::from_mins([0])) .horizontal_margin(1) .split(area); - as Render>::render(&self.input, frame, layout[0], ()); + self.input.render(frame, layout[0], &()); } } @@ -698,70 +752,93 @@ impl<'a> From<&State> for HelpProps<'a> { } } -pub struct Help<'a> { - /// Send messages - pub action_tx: UnboundedSender, - /// This widget's render properties - pub props: HelpProps<'a>, +pub struct Help<'a, B: Backend> { + /// Internal properties + props: HelpProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, /// Container header - header: Header<'a, Action>, + header: BoxedWidget, /// Content widget - content: Paragraph<'a, Action>, + content: BoxedWidget, /// Container footer - footer: Footer<'a, Action>, + footer: BoxedWidget, } -impl<'a> View for Help<'a> { +impl<'a, B: Backend> View for Help<'a, B> { fn new(state: &State, action_tx: UnboundedSender) -> Self where Self: Sized, { - let props = HelpProps::from(state); - Self { action_tx: action_tx.clone(), - props: props.clone(), - header: Header::new(&(), action_tx.clone()), - content: Paragraph::new(state, action_tx.clone()), - footer: Footer::new(&(), action_tx), + props: HelpProps::from(state), + header: Header::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + HeaderProps::default() + .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) + .focus(props.focus), + ) + }) + .to_boxed(), + content: Paragraph::new(state, action_tx.clone()) + .on_update(|state| { + let props = HelpProps::from(state); + + Box::>::new( + ParagraphProps::default() + .text(&props.content) + .page_size(props.page_size) + .focus(props.focus), + ) + }) + .to_boxed(), + footer: Footer::new(state, action_tx) + .on_update(|state| { + let props = HelpProps::from(state); + let progress = span::default(format!("{}%", 0)).dim(); + + Box::>::new( + FooterProps::default() + .columns( + [ + Column::new(Text::raw(""), Constraint::Fill(1)), + Column::new(Text::from(progress), Constraint::Min(4)), + ] + .to_vec(), + ) + .focus(props.focus), + ) + }) + .to_boxed(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, state: &State) -> Self - where - Self: Sized, - { - let props = HelpProps::from(state); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } - let header = self.header.move_with_state(&()); - let header = header - .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) - .focus(props.focus); + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } - let content = self.content.move_with_state(state); - let content = content.text(&props.content).page_size(props.page_size); + fn update(&mut self, state: &State) { + self.props = HelpProps::from(state); - let progress = span::default(format!("{}%", content.progress())).dim(); - - let footer = self.footer.move_with_state(&()); - let footer = footer - .columns( - [ - Column::new(Text::raw(""), Constraint::Fill(1)), - Column::new(Text::from(progress), Constraint::Min(4)), - ] - .to_vec(), - ) - .focus(props.focus); - - Self { - props, - header, - content, - footer, - ..self - } + self.header.update(state); + self.content.update(state); + self.footer.update(state); } fn handle_key_event(&mut self, key: termion::event::Key) { @@ -773,14 +850,14 @@ impl<'a> View for Help<'a> { let _ = self.action_tx.send(Action::CloseHelp); } _ => { - as View<(), _>>::handle_key_event(&mut self.content, key); + self.content.handle_key_event(key); } } } } -impl<'a, B: Backend> Render for Help<'a> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a, B: Backend> Widget for Help<'a, B> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let [header_area, content_area, footer_area] = Layout::vertical([ Constraint::Length(3), Constraint::Min(1), @@ -788,9 +865,9 @@ impl<'a, B: Backend> Render for Help<'a> { ]) .areas(area); - as Render>::render(&self.header, frame, header_area, ()); - as Render>::render(&self.content, frame, content_area, ()); - as Render>::render(&self.footer, frame, footer_area, ()); + self.header.render(frame, header_area, &()); + self.content.render(frame, content_area, &()); + self.footer.render(frame, footer_area, &()); let page_size = content_area.height as usize; if page_size != self.props.page_size { diff --git a/src/terminal.rs b/src/terminal.rs index 14030880..e5faf2e8 100644 --- a/src/terminal.rs +++ b/src/terminal.rs @@ -10,7 +10,7 @@ use tokio::sync::mpsc::{self}; use super::event::Event; -type Backend = TermionBackendExt>; +pub type Backend = TermionBackendExt>; /// FIXME Remove workaround after a new `ratatui` version with /// https://github.com/ratatui-org/ratatui/pull/981/ included was released. diff --git a/src/ui.rs b/src/ui.rs index 64d0b238..d9849e32 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -20,7 +20,7 @@ use super::store::State; use super::task::Interrupted; use super::terminal; use super::terminal::TermionBackendExt; -use super::ui::widget::{Render, View}; +use super::ui::widget::Widget; type Backend = TermionBackendExt>; @@ -45,7 +45,7 @@ impl Frontend { ) -> anyhow::Result> where S: State, - W: View + Render, + W: Widget, P: Clone + Send + Sync + Debug, { let mut ticker = tokio::time::interval(RENDERING_TICK_RATE); @@ -69,7 +69,7 @@ impl Frontend { }, // Handle state updates Some(state) = state_rx.recv() => { - root = root.move_with_state(&state); + root.update(&state); }, // Catch and handle interrupt signal to gracefully shutdown Ok(interrupted) = interrupt_rx.recv() => { @@ -79,7 +79,7 @@ impl Frontend { break Ok(interrupted); } } - terminal.draw(|frame| root.render(frame, frame.size(), ()))?; + terminal.draw(|frame| root.render(frame, frame.size(), &()))?; }; terminal::restore(&mut terminal)?; diff --git a/src/ui/widget.rs b/src/ui/widget.rs index eb48801b..af19da8b 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -2,6 +2,7 @@ pub mod container; pub mod input; pub mod text; +use std::any::Any; use std::cmp; use std::fmt::Debug; @@ -12,33 +13,48 @@ use termion::event::Key; use ratatui::prelude::*; use ratatui::widgets::{Block, BorderType, Borders, Cell, Row, TableState}; -use self::container::Header; - use super::theme::style; use super::{layout, span}; +pub type BoxedWidget = Box>; + +pub type UpdateCallback = fn(&S) -> Box; +pub type EventCallback = fn(&dyn Any, UnboundedSender); + pub trait View { fn new(state: &S, action_tx: UnboundedSender) -> Self where Self: Sized; - fn move_with_state(self, state: &S) -> Self + fn on_change(self, callback: EventCallback) -> Self + where + Self: Sized; + + fn on_update(self, callback: UpdateCallback) -> Self where Self: Sized; fn handle_key_event(&mut self, key: Key); + + fn update(&mut self, state: &S); + + fn to_boxed(self) -> Box + where + Self: Sized, + { + Box::new(self) + } } -pub trait Render { - fn render(&self, frame: &mut Frame, area: Rect, props: P); +pub trait Widget: View { + fn render(&self, frame: &mut Frame, area: Rect, _props: &dyn Any); } -pub trait Widget: View + Render {} - pub trait ToRow { fn to_row(&self) -> Vec; } +#[derive(Clone)] pub struct ShortcutsProps { pub shortcuts: Vec<(String, String)>, pub divider: char, @@ -53,14 +69,33 @@ impl Default for ShortcutsProps { } } -pub struct Shortcuts { - /// Message sender - pub action_tx: UnboundedSender, - /// Internal properties - props: ShortcutsProps, +impl ShortcutsProps { + pub fn divider(mut self, divider: char) -> Self { + self.divider = divider; + self + } + + pub fn shortcuts(mut self, shortcuts: &[(&str, &str)]) -> Self { + self.shortcuts.clear(); + for (short, long) in shortcuts { + self.shortcuts.push((short.to_string(), long.to_string())); + } + self + } } -impl Shortcuts { +pub struct Shortcuts { + /// Internal properties + props: ShortcutsProps, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, +} + +impl Shortcuts { pub fn divider(mut self, divider: char) -> Self { self.props.divider = divider; self @@ -77,24 +112,43 @@ impl Shortcuts { } } -impl View for Shortcuts { - fn new(state: &S, action_tx: UnboundedSender) -> Self { +impl View for Shortcuts { + fn new(_state: &S, action_tx: UnboundedSender) -> Self { Self { action_tx: action_tx.clone(), props: ShortcutsProps::default(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, _state: &S) -> Self { - Self { ..self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self } - fn handle_key_event(&mut self, _key: termion::event::Key) {} + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } + + fn handle_key_event(&mut self, _key: Key) { + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::() { + self.props.shortcuts = props.shortcuts.clone(); + } + } + } } -impl Render for Shortcuts { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl Widget for Shortcuts { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { use ratatui::widgets::Table; let mut shortcuts = self.props.shortcuts.iter().peekable(); @@ -133,8 +187,6 @@ impl Render for Shortcuts { } } -impl Widget for Shortcuts {} - #[derive(Clone, Debug)] pub struct Column<'a> { pub text: Text<'a>, @@ -157,9 +209,13 @@ impl<'a> Column<'a> { } } -#[derive(Debug)] -pub struct TableProps<'a, R: ToRow> { +#[derive(Clone, Debug)] +pub struct TableProps<'a, R> +where + R: ToRow, +{ pub items: Vec, + pub selected: Option, pub focus: bool, pub columns: Vec>, pub has_footer: bool, @@ -168,7 +224,10 @@ pub struct TableProps<'a, R: ToRow> { pub page_size: usize, } -impl<'a, R: ToRow> Default for TableProps<'a, R> { +impl<'a, R> Default for TableProps<'a, R> +where + R: ToRow, +{ fn default() -> Self { Self { items: vec![], @@ -178,61 +237,87 @@ impl<'a, R: ToRow> Default for TableProps<'a, R> { cutoff: usize::MAX, cutoff_after: usize::MAX, page_size: 1, + selected: Some(0), } } } -pub struct Table<'a, A, R: ToRow> { - /// Sending actions to the state store - pub action_tx: UnboundedSender, - /// Internal table properties - pub props: TableProps<'a, R>, - /// Internal selection state - state: TableState, - /// Table header widget - header: Option>, -} - -impl<'a, A, R: ToRow> Table<'a, A, R> { +impl<'a, R> TableProps<'a, R> +where + R: ToRow, +{ pub fn items(mut self, items: Vec) -> Self { - self.props.items = items; + self.items = items; + self + } + + pub fn selected(mut self, selected: Option) -> Self { + self.selected = selected; self } pub fn columns(mut self, columns: Vec>) -> Self { - self.props.columns = columns; + self.columns = columns; self } pub fn footer(mut self, has_footer: bool) -> Self { - self.props.has_footer = has_footer; + self.has_footer = has_footer; self } pub fn cutoff(mut self, cutoff: usize, cutoff_after: usize) -> Self { - self.props.cutoff = cutoff; - self.props.cutoff_after = cutoff_after; + self.cutoff = cutoff; + self.cutoff_after = cutoff_after; self } pub fn page_size(mut self, page_size: usize) -> Self { - self.props.page_size = page_size; + self.page_size = page_size; self } +} - pub fn header(mut self, header: Header<'a, A>) -> Self { +pub struct Table<'a, S, A, B, R> +where + B: Backend, + R: ToRow, +{ + /// Internal table properties + props: TableProps<'a, R>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, + /// Internal selection and offset state + state: TableState, + /// Table header widget + header: Option>, +} + +impl<'a, S, A, B, R> Table<'a, S, A, B, R> +where + B: Backend, + R: ToRow, +{ + pub fn header(mut self, header: BoxedWidget) -> Self { self.header = Some(header); self } fn prev(&mut self) -> Option { - let selected = self.selected().map(|current| current.saturating_sub(1)); + let selected = self + .state + .selected() + .map(|current| current.saturating_sub(1)); self.state.select(selected); selected } fn next(&mut self, len: usize) -> Option { - let selected = self.selected().map(|current| { + let selected = self.state.selected().map(|current| { if current < len.saturating_sub(1) { current.saturating_add(1) } else { @@ -245,6 +330,7 @@ impl<'a, A, R: ToRow> Table<'a, A, R> { fn prev_page(&mut self, page_size: usize) -> Option { let selected = self + .state .selected() .map(|current| current.saturating_sub(page_size)); self.state.select(selected); @@ -252,7 +338,7 @@ impl<'a, A, R: ToRow> Table<'a, A, R> { } fn next_page(&mut self, len: usize, page_size: usize) -> Option { - let selected = self.selected().map(|current| { + let selected = self.state.selected().map(|current| { if current < len.saturating_sub(1) { cmp::min(current.saturating_add(page_size), len.saturating_sub(1)) } else { @@ -263,18 +349,12 @@ impl<'a, A, R: ToRow> Table<'a, A, R> { selected } - fn begin(&mut self) -> Option { + fn begin(&mut self) { self.state.select(Some(0)); - self.state.selected() } - fn end(&mut self, len: usize) -> Option { + fn end(&mut self, len: usize) { self.state.select(Some(len.saturating_sub(1))); - self.state.selected() - } - - pub fn selected(&self) -> Option { - self.state.selected() } pub fn progress(selected: usize, len: usize, page_size: usize) -> usize { @@ -297,29 +377,45 @@ impl<'a, A, R: ToRow> Table<'a, A, R> { } } -impl<'a, S, A, R> View for Table<'a, A, R> +impl<'a: 'static, S, A, B, R> View for Table<'a, S, A, B, R> where - R: ToRow, + B: Backend, + R: ToRow + Clone + 'static, { - fn new(state: &S, action_tx: UnboundedSender) -> Self { + fn new(_state: &S, action_tx: UnboundedSender) -> Self { Self { action_tx: action_tx.clone(), props: TableProps::default(), state: TableState::default().with_selected(Some(0)), header: None, + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, _state: &S) -> Self { - let mut me = self; - if let Some(selected) = me.selected() { - if selected > me.props.items.len() { - me.begin(); + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } + + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::>() { + self.props = props.clone(); } } - me + // TODO: Move to state reducer + if let Some(selected) = self.state.selected() { + if selected > self.props.items.len() { + self.begin(); + } + } } fn handle_key_event(&mut self, key: Key) { @@ -344,15 +440,21 @@ where } _ => {} } + + self.props.selected = self.state.selected(); + + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } } } -impl<'a, A, B, R> Render for Table<'a, A, R> +impl<'a: 'static, S, A, B, R> Widget for Table<'a, S, A, B, R> where B: Backend, - R: ToRow + Debug, + R: ToRow + Clone + Debug + 'static, { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let header_height = if self.header.is_some() { 3 } else { 0 }; let [header_area, table_area] = Layout::vertical([Constraint::Length(header_height), Constraint::Min(1)]).areas(area); @@ -414,8 +516,9 @@ where .highlight_style(style::highlight()); if let Some(header) = &self.header { - as Render>::render(header, frame, header_area, ()); + header.render(frame, header_area, &()); } + frame.render_stateful_widget(rows, table_area, &mut self.state.clone()); } else { let block = Block::default() @@ -424,7 +527,7 @@ where .borders(borders); if let Some(header) = &self.header { - as Render>::render(header, frame, header_area, ()); + header.render(frame, header_area, &()); } frame.render_widget(block, table_area); @@ -438,10 +541,3 @@ where } } } - -impl<'a, S, A, B, R> Widget for Table<'a, A, R> -where - B: Backend, - R: ToRow + Debug, -{ -} diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 2960e02f..928acfd0 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -1,3 +1,4 @@ +use std::any::Any; use std::fmt::Debug; use tokio::sync::mpsc::UnboundedSender; @@ -10,9 +11,9 @@ use ratatui::widgets::{BorderType, Borders, Row}; use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock}; use crate::ui::theme::style; -use super::{Column, Render, View}; +use super::{Column, EventCallback, UpdateCallback, View, Widget}; -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct HeaderProps<'a> { pub columns: Vec>, pub cutoff: usize, @@ -20,6 +21,24 @@ pub struct HeaderProps<'a> { pub focus: bool, } +impl<'a> HeaderProps<'a> { + pub fn columns(mut self, columns: Vec>) -> Self { + self.columns = columns; + self + } + + pub fn focus(mut self, focus: bool) -> Self { + self.focus = focus; + self + } + + pub fn cutoff(mut self, cutoff: usize, cutoff_after: usize) -> Self { + self.cutoff = cutoff; + self.cutoff_after = cutoff_after; + self + } +} + impl<'a> Default for HeaderProps<'a> { fn default() -> Self { Self { @@ -31,14 +50,18 @@ impl<'a> Default for HeaderProps<'a> { } } -pub struct Header<'a, A> { - /// Sending actions to the state store - pub action_tx: UnboundedSender, +pub struct Header<'a, S, A> { /// Internal props props: HeaderProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, } -impl<'a, A> Header<'a, A> { +impl<'a, S, A> Header<'a, S, A> { pub fn columns(mut self, columns: Vec>) -> Self { self.props.columns = columns; self @@ -56,24 +79,43 @@ impl<'a, A> Header<'a, A> { } } -impl<'a, A> View<(), A> for Header<'a, A> { - fn new(state: &(), action_tx: UnboundedSender) -> Self { +impl<'a: 'static, S, A> View for Header<'a, S, A> { + fn new(_state: &S, action_tx: UnboundedSender) -> Self { Self { action_tx: action_tx.clone(), props: HeaderProps::default(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, _state: &()) -> Self { - Self { ..self } + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self } - fn handle_key_event(&mut self, _key: Key) {} + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::>() { + self.props = props.clone(); + } + } + } + + fn handle_key_event(&mut self, _key: Key) { + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } + } } -impl<'a, A, B: Backend> Render for Header<'a, A> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, S, A, B: Backend> Widget for Header<'a, S, A> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let widths: Vec = self .props .columns @@ -132,7 +174,7 @@ impl<'a, A, B: Backend> Render for Header<'a, A> { } } -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct FooterProps<'a> { pub columns: Vec>, pub cutoff: usize, @@ -140,6 +182,24 @@ pub struct FooterProps<'a> { pub focus: bool, } +impl<'a> FooterProps<'a> { + pub fn columns(mut self, columns: Vec>) -> Self { + self.columns = columns; + self + } + + pub fn cutoff(mut self, cutoff: usize, cutoff_after: usize) -> Self { + self.cutoff = cutoff; + self.cutoff_after = cutoff_after; + self + } + + pub fn focus(mut self, focus: bool) -> Self { + self.focus = focus; + self + } +} + impl<'a> Default for FooterProps<'a> { fn default() -> Self { Self { @@ -151,14 +211,18 @@ impl<'a> Default for FooterProps<'a> { } } -pub struct Footer<'a, A> { - /// Message sender - pub action_tx: UnboundedSender, +pub struct Footer<'a, S, A> { /// Internal properties props: FooterProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, } -impl<'a, A> Footer<'a, A> { +impl<'a, S, A> Footer<'a, S, A> { pub fn columns(mut self, columns: Vec>) -> Self { self.props.columns = columns; self @@ -176,23 +240,42 @@ impl<'a, A> Footer<'a, A> { } } -impl<'a, A> View<(), A> for Footer<'a, A> { - fn new(_state: &(), action_tx: UnboundedSender) -> Self { +impl<'a: 'static, S, A> View for Footer<'a, S, A> { + fn new(_state: &S, action_tx: UnboundedSender) -> Self { Self { action_tx: action_tx.clone(), props: FooterProps::default(), + on_update: None, + on_change: None, } - .move_with_state(&()) } - fn move_with_state(self, _state: &()) -> Self { - Self { ..self } + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self } - fn handle_key_event(&mut self, _key: Key) {} + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::>() { + self.props = props.clone(); + } + } + } + + fn handle_key_event(&mut self, _key: Key) { + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } + } } -impl<'a, A> Footer<'a, A> { +impl<'a, S, A> Footer<'a, S, A> { fn render_cell( &self, frame: &mut ratatui::Frame, @@ -216,8 +299,8 @@ impl<'a, A> Footer<'a, A> { } } -impl<'a, A, B: Backend> Render for Footer<'a, A> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, S, A, B: Backend> Widget for Footer<'a, S, A> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let widths = self .props .columns diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 8e843b2a..83598aca 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -1,3 +1,5 @@ +use std::any::Any; + use termion::event::Key; use tokio::sync::mpsc::UnboundedSender; @@ -7,14 +9,35 @@ use ratatui::prelude::{Backend, Rect}; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{Render, View}; +use super::{EventCallback, UpdateCallback, View, Widget}; +#[derive(Clone)] pub struct TextFieldProps { - title: String, - inline_label: bool, - show_cursor: bool, - text: String, - cursor_position: usize, + pub title: String, + pub inline_label: bool, + pub show_cursor: bool, + pub text: String, + pub cursor_position: usize, +} + +impl TextFieldProps { + pub fn text(mut self, new_text: &str) -> Self { + if self.text != new_text { + self.text = String::from(new_text); + self.cursor_position = self.text.len(); + } + self + } + + pub fn title(mut self, title: &str) -> Self { + self.title = title.to_string(); + self + } + + pub fn inline(mut self, inline: bool) -> Self { + self.inline_label = inline; + self + } } impl Default for TextFieldProps { @@ -29,14 +52,18 @@ impl Default for TextFieldProps { } } -pub struct TextField { - /// Message sender - pub action_tx: UnboundedSender, +pub struct TextField { /// Internal props props: TextFieldProps, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, } -impl TextField { +impl TextField { pub fn read(&self) -> &str { &self.props.text } @@ -101,19 +128,34 @@ impl TextField { } } -impl View for TextField { - fn new(state: &S, action_tx: UnboundedSender) -> Self { +impl View for TextField { + fn new(_state: &S, action_tx: UnboundedSender) -> Self { Self { action_tx, props: TextFieldProps::default(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, _state: &S) -> Self { + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::() { + self.props = props.clone(); + } + } + } + fn handle_key_event(&mut self, key: Key) { match key { Key::Char(to_insert) @@ -134,11 +176,15 @@ impl View for TextField { } _ => {} } + + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } } } -impl Render for TextField { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl Widget for TextField { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let layout = Layout::vertical(Constraint::from_lengths([1, 1])).split(area); let input = self.props.text.as_str(); diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index d0994866..d3423249 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -1,3 +1,5 @@ +use std::any::Any; + use tokio::sync::mpsc::UnboundedSender; use termion::event::Key; @@ -9,8 +11,9 @@ use ratatui::widgets::{Block, BorderType, Borders}; use crate::ui::theme::style; -use super::{Render, View}; +use super::{EventCallback, UpdateCallback, View, Widget}; +#[derive(Clone)] pub struct ParagraphProps<'a> { pub content: Text<'a>, pub focus: bool, @@ -19,6 +22,23 @@ pub struct ParagraphProps<'a> { pub page_size: usize, } +impl<'a> ParagraphProps<'a> { + pub fn page_size(mut self, page_size: usize) -> Self { + self.page_size = page_size; + self + } + + pub fn text(mut self, text: &Text<'a>) -> Self { + self.content = text.clone(); + self + } + + pub fn focus(mut self, focus: bool) -> Self { + self.focus = focus; + self + } +} + impl<'a> Default for ParagraphProps<'a> { fn default() -> Self { Self { @@ -31,18 +51,22 @@ impl<'a> Default for ParagraphProps<'a> { } } -pub struct Paragraph<'a, A> { - /// Sending actions to the state store - pub action_tx: UnboundedSender, +pub struct Paragraph<'a, S, A> { + /// Internal properties + props: ParagraphProps<'a>, + /// Message sender + action_tx: UnboundedSender, + /// Custom update handler + on_update: Option>, + /// Additional custom event handler + on_change: Option>, /// Internal offset offset: usize, /// Internal progress progress: usize, - /// Internal properties - props: ParagraphProps<'a>, } -impl<'a, A> Paragraph<'a, A> { +impl<'a, S, A> Paragraph<'a, S, A> { pub fn scroll(&self) -> (u16, u16) { (self.offset as u16, 0) } @@ -116,8 +140,8 @@ impl<'a, A> Paragraph<'a, A> { } } -impl<'a, S, A> View for Paragraph<'a, A> { - fn new(state: &S, action_tx: UnboundedSender) -> Self +impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { + fn new(_state: &S, action_tx: UnboundedSender) -> Self where Self: Sized, { @@ -126,15 +150,27 @@ impl<'a, S, A> View for Paragraph<'a, A> { offset: 0, progress: 0, props: ParagraphProps::default(), + on_update: None, + on_change: None, } - .move_with_state(state) } - fn move_with_state(self, _state: &S) -> Self - where - Self: Sized, - { - Self { ..self } + fn on_change(mut self, callback: EventCallback) -> Self { + self.on_change = Some(callback); + self + } + + fn on_update(mut self, callback: UpdateCallback) -> Self { + self.on_update = Some(callback); + self + } + + fn update(&mut self, state: &S) { + if let Some(on_update) = self.on_update { + if let Some(props) = (on_update)(state).downcast_ref::>() { + self.props = props.clone(); + } + } } fn handle_key_event(&mut self, key: Key) { @@ -162,11 +198,15 @@ impl<'a, S, A> View for Paragraph<'a, A> { } _ => {} } + + if let Some(on_change) = self.on_change { + (on_change)(&self.props, self.action_tx.clone()); + } } } -impl<'a, A, B: Backend> Render for Paragraph<'a, A> { - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: ()) { +impl<'a: 'static, S, A, B: Backend> Widget for Paragraph<'a, S, A> { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: &dyn Any) { let block = Block::default() .borders(Borders::LEFT | Borders::RIGHT) .border_type(BorderType::Rounded)