diff options
| author | lukeflo | 2024-11-15 15:56:57 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-15 15:56:57 +0100 |
| commit | 511d380da8e198c01cd73fe9da35d557584a0023 (patch) | |
| tree | f385c11fb10ebed45656207cce602b7d7f17494c | |
| parent | b9a2a79b5ae1315c03029adf37661deb9140b8dd (diff) | |
| download | bibiman-511d380da8e198c01cd73fe9da35d557584a0023.tar.gz bibiman-511d380da8e198c01cd73fe9da35d557584a0023.zip | |
selection popup: not impl as trait...
| -rw-r--r-- | src/app.rs | 4 | ||||
| -rw-r--r-- | src/tui/popup.rs | 7 | ||||
| -rw-r--r-- | src/tui/ui.rs | 128 |
3 files changed, 92 insertions, 47 deletions
@@ -141,6 +141,8 @@ impl App { CurrentArea::PopupArea => { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.popup_area.popup_scroll_down(); + } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { + self.bibiman.popup_area.popup_state.scroll_down_by(1) } } _ => {} @@ -156,6 +158,8 @@ impl App { CurrentArea::PopupArea => { if let Some(PopupKind::Help) = self.bibiman.popup_area.popup_kind { self.bibiman.popup_area.popup_scroll_up(); + } else if let Some(PopupKind::Selection) = self.bibiman.popup_area.popup_kind { + self.bibiman.popup_area.popup_state.scroll_up_by(1) } } _ => {} diff --git a/src/tui/popup.rs b/src/tui/popup.rs index 08abfb0..7c54b94 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -28,7 +28,6 @@ pub enum PopupKind { Help, Message, Selection, - Search, } #[derive(Debug, Default)] @@ -111,6 +110,12 @@ impl PopupArea { self.is_popup = true; } + pub fn popup_selection(&mut self, items: Vec<String>) { + self.popup_list = items; + self.popup_kind = Some(PopupKind::Selection); + self.is_popup = true + } + pub fn popup_scroll_down(&mut self) { self.popup_scroll_pos = self.popup_scroll_pos.saturating_add(1) } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 19b5e31..50fac50 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -25,7 +25,7 @@ use crate::{ TEXT_HIGHLIGHT_COLOR_INDEX, }; use ratatui::layout::{Direction, Position}; -use ratatui::widgets::Clear; +use ratatui::widgets::{Clear, ListState, StatefulWidget}; use ratatui::Frame; use ratatui::{ layout::{Alignment, Constraint, Layout, Rect}, @@ -85,6 +85,10 @@ const KEYWORD_SELECTED_ROW_STYLE: Style = Style::new() .fg(MAIN_PURPLE) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); +const SELECTION_SELECTED_ROW_STYLE: Style = Style::new() + // .fg(MAIN_BLUE) + .add_modifier(Modifier::BOLD) + .add_modifier(Modifier::REVERSED); const SELECTED_TABLE_COL_STYLE: Style = Style::new().add_modifier(Modifier::BOLD); const SELECTEC_TABLE_CELL_STYLE: Style = Style::new().add_modifier(Modifier::REVERSED); @@ -157,54 +161,86 @@ pub fn render_ui(app: &mut App, frame: &mut Frame) { } pub fn render_popup(app: &mut App, frame: &mut Frame) { - if let Some(PopupKind::Help) = app.bibiman.popup_area.popup_kind { - let text: Text = 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)); - let par_width = par.line_width(); - - // Needed to use scrollable Parapgraph as popup content - let sized_par = SizedWrapper { - inner: par, - // width: (frame.area().width / 2) as usize, - width: if par_width > frame.area().width as usize { - frame.area().width as usize + match app.bibiman.popup_area.popup_kind { + Some(PopupKind::Help) => { + let text: Text = 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 { - par_width - }, - // width: par_width, - height: (frame.area().height / 2) as usize, - }; - - let popup = Popup::new(sized_par) - .title(" Keybindings (j,k|↓,↑)".bold().into_centered_line()) - .style(POPUP_HELP_BOX) - .border_set(symbols::border::THICK) - .border_style(Style::new().fg(MAIN_BLUE)); + app.bibiman.popup_area.popup_scroll_pos + }; + + let par = Paragraph::new(text).scroll((scroll_pos, 0)); + let par_width = par.line_width(); + + // Needed to use scrollable Parapgraph as popup content + let sized_par = SizedWrapper { + inner: par, + // width: (frame.area().width / 2) as usize, + width: if par_width > frame.area().width as usize { + frame.area().width as usize + } else { + par_width + }, + // width: par_width, + height: (frame.area().height / 2) as usize, + }; - frame.render_widget_ref(popup, frame.area()) - } else if let Some(PopupKind::Message) = app.bibiman.popup_area.popup_kind { - 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)) + let popup = Popup::new(sized_par) + .title(" Keybindings (j,k|↓,↑)".bold().into_centered_line()) + .style(POPUP_HELP_BOX) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(MAIN_GREEN)) - .style(POPUP_HELP_BOX); - frame.render_widget(&popup, frame.area()) - } else { - panic!("No popup text detected") - }; + .border_style(Style::new().fg(MAIN_BLUE)); + + frame.render_widget_ref(popup, frame.area()) + } + Some(PopupKind::Message) => { + 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)) + .border_set(symbols::border::THICK) + .border_style(Style::new().fg(MAIN_GREEN)) + .style(POPUP_HELP_BOX); + frame.render_widget(&popup, frame.area()) + } + Some(PopupKind::Selection) => { + // let list_items: Vec<ListItem> = app + // .bibiman + // .popup_area + // .popup_list + // .iter() + // .map(|item| ListItem::from(item.to_owned())) + // .collect(); + + // let list = List::new(list_items).highlight_style(SELECTION_SELECTED_ROW_STYLE); + + // let sized_list = SizedWrapper { + // inner: list.clone(), + // width: (frame.area().width / 2) as usize, + // height: list.len(), + // }; + + // let popup = Popup::new(sized_list) + // .title(" Select ".bold().into_centered_line().fg(MAIN_PURPLE)) + // .border_set(symbols::border::THICK) + // // .border_style(Style::new().fg(MAIN_GREEN)) + // .style(POPUP_HELP_BOX); + + // frame.render_stateful_widget( + // &popup, + // frame.area(), + // &mut app.bibiman.popup_area.popup_state, + // ) + } + None => {} + } } pub fn render_header(frame: &mut Frame, rect: Rect) { |
