diff --git a/src/tui/tests/snapshots/rmcl__tui__tests__snapshots__empty_app_renders_the_complete_frame.snap b/src/tui/tests/snapshots/rmcl__tui__tests__snapshots__empty_app_renders_the_complete_frame.snap index fdaaf53..adbb9f0 100644 --- a/src/tui/tests/snapshots/rmcl__tui__tests__snapshots__empty_app_renders_the_complete_frame.snap +++ b/src/tui/tests/snapshots/rmcl__tui__tests__snapshots__empty_app_renders_the_complete_frame.snap @@ -25,8 +25,8 @@ expression: ui.screen() "│ ││ │" "│ ││ │" "│ ││ │" -"│ ││ │" -"│ │╰] open dir [Esc] kill [a] add─[i] import [d] delete [r] rename [/] search╯" +"│ ││ [l] launch [⏎] content [Shift+⏎] open dir [Esc] kill [a] add [i] import│" +"│ │╰────────────────────────────────────────────[d] delete [r] rename [/] search╯" "│ │╭Accounts─────────╮╭Settings───────────────────────────╮╭Overview──────────────╮" "│ ││No accounts. ││▸ instance default ││Ready │" "│ ││ ││ ││ │" diff --git a/src/tui/widgets/account.rs b/src/tui/widgets/account.rs index 1a6e20f..b22b335 100644 --- a/src/tui/widgets/account.rs +++ b/src/tui/widgets/account.rs @@ -220,18 +220,21 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut A .border_type(BORDER_STYLE.to_border_type()) .border_style(Style::default().fg(color)); - if focused == FocusedArea::Account { - let lines = super::popups::keybind_lines_wrapped( + let keybind_lines = if focused == FocusedArea::Account { + super::popups::keybind_lines_wrapped( &[("⏎", " select"), ("a", " add"), ("d", " del")], area.width.saturating_sub(2), - ); - for line in lines { - block = block.title_bottom(line); - } + ) + } else { + Vec::new() + }; + if let Some(line) = keybind_lines.last() { + block = block.title_bottom(line.clone()); } - let inner = block.inner(area); + let mut inner = block.inner(area); frame.render_widget(block, area); + inner = super::popups::render_keybind_overflow(frame, inner, &keybind_lines); if state.store.accounts.is_empty() { frame.render_widget( diff --git a/src/tui/widgets/content/tabs.rs b/src/tui/widgets/content/tabs.rs index 53ea4fc..a42631e 100644 --- a/src/tui/widgets/content/tabs.rs +++ b/src/tui/widgets/content/tabs.rs @@ -328,16 +328,17 @@ pub fn render( None }; - if let Some(kb) = kb { - let lines = - crate::tui::widgets::popups::keybind_lines_wrapped(kb, area.width.saturating_sub(2)); - for line in lines { - block = block.title_bottom(line); - } + let keybind_lines = kb.map_or_else(Vec::new, |keybinds| { + crate::tui::widgets::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2)) + }); + if let Some(line) = keybind_lines.last() { + block = block.title_bottom(line.clone()); } - let content_area = block.inner(area); + let mut content_area = block.inner(area); frame.render_widget(block, area); + content_area = + crate::tui::widgets::popups::render_keybind_overflow(frame, content_area, &keybind_lines); // lazy-load: only scan when switching to an instance that hasn't been loaded yet match tab { diff --git a/src/tui/widgets/popups/mod.rs b/src/tui/widgets/popups/mod.rs index 857ed67..045ca74 100644 --- a/src/tui/widgets/popups/mod.rs +++ b/src/tui/widgets/popups/mod.rs @@ -7,7 +7,12 @@ pub mod error; pub mod import_modpack; pub mod new_instance; -use ratatui::layout::Rect; +use ratatui::{ + Frame, + layout::{Alignment, Constraint, Layout, Rect}, + text::Line, + widgets::Paragraph, +}; // figures out the (width, height) a text block will need after word wrapping. // used to size popups before rendering so they fit their content snugly. @@ -77,7 +82,7 @@ pub fn keybind_line(binds: &[(&str, &str)]) -> ratatui::text::Line<'static> { Line::from(spans) } -// same as keybind_line but wraps to multiple rows when the popup is too narrow +// same as keybind_line but wraps to multiple rows when the panel is too narrow // to fit everything on one line pub fn keybind_lines_wrapped( binds: &[(&str, &str)], @@ -104,7 +109,7 @@ pub fn keybind_lines_wrapped( } else { 0 }; - let item_w = key.len() + 2 + label.len(); + let item_w = Span::raw(format!("[{key}]{label}")).width(); let needed = sep_w + item_w; if !current_spans.is_empty() && current_width + needed > max_width as usize { @@ -131,3 +136,21 @@ pub fn keybind_lines_wrapped( rows } + +pub fn render_keybind_overflow(frame: &mut Frame, area: Rect, lines: &[Line<'static>]) -> Rect { + let overflow = lines.len().saturating_sub(1); + if overflow == 0 { + return area; + } + + let [content, footer] = + Layout::vertical([Constraint::Min(0), Constraint::Length(overflow as u16)]).areas(area); + frame.render_widget( + Paragraph::new(lines[..overflow].to_vec()).alignment(Alignment::Right), + footer, + ); + content +} + +#[cfg(test)] +mod tests; diff --git a/src/tui/widgets/popups/tests.rs b/src/tui/widgets/popups/tests.rs new file mode 100644 index 0000000..a30b126 --- /dev/null +++ b/src/tui/widgets/popups/tests.rs @@ -0,0 +1,9 @@ +use super::keybind_lines_wrapped; + +#[test] +fn keybind_wrapping_uses_terminal_width_for_unicode_keys() { + let lines = keybind_lines_wrapped(&[("⏎", " select"), ("a", " add")], 19); + + assert_eq!(lines.len(), 1); + assert_eq!(lines[0].width(), 19); +} diff --git a/src/tui/widgets/settings.rs b/src/tui/widgets/settings.rs index 4b32004..9aa01e4 100644 --- a/src/tui/widgets/settings.rs +++ b/src/tui/widgets/settings.rs @@ -210,7 +210,7 @@ pub fn render( .border_type(BORDER_STYLE.to_border_type()) .border_style(Style::default().fg(color)); - if focused == FocusedArea::Settings { + let keybind_lines = if focused == FocusedArea::Settings { let keybinds: &[(&str, &str)] = match state.pane { SettingsPane::Profile => &[ ("⏎", " select"), @@ -226,14 +226,17 @@ pub fn render( ("h/l", " tab"), ], }; - let lines = super::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2)); - for line in lines { - block = block.title_bottom(line); - } + super::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2)) + } else { + Vec::new() + }; + if let Some(line) = keybind_lines.last() { + block = block.title_bottom(line.clone()); } - let inner = block.inner(area); + let mut inner = block.inner(area); frame.render_widget(block, area); + inner = super::popups::render_keybind_overflow(frame, inner, &keybind_lines); if inner.width >= 42 { let chunks = Layout::default()