Radish alpha
r
rad:z39mP9rQAaGmERfUMPULfPUi473tY
Radicle terminal user interface
Radicle
Git
Introduce `BoxedAny` trait
Merged did:key:z6MkswQE...2C1V opened 1 year ago
9 files changed +37 -26 780dbaf9 f5102451
modified bin/commands/inbox/select/ui.rs
@@ -23,7 +23,7 @@ use tui::ui::widget::input::{TextField, TextFieldProps, TextFieldState};
use tui::ui::widget::list::{Table, TableProps, TableUtils};
use tui::ui::widget::text::{Paragraph, ParagraphProps, ParagraphState};
use tui::ui::widget::window::{Shortcuts, ShortcutsProps};
-
use tui::ui::widget::{BaseView, Properties, RenderProps, Widget, WidgetState};
+
use tui::ui::widget::{BaseView, BoxedAny, Properties, RenderProps, Widget};

use tui::Selection;

@@ -110,6 +110,7 @@ impl<'a> From<&State> for BrowserProps<'a> {
}

impl<'a> Properties for BrowserProps<'a> {}
+
impl<'a> BoxedAny for BrowserProps<'a> {}

pub struct Browser<'a> {
    /// Internal base
@@ -301,6 +302,7 @@ impl<'a> From<&State> for BrowserPageProps<'a> {
}

impl<'a> Properties for BrowserPageProps<'a> {}
+
impl<'a> BoxedAny for BrowserPageProps<'a> {}

pub struct BrowserPage<'a> {
    /// Internal base
@@ -497,6 +499,7 @@ impl<'a> From<&State> for HelpPageProps<'a> {
}

impl<'a> Properties for HelpPageProps<'a> {}
+
impl<'a> BoxedAny for HelpPageProps<'a> {}

pub struct HelpPage<'a> {
    /// Internal base
modified bin/commands/issue/select/ui.rs
@@ -24,7 +24,7 @@ use tui::ui::widget::input::{TextField, TextFieldProps, TextFieldState};
use tui::ui::widget::list::{Table, TableProps, TableUtils};
use tui::ui::widget::text::{Paragraph, ParagraphProps, ParagraphState};
use tui::ui::widget::window::{Shortcuts, ShortcutsProps};
-
use tui::ui::widget::{BaseView, Properties, RenderProps, Widget, WidgetState};
+
use tui::ui::widget::{BaseView, BoxedAny, Properties, RenderProps, Widget};

use tui::Selection;

@@ -116,6 +116,7 @@ impl<'a> From<&State> for BrowsePageProps<'a> {
}

impl<'a> Properties for BrowsePageProps<'a> {}
+
impl<'a> BoxedAny for BrowsePageProps<'a> {}

pub struct BrowsePage<'a> {
    /// Internal base
@@ -401,6 +402,7 @@ impl<'a> From<&State> for HelpPageProps<'a> {
}

impl<'a> Properties for HelpPageProps<'a> {}
+
impl<'a> BoxedAny for HelpPageProps<'a> {}

pub struct HelpPage<'a> {
    /// Internal base
modified bin/commands/patch/select/ui.rs
@@ -25,7 +25,7 @@ use tui::ui::widget::input::{TextField, TextFieldProps, TextFieldState};
use tui::ui::widget::list::{Table, TableProps, TableUtils};
use tui::ui::widget::text::{Paragraph, ParagraphProps, ParagraphState};
use tui::ui::widget::window::{Shortcuts, ShortcutsProps};
-
use tui::ui::widget::{BaseView, Properties, RenderProps, Widget, WidgetState};
+
use tui::ui::widget::{BaseView, BoxedAny, Properties, RenderProps, Widget};

use tui::Selection;

@@ -116,6 +116,7 @@ impl<'a> From<&State> for BrowsePageProps<'a> {
}

impl<'a: 'static> Properties for BrowsePageProps<'a> {}
+
impl<'a: 'static> BoxedAny for BrowsePageProps<'a> {}

pub struct BrowsePage<'a> {
    /// Internal base
@@ -418,6 +419,7 @@ impl<'a> From<&State> for HelpPageProps<'a> {
}

impl<'a> Properties for HelpPageProps<'a> {}
+
impl<'a> BoxedAny for HelpPageProps<'a> {}

pub struct HelpPage<'a> {
    /// Internal base
modified src/ui/widget.rs
@@ -144,16 +144,9 @@ pub trait Properties {
        Box::new(self)
    }

-
    fn from_boxed_any(any: Box<dyn Any>) -> Option<Self>
-
    where
-
        Self: Sized + Clone + 'static,
-
    {
-
        any.downcast_ref::<Self>().cloned()
-
    }
-

    fn from_callback<S>(callback: Option<UpdateCallback<S>>, state: &S) -> Option<Self>
    where
-
        Self: Sized + Clone + 'static,
+
        Self: Sized + Clone + 'static + BoxedAny,
    {
        callback
            .map(|callback| (callback)(state))
@@ -161,18 +154,19 @@ pub trait Properties {
    }
}

-
pub trait WidgetState {
-
    fn to_boxed_any(self) -> Box<dyn Any>
+
/// Provide default implementations for conversions to and from `Box<dyn Any>`.
+
pub trait BoxedAny {
+
    fn from_boxed_any(any: Box<dyn Any>) -> Option<Self>
    where
        Self: Sized + Clone + 'static,
    {
-
        Box::new(self)
+
        any.downcast_ref::<Self>().cloned()
    }

-
    fn from_boxed_any(any: Box<dyn Any>) -> Option<Self>
+
    fn to_boxed_any(self) -> Box<dyn Any>
    where
        Self: Sized + Clone + 'static,
    {
-
        any.downcast_ref::<Self>().cloned()
+
        Box::new(self)
    }
-
}
+
}

