Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
lib: Improve generic params of widgets
Erik Kundt committed 2 years ago
commit b25e1b5612ef9d9e8c994088bcff4303f939fa24
parent f928590d6f180eefa5e521cae5fd3022cc553017
5 files changed +34 -16
modified src/ui.rs
@@ -45,7 +45,7 @@ impl<A> Frontend<A> {
    ) -> anyhow::Result<Interrupted<P>>
    where
        S: State<A, P>,
-
        W: Widget<S, A, Backend>,
+
        W: Widget<Backend, S, A>,
        P: Clone + Send + Sync + Debug,
    {
        let mut ticker = tokio::time::interval(RENDERING_TICK_RATE);
modified src/ui/widget.rs
@@ -16,7 +16,7 @@ use ratatui::widgets::{Block, BorderType, Borders, Cell, Row, TableState};
use super::theme::style;
use super::{layout, span};

-
pub type BoxedWidget<S, A, B> = Box<dyn Widget<S, A, B>>;
+
pub type BoxedWidget<B, S, A> = Box<dyn Widget<B, S, A>>;

pub type UpdateCallback<S> = fn(&S) -> Box<dyn Any>;
pub type EventCallback<A> = fn(&dyn Any, UnboundedSender<A>);
@@ -46,7 +46,10 @@ pub trait View<S, A> {
    }
}

-
pub trait Widget<S, A, B: Backend>: View<S, A> {
+
pub trait Widget<B, S, A>: View<S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut Frame, area: Rect, _props: &dyn Any);
}

@@ -147,7 +150,10 @@ impl<S, A> View<S, A> for Shortcuts<S, A> {
    }
}

-
impl<S, A, B: Backend> Widget<S, A, B> for Shortcuts<S, A> {
+
impl<B, S, A> Widget<B, S, A> for Shortcuts<S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: &dyn Any) {
        use ratatui::widgets::Table;

@@ -281,7 +287,7 @@ where
    }
}

-
pub struct Table<'a, S, A, B, R>
+
pub struct Table<'a, B, S, A, R>
where
    B: Backend,
    R: ToRow,
@@ -297,15 +303,15 @@ where
    /// Internal selection and offset state
    state: TableState,
    /// Table header widget
-
    header: Option<BoxedWidget<S, A, B>>,
+
    header: Option<BoxedWidget<B, S, A>>,
}

-
impl<'a, S, A, B, R> Table<'a, S, A, B, R>
+
impl<'a, B, S, A, R> Table<'a, B, S, A, R>
where
    B: Backend,
    R: ToRow,
{
-
    pub fn header(mut self, header: BoxedWidget<S, A, B>) -> Self {
+
    pub fn header(mut self, header: BoxedWidget<B, S, A>) -> Self {
        self.header = Some(header);
        self
    }
@@ -380,7 +386,7 @@ where
    }
}

-
impl<'a: 'static, S, A, B, R> View<S, A> for Table<'a, S, A, B, R>
+
impl<'a: 'static, B, S, A, R> View<S, A> for Table<'a, B, S, A, R>
where
    B: Backend,
    R: ToRow + Clone + 'static,
@@ -452,7 +458,7 @@ where
    }
}

-
impl<'a: 'static, S, A, B, R> Widget<S, A, B> for Table<'a, S, A, B, R>
+
impl<'a: 'static, B, S, A, R> Widget<B, S, A> for Table<'a, B, S, A, R>
where
    B: Backend,
    R: ToRow + Clone + Debug + 'static,
modified src/ui/widget/container.rs
@@ -114,7 +114,10 @@ impl<'a: 'static, S, A> View<S, A> for Header<'a, S, A> {
    }
}

-
impl<'a: 'static, S, A, B: Backend> Widget<S, A, B> for Header<'a, S, A> {
+
impl<'a: 'static, B, S, A> Widget<B, S, A> for Header<'a, S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: &dyn Any) {
        let props = props
            .downcast_ref::<HeaderProps<'_>>()
@@ -298,7 +301,10 @@ impl<'a, S, A> Footer<'a, S, A> {
    }
}

-
impl<'a: 'static, S, A, B: Backend> Widget<S, A, B> for Footer<'a, S, A> {
+
impl<'a: 'static, B, S, A> Widget<B, S, A> for Footer<'a, S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: &dyn Any) {
        let props = props
            .downcast_ref::<FooterProps<'_>>()
modified src/ui/widget/input.rs
@@ -183,7 +183,10 @@ impl<S, A> View<S, A> for TextField<S, A> {
    }
}

-
impl<S, A, B: Backend> Widget<S, A, B> for TextField<S, A> {
+
impl<B, S, A> Widget<B, S, A> for TextField<S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: &dyn Any) {
        let props = props
            .downcast_ref::<TextFieldProps>()
modified src/ui/widget/text.rs
@@ -205,7 +205,10 @@ impl<'a: 'static, S, A> View<S, A> for Paragraph<'a, S, A> {
    }
}

-
impl<'a: 'static, S, A, B: Backend> Widget<S, A, B> for Paragraph<'a, S, A> {
+
impl<'a: 'static, B, S, A> Widget<B, S, A> for Paragraph<'a, S, A>
+
where
+
    B: Backend,
+
{
    fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: &dyn Any) {
        let props = props
            .downcast_ref::<ParagraphProps<'_>>()
@@ -220,8 +223,8 @@ impl<'a: 'static, S, A, B: Backend> Widget<S, A, B> for Paragraph<'a, S, A> {
        let [content_area] = Layout::horizontal([Constraint::Min(1)])
            .horizontal_margin(2)
            .areas(area);
-
        let content = ratatui::widgets::Paragraph::new(props.content.clone())
-
            .scroll((self.offset as u16, 0));
+
        let content =
+
            ratatui::widgets::Paragraph::new(props.content.clone()).scroll((self.offset as u16, 0));

        frame.render_widget(content, content_area);
    }