From a213c3cf265668370ff3ac4f9edc9dd96b68b5d9 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 24 Apr 2024 14:15:45 +0200 Subject: [PATCH] lib: Remove unnecessary generic backend parameter --- src/ui.rs | 8 +------- src/ui/widget.rs | 34 +++++++++--------------------- src/ui/widget/container.rs | 42 +++++++++++--------------------------- src/ui/widget/input.rs | 7 ++----- src/ui/widget/text.rs | 6 +----- 5 files changed, 26 insertions(+), 71 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index 18290984..384c337c 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -7,11 +7,8 @@ pub mod theme; pub mod widget; use std::fmt::Debug; -use std::io::{self}; use std::time::Duration; -use termion::raw::RawTerminal; - use tokio::sync::broadcast; use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender}; @@ -19,11 +16,8 @@ use super::event::Event; use super::store::State; use super::task::Interrupted; use super::terminal; -use super::terminal::TermionBackendExt; use super::ui::widget::Widget; -type Backend = TermionBackendExt>; - const RENDERING_TICK_RATE: Duration = Duration::from_millis(250); const INLINE_HEIGHT: usize = 20; @@ -72,7 +66,7 @@ impl Frontend { ) -> anyhow::Result> where S: State

, - W: Widget, + W: Widget, P: Clone + Send + Sync + Debug, { let mut ticker = tokio::time::interval(RENDERING_TICK_RATE); diff --git a/src/ui/widget.rs b/src/ui/widget.rs index 1b19a2f7..ce4cbaba 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -18,7 +18,7 @@ use ratatui::widgets::{Cell, Row, TableState}; use super::theme::style; use super::{layout, span}; -pub type BoxedWidget = Box>; +pub type BoxedWidget = Box>; pub type UpdateCallback = fn(&S) -> Box; pub type EventCallback = fn(&dyn Any, UnboundedSender); @@ -97,10 +97,7 @@ pub trait View { /// A `Widget` is a `View` that can be rendered using a specific backend. /// /// This is the second trait that you should implement to define a custom `Widget`. -pub trait Widget: View -where - B: Backend, -{ +pub trait Widget: View { /// Renders a widget to the given frame in the given area. /// /// Optional props take precedence over the internal ones. @@ -158,33 +155,27 @@ impl Default for WindowProps { impl Properties for WindowProps {} -pub struct Window -where - B: Backend, -{ +pub struct Window { /// Internal base base: BaseView, /// Internal properties props: WindowProps, /// All pages known - pages: HashMap>, + pages: HashMap>, } -impl Window +impl Window where - B: Backend, Id: Clone + Hash + Eq + PartialEq, { - pub fn page(mut self, id: Id, page: BoxedWidget) -> Self { - // self.pages.inse + pub fn page(mut self, id: Id, page: BoxedWidget) -> Self { self.pages.insert(id, page); self } } -impl<'a: 'static, B, S, A, Id> View for Window +impl<'a: 'static, S, A, Id> View for Window where - B: Backend + 'a, Id: Clone + Hash + Eq + PartialEq + 'a, { type Action = A; @@ -236,9 +227,8 @@ where } } -impl<'a: 'static, B, S, A, Id> Widget for Window +impl<'a: 'static, S, A, Id> Widget for Window where - B: Backend + 'a, Id: Clone + Hash + Eq + PartialEq + 'a, { fn render(&self, frame: &mut ratatui::Frame, _area: Rect, props: Option>) { @@ -343,10 +333,7 @@ impl View for Shortcuts { } } -impl Widget for Shortcuts -where - B: Backend, -{ +impl Widget for Shortcuts { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { use ratatui::widgets::Table; @@ -615,9 +602,8 @@ where } } -impl<'a: 'static, B, S, A, R> Widget for Table<'a, S, A, R> +impl<'a: 'static, S, A, R> Widget for Table<'a, S, A, R> where - B: Backend, R: ToRow + Clone + Debug + 'static, { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index f2c338f1..5b2d6af4 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -111,10 +111,7 @@ impl<'a: 'static, S, A> View for Header<'a, S, A> { } } -impl<'a: 'static, B, S, A> Widget for Header<'a, S, A> -where - B: Backend, -{ +impl<'a: 'static, S, A> Widget for Header<'a, S, A> { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(HeaderProps::from_boxed_any) @@ -295,10 +292,7 @@ impl<'a, S, A> Footer<'a, S, A> { } } -impl<'a: 'static, B, S, A> Widget for Footer<'a, S, A> -where - B: Backend, -{ +impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(FooterProps::from_boxed_any) @@ -356,46 +350,37 @@ impl ContainerProps { impl Properties for ContainerProps {} -pub struct Container -where - B: Backend, -{ +pub struct Container { /// Internal base base: BaseView, /// Internal props props: ContainerProps, /// Container header - header: Option>, + header: Option>, /// Content widget - content: Option>, + content: Option>, /// Container footer - footer: Option>, + footer: Option>, } -impl Container -where - B: Backend, -{ - pub fn header(mut self, header: BoxedWidget) -> Self { +impl Container { + pub fn header(mut self, header: BoxedWidget) -> Self { self.header = Some(header); self } - pub fn content(mut self, content: BoxedWidget) -> Self { + pub fn content(mut self, content: BoxedWidget) -> Self { self.content = Some(content); self } - pub fn footer(mut self, footer: BoxedWidget) -> Self { + pub fn footer(mut self, footer: BoxedWidget) -> Self { self.footer = Some(footer); self } } -impl View for Container -where - B: Backend, -{ +impl View for Container { type Action = A; type State = S; @@ -445,10 +430,7 @@ where } } -impl<'a: 'static, B, S, A> Widget for Container -where - B: Backend, -{ +impl<'a: 'static, S, A> Widget for Container { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(ContainerProps::from_boxed_any) diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index b3d53285..56579d7e 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -5,7 +5,7 @@ use termion::event::Key; use tokio::sync::mpsc::UnboundedSender; use ratatui::layout::{Constraint, Layout}; -use ratatui::prelude::{Backend, Rect}; +use ratatui::prelude::Rect; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; @@ -189,10 +189,7 @@ impl View for TextField { } } -impl Widget for TextField -where - B: Backend, -{ +impl Widget for TextField { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(TextFieldProps::from_boxed_any) diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index f204ce30..a6c6c1cb 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -4,7 +4,6 @@ use tokio::sync::mpsc::UnboundedSender; use termion::event::Key; -use ratatui::backend::Backend; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::text::Text; @@ -205,10 +204,7 @@ impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { } } -impl<'a: 'static, B, S, A> Widget for Paragraph<'a, S, A> -where - B: Backend, -{ +impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(ParagraphProps::from_boxed_any)