Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Add default impl for callback builders
Erik Kundt committed 2 years ago
commit 5c8ecd5f0a1983e01a42f2f608ebf9c5a7a11a7b
parent c5deca62471c5f0b36051fb8f6ed6603303be16e
4 files changed +16 -89
modified src/ui/widget.rs
@@ -43,14 +43,23 @@ pub trait View<S, A> {
        Self: Sized;

    /// Should set the optional custom event handler.
-
    fn on_event(self, callback: EventCallback<A>) -> Self
+
    fn on_event(mut self, callback: EventCallback<A>) -> Self
    where
-
        Self: Sized;
+
        Self: Sized,
+
    {
+
        self.base_mut().on_event = Some(callback);
+
        self
+
    }

    /// Should set the optional update handler.
-
    fn on_update(self, callback: UpdateCallback<S>) -> Self
+
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self
    where
-
        Self: Sized;
+
        Self: Sized,
+

+
    {
+
        self.base_mut().on_update = Some(callback);
+
        self
+
    }

    /// Returns a boxed `View`
    fn to_boxed(self) -> Box<Self>
@@ -193,16 +202,6 @@ where
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props =
            WindowProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());
@@ -327,16 +326,6 @@ impl<S, A> View<S, A> for Shortcuts<S, A> {
        &mut self.base
    }

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

    fn handle_key_event(&mut self, _key: Key) {}

    fn update(&mut self, state: &S) {
@@ -571,16 +560,6 @@ where
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props = TableProps::<'_, R>::from_callback(self.base.on_update, state)
            .unwrap_or(self.props.clone());
modified src/ui/widget/container.rs
@@ -11,9 +11,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;

-
use super::{
-
    BaseView, BoxedWidget, Column, EventCallback, Properties, UpdateCallback, View, Widget,
-
};
+
use super::{BaseView, BoxedWidget, Column, Properties, View, Widget};

#[derive(Clone, Debug)]
pub struct HeaderProps<'a> {
@@ -95,16 +93,6 @@ impl<'a: 'static, S, A> View<S, A> for Header<'a, S, A> {
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props = self
            .base
@@ -262,16 +250,6 @@ impl<'a: 'static, S, A> View<S, A> for Footer<'a, S, A> {
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props = self
            .base
@@ -434,16 +412,6 @@ where
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props =
            ContainerProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());
modified src/ui/widget/input.rs
@@ -9,7 +9,7 @@ use ratatui::prelude::{Backend, Rect};
use ratatui::style::Stylize;
use ratatui::text::{Line, Span};

-
use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget};
+
use super::{BaseView, Properties, View, Widget};

#[derive(Clone)]
pub struct TextFieldProps {
@@ -146,16 +146,6 @@ impl<S, A> View<S, A> for TextField<S, A> {
        &mut self.base
    }

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        if let Some(on_update) = self.base.on_update {
            if let Some(props) = (on_update)(state).downcast_ref::<TextFieldProps>() {
modified src/ui/widget/text.rs
@@ -8,7 +8,7 @@ use ratatui::backend::Backend;
use ratatui::layout::{Constraint, Layout, Rect};
use ratatui::text::Text;

-
use super::{BaseView, EventCallback, Properties, UpdateCallback, View, Widget};
+
use super::{BaseView, Properties, View, Widget};

#[derive(Clone)]
pub struct ParagraphProps<'a> {
@@ -165,16 +165,6 @@ impl<'a: 'static, S, A> View<S, A> for Paragraph<'a, S, A> {
        &mut self.base
    }

-
    fn on_event(mut self, callback: EventCallback<A>) -> Self {
-
        self.base.on_event = Some(callback);
-
        self
-
    }
-

-
    fn on_update(mut self, callback: UpdateCallback<S>) -> Self {
-
        self.base.on_update = Some(callback);
-
        self
-
    }
-

    fn update(&mut self, state: &S) {
        self.props =
            ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone());