From fe4d48282e43e725309da0d513620d6afdeb395e Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Mon, 29 Apr 2024 13:04:25 +0200 Subject: [PATCH] lib: Pass render props by reference --- src/ui/widget.rs | 20 ++++++++++---------- src/ui/widget/container.rs | 18 +++++++++--------- src/ui/widget/input.rs | 6 +++--- src/ui/widget/text.rs | 6 +++--- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/ui/widget.rs b/src/ui/widget.rs index cd1adb94..fcd303e3 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -67,7 +67,7 @@ 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>); + fn render(&self, frame: &mut Frame, area: Rect, props: Option<&dyn Any>); /// Return a mutable reference to this widgets' base view. fn base_mut(&mut self) -> &mut BaseView; @@ -234,10 +234,10 @@ where } } - fn render(&self, frame: &mut ratatui::Frame, _area: Rect, props: Option>) { + fn render(&self, frame: &mut ratatui::Frame, _area: Rect, props: Option<&dyn Any>) { let _props = props - .and_then(WindowProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::>()) + .unwrap_or(&self.props); let area = frame.size(); @@ -335,12 +335,12 @@ 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>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { use ratatui::widgets::Table; let props = props - .and_then(ShortcutsProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let mut shortcuts = props.shortcuts.iter().peekable(); let mut row = vec![]; @@ -606,10 +606,10 @@ where } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(TableProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::>()) + .unwrap_or(&self.props); let widths: Vec = self .props diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index ee40a4f0..661a24b4 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -102,10 +102,10 @@ 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>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(HeaderProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let widths: Vec = props .columns @@ -275,10 +275,10 @@ 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>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(FooterProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let widths = props .columns @@ -411,10 +411,10 @@ impl Widget for Container { } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(ContainerProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let header_h = if self.header.is_some() { 3 } else { 0 }; let footer_h = if self.footer.is_some() && !props.hide_footer { diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index ac83b78c..7b22f2f6 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -191,10 +191,10 @@ impl Widget for TextField { } } - fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(TextFieldProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let layout = Layout::vertical(Constraint::from_lengths([1, 1])).split(area); diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index 61f689c8..dac4bfda 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -205,10 +205,10 @@ 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>) { + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option<&dyn Any>) { let props = props - .and_then(ParagraphProps::from_boxed_any) - .unwrap_or(self.props.clone()); + .and_then(|props| props.downcast_ref::()) + .unwrap_or(&self.props); let [content_area] = Layout::horizontal([Constraint::Min(1)]) .horizontal_margin(1)