diff options
| author | lukeflo | 2024-11-16 00:35:29 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-16 00:35:29 +0100 |
| commit | c639cd2b7bd37e98e4469b8fb6f6d92660acaed0 (patch) | |
| tree | 7075e830f2beb6d52e5af802c18957d994b8d2e1 | |
| parent | 511d380da8e198c01cd73fe9da35d557584a0023 (diff) | |
| download | bibiman-c639cd2b7bd37e98e4469b8fb6f6d92660acaed0.tar.gz bibiman-c639cd2b7bd37e98e4469b8fb6f6d92660acaed0.zip | |
removed tui-popup, wrote own popup code
| -rw-r--r-- | Cargo.lock | 98 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/tui/ui.rs | 84 |
3 files changed, 51 insertions, 132 deletions
@@ -94,7 +94,6 @@ dependencies = [ "tokio", "tokio-util", "tui-input", - "tui-popup", ] [[package]] @@ -311,52 +310,6 @@ dependencies = [ ] [[package]] -name = "darling" -version = "0.20.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" -dependencies = [ - "darling_core", - "darling_macro", -] - -[[package]] -name = "darling_core" -version = "0.20.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" -dependencies = [ - "fnv", - "ident_case", - "proc-macro2", - "quote", - "strsim", - "syn", -] - -[[package]] -name = "darling_macro" -version = "0.20.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" -dependencies = [ - "darling_core", - "quote", - "syn", -] - -[[package]] -name = "derive-getters" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "74ef43543e701c01ad77d3a5922755c6a1d71b22d942cb8042be4994b380caff" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "derive-new" version = "0.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -368,18 +321,6 @@ dependencies = [ ] [[package]] -name = "derive_setters" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4e8ef033054e131169b8f0f9a7af8f5533a9436fadf3c500ed547f730f07090d" -dependencies = [ - "darling", - "proc-macro2", - "quote", - "syn", -] - -[[package]] name = "displaydoc" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -400,15 +341,6 @@ dependencies = [ ] [[package]] -name = "document-features" -version = "0.2.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb6969eaabd2421f8a2775cfd2471a2b634372b4a25d41e3bd647b79912850a0" -dependencies = [ - "litrs", -] - -[[package]] name = "doi" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -816,12 +748,6 @@ dependencies = [ ] [[package]] -name = "ident_case" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" - -[[package]] name = "idna" version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -943,12 +869,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "643cb0b8d4fcc284004d5fd0d67ccf61dfffadb7f75e1e71bc420f4688a3a704" [[package]] -name = "litrs" -version = "0.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" - -[[package]] name = "lock_api" version = "0.4.12" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1556,12 +1476,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" [[package]] -name = "strsim" -version = "0.11.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" - -[[package]] name = "strum" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1798,18 +1712,6 @@ dependencies = [ ] [[package]] -name = "tui-popup" -version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9ee3d08800c83ba0a2efaec44d225bcc3f885f30e2b520a17e2cd962b7da6ab" -dependencies = [ - "derive-getters", - "derive_setters", - "document-features", - "ratatui", -] - -[[package]] name = "unicode-ident" version = "1.0.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -25,4 +25,3 @@ signal-hook = "0.3.17" tokio = { version = "1.39.3", features = ["full"] } tokio-util = "0.7.12" tui-input = "0.11.0" -tui-popup = "0.6.0" diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 50fac50..6971fcb 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -25,10 +25,10 @@ use crate::{ TEXT_HIGHLIGHT_COLOR_INDEX, }; use ratatui::layout::{Direction, Position}; -use ratatui::widgets::{Clear, ListState, StatefulWidget}; +use ratatui::widgets::Clear; use ratatui::Frame; use ratatui::{ - layout::{Alignment, Constraint, Layout, Rect}, + layout::{Alignment, Constraint, Flex, Layout, Rect}, style::{Color, Modifier, Style, Stylize}, symbols, text::{Line, Span, Text}, @@ -37,7 +37,6 @@ use ratatui::{ ScrollbarOrientation, Table, Wrap, }, }; -use tui_popup::{Popup, SizedWrapper}; // Text colors const TEXT_FG_COLOR: Color = Color::Indexed(TEXT_FG_COLOR_INDEX); @@ -163,52 +162,60 @@ pub fn render_ui(app: &mut App, frame: &mut Frame) { pub fn render_popup(app: &mut App, frame: &mut Frame) { match app.bibiman.popup_area.popup_kind { Some(PopupKind::Help) => { + let block = Block::bordered() + .title_top(" Keybindings (j,k|↓,↑)".bold()) + .title_alignment(Alignment::Center) + .style(POPUP_HELP_BOX) + .border_set(symbols::border::THICK) + .border_style(Style::new().fg(MAIN_BLUE)); + let text: Text = PopupArea::popup_help(); // Calculate max scroll position depending on hight of terminal window + // Needed length is number of text lines plus two for borders at bottom and top + // minus half the height of the frame (or the height set for the popup) + let popup_height: u16 = frame.area().height / 2; + let scroll_pos = if app.bibiman.popup_area.popup_scroll_pos - > text.lines.len() as u16 - (frame.area().height / 2) + > text.lines.len() as u16 + 2 - popup_height { app.bibiman.popup_area.popup_scroll_pos = - text.lines.len() as u16 - (frame.area().height / 2); + text.lines.len() as u16 + 2 - popup_height; 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 = Paragraph::new(text).scroll((scroll_pos, 0)).block(block); 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, - }; + let popup_area = popup_area(frame.area(), par_width as u16, popup_height); - 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)); - - frame.render_widget_ref(popup, frame.area()) + frame.render_widget(Clear, popup_area); + frame.render_widget(par, popup_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()) + let area = frame.area(); + + let block = Block::bordered() + .title_top(" Message ".bold().fg(MAIN_GREEN)) + .border_style(Style::new().fg(MAIN_GREEN)) + .style(POPUP_HELP_BOX); + + let content = Paragraph::new(app.bibiman.popup_area.popup_message.clone()) + .block(block) + .style(Style::new().fg(MAIN_GREEN)); + + // Calculate popup size. Width is number of string chars plus 2 for border + let popup_area = popup_area( + area, + (app.bibiman.popup_area.popup_message.chars().count() + 2) as u16, + 3, + ); + + // Clear area and draw popup + frame.render_widget(Clear, popup_area); + frame.render_widget(&content, popup_area) } Some(PopupKind::Selection) => { // let list_items: Vec<ListItem> = app @@ -859,3 +866,14 @@ fn render_cursor(app: &mut App, frame: &mut Frame, area: Rect, x_offset: u16) { frame.set_cursor_position(Position::new(x, y)); } } + +/// helper function to create a centered rect using up certain percentage of the available rect `r` +fn popup_area(area: Rect, percent_x: u16, percent_y: u16) -> Rect { + // let vertical = Layout::vertical([Constraint::Percentage(percent_y)]).flex(Flex::Center); + // let horizontal = Layout::horizontal([Constraint::Percentage(percent_x)]).flex(Flex::Center); + let vertical = Layout::vertical([Constraint::Length(percent_y)]).flex(Flex::Center); + let horizontal = Layout::horizontal([Constraint::Length(percent_x)]).flex(Flex::Center); + let [area] = vertical.areas(area); + let [area] = horizontal.areas(area); + area +} |
