From eb80772e8e500efb50a91ad175d49a9497edf262 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 9 May 2024 16:03:02 +0200 Subject: [PATCH] inbox: Factor out browser widget on browser page --- bin/commands/inbox/select.rs | 4 +- bin/commands/inbox/select/ui.rs | 230 ++++++++++++++++++++++---------- 2 files changed, 162 insertions(+), 72 deletions(-) diff --git a/bin/commands/inbox/select.rs b/bin/commands/inbox/select.rs index e3b2f7eb..899a2470 100644 --- a/bin/commands/inbox/select.rs +++ b/bin/commands/inbox/select.rs @@ -25,7 +25,7 @@ use tui::Exit; use tui::PageStack; -use self::ui::BrowsePage; +use self::ui::BrowserPage; use self::ui::HelpPage; use super::common::{Mode, RepositoryMode}; @@ -283,7 +283,7 @@ impl App { let window: Window = Window::new(&state, action_tx.clone()) .page( Page::Browse, - BrowsePage::new(&state, action_tx.clone()).to_boxed(), + BrowserPage::new(&state, action_tx.clone()).to_boxed(), ) .page( Page::Help, diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index 374d426a..f242a527 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -30,28 +30,44 @@ use super::{Action, State}; type BoxedWidget = widget::BoxedWidget; #[derive(Clone)] -struct BrowsePageProps<'a> { - notifications: Vec, - selected: Option, +pub struct BrowserProps<'a> { + /// Application mode: openation and id or id only. mode: Mode, + /// Table title + header: String, + /// Filtered notifications. + notifications: Vec, + /// Current (selected) table index + selected: Option, + /// Notification statistics. stats: HashMap, + /// Table columns columns: Vec>, + /// Max. width, before columns are cut-off. cutoff: usize, + /// Column index that marks where to cut. cutoff_after: usize, + /// Current page size (height of table content). page_size: usize, - search: String, + /// If search widget should be shown. show_search: bool, - shortcuts: Vec<(&'a str, &'a str)>, + /// Current search string. + search: String, } -impl<'a> From<&State> for BrowsePageProps<'a> { +impl<'a> From<&State> for BrowserProps<'a> { fn from(state: &State) -> Self { - let mut seen = 0; - let mut unseen = 0; + let header = match state.mode.repository() { + RepositoryMode::Contextual => state.project.name().to_string(), + RepositoryMode::All => "All repositories".to_string(), + RepositoryMode::ByRepo((_, name)) => name.clone().unwrap_or_default(), + }; let notifications = state.browser.notifications(); // Compute statistics + let mut seen = 0; + let mut unseen = 0; for notification in ¬ifications { if notification.seen { seen += 1; @@ -59,12 +75,13 @@ impl<'a> From<&State> for BrowsePageProps<'a> { unseen += 1; } } - let stats = HashMap::from([("Seen".to_string(), seen), ("Unseen".to_string(), unseen)]); Self { - notifications, mode: state.mode.clone(), + header, + notifications, + selected: state.browser.selected, stats, columns: [ Column::new("", Constraint::Length(5)), @@ -81,49 +98,32 @@ impl<'a> From<&State> for BrowsePageProps<'a> { .to_vec(), cutoff: 200, cutoff_after: 5, - search: state.browser.search.read(), page_size: state.browser.page_size, + search: state.browser.search.read(), show_search: state.browser.show_search, - selected: state.browser.selected, - shortcuts: match state.mode.selection() { - SelectionMode::Id => vec![("enter", "select"), ("/", "search")], - SelectionMode::Operation => vec![ - ("enter", "show"), - ("c", "clear"), - ("/", "search"), - ("?", "help"), - ], - }, } } } -impl<'a> Properties for BrowsePageProps<'a> {} +impl<'a> Properties for BrowserProps<'a> {} -pub struct BrowsePage<'a> { +pub struct Browser<'a> { /// Internal base base: BaseView, /// Internal props - props: BrowsePageProps<'a>, - /// Notifications widget + props: BrowserProps<'a>, + /// Notification widget notifications: BoxedWidget, /// Search widget search: BoxedWidget, - /// Shortcut widget - shortcuts: BoxedWidget, } -impl<'a: 'static> Widget for BrowsePage<'a> { +impl<'a: 'static> Widget for Browser<'a> { type Action = Action; type State = State; fn new(state: &State, action_tx: UnboundedSender) -> Self { - let props = BrowsePageProps::from(state); - let name = match state.mode.repository() { - RepositoryMode::Contextual => state.project.name().to_string(), - RepositoryMode::All => "All repositories".to_string(), - RepositoryMode::ByRepo((_, name)) => name.clone().unwrap_or_default(), - }; + let props = BrowserProps::from(state); Self { base: BaseView { @@ -138,7 +138,7 @@ impl<'a: 'static> Widget for BrowsePage<'a> { .columns( [ Column::new("", Constraint::Length(0)), - Column::new(Text::from(name), Constraint::Fill(1)), + Column::new(Text::from(props.header), Constraint::Fill(1)), ] .to_vec(), ) @@ -157,7 +157,7 @@ impl<'a: 'static> Widget for BrowsePage<'a> { }); }) .on_update(|state| { - let props = BrowsePageProps::from(state); + let props = BrowserProps::from(state); TableProps::default() .columns(props.columns) @@ -171,7 +171,7 @@ impl<'a: 'static> Widget for BrowsePage<'a> { .footer( Footer::new(state, action_tx.clone()) .on_update(|state| { - let props = BrowsePageProps::from(state); + let props = BrowserProps::from(state); FooterProps::default() .columns(browse_footer(&props, props.selected)) @@ -181,18 +181,11 @@ impl<'a: 'static> Widget for BrowsePage<'a> { ) .on_update(|state| { ContainerProps::default() - .hide_footer(BrowsePageProps::from(state).show_search) + .hide_footer(BrowserProps::from(state).show_search) .to_boxed() }) .to_boxed(), search: Search::new(state, action_tx.clone()).to_boxed(), - shortcuts: Shortcuts::new(state, action_tx.clone()) - .on_update(|state| { - ShortcutsProps::default() - .shortcuts(&BrowsePageProps::from(state).shortcuts) - .to_boxed() - }) - .to_boxed(), } } @@ -201,12 +194,6 @@ impl<'a: 'static> Widget for BrowsePage<'a> { self.search.handle_event(key); } else { match key { - Key::Esc | Key::Ctrl('c') => { - let _ = self.base.action_tx.send(Action::Exit { selection: None }); - } - Key::Char('?') => { - let _ = self.base.action_tx.send(Action::OpenHelp); - } Key::Char('/') => { let _ = self.base.action_tx.send(Action::OpenSearch); } @@ -247,19 +234,130 @@ impl<'a: 'static> Widget for BrowsePage<'a> { .ok() }); } - _ => { - self.notifications.handle_event(key); - } + _ => self.notifications.handle_event(key), } } } fn update(&mut self, state: &State) { - self.props = BrowsePageProps::from_callback(self.base.on_update, state) - .unwrap_or(BrowsePageProps::from(state)); + self.props = BrowserProps::from_callback(self.base.on_update, state) + .unwrap_or(BrowserProps::from(state)); self.notifications.update(state); self.search.update(state); + } + + fn render(&self, frame: &mut ratatui::Frame, props: RenderProps) { + if self.props.show_search { + let [table_area, search_area] = + Layout::vertical([Constraint::Min(1), Constraint::Length(2)]).areas(props.area); + + self.notifications + .render(frame, RenderProps::from(table_area)); + self.search + .render(frame, RenderProps::from(search_area).focus(true)); + } else { + self.notifications + .render(frame, RenderProps::from(props.area)); + } + } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } +} + +#[derive(Clone)] +struct BrowserPageProps<'a> { + /// Current page size (height of table content). + page_size: usize, + /// If this pages' keys should be handled (`false` if search is shown). + global_keys: bool, + /// This pages' shortcuts. + shortcuts: Vec<(&'a str, &'a str)>, +} + +impl<'a> From<&State> for BrowserPageProps<'a> { + fn from(state: &State) -> Self { + Self { + page_size: state.browser.page_size, + global_keys: !state.browser.show_search, + shortcuts: if state.browser.show_search { + vec![("esc", "cancel"), ("enter", "apply")] + } else { + match state.mode.selection() { + SelectionMode::Id => vec![("enter", "select"), ("/", "search")], + SelectionMode::Operation => vec![ + ("enter", "show"), + ("c", "clear"), + ("/", "search"), + ("?", "help"), + ], + } + }, + } + } +} + +impl<'a> Properties for BrowserPageProps<'a> {} + +pub struct BrowserPage<'a> { + /// Internal base + base: BaseView, + /// Internal props + props: BrowserPageProps<'a>, + /// Notifications widget + browser: BoxedWidget, + /// Shortcut widget + shortcuts: BoxedWidget, +} + +impl<'a: 'static> Widget for BrowserPage<'a> { + type Action = Action; + type State = State; + + fn new(state: &State, action_tx: UnboundedSender) -> Self { + let props = BrowserPageProps::from(state); + + Self { + base: BaseView { + action_tx: action_tx.clone(), + on_update: None, + on_event: None, + }, + props: props.clone(), + browser: Browser::new(state, action_tx.clone()).to_boxed(), + shortcuts: Shortcuts::new(state, action_tx.clone()) + .on_update(|state| { + ShortcutsProps::default() + .shortcuts(&BrowserPageProps::from(state).shortcuts) + .to_boxed() + }) + .to_boxed(), + } + } + + fn handle_event(&mut self, key: Key) { + self.browser.handle_event(key); + + if self.props.global_keys { + match key { + Key::Esc | Key::Ctrl('c') => { + let _ = self.base.action_tx.send(Action::Exit { selection: None }); + } + Key::Char('?') => { + let _ = self.base.action_tx.send(Action::OpenHelp); + } + _ => {} + } + } + } + + fn update(&mut self, state: &State) { + self.props = BrowserPageProps::from_callback(self.base.on_update, state) + .unwrap_or(BrowserPageProps::from(state)); + + self.browser.update(state); self.shortcuts.update(state); } @@ -269,19 +367,8 @@ impl<'a: 'static> Widget for BrowsePage<'a> { let [content_area, shortcuts_area] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(props.area); - if self.props.show_search { - let [table_area, search_area] = - Layout::vertical([Constraint::Min(1), Constraint::Length(2)]).areas(content_area); - - self.notifications - .render(frame, RenderProps::from(table_area)); - self.search - .render(frame, RenderProps::from(search_area).focus(true)); - } else { - self.notifications - .render(frame, RenderProps::from(content_area).focus(true)); - } - + self.browser + .render(frame, RenderProps::from(content_area).focus(true)); self.shortcuts .render(frame, RenderProps::from(shortcuts_area)); @@ -378,8 +465,11 @@ impl Widget for Search { #[derive(Clone)] struct HelpPageProps<'a> { + /// Current page size (height of table content). page_size: usize, + /// Scroll progress of help paragraph. help_progress: usize, + /// This pages' shortcuts. shortcuts: Vec<(&'a str, &'a str)>, } @@ -527,7 +617,7 @@ impl<'a: 'static> Widget for HelpPage<'a> { } } -fn browse_footer<'a>(props: &BrowsePageProps<'a>, selected: Option) -> Vec> { +fn browse_footer<'a>(props: &BrowserProps<'a>, selected: Option) -> Vec> { let search = Line::from(vec![ span::default(" Search ").cyan().dim().reversed(), span::default(" "),