diff options
| author | lukeflo | 2024-11-14 14:15:35 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-14 14:15:35 +0100 |
| commit | ca51eea300b84a6fa865c6f3e00823a8099bb9bc (patch) | |
| tree | 920a86a4466ed7172afc1351f7a454c75afb6745 /src/tui | |
| parent | 5731d4a302ee7eabd78d67ec254e2bf09c06d086 (diff) | |
| download | bibiman-ca51eea300b84a6fa865c6f3e00823a8099bb9bc.tar.gz bibiman-ca51eea300b84a6fa865c6f3e00823a8099bb9bc.zip | |
implement scrolling for help popup
Diffstat (limited to 'src/tui')
| -rw-r--r-- | src/tui/commands.rs | 4 | ||||
| -rw-r--r-- | src/tui/popup.rs | 12 | ||||
| -rw-r--r-- | src/tui/ui.rs | 41 |
3 files changed, 43 insertions, 14 deletions
diff --git a/src/tui/commands.rs b/src/tui/commands.rs index 6a2ab13..a9566d0 100644 --- a/src/tui/commands.rs +++ b/src/tui/commands.rs @@ -52,7 +52,7 @@ pub enum CmdAction { // Search list SearchList, // Reset lists - ResetList, + Reset, // Confirm search/selection Confirm, // Sort table/list @@ -140,7 +140,7 @@ impl From<KeyEvent> for CmdAction { // Confirm selection KeyCode::Enter => Self::Confirm, // Reset lists/tables - KeyCode::Esc => Self::ResetList, + KeyCode::Esc => Self::Reset, // Open linked ressource KeyCode::Char('o') => Self::Open(OpenRessource::Pdf), // KeyCode::Char('u') => Self::Open(OpenRessource::WebLink), diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 20ff467..fe76956 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -35,6 +35,7 @@ pub struct PopupArea { pub is_popup: bool, pub popup_kind: Option<PopupKind>, pub popup_message: String, + pub popup_scroll_pos: u16, pub popup_list: Vec<String>, pub popup_state: ListState, } @@ -45,6 +46,7 @@ impl Default for PopupArea { is_popup: false, popup_kind: None, popup_message: String::new(), + popup_scroll_pos: 0, popup_list: Vec::new(), popup_state: ListState::default(), } @@ -146,9 +148,11 @@ impl PopupArea { self.is_popup = true; } - pub fn popup_close_message(&mut self) { - self.is_popup = false; - self.popup_message.clear(); - self.popup_kind = None + pub fn popup_scroll_down(&mut self) { + self.popup_scroll_pos = self.popup_scroll_pos.saturating_add(1) + } + + pub fn popup_scroll_up(&mut self) { + self.popup_scroll_pos = self.popup_scroll_pos.saturating_sub(1) } } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 260f401..dd08291 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -37,7 +37,7 @@ use ratatui::{ ScrollbarOrientation, Table, Wrap, }, }; -use tui_popup::Popup; +use tui_popup::{Popup, SizedWrapper}; // Text colors const TEXT_FG_COLOR: Color = Color::Indexed(TEXT_FG_COLOR_INDEX); @@ -151,19 +151,44 @@ pub fn render_ui(app: &mut App, frame: &mut Frame) { } pub fn render_popup(app: &mut App, frame: &mut Frame) { - let popup = if let Some(PopupKind::Help) = app.bibiman.popup_area.popup_kind { - Popup::new(PopupArea::popup_help()) + if let Some(PopupKind::Help) = app.bibiman.popup_area.popup_kind { + let text: Text = Text::from(PopupArea::popup_help()); + + // Calculate max scroll position depending on hight of terminal window + let scroll_pos = if app.bibiman.popup_area.popup_scroll_pos + > text.lines.len() as u16 - (frame.area().height / 2) + { + app.bibiman.popup_area.popup_scroll_pos = + text.lines.len() as u16 - (frame.area().height / 2); + app.bibiman.popup_area.popup_scroll_pos + } else { + app.bibiman.popup_area.popup_scroll_pos + }; + + let par = Paragraph::new(text).scroll((scroll_pos, 0)); + + // Needed to use scrollable Parapgraph as popup content + let sized_par = SizedWrapper { + inner: par, + width: (frame.area().width / 2) as usize, + height: (frame.area().height / 2) as usize, + }; + + let popup = Popup::new(sized_par) .title(" Keybindings ".bold().into_centered_line()) .style(POPUP_HELP_BOX) - .border_style(Style::new().fg(MAIN_BLUE)) + .border_style(Style::new().fg(MAIN_BLUE)); + + frame.render_widget_ref(popup, frame.area()) } else if let Some(PopupKind::Message) = app.bibiman.popup_area.popup_kind { - Popup::new(Text::from(app.bibiman.popup_area.popup_message.as_str()).fg(MAIN_GREEN)) - .title(" Message ".bold().into_centered_line().fg(MAIN_GREEN)) - .style(POPUP_HELP_BOX) + let popup = + Popup::new(Text::from(app.bibiman.popup_area.popup_message.as_str()).fg(MAIN_GREEN)) + .title(" Message ".bold().into_centered_line().fg(MAIN_GREEN)) + .style(POPUP_HELP_BOX); + frame.render_widget(&popup, frame.area()) } else { panic!("No popup text detected") }; - frame.render_widget(&popup, frame.area()) } pub fn render_header(frame: &mut Frame, rect: Rect) { |
