Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Introduce trait for properties
Erik Kundt committed 2 years ago
commit ab6584f6b7305903d047df1d5691802261f0207e
parent c5800738411a8d11e0501d5fae378097b937c98c
4 files changed +31 -26
modified src/ui/widget.rs
@@ -57,21 +57,21 @@ pub trait ToRow {
    fn to_row(&self) -> Vec<Cell>;
}

+
pub trait Properties {
+
    fn to_boxed(self) -> Box<Self>
+
    where
+
        Self: Sized,
+
    {
+
        Box::new(self)
+
    }
+
}
+

#[derive(Clone)]
pub struct ShortcutsProps {
    pub shortcuts: Vec<(String, String)>,
    pub divider: char,
}

-
impl Default for ShortcutsProps {
-
    fn default() -> Self {
-
        Self {
-
            shortcuts: vec![],
-
            divider: '∙',
-
        }
-
    }
-
}
-

impl ShortcutsProps {
    pub fn divider(mut self, divider: char) -> Self {
        self.divider = divider;
@@ -85,12 +85,19 @@ impl ShortcutsProps {
        }
        self
    }
+
}

-
    pub fn to_boxed(self) -> Box<Self> {
-
        Box::new(self)
+
impl Default for ShortcutsProps {
+
    fn default() -> Self {
+
        Self {
+
            shortcuts: vec![],
+
            divider: '∙',
+
        }
    }
}

+
impl Properties for ShortcutsProps {}
+

pub struct Shortcuts<S, A> {
    /// Internal properties
    props: ShortcutsProps,
@@ -289,12 +296,10 @@ where
        self.page_size = page_size;
        self
    }
-

-
    pub fn to_boxed(self) -> Box<Self> {
-
        Box::new(self)
-
    }
}

+
impl<'a, R> Properties for TableProps<'a, R> where R: ToRow {}
+

pub struct Table<'a, B, S, A, R>
where
    B: Backend,
modified src/ui/widget/container.rs
@@ -11,7 +11,7 @@ use ratatui::widgets::{BorderType, Borders, Row};
use crate::ui::ext::{FooterBlock, FooterBlockType, HeaderBlock};
use crate::ui::theme::style;

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

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

+
impl<'a> Properties for HeaderProps<'a> {}
+

pub struct Header<'a, S, A> {
    /// Internal props
    props: HeaderProps<'a>,
@@ -213,6 +215,8 @@ impl<'a> Default for FooterProps<'a> {
    }
}

+
impl<'a> Properties for FooterProps<'a> {}
+

pub struct Footer<'a, S, A> {
    /// Internal properties
    props: FooterProps<'a>,
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::{EventCallback, UpdateCallback, View, Widget};
+
use super::{EventCallback, Properties, UpdateCallback, View, Widget};

#[derive(Clone)]
pub struct TextFieldProps {
@@ -38,10 +38,6 @@ impl TextFieldProps {
        self.inline_label = inline;
        self
    }
-

-
    pub fn to_boxed(self) -> Box<Self> {
-
        Box::new(self)
-
    }
}

impl Default for TextFieldProps {
@@ -56,6 +52,8 @@ impl Default for TextFieldProps {
    }
}

+
impl Properties for TextFieldProps {}
+

pub struct TextField<S, A> {
    /// Internal props
    props: TextFieldProps,
modified src/ui/widget/text.rs
@@ -11,7 +11,7 @@ use ratatui::widgets::{Block, BorderType, Borders};

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

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

#[derive(Clone)]
pub struct ParagraphProps<'a> {
@@ -37,10 +37,6 @@ impl<'a> ParagraphProps<'a> {
        self.focus = focus;
        self
    }
-

-
    pub fn to_boxed(self) -> Box<Self> {
-
        Box::new(self)
-
    }
}

impl<'a> Default for ParagraphProps<'a> {
@@ -55,6 +51,8 @@ impl<'a> Default for ParagraphProps<'a> {
    }
}

+
impl<'a> Properties for ParagraphProps<'a> {}
+

pub struct Paragraph<'a, S, A> {
    /// Internal properties
    props: ParagraphProps<'a>,