From dcffff6b74594b2d6dd883c32fd68b02715495e9 Mon Sep 17 00:00:00 2001 From: Erik Kundt Date: Wed, 28 Aug 2024 14:09:12 +0200 Subject: [PATCH] lib: Add scrollable text view to imUI --- src/ui/im.rs | 175 ++++++++++++++++++++++++++++++++++++++++++++---- src/ui/theme.rs | 5 +- 2 files changed, 166 insertions(+), 14 deletions(-) diff --git a/src/ui/im.rs b/src/ui/im.rs index 5eea53b6..87f1f0cd 100644 --- a/src/ui/im.rs +++ b/src/ui/im.rs @@ -5,6 +5,7 @@ use std::time::Duration; use anyhow::Result; +use ratatui::text::Text; use tokio::sync::broadcast; use tokio::sync::mpsc::UnboundedReceiver; @@ -228,7 +229,7 @@ impl Layout { #[derive(Default, Clone, Debug)] pub struct Ui { - pub(crate) theme: Theme, + pub theme: Theme, pub(crate) area: Rect, pub(crate) layout: Layout, focus: Option, @@ -311,6 +312,14 @@ impl Ui { pub fn set_focus(&mut self, focus: Option) { self.focus = focus; } + + pub fn focus_next(&mut self) { + if self.focus.is_none() { + self.focus = Some(0); + } else { + self.focus = Some(self.focus.unwrap().saturating_add(1)); + } + } } impl Ui { @@ -364,6 +373,16 @@ impl Ui { widget::Group::new(len, focus).show(&mut child_ui, add_contents) } + pub fn label<'a>(&mut self, frame: &mut Frame, content: impl Into>) -> Response { + widget::Label::new(content).ui(self, frame) + } + + pub fn overline<'a>(&mut self, frame: &mut Frame) -> Response { + // let overline = String::from("▔").repeat(256); + let overline = String::from("━").repeat(256); + self.label(frame, overline) + } + pub fn table<'a, R, const W: usize>( &mut self, frame: &mut Frame, @@ -396,13 +415,23 @@ impl Ui { widget::Columns::new(columns, borders).ui(self, frame) } + pub fn bar<'a>( + &mut self, + frame: &mut Frame, + columns: Vec>, + borders: Option, + ) -> Response { + widget::Bar::new(columns, borders).ui(self, frame) + } + pub fn text_view( &mut self, frame: &mut Frame, text: String, + scroll: (usize, usize), borders: Option, ) -> Response { - widget::TextView::new(text, borders).ui(self, frame) + widget::TextView::new(text, scroll, borders).ui(self, frame) } pub fn text_edit_singleline( @@ -560,6 +589,29 @@ pub mod widget { } } + pub struct Label<'a> { + content: Text<'a>, + } + + impl<'a> Label<'a> { + pub fn new(content: impl Into>) -> Self { + Self { + content: content.into(), + } + } + } + + impl<'a> Widget for Label<'a> { + fn ui(self, ui: &mut Ui, frame: &mut Frame) -> Response { + let mut response = Response::default(); + + let (area, has_focus) = ui.next_area().unwrap_or_default(); + frame.render_widget(self.content, area); + + response + } + } + #[derive(Clone, Debug)] pub struct TableState { items: Vec, @@ -885,21 +937,18 @@ pub mod widget { } } - pub struct TextView { - text: String, + pub struct Bar<'a> { + columns: Vec>, borders: Option, } - impl TextView { - pub fn new(text: impl ToString, borders: Option) -> Self { - Self { - text: text.to_string(), - borders, - } + impl<'a> Bar<'a> { + pub fn new(columns: Vec>, borders: Option) -> Self { + Self { columns, borders } } } - impl Widget for TextView { + impl<'a> Widget for Bar<'a> { fn ui(self, ui: &mut Ui, frame: &mut Frame) -> Response { let (area, has_focus) = ui.next_area().unwrap_or_default(); @@ -909,9 +958,109 @@ pub mod widget { ui.theme.border_style }; - let area = render_block(frame, area, self.borders, border_style); + let widths = self.columns.iter().map(|c| c.width).collect::>(); + let cells = self + .columns + .iter() + .map(|c| c.text.clone()) + .collect::>(); - frame.render_widget(Paragraph::new(self.text), area); + let area = render_block(frame, area, self.borders, border_style); + let table = ratatui::widgets::Table::default() + .header(Row::new(cells)) + .widths(widths) + .column_spacing(0); + frame.render_widget(table, area); + + Response::default() + } + } + + #[derive(Clone, Debug)] + pub struct TextViewState { + text: String, + scroll: (usize, usize), + } + + impl TextViewState { + pub fn new(text: impl Into, scroll: (usize, usize)) -> Self { + Self { + text: text.into(), + scroll, + } + } + + pub fn scroll(&self) -> (usize, usize) { + self.scroll + } + } + + pub struct TextView { + text: String, + borders: Option, + scroll: (usize, usize), + } + + impl TextView { + pub fn new(text: impl ToString, scroll: (usize, usize), borders: Option) -> Self { + Self { + text: text.to_string(), + borders, + scroll, + } + } + } + + impl Widget for TextView { + fn ui(self, ui: &mut Ui, frame: &mut Frame) -> Response { + let (area, has_focus) = ui.next_area().unwrap_or_default(); + + let show_scrollbar = true; + let border_style = if has_focus { + ui.theme.focus_border_style + } else { + ui.theme.border_style + }; + let length = self.text.lines().count(); + // let virtual_length = length * ((length as f64).log2() as usize) / 100; + // let content_length = area.height as usize + virtual_length; + // let content_length = length; + let content_length = area.height as usize; + + let area = render_block(frame, area, self.borders, border_style); + let area = Rect { + x: area.x.saturating_add(1), + width: area.width.saturating_sub(1), + ..area + }; + let [text_area, scroller_area] = Layout::horizontal([ + Constraint::Min(1), + if show_scrollbar { + Constraint::Length(1) + } else { + Constraint::Length(0) + }, + ]) + .areas(area); + + let scroller = Scrollbar::default() + .begin_symbol(None) + .track_symbol(None) + .end_symbol(None) + .thumb_symbol("┃") + .style(if has_focus { + Style::default() + } else { + Style::default().dim() + }); + + let mut scroller_state = ScrollbarState::default() + .content_length(length.saturating_sub(content_length)) + .viewport_content_length(1) + .position(self.scroll.1); + + frame.render_stateful_widget(scroller, scroller_area, &mut scroller_state); + frame.render_widget(Paragraph::new(self.text), text_area); Response::default() } diff --git a/src/ui/theme.rs b/src/ui/theme.rs index 93318d91..3df3067a 100644 --- a/src/ui/theme.rs +++ b/src/ui/theme.rs @@ -9,6 +9,7 @@ pub struct Theme { pub textview_style: Style, pub textview_scroll_style: Style, pub textview_focus_scroll_style: Style, + pub bar_on_black_style: Style, pub dim_no_focus: bool, } @@ -28,6 +29,7 @@ impl Theme { textview_style: style::reset(), textview_scroll_style: style::cyan().dim(), textview_focus_scroll_style: style::cyan(), + bar_on_black_style: Style::default().on_gray(), dim_no_focus: false, } } @@ -37,10 +39,11 @@ impl Theme { border_style: Style::default().fg(Color::Indexed(240)), focus_border_style: Style::default().fg(Color::Indexed(246)), shortcuts_keys_style: style::yellow().dim(), - shortcuts_action_style: style::gray(), + shortcuts_action_style: style::gray().dim(), textview_style: style::reset(), textview_scroll_style: style::cyan().dim(), textview_focus_scroll_style: style::cyan(), + bar_on_black_style: Style::default().on_black(), dim_no_focus: false, } }