\ No newline at end of file
modified src/ui/widget/container.rs
@@ -10,7 +10,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;

-
use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget, WidgetState};
+
use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget};

#[derive(Clone, Debug)]
pub struct Column<'a> {
@@ -65,6 +65,7 @@ impl<'a> Default for HeaderProps<'a> {
}

impl<'a: 'static> Properties for HeaderProps<'a> {}
+
impl<'a: 'static> BoxedAny for HeaderProps<'a> {}

pub struct Header<'a: 'static, S, A> {
    /// Internal props
@@ -205,6 +206,7 @@ impl<'a> Default for FooterProps<'a> {
}

impl<'a: 'static> Properties for FooterProps<'a> {}
+
impl<'a: 'static> BoxedAny for FooterProps<'a> {}

pub struct Footer<'a, S, A> {
    /// Internal props
@@ -325,6 +327,7 @@ impl ContainerProps {
}

impl Properties for ContainerProps {}
+
impl BoxedAny for ContainerProps {}

pub struct Container<S, A> {
    /// Internal base
@@ -459,7 +462,7 @@ pub struct SectionGroupState {
    focus: Option<usize>,
}

-
impl WidgetState for SectionGroupState {}
+
impl BoxedAny for SectionGroupState {}

#[derive(Clone, Default)]
pub struct SectionGroupProps {
@@ -475,6 +478,7 @@ impl SectionGroupProps {
}

impl Properties for SectionGroupProps {}
+
impl BoxedAny for SectionGroupProps {}

pub struct SectionGroup<S, A> {
    /// Internal base
modified src/ui/widget/input.rs
@@ -6,7 +6,7 @@ use ratatui::layout::{Constraint, Layout};
use ratatui::style::Stylize;
use ratatui::text::{Line, Span};

-
use super::{BaseView, Properties, RenderProps, Widget, WidgetState};
+
use super::{BaseView, BoxedAny, Properties, RenderProps, Widget};

#[derive(Clone)]
pub struct TextFieldProps {
@@ -54,7 +54,7 @@ pub struct TextFieldState {
    pub cursor_position: usize,
}

-
impl WidgetState for TextFieldState {}
+
impl BoxedAny for TextFieldState {}

pub struct TextField<S, A> {
    /// Internal base
modified src/ui/widget/list.rs
@@ -13,7 +13,8 @@ use ratatui::widgets::TableState;
use crate::ui::theme::style;
use crate::ui::{layout, span};

-
use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget, WidgetState};
+
use super::BoxedAny;
+
use super::{container::Column, BaseView, Properties, RenderProps, ToRow, Widget};

#[derive(Clone, Debug)]
pub struct TableProps<'a, R, const W: usize>
@@ -83,7 +84,9 @@ where
}

impl<'a: 'static, R, const W: usize> Properties for TableProps<'a, R, W> where R: ToRow<W> + 'static {}
-
impl WidgetState for TableState {}
+
impl<'a: 'static, R, const W: usize> BoxedAny for TableProps<'a, R, W> where R: ToRow<W> + 'static {}
+

+
impl BoxedAny for TableState {}

pub struct Table<'a, S, A, R, const W: usize>
where
modified src/ui/widget/text.rs
@@ -5,7 +5,7 @@ use termion::event::Key;
use ratatui::layout::{Constraint, Layout};
use ratatui::text::Text;

-
use super::{BaseView, Properties, RenderProps, Widget, WidgetState};
+
use super::{BaseView, BoxedAny, Properties, RenderProps, Widget};

#[derive(Clone)]
pub struct ParagraphProps<'a> {
@@ -41,6 +41,7 @@ impl<'a> Default for ParagraphProps<'a> {
}

impl<'a: 'static> Properties for ParagraphProps<'a> {}
+
impl<'a: 'static> BoxedAny for ParagraphProps<'a> {}

#[derive(Clone)]
pub struct ParagraphState {
@@ -50,7 +51,7 @@ pub struct ParagraphState {
    pub progress: usize,
}

-
impl WidgetState for ParagraphState {}
+
impl BoxedAny for ParagraphState {}

pub struct Paragraph<'a, S, A> {
    /// Internal base
modified src/ui/widget/window.rs
@@ -12,7 +12,7 @@ use ratatui::widgets::Row;

use crate::ui::theme::style;

-
use super::{BaseView, BoxedWidget, Properties, RenderProps, Widget};
+
use super::{BaseView, BoxedAny, BoxedWidget, Properties, RenderProps, Widget};

#[derive(Clone)]
pub struct WindowProps<Id> {
@@ -33,6 +33,7 @@ impl<Id> Default for WindowProps<Id> {
}

impl<Id> Properties for WindowProps<Id> {}
+
impl<Id> BoxedAny for WindowProps<Id> {}

pub struct Window<S, A, Id> {
    /// Internal base
@@ -152,6 +153,7 @@ impl Default for ShortcutsProps {
}

impl Properties for ShortcutsProps {}
+
impl BoxedAny for ShortcutsProps {}

pub struct Shortcuts<S, A> {
    /// Internal properties