From 97659849be7d855aad0ab4525ea69f0b6b664ff0 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Tue, 23 Apr 2024 17:37:42 +0200 Subject: [PATCH] inbox: Cleanup base and props usage --- bin/commands/inbox/select/ui.rs | 52 +++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index 68dc475d..2a82b183 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -109,7 +109,9 @@ impl<'a> Properties for BrowsePageProps<'a> {} pub struct BrowsePage<'a, B> { /// Internal base - base: BaseView>, + base: BaseView, + /// Internal props + props: BrowsePageProps<'a>, /// Notifications widget notifications: BoxedWidget, /// Search widget @@ -133,10 +135,10 @@ where Self { base: BaseView { action_tx: action_tx.clone(), - props: props.clone(), on_update: None, on_event: None, }, + props: props.clone(), notifications: Container::new(state, action_tx.clone()) .header( Header::new(state, action_tx.clone()) @@ -191,6 +193,10 @@ where } } + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } + fn on_event(mut self, callback: EventCallback) -> Self { self.base.on_event = Some(callback); self @@ -202,7 +208,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.notifications.update(state); @@ -211,7 +217,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 { @@ -225,12 +231,11 @@ where let _ = self.base.action_tx.send(Action::OpenSearch); } Key::Char('\n') => { - self.base - .props + self.props .selected - .and_then(|selected| self.base.props.notifications.get(selected)) + .and_then(|selected| self.props.notifications.get(selected)) .and_then(|notif| { - let selection = match self.base.props.mode.selection() { + let selection = match self.props.mode.selection() { SelectionMode::Operation => Selection::default() .with_operation(InboxOperation::Show.to_string()) .with_id(notif.id), @@ -246,10 +251,9 @@ where }); } Key::Char('c') => { - self.base - .props + self.props .selected - .and_then(|selected| self.base.props.notifications.get(selected)) + .and_then(|selected| self.props.notifications.get(selected)) .and_then(|notif| { self.base .action_tx @@ -341,7 +345,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; @@ -396,7 +400,9 @@ impl Properties for SearchProps {} pub struct Search { /// Internal base - base: BaseView, + base: BaseView, + /// Internal props + _props: SearchProps, /// Search input field input: BoxedWidget, } @@ -427,14 +433,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 @@ -503,7 +513,9 @@ where B: Backend, { /// Internal base - base: BaseView>, + base: BaseView, + /// Internal props + props: HelpPageProps<'a>, /// Content widget content: BoxedWidget, /// Shortcut widget @@ -521,10 +533,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()) @@ -587,6 +599,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 @@ -598,7 +614,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); @@ -626,7 +642,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;