From bfd0ecf993cd74254e41094f84c809efae87d54e Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 22 Apr 2024 10:43:53 +0200 Subject: [PATCH] bin: Improve update callback usage --- bin/commands/inbox/select/ui.rs | 45 +++++++++++++++++---------------- bin/commands/issue/select/ui.rs | 45 +++++++++++++++++---------------- bin/commands/patch/select/ui.rs | 45 +++++++++++++++++---------------- 3 files changed, 69 insertions(+), 66 deletions(-) diff --git a/bin/commands/inbox/select/ui.rs b/bin/commands/inbox/select/ui.rs index 3de1c9fa..76a1022a 100644 --- a/bin/commands/inbox/select/ui.rs +++ b/bin/commands/inbox/select/ui.rs @@ -57,7 +57,7 @@ pub struct Window { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// All pages known pages: HashMap>, } @@ -84,7 +84,7 @@ where ), ]), on_update: None, - on_change: None, + on_event: None, } } @@ -93,8 +93,8 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -211,7 +211,7 @@ struct BrowsePage<'a, B> { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Notifications widget notifications: BoxedWidget, /// Search widget @@ -251,7 +251,7 @@ where ) .content(Box::>::new( Table::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::Select { @@ -287,12 +287,12 @@ where search: Search::new(state, action_tx.clone()).to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -302,8 +302,8 @@ where } fn update(&mut self, state: &State) { - // TODO call mapper here instead? - self.props = BrowsePageProps::from(state); + self.props = BrowsePageProps::from_callback(self.on_update, state) + .unwrap_or(BrowsePageProps::from(state)); self.notifications.update(state); self.search.update(state); @@ -504,7 +504,7 @@ pub struct Search { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Search input field input: BoxedWidget, } @@ -515,7 +515,7 @@ impl View for Search { Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::UpdateSearch { @@ -536,7 +536,7 @@ impl View for Search { action_tx, input, on_update: None, - on_change: None, + on_event: None, } } @@ -545,8 +545,8 @@ impl View for Search { self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -614,7 +614,7 @@ where /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Content widget content: BoxedWidget, /// Shortcut widget @@ -656,7 +656,7 @@ where .focus(props.focus) .to_boxed() }) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::ScrollHelp { @@ -692,7 +692,7 @@ where .to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } @@ -701,13 +701,14 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } fn update(&mut self, state: &State) { - self.props = HelpPageProps::from(state); + self.props = HelpPageProps::from_callback(self.on_update, state) + .unwrap_or(HelpPageProps::from(state)); self.content.update(state); } diff --git a/bin/commands/issue/select/ui.rs b/bin/commands/issue/select/ui.rs index c47e196f..e59aebb6 100644 --- a/bin/commands/issue/select/ui.rs +++ b/bin/commands/issue/select/ui.rs @@ -60,7 +60,7 @@ pub struct Window { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// All pages known pages: HashMap>, } @@ -87,7 +87,7 @@ where ), ]), on_update: None, - on_change: None, + on_event: None, } } @@ -96,8 +96,8 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -225,7 +225,7 @@ struct BrowsePage<'a, B> { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Patches widget issues: BoxedWidget, /// Search widget @@ -254,7 +254,7 @@ where ) .content(Box::>::new( Table::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::Select { @@ -290,7 +290,7 @@ where search: Search::new(state, action_tx.clone()).to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } @@ -299,14 +299,14 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } fn update(&mut self, state: &State) { - // TODO call mapper here instead? - self.props = BrowsePageProps::from(state); + self.props = BrowsePageProps::from_callback(self.on_update, state) + .unwrap_or(BrowsePageProps::from(state)); self.issues.update(state); self.search.update(state); @@ -529,7 +529,7 @@ pub struct Search { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Search input field input: BoxedWidget, } @@ -540,7 +540,7 @@ impl View for Search { Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::UpdateSearch { @@ -561,7 +561,7 @@ impl View for Search { action_tx, input, on_update: None, - on_change: None, + on_event: None, } } @@ -570,8 +570,8 @@ impl View for Search { self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -639,7 +639,7 @@ where /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Content widget content: BoxedWidget, /// Shortcut widget @@ -681,7 +681,7 @@ where .focus(props.focus) .to_boxed() }) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::ScrollHelp { @@ -717,7 +717,7 @@ where .to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } @@ -726,13 +726,14 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } fn update(&mut self, state: &State) { - self.props = HelpPageProps::from(state); + self.props = HelpPageProps::from_callback(self.on_update, state) + .unwrap_or(HelpPageProps::from(state)); self.content.update(state); } diff --git a/bin/commands/patch/select/ui.rs b/bin/commands/patch/select/ui.rs index cf52a6ba..2966c885 100644 --- a/bin/commands/patch/select/ui.rs +++ b/bin/commands/patch/select/ui.rs @@ -62,7 +62,7 @@ pub struct Window { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// All pages known pages: HashMap>, } @@ -89,7 +89,7 @@ where ), ]), on_update: None, - on_change: None, + on_event: None, } } @@ -98,8 +98,8 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -226,7 +226,7 @@ struct BrowsePage<'a, B> { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Patches widget patches: BoxedWidget, /// Search widget @@ -255,7 +255,7 @@ where ) .content(Box::>::new( Table::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::Select { @@ -291,7 +291,7 @@ where search: Search::new(state, action_tx.clone()).to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } @@ -300,14 +300,14 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } fn update(&mut self, state: &State) { - // TODO call mapper here instead? - self.props = BrowsePageProps::from(state); + self.props = BrowsePageProps::from_callback(self.on_update, state) + .unwrap_or(BrowsePageProps::from(state)); self.patches.update(state); self.search.update(state); @@ -561,7 +561,7 @@ pub struct Search { /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Search input field input: BoxedWidget, } @@ -572,7 +572,7 @@ impl View for Search { Self: Sized, { let input = TextField::new(state, action_tx.clone()) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::UpdateSearch { @@ -593,7 +593,7 @@ impl View for Search { action_tx, input, on_update: None, - on_change: None, + on_event: None, } } @@ -602,8 +602,8 @@ impl View for Search { self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } @@ -671,7 +671,7 @@ where /// Custom update handler on_update: Option>, /// Additional custom event handler - on_change: Option>, + on_event: Option>, /// Content widget content: BoxedWidget, /// Shortcut widget @@ -713,7 +713,7 @@ where .focus(props.focus) .to_boxed() }) - .on_change(|state, action_tx| { + .on_event(|state, action_tx| { state.downcast_ref::().and_then(|state| { action_tx .send(Action::ScrollHelp { @@ -749,7 +749,7 @@ where .to_boxed(), shortcuts: Shortcuts::new(state, action_tx.clone()).to_boxed(), on_update: None, - on_change: None, + on_event: None, } } @@ -758,13 +758,14 @@ where self } - fn on_change(mut self, callback: EventCallback) -> Self { - self.on_change = Some(callback); + fn on_event(mut self, callback: EventCallback) -> Self { + self.on_event = Some(callback); self } fn update(&mut self, state: &State) { - self.props = HelpPageProps::from(state); + self.props = HelpPageProps::from_callback(self.on_update, state) + .unwrap_or(HelpPageProps::from(state)); self.content.update(state); }