lib: Improve update callback

This commit is contained in:
Erik Kundt
2024-04-17 15:34:20 +02:00
parent 4f29b65060
commit 0ef95e71d2
4 changed files with 48 additions and 30 deletions
+16 -10
View File
@@ -153,11 +153,14 @@ impl<S, A> View<S, A> for Shortcuts<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::<ShortcutsProps>() {
self.props.shortcuts = props.shortcuts.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<ShortcutsProps>()
.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::<TableProps<'_, R>>() {
self.props = props.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<TableProps<'_, R>>()
.map(|props| props.clone())
})
.unwrap_or(self.props.clone());
// TODO: Move to state reducer
if let Some(selected) = self.state.selected() {
+16 -10
View File
@@ -102,11 +102,14 @@ impl<'a: 'static, S, A> View<S, A> 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::<HeaderProps>() {
self.props = props.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<HeaderProps>()
.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<S, A> 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::<FooterProps>() {
self.props = props.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<FooterProps>()
.map(|props| props.clone())
})
.unwrap_or(self.props.clone());
}
fn handle_key_event(&mut self, _key: Key) {
+8 -5
View File
@@ -151,11 +151,14 @@ impl<S, A> View<S, A> for TextField<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::<TextFieldProps>() {
self.props = props.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<TextFieldProps>()
.map(|props| props.clone())
})
.unwrap_or(self.props.clone());
}
fn handle_key_event(&mut self, key: Key) {
+8 -5
View File
@@ -168,11 +168,14 @@ impl<'a: 'static, S, A> View<S, A> 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::<ParagraphProps>() {
self.props = props.clone();
}
}
self.props = self
.on_update
.and_then(|on_update| {
(on_update)(state)
.downcast_ref::<ParagraphProps>()
.map(|props| props.clone())
})
.unwrap_or(self.props.clone());
}
fn handle_key_event(&mut self, key: Key) {