diff --git a/src/ui/widget.rs b/src/ui/widget.rs index ce4cbaba..c663ccf6 100644 --- a/src/ui/widget.rs +++ b/src/ui/widget.rs @@ -33,10 +33,10 @@ pub struct BaseView { pub on_event: Option>, } -/// Main trait defining a `View` behaviour. +/// Main trait defining a `Widget` behaviour. /// -/// This is the first trait that you should implement to define a custom `Widget`. -pub trait View { +/// This is the trait that you should implement to define a custom `Widget`. +pub trait Widget { type State; type Action; @@ -46,6 +46,32 @@ pub trait View { where Self: Sized; + /// Should handle key events and call `handle_event` on all children. + /// + /// After key events have been handled, the custom event handler `on_event` should + /// be called + fn handle_event(&mut self, key: Key); + + /// Should update the internal props of this and all children. + /// + /// Applications are usually defined by app-specific widgets that do know + /// the type of `state`. These can use widgets from the library that do not know the + /// type of `state`. + /// + /// If `on_update` is set, implementations of this function should call it to + /// construct and update the internal props. If it is not set, app widgets can construct + /// props directly via their state converters, whereas library widgets can just fallback + /// to their current props. + fn update(&mut self, state: &Self::State); + + /// 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>); + + /// Return a mutable reference to this widgets' base view. + fn base_mut(&mut self) -> &mut BaseView; + /// Should set the optional custom event handler. fn on_event(mut self, callback: EventCallback) -> Self where @@ -64,44 +90,13 @@ pub trait View { self } - /// Returns a boxed `View` + /// Returns a boxed `Widget` fn to_boxed(self) -> Box where Self: Sized, { Box::new(self) } - - /// Return a mutable reference to this widgets' base view. - fn base_mut(&mut self) -> &mut BaseView; - - /// Should handle key events and call `handle_event` on all children. - /// - /// After key events have been handled, the custom event handler `on_event` should - /// be called - fn handle_event(&mut self, key: Key); - - /// Should update the internal props of this and all children. - /// - /// Applications are usually defined by app-specific widgets that do know - /// the type of `state`. These can use widgets from the library that do not know the - /// type of `state`. - /// - /// If `on_update` is set, implementations of this function should call it to - /// construct and update the internal props. If it is not set, app widgets can construct - /// props directly via their state converters, whereas library widgets can just fallback - /// to their current props. - fn update(&mut self, state: &Self::State); -} - -/// 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 { - /// 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>); } /// Needs to be implemented for items that are supposed to be rendered in tables. @@ -174,7 +169,7 @@ where } } -impl<'a: 'static, S, A, Id> View for Window +impl<'a: 'static, S, A, Id> Widget for Window where Id: Clone + Hash + Eq + PartialEq + 'a, { @@ -196,9 +191,18 @@ where } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base + fn handle_event(&mut self, key: termion::event::Key) { + let page = self + .props + .current_page + .as_ref() + .and_then(|id| self.pages.get_mut(id)); + + if let Some(page) = page { + page.handle_event(key); + } } + fn update(&mut self, state: &S) { self.props = WindowProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); @@ -214,23 +218,6 @@ where } } - fn handle_event(&mut self, key: termion::event::Key) { - let page = self - .props - .current_page - .as_ref() - .and_then(|id| self.pages.get_mut(id)); - - if let Some(page) = page { - page.handle_event(key); - } - } -} - -impl<'a: 'static, S, A, Id> Widget for Window -where - Id: Clone + Hash + Eq + PartialEq + 'a, -{ fn render(&self, frame: &mut ratatui::Frame, _area: Rect, props: Option>) { let _props = props .and_then(WindowProps::from_boxed_any) @@ -248,6 +235,10 @@ where page.render(frame, area, None); } } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } #[derive(Clone)] @@ -306,7 +297,7 @@ impl Shortcuts { } } -impl View for Shortcuts { +impl Widget for Shortcuts { type Action = A; type State = S; @@ -321,19 +312,13 @@ impl View for Shortcuts { } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base - } - fn handle_event(&mut self, _key: Key) {} fn update(&mut self, state: &S) { self.props = ShortcutsProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); } -} -impl Widget for Shortcuts { fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { use ratatui::widgets::Table; @@ -374,6 +359,10 @@ impl Widget for Shortcuts { let table = Table::new([Row::new(row)], widths).column_spacing(0); frame.render_widget(table, area); } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } #[derive(Clone, Debug)] @@ -536,7 +525,7 @@ where } } -impl<'a: 'static, S, A, R> View for Table<'a, S, A, R> +impl<'a: 'static, S, A, R> Widget for Table<'a, S, A, R> where R: ToRow + Clone + 'static, { @@ -555,22 +544,6 @@ where } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base - } - - fn update(&mut self, state: &S) { - self.props = - TableProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); - - // TODO: Move to state reducer - if let Some(selected) = self.state.selected() { - if selected > self.props.items.len() { - self.begin(); - } - } - } - fn handle_event(&mut self, key: Key) { match key { Key::Up | Key::Char('k') => { @@ -600,12 +573,19 @@ where (on_event)(&self.state, self.base.action_tx.clone()); } } -} -impl<'a: 'static, S, A, R> Widget for Table<'a, S, A, R> -where - R: ToRow + Clone + Debug + 'static, -{ + fn update(&mut self, state: &S) { + self.props = + TableProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); + + // TODO: Move to state reducer + if let Some(selected) = self.state.selected() { + if selected > self.props.items.len() { + self.begin(); + } + } + } + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(TableProps::from_boxed_any) @@ -662,6 +642,10 @@ where frame.render_widget(hint, center); } } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } pub struct TableUtils {} diff --git a/src/ui/widget/container.rs b/src/ui/widget/container.rs index 5b2d6af4..a6552b0c 100644 --- a/src/ui/widget/container.rs +++ b/src/ui/widget/container.rs @@ -11,7 +11,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, View, Widget}; +use super::{BaseView, BoxedWidget, Column, Properties, Widget}; #[derive(Clone, Debug)] pub struct HeaderProps<'a> { @@ -77,7 +77,7 @@ impl<'a, S, A> Header<'a, S, A> { } } -impl<'a: 'static, S, A> View for Header<'a, S, A> { +impl<'a: 'static, S, A> Widget for Header<'a, S, A> { type Action = A; type State = S; @@ -92,8 +92,10 @@ impl<'a: 'static, S, A> View for Header<'a, S, A> { } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base + fn handle_event(&mut self, _key: Key) { + if let Some(on_event) = self.base.on_event { + (on_event)(&self.props, self.base.action_tx.clone()); + } } fn update(&mut self, state: &S) { @@ -104,14 +106,6 @@ impl<'a: 'static, S, A> View for Header<'a, S, A> { .unwrap_or(self.props.clone()); } - fn handle_event(&mut self, _key: Key) { - if let Some(on_event) = self.base.on_event { - (on_event)(&self.props, self.base.action_tx.clone()); - } - } -} - -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) @@ -168,6 +162,10 @@ impl<'a: 'static, S, A> Widget for Header<'a, S, A> { frame.render_widget(block, area); frame.render_widget(header, header_layout[0]); } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } #[derive(Clone, Debug)] @@ -232,43 +230,7 @@ impl<'a, S, A> Footer<'a, S, A> { self.props.focus = focus; self } -} -impl<'a: 'static, S, A> View for Footer<'a, S, A> { - type Action = A; - type State = S; - - fn new(_state: &S, action_tx: UnboundedSender) -> Self { - Self { - base: BaseView { - action_tx: action_tx.clone(), - on_update: None, - on_event: None, - }, - props: FooterProps::default(), - } - } - - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base - } - - fn update(&mut self, state: &S) { - self.props = self - .base - .on_update - .and_then(|on_update| FooterProps::from_boxed_any((on_update)(state))) - .unwrap_or(self.props.clone()); - } - - fn handle_event(&mut self, _key: Key) { - if let Some(on_event) = self.base.on_event { - (on_event)(&self.props, self.base.action_tx.clone()); - } - } -} - -impl<'a, S, A> Footer<'a, S, A> { fn render_cell( &self, frame: &mut ratatui::Frame, @@ -293,6 +255,34 @@ impl<'a, S, A> Footer<'a, S, A> { } impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { + type Action = A; + type State = S; + + fn new(_state: &S, action_tx: UnboundedSender) -> Self { + Self { + base: BaseView { + action_tx: action_tx.clone(), + on_update: None, + on_event: None, + }, + props: FooterProps::default(), + } + } + + fn handle_event(&mut self, _key: Key) { + if let Some(on_event) = self.base.on_event { + (on_event)(&self.props, self.base.action_tx.clone()); + } + } + + fn update(&mut self, state: &S) { + self.props = self + .base + .on_update + .and_then(|on_update| FooterProps::from_boxed_any((on_update)(state))) + .unwrap_or(self.props.clone()); + } + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(FooterProps::from_boxed_any) @@ -328,6 +318,10 @@ impl<'a: 'static, S, A> Widget for Footer<'a, S, A> { self.render_cell(frame, *area, block_type, cell.clone(), props.focus); } } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } #[derive(Clone, Default)] @@ -380,7 +374,7 @@ impl Container { } } -impl View for Container { +impl Widget for Container { type Action = A; type State = S; @@ -402,8 +396,10 @@ impl View for Container { } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base + fn handle_event(&mut self, key: termion::event::Key) { + if let Some(content) = &mut self.content { + content.handle_event(key); + } } fn update(&mut self, state: &S) { @@ -423,14 +419,6 @@ impl View for Container { } } - fn handle_event(&mut self, key: termion::event::Key) { - if let Some(content) = &mut self.content { - content.handle_event(key); - } - } -} - -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) @@ -478,4 +466,8 @@ impl<'a: 'static, S, A> Widget for Container { footer.render(frame, footer_area, None); } } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } diff --git a/src/ui/widget/input.rs b/src/ui/widget/input.rs index 56579d7e..733754ae 100644 --- a/src/ui/widget/input.rs +++ b/src/ui/widget/input.rs @@ -9,7 +9,7 @@ use ratatui::prelude::Rect; use ratatui::style::Stylize; use ratatui::text::{Line, Span}; -use super::{BaseView, Properties, View, Widget}; +use super::{BaseView, Properties, Widget}; #[derive(Clone)] pub struct TextFieldProps { @@ -126,7 +126,7 @@ impl TextField { } } -impl View for TextField { +impl Widget for TextField { type Action = A; type State = S; @@ -145,23 +145,6 @@ impl View for TextField { } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base - } - - fn update(&mut self, state: &S) { - if let Some(on_update) = self.base.on_update { - if let Some(props) = (on_update)(state).downcast_ref::() { - self.props = props.clone(); - - if self.state.text.is_none() { - self.state.cursor_position = props.text.len().saturating_sub(1); - } - self.state.text = Some(props.text.clone()); - } - } - } - fn handle_event(&mut self, key: Key) { match key { Key::Char(to_insert) @@ -187,9 +170,20 @@ impl View for TextField { (on_event)(&self.state, self.base.action_tx.clone()); } } -} -impl Widget for TextField { + fn update(&mut self, state: &S) { + if let Some(on_update) = self.base.on_update { + if let Some(props) = (on_update)(state).downcast_ref::() { + self.props = props.clone(); + + if self.state.text.is_none() { + self.state.cursor_position = props.text.len().saturating_sub(1); + } + self.state.text = Some(props.text.clone()); + } + } + } + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(TextFieldProps::from_boxed_any) @@ -241,4 +235,8 @@ impl Widget for TextField { } } } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index a6c6c1cb..a2fe59b4 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -7,7 +7,7 @@ use termion::event::Key; use ratatui::layout::{Constraint, Layout, Rect}; use ratatui::text::Text; -use super::{BaseView, Properties, View, Widget}; +use super::{BaseView, Properties, Widget}; #[derive(Clone)] pub struct ParagraphProps<'a> { @@ -141,7 +141,7 @@ impl<'a, S, A> Paragraph<'a, S, A> { } } -impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { +impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { type Action = A; type State = S; @@ -163,15 +163,6 @@ impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { } } - fn base_mut(&mut self) -> &mut BaseView { - &mut self.base - } - - fn update(&mut self, state: &S) { - self.props = - ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); - } - fn handle_event(&mut self, key: Key) { let len = self.props.content.lines.len() + 1; let page_size = self.props.page_size; @@ -202,9 +193,12 @@ impl<'a: 'static, S, A> View for Paragraph<'a, S, A> { (on_event)(&self.state, self.base.action_tx.clone()); } } -} -impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { + fn update(&mut self, state: &S) { + self.props = + ParagraphProps::from_callback(self.base.on_update, state).unwrap_or(self.props.clone()); + } + fn render(&self, frame: &mut ratatui::Frame, area: Rect, props: Option>) { let props = props .and_then(ParagraphProps::from_boxed_any) @@ -218,4 +212,8 @@ impl<'a: 'static, S, A> Widget for Paragraph<'a, S, A> { frame.render_widget(content, content_area); } + + fn base_mut(&mut self) -> &mut BaseView { + &mut self.base + } }