fix(tui): keep keybind footers on one line
This commit is contained in:
1 parent
63b4adaca8
commit
59707b77d2
7 files changed
+48
-109
No files matched your search
@@ -220,21 +220,20 @@ 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));
|
||||
|
||||
let keybind_lines = if focused == FocusedArea::Account {
|
||||
super::popups::keybind_lines_wrapped(
|
||||
let keybind_line = if focused == FocusedArea::Account {
|
||||
Some(super::popups::keybind_line_fitted(
|
||||
&[("⏎", " select"), ("a", " add"), ("d", " del")],
|
||||
area.width.saturating_sub(2),
|
||||
)
|
||||
))
|
||||
} else {
|
||||
Vec::new()
|
||||
None
|
||||
};
|
||||
if let Some(line) = keybind_lines.last() {
|
||||
block = block.title_bottom(line.clone());
|
||||
if let Some(line) = keybind_line {
|
||||
block = block.title_bottom(line);
|
||||
}
|
||||
|
||||
let mut inner = block.inner(area);
|
||||
let 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(
|
||||
|
||||
@@ -345,7 +345,7 @@ pub fn render(
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Worlds) if open_world_datapacks.is_some() => &[
|
||||
("j/k", " navigate"),
|
||||
@@ -364,7 +364,7 @@ pub fn render(
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Worlds) => &[
|
||||
("j/k", " navigate"),
|
||||
@@ -373,7 +373,7 @@ pub fn render(
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::DataPacks) => &[],
|
||||
(ContentMode::Installed, ContentTab::Screenshots) => &[
|
||||
@@ -383,7 +383,7 @@ pub fn render(
|
||||
("Shift+⏎", " open dir"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
],
|
||||
(ContentMode::Installed, ContentTab::Logs) => {
|
||||
if logs_state.viewer_focused {
|
||||
@@ -393,7 +393,7 @@ pub fn render(
|
||||
("d", " delete"),
|
||||
("Esc", " back"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
]
|
||||
} else {
|
||||
&[
|
||||
@@ -402,7 +402,7 @@ pub fn render(
|
||||
("d", " delete"),
|
||||
("h/l", " tabs"),
|
||||
("/", " search"),
|
||||
("Tab", " discover"),
|
||||
("Tab", " discovery"),
|
||||
]
|
||||
}
|
||||
}
|
||||
@@ -428,16 +428,14 @@ pub fn render(
|
||||
if is_focused && mode == ContentMode::Installed && !has_updates {
|
||||
keybinds.retain(|(key, _)| *key != "u");
|
||||
}
|
||||
let keybind_lines =
|
||||
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());
|
||||
}
|
||||
// The main panel footer stays on its border; lower-priority hints are omitted when narrow.
|
||||
block = block.title_bottom(crate::tui::widgets::popups::keybind_line_fitted(
|
||||
&keybinds,
|
||||
area.width.saturating_sub(2),
|
||||
));
|
||||
|
||||
let mut content_area = block.inner(area);
|
||||
let content_area = block.inner(area);
|
||||
frame.render_widget(block, area);
|
||||
content_area =
|
||||
crate::tui::widgets::popups::render_keybind_overflow(frame, content_area, &keybind_lines);
|
||||
|
||||
let downloadable_state = match tab {
|
||||
ContentTab::Mods => Some((mods_state, mods_discovery_state)),
|
||||
|
||||
@@ -11,12 +11,7 @@ pub mod new_instance;
|
||||
|
||||
pub use load_state::LoadState;
|
||||
|
||||
use ratatui::{
|
||||
Frame,
|
||||
layout::{Alignment, Constraint, Layout, Rect},
|
||||
text::{Line, Span},
|
||||
widgets::Paragraph,
|
||||
};
|
||||
use ratatui::{layout::Rect, text::Span};
|
||||
|
||||
pub(crate) fn compare_game_versions(a: &str, b: &str) -> std::cmp::Ordering {
|
||||
let parse_parts = |version: &str| {
|
||||
@@ -106,74 +101,19 @@ 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 panel is too narrow
|
||||
// to fit everything on one line
|
||||
pub fn keybind_lines_wrapped(
|
||||
binds: &[(&str, &str)],
|
||||
max_width: u16,
|
||||
) -> Vec<ratatui::text::Line<'static>> {
|
||||
use crate::config::theme::THEME;
|
||||
use ratatui::{
|
||||
style::{Modifier, Style},
|
||||
text::{Line, Span},
|
||||
};
|
||||
let theme = THEME.as_ref();
|
||||
let key_style = Style::default()
|
||||
.fg(theme.accent())
|
||||
.add_modifier(Modifier::BOLD);
|
||||
let label_style = Style::default().fg(theme.text());
|
||||
|
||||
let mut rows: Vec<Line<'static>> = Vec::new();
|
||||
let mut current_spans: Vec<Span<'static>> = Vec::new();
|
||||
let mut current_width: usize = 0;
|
||||
|
||||
for (i, (key, label)) in binds.iter().enumerate() {
|
||||
let sep_w = if i > 0 && !current_spans.is_empty() {
|
||||
2
|
||||
} else {
|
||||
0
|
||||
};
|
||||
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 {
|
||||
rows.push(Line::from(current_spans).right_aligned());
|
||||
current_spans = Vec::new();
|
||||
current_width = 0;
|
||||
pub fn keybind_line_fitted(binds: &[(&str, &str)], max_width: u16) -> ratatui::text::Line<'static> {
|
||||
let mut width = 0;
|
||||
let mut count = 0;
|
||||
for (key, label) in binds {
|
||||
let item_width = Span::raw(format!("[{key}]{label}")).width() + usize::from(count > 0) * 2;
|
||||
if width + item_width > max_width as usize {
|
||||
break;
|
||||
}
|
||||
|
||||
if !current_spans.is_empty() {
|
||||
current_spans.push(Span::styled(" ", label_style));
|
||||
current_width += 2;
|
||||
}
|
||||
|
||||
current_spans.push(Span::styled(format!("[{}]", key), key_style));
|
||||
if !label.is_empty() {
|
||||
current_spans.push(Span::styled(label.to_string(), label_style));
|
||||
}
|
||||
current_width += item_w;
|
||||
width += item_width;
|
||||
count += 1;
|
||||
}
|
||||
|
||||
if !current_spans.is_empty() {
|
||||
rows.push(Line::from(current_spans).right_aligned());
|
||||
}
|
||||
|
||||
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
|
||||
keybind_line(&binds[..count]).right_aligned()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -210,7 +210,7 @@ pub fn render(
|
||||
.border_type(BORDER_STYLE.to_border_type())
|
||||
.border_style(Style::default().fg(color));
|
||||
|
||||
let keybind_lines = if focused == FocusedArea::Settings {
|
||||
let keybind_line = if focused == FocusedArea::Settings {
|
||||
let keybinds: &[(&str, &str)] = match state.pane {
|
||||
SettingsPane::Profile => &[
|
||||
("⏎", " select"),
|
||||
@@ -226,17 +226,19 @@ pub fn render(
|
||||
("h/l", " tab"),
|
||||
],
|
||||
};
|
||||
super::popups::keybind_lines_wrapped(keybinds, area.width.saturating_sub(2))
|
||||
Some(super::popups::keybind_line_fitted(
|
||||
keybinds,
|
||||
area.width.saturating_sub(2),
|
||||
))
|
||||
} else {
|
||||
Vec::new()
|
||||
None
|
||||
};
|
||||
if let Some(line) = keybind_lines.last() {
|
||||
block = block.title_bottom(line.clone());
|
||||
if let Some(line) = keybind_line {
|
||||
block = block.title_bottom(line);
|
||||
}
|
||||
|
||||
let mut inner = block.inner(area);
|
||||
let 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