From f965993b35b9f91a1fa93ad52e6676ce4e0bd8b8 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Thu, 9 May 2024 15:06:08 +0200 Subject: [PATCH] patch: Use render properties /w focus --- bin/commands/patch/select/ui.rs | 85 ++++++++++++++------------------- 1 file changed, 35 insertions(+), 50 deletions(-) diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index 1e0b0fe3..f832b777 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -1,4 +1,3 @@ -use std::any::Any; use std::collections::HashMap; use std::str::FromStr; use std::vec; @@ -23,7 +22,7 @@ use tui::ui::widget::container::{ }; use tui::ui::widget::input::{TextField, TextFieldProps, TextFieldState}; use tui::ui::widget::text::{Paragraph, ParagraphProps, ParagraphState}; -use tui::ui::widget::{self, BaseView}; +use tui::ui::widget::{self, BaseView, RenderProps}; use tui::ui::widget::{Column, Properties, Shortcuts, ShortcutsProps, Table, TableProps, Widget}; use tui::ui::widget::{TableUtils, WidgetState}; use tui::Selection; @@ -45,7 +44,6 @@ pub struct BrowsePageProps<'a> { columns: Vec>, cutoff: usize, cutoff_after: usize, - focus: bool, page_size: usize, show_search: bool, shortcuts: Vec<(&'a str, &'a str)>, @@ -97,7 +95,6 @@ impl<'a> From<&State> for BrowsePageProps<'a> { .to_vec(), cutoff: 150, cutoff_after: 5, - focus: false, stats, page_size: state.browser.page_size, show_search: state.browser.show_search, @@ -150,7 +147,6 @@ impl<'a: 'static> Widget for BrowsePage<'a> { Header::new(state, action_tx.clone()) .columns(props.columns.clone()) .cutoff(props.cutoff, props.cutoff_after) - .focus(props.focus) .to_boxed(), ) .content(Box::>::new( @@ -187,9 +183,20 @@ impl<'a: 'static> Widget for BrowsePage<'a> { }) .to_boxed(), ) + .on_update(|state| { + ContainerProps::default() + .hide_footer(BrowsePageProps::from(state).show_search) + .to_boxed() + }) .to_boxed(), search: Search::new(state, action_tx.clone()).to_boxed(), - shortcuts: Shortcuts::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(), } } @@ -279,41 +286,28 @@ impl<'a: 'static> Widget for BrowsePage<'a> { self.shortcuts.update(state); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let page_size = area.height.saturating_sub(6) as usize; let [content_area, shortcuts_area] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(area); - if props.show_search { + if self.props.show_search { let [table_area, search_area] = Layout::vertical([Constraint::Min(1), Constraint::Length(2)]).areas(content_area); - self.patches.render( - frame, - table_area, - Some(&ContainerProps::default().hide_footer(props.show_search)), - ); - self.search.render(frame, search_area, None); + self.patches + .render(frame, table_area, Some(RenderProps::default())); + self.search + .render(frame, search_area, Some(RenderProps::focused())); } else { - self.patches.render( - frame, - content_area, - Some(&ContainerProps::default().hide_footer(props.show_search)), - ); + self.patches + .render(frame, content_area, Some(RenderProps::focused())); } - self.shortcuts.render( - frame, - shortcuts_area, - Some(&ShortcutsProps::default().shortcuts(&props.shortcuts)), - ); + self.shortcuts.render(frame, shortcuts_area, None); - if page_size != props.page_size { + if page_size != self.props.page_size { let _ = self.base.action_tx.send(Action::BrowserPageSize(page_size)); } } @@ -391,7 +385,7 @@ impl Widget for Search { self.input.update(state); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option<&dyn Any>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let layout = Layout::horizontal(Constraint::from_mins([0])) .horizontal_margin(1) .split(area); @@ -406,7 +400,6 @@ impl Widget for Search { #[derive(Clone)] pub struct HelpPageProps<'a> { - focus: bool, page_size: usize, help_progress: usize, shortcuts: Vec<(&'a str, &'a str)>, @@ -415,7 +408,6 @@ pub struct HelpPageProps<'a> { impl<'a> From<&State> for HelpPageProps<'a> { fn from(state: &State) -> Self { Self { - focus: false, page_size: state.help.page_size, help_progress: state.help.progress, shortcuts: vec![("?", "close")], @@ -454,12 +446,9 @@ impl<'a: 'static> Widget for HelpPage<'a> { content: Container::new(state, action_tx.clone()) .header( Header::new(state, action_tx.clone()) - .on_update(|state| { - let props = HelpPageProps::from(state); - + .on_update(|_| { HeaderProps::default() .columns([Column::new(" Help ", Constraint::Fill(1))].to_vec()) - .focus(props.focus) .to_boxed() }) .to_boxed(), @@ -472,7 +461,6 @@ impl<'a: 'static> Widget for HelpPage<'a> { ParagraphProps::default() .text(&help_text()) .page_size(props.page_size) - .focus(props.focus) .to_boxed() }) .on_event(|paragraph, action_tx| { @@ -503,13 +491,18 @@ impl<'a: 'static> Widget for HelpPage<'a> { ] .to_vec(), ) - .focus(props.focus) .to_boxed() }) .to_boxed(), ) .to_boxed(), - shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), + shortcuts: Shortcuts::new(state, action_tx.clone()) + .on_update(|state| { + ShortcutsProps::default() + .shortcuts(&HelpPageProps::from(state).shortcuts) + .to_boxed() + }) + .to_boxed(), } } @@ -534,24 +527,16 @@ impl<'a: 'static> Widget for HelpPage<'a> { self.content.update(state); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let page_size = area.height.saturating_sub(6) as usize; let [content_area, shortcuts_area] = Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(area); self.content.render(frame, content_area, None); - self.shortcuts.render( - frame, - shortcuts_area, - Some(&ShortcutsProps::default().shortcuts(&props.shortcuts)), - ); + self.shortcuts.render(frame, shortcuts_area, None); - if page_size != props.page_size { + if page_size != self.props.page_size { let _ = self.base.action_tx.send(Action::HelpPageSize(page_size)); } }