From 0ef95e71d27879f7b72071c29c595cd0e9ab10ee Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 17 Apr 2024 15:34:20 +0200 Subject: [PATCH] lib: Improve update callback --- src/ui/widget.rs | 26 ++++++++++++++++---------- src/ui/widget/container.rs | 26 ++++++++++++++++---------- src/ui/widget/input.rs | 13 ++++++++----- src/ui/widget/text.rs | 13 ++++++++----- 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index c3a45da1..6b4a39e6 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -153,11 +153,14 @@ impl View for Shortcuts { } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props.shortcuts = props.shortcuts.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()) } } @@ -407,11 +410,14 @@ where } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::>() { - self.props = props.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::>() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()); // TODO: Move to state reducer if let Some(selected) = self.state.selected() { diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 01374ea5..82a151ee 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -102,11 +102,14 @@ impl<'a: 'static, S, A> View for Header<'a, S, A> { } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props = props.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()); } fn handle_key_event(&mut self, _key: Key) { @@ -267,11 +270,14 @@ impl<'a: 'static, S, A> View for Footer<'a, S, A> { } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props = props.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()); } fn handle_key_event(&mut self, _key: Key) { diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 95a5bf76..a3ec3cc7 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -151,11 +151,14 @@ impl View for TextField { } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props = props.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()); } fn handle_key_event(&mut self, key: Key) { diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index fe101164..70b1c001 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -168,11 +168,14 @@ impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { } fn update(&mut self, state: &S) { - if let Some(on_update) = self.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props = props.clone(); - } - } + self.props = self + .on_update + .and_then(|on_update| { + (on_update)(state) + .downcast_ref::() + .map(|props| props.clone()) + }) + .unwrap_or(self.props.clone()); } fn handle_key_event(&mut self, key: Key) {