diff --git a/src/ui/widget.rs b/src/ui/widget.rs index fcd303e3..4662b027 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -33,6 +33,38 @@ pub struct BaseView { pub on_event: Option>, } +/// General properties that specify how a `Widget` is rendered. +/// They can be passed to a widgets' `render` function. +#[derive(Clone, Default)] +pub struct RenderProps { + /// Focus of the render props. + pub focus: bool, +} + +impl RenderProps { + /// Creates render props with focus. + pub fn focused() -> Self { + Self { focus: true } + } + + /// Creates render props with no focus. + pub fn blurred() -> Self { + Self { focus: false } + } + + /// Sets the focus of these render props. + pub fn focus(mut self) -> Self { + self.focus = true; + self + } + + /// Removes the focus from these render props. + pub fn blur(mut self) -> Self { + self.focus = false; + self + } +} + /// Main trait defining a `Widget` behaviour. /// /// This is the trait that you should implement to define a custom `Widget`. @@ -66,8 +98,8 @@ pub trait Widget { /// Renders a widget to the given frame in the given area. /// - /// Optional props take precedence over the internal ones. - fn render(&self, frame: &mut Frame, area: Rect, props: Option<&dyn Any>); + /// Optional render props can be given. + fn render(&self, frame: &mut Frame, area: Rect, props: Option); /// Return a mutable reference to this widgets' base view. fn base_mut(&mut self) -> &mut BaseView; @@ -104,7 +136,7 @@ pub trait ToRow { fn to_row(&self) -> [Cell; W]; } -/// Common trait for view properties. +/// Common trait for widget properties. pub trait Properties { fn to_boxed(self) -> Box where @@ -234,11 +266,7 @@ where } } - fn render(&self, frame: &mut ratatui::Frame, _area: Rect, props: Option<&dyn Any>) { - let _props = props - .and_then(|props| props.downcast_ref::>()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, _area: Rect, _props: Option) { let area = frame.size(); let page = self @@ -335,21 +363,18 @@ impl Widget for Shortcuts { ShortcutsProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { use ratatui::widgets::Table; - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - - let mut shortcuts = props.shortcuts.iter().peekable(); + let mut shortcuts = self.props.shortcuts.iter().peekable(); let mut row = vec![]; while let Some(shortcut) = shortcuts.next() { let short = Text::from(shortcut.0.clone()).style(style::gray()); let long = Text::from(shortcut.1.clone()).style(style::gray().dim()); let spacer = Text::from(String::new()); - let divider = Text::from(format!(" {} ", props.divider)).style(style::gray().dim()); + let divider = + Text::from(format!(" {} ", self.props.divider)).style(style::gray().dim()); row.push((shortcut.0.chars().count(), short)); row.push((1, spacer)); @@ -606,11 +631,7 @@ where } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::>()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let widths: Vec = self .props .columns @@ -618,19 +639,23 @@ where .filter_map(|c| if !c.skip { Some(c.width) } else { None }) .collect(); - let widths = if area.width < props.cutoff as u16 { - widths.iter().take(props.cutoff_after).collect::>() + let widths = if area.width < self.props.cutoff as u16 { + widths + .iter() + .take(self.props.cutoff_after) + .collect::>() } else { widths.iter().collect::>() }; - if !props.items.is_empty() { - let rows = props + if !self.props.items.is_empty() { + let rows = self + .props .items .iter() .map(|item| { let mut cells = vec![]; - let mut it = props.columns.iter(); + let mut it = self.props.columns.iter(); for cell in item.to_row() { if let Some(col) = it.next() { diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 661a24b4..473b5f36 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -1,4 +1,3 @@ -use std::any::Any; use std::fmt::Debug; use tokio::sync::mpsc::UnboundedSender; @@ -11,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, Column, Properties, Widget}; +use super::{BaseView, BoxedWidget, Column, Properties, RenderProps, Widget}; #[derive(Clone, Debug)] pub struct HeaderProps<'a> { @@ -102,12 +101,9 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { .unwrap_or(self.props.clone()); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - - let widths: Vec = props + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { + let widths: Vec = self + .props .columns .iter() .filter_map(|column| { @@ -118,7 +114,8 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { } }) .collect(); - let cells = props + let cells = self + .props .columns .iter() .filter_map(|column| { @@ -130,8 +127,11 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { }) .collect::>(); - let widths = if area.width < props.cutoff as u16 { - widths.iter().take(props.cutoff_after).collect::>() + let widths = if area.width < self.props.cutoff as u16 { + widths + .iter() + .take(self.props.cutoff_after) + .collect::>() } else { widths.iter().collect::>() }; @@ -139,7 +139,7 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { // Render header let block = HeaderBlock::default() .borders(Borders::ALL) - .border_style(style::border(props.focus)) + .border_style(style::border(self.props.focus)) .border_type(BorderType::Rounded); let header_layout = Layout::default() @@ -275,12 +275,9 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { .unwrap_or(self.props.clone()); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - - let widths = props + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { + let widths = self + .props .columns .iter() .map(|c| match c.width { @@ -290,7 +287,8 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { .collect::>(); let layout = Layout::horizontal(widths).split(area); - let cells = props + let cells = self + .props .columns .iter() .map(|c| c.text.clone()) @@ -307,7 +305,7 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { _ if i == last => FooterBlockType::End, _ => FooterBlockType::Repeat, }; - self.render_cell(frame, *area, block_type, cell.clone(), props.focus); + self.render_cell(frame, *area, block_type, cell.clone(), self.props.focus); } } @@ -411,13 +409,9 @@ impl Widget for Container { } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let header_h = if self.header.is_some() { 3 } else { 0 }; - let footer_h = if self.footer.is_some() && !props.hide_footer { + let footer_h = if self.footer.is_some() && !self.props.hide_footer { 3 } else { 0 @@ -432,7 +426,7 @@ impl Widget for Container { let borders = match ( self.header.is_some(), - (self.footer.is_some() && !props.hide_footer), + (self.footer.is_some() && !self.props.hide_footer), ) { (false, false) => Borders::ALL, (true, false) => Borders::BOTTOM | Borders::LEFT | Borders::RIGHT, @@ -441,7 +435,7 @@ impl Widget for Container { }; let block = Block::default() - .border_style(style::border(props.focus)) + .border_style(style::border(self.props.focus)) .border_type(BorderType::Rounded) .borders(borders); frame.render_widget(block.clone(), content_area); diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 7b22f2f6..0b8fafe1 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -1,5 +1,3 @@ -use std::any::Any; - use termion::event::Key; use tokio::sync::mpsc::UnboundedSender; @@ -9,7 +7,7 @@ use ratatui::prelude::Rect; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{BaseView, Properties, Widget, WidgetState}; +use super::{BaseView, Properties, RenderProps, Widget, WidgetState}; #[derive(Clone)] pub struct TextFieldProps { @@ -191,20 +189,16 @@ impl Widget for TextField { } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let layout = Layout::vertical(Constraint::from_lengths([1, 1])).split(area); let text = self.state.text.clone().unwrap_or_default(); let input = text.as_str(); - let label = format!(" {} ", props.title); + let label = format!(" {} ", self.props.title); let overline = String::from("▔").repeat(area.width as usize); let cursor_pos = self.state.cursor_position as u16; - if props.inline_label { + if self.props.inline_label { let top_layout = Layout::horizontal([ Constraint::Length(label.chars().count() as u16), Constraint::Length(1), @@ -221,7 +215,7 @@ impl Widget for TextField { frame.render_widget(input, top_layout[2]); frame.render_widget(overline, layout[1]); - if props.show_cursor { + if self.props.show_cursor { frame.set_cursor(top_layout[2].x + cursor_pos, top_layout[2].y) } } else { @@ -237,7 +231,7 @@ impl Widget for TextField { frame.render_widget(top, layout[0]); frame.render_widget(bottom, layout[1]); - if props.show_cursor { + if self.props.show_cursor { frame.set_cursor(area.x + cursor_pos, area.y) } } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index dac4bfda..1faf8678 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -1,5 +1,3 @@ -use std::any::Any; - use tokio::sync::mpsc::UnboundedSender; use termion::event::Key; @@ -7,7 +5,7 @@ use termion::event::Key; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::text::Text; -use super::{BaseView, Properties, Widget, WidgetState}; +use super::{BaseView, Properties, RenderProps, Widget, WidgetState}; #[derive(Clone)] pub struct ParagraphProps<'a> { @@ -205,15 +203,11 @@ impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { - let props = props - .and_then(|props| props.downcast_ref::()) - .unwrap_or(&self.props); - + fn render(&self, frame: &mut ratatui::Frame, area: Rect, _props: Option) { let [content_area] = Layout::horizontal([Constraint::Min(1)]) .horizontal_margin(1) .areas(area); - let content = ratatui::widgets::Paragraph::new(props.content.clone()) + let content = ratatui::widgets::Paragraph::new(self.props.content.clone()) .scroll((self.state.offset as u16, 0)); frame.render_widget(content, content_area);