diff --git a/src/ui/widget/window.rs b/src/ui/widget/window.rs index c2986132..6c0651ff 100644 --- a/src/ui/widget/window.rs +++ b/src/ui/widget/window.rs @@ -4,7 +4,7 @@ use std::{collections::HashMap, marker::PhantomData}; use ratatui::Frame; use termion::event::Key; -use ratatui::layout::Constraint; +use ratatui::layout::{Constraint, Layout}; use ratatui::style::Stylize; use ratatui::text::Text; use ratatui::widgets::Row; @@ -117,6 +117,98 @@ where } } +#[derive(Clone, Default)] +pub struct PageProps { + /// Current page size (height of table content etc.). + pub page_size: usize, + /// If this view's should handle keys + pub handle_keys: bool, +} + +impl PageProps { + pub fn page_size(mut self, page_size: usize) -> Self { + self.page_size = page_size; + self + } + + pub fn handle_keys(mut self, handle_keys: bool) -> Self { + self.handle_keys = handle_keys; + self + } +} + +pub struct Page { + /// Content widget + content: Option>, + /// Shortcut widget + shortcuts: Option>, +} + +impl Default for Page { + fn default() -> Self { + Self { + content: None, + shortcuts: None, + } + } +} + +impl Page { + pub fn content(mut self, content: Widget) -> Self { + self.content = Some(content); + self + } + + pub fn shortcuts(mut self, shortcuts: Widget) -> Self { + self.shortcuts = Some(shortcuts); + self + } +} + +impl View for Page +where + S: 'static, + M: 'static, +{ + type State = S; + type Message = M; + + fn handle_event(&mut self, _props: Option<&ViewProps>, key: Key) -> Option { + if let Some(content) = self.content.as_mut() { + content.handle_event(key); + } + + None + } + + fn update(&mut self, _props: Option<&ViewProps>, state: &Self::State) { + if let Some(content) = self.content.as_mut() { + content.update(state); + } + if let Some(shortcuts) = self.shortcuts.as_mut() { + shortcuts.update(state); + } + } + + fn render(&self, _props: Option<&ViewProps>, render: RenderProps, frame: &mut Frame) { + let [content_area, shortcuts_area] = + Layout::vertical([Constraint::Min(1), Constraint::Length(1)]).areas(render.area); + + if let Some(content) = self.content.as_ref() { + content.render( + RenderProps::from(content_area) + .layout(Layout::horizontal([Constraint::Min(1)])) + .focus(true), + frame, + ); + } + + if let Some(shortcuts) = self.shortcuts.as_ref() { + shortcuts.render(RenderProps::from(shortcuts_area), frame); + } + } +} + #[derive(Clone)] pub struct ShortcutsProps { pub shortcuts: Vec<(String, String)>,