Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Improve update callback
Erik Kundt committed 2 years ago
commit 0ef95e71d27879f7b72071c29c595cd0e9ab10ee
parent 4f29b65060aa9c23fefe4a8eebdc4cdf5ae69464
4 files changed +48 -30
modified src/ui/widget.rs
@@ -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() {
modified src/ui/widget/container.rs
@@ -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) {
modified src/ui/widget/input.rs
@@ -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) {
modified src/ui/widget/text.rs
@@ -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) {