fix(tui): render wrapped keybind footers
This commit is contained in:
1 parent
d123bb5c19
commit
1de0c7e139
6 files changed
+64
-25
No files matched your search
@@ -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(
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
Reference in new issue
Block a user