From 2c9a9275457d9540e08fb47fb2091c044195c5ef Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 23 Apr 2024 17:37:36 +0200 Subject: [PATCH] patch: Cleanup base and props usage --- bin/commands/patch/select/ui.rs | 61 ++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 23 deletions(-) diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index f4da8cc7..911cffd7 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -122,10 +122,12 @@ impl<'a> From<&State> for BrowsePageProps<'a> { impl<'a: 'static> Properties for BrowsePageProps<'a> {} -pub struct BrowsePage<'a: 'static, B> { +pub struct BrowsePage<'a, B> { /// Internal base - base: BaseView>, - /// Patches widget + base: BaseView, + /// Internal props + props: BrowsePageProps<'a>, + /// Notifications widget patches: BoxedWidget, /// Search widget search: BoxedWidget, @@ -143,10 +145,10 @@ where Self { base: BaseView { action_tx: action_tx.clone(), - props: BrowsePageProps::from(state), on_update: None, on_event: None, }, + props: props.clone(), patches: Container::new(state, action_tx.clone()) .header( Header::new(state, action_tx.clone()) @@ -195,6 +197,10 @@ where } } + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } + fn on_update(mut self, callback: UpdateCallback) -> Self { self.base.on_update = Some(callback); self @@ -206,7 +212,7 @@ where } fn update(&mut self, state: &State) { - self.base.props = BrowsePageProps::from_callback(self.base.on_update, state) + self.props = BrowsePageProps::from_callback(self.base.on_update, state) .unwrap_or(BrowsePageProps::from(state)); self.patches.update(state); @@ -215,7 +221,7 @@ where } fn handle_key_event(&mut self, key: Key) { - if self.base.props.show_search { + if self.props.show_search { self.search.handle_key_event(key); } else { match key { @@ -229,15 +235,14 @@ where let _ = self.base.action_tx.send(Action::OpenSearch); } Key::Char('\n') => { - let operation = match self.base.props.mode { + let operation = match self.props.mode { Mode::Operation => Some(PatchOperation::Show.to_string()), Mode::Id => None, }; - self.base - .props + self.props .selected - .and_then(|selected| self.base.props.patches.get(selected)) + .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.base .action_tx @@ -252,10 +257,9 @@ where }); } Key::Char('c') => { - self.base - .props + self.props .selected - .and_then(|selected| self.base.props.patches.get(selected)) + .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.base .action_tx @@ -270,10 +274,9 @@ where }); } Key::Char('d') => { - self.base - .props + self.props .selected - .and_then(|selected| self.base.props.patches.get(selected)) + .and_then(|selected| self.props.patches.get(selected)) .and_then(|patch| { self.base .action_tx @@ -389,7 +392,7 @@ where fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(BrowsePageProps::from_boxed_any) - .unwrap_or(self.base.props.clone()); + .unwrap_or(self.props.clone()); let page_size = area.height.saturating_sub(6) as usize; @@ -444,7 +447,9 @@ impl Properties for SearchProps {} pub struct Search { /// Internal base - base: BaseView, + base: BaseView, + /// Internal props + _props: SearchProps, /// Search input field input: BoxedWidget, } @@ -475,14 +480,18 @@ impl View for Search { Self { base: BaseView { action_tx: action_tx.clone(), - props: SearchProps {}, on_update: None, on_event: None, }, + _props: SearchProps {}, input, } } + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } + fn on_update(mut self, callback: UpdateCallback) -> Self { self.base.on_update = Some(callback); self @@ -551,7 +560,9 @@ where B: Backend, { /// Internal base - base: BaseView>, + base: BaseView, + /// Internal props + props: HelpPageProps<'a>, /// Content widget content: BoxedWidget, /// Shortcut widget @@ -569,10 +580,10 @@ where Self { base: BaseView { action_tx: action_tx.clone(), - props: HelpPageProps::from(state), on_update: None, on_event: None, }, + props: HelpPageProps::from(state), content: Container::new(state, action_tx.clone()) .header( Header::new(state, action_tx.clone()) @@ -635,6 +646,10 @@ where } } + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } + fn on_update(mut self, callback: UpdateCallback) -> Self { self.base.on_update = Some(callback); self @@ -646,7 +661,7 @@ where } fn update(&mut self, state: &State) { - self.base.props = HelpPageProps::from_callback(self.base.on_update, state) + self.props = HelpPageProps::from_callback(self.base.on_update, state) .unwrap_or(HelpPageProps::from(state)); self.content.update(state); @@ -674,7 +689,7 @@ where fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(HelpPageProps::from_boxed_any) - .unwrap_or(self.base.props.clone()); + .unwrap_or(self.props.clone()); let page_size = area.height.saturating_sub(6) as usize;