fix: preserve status badges in selectors

This commit is contained in:
objz committed 2026-09-04 15:46:29 +02:00
1 parent 17aff8ec6d
commit 3566d6b50d
7 files changed
+110 -26

No files matched your search

+1 -4
View File
@@ -1685,10 +1685,7 @@ pub fn render(
theme.success()
}
});
let title_suffix_style = Style::default()
.fg(theme.background())
.bg(title_suffix_color)
.add_modifier(Modifier::BOLD);
let title_suffix_style = crate::tui::widgets::status_badge_style(title_suffix_color);
let footer_label_style = Style::default().fg(if world_details.is_some() {
theme.text_dim()
} else {
+1 -4
View File
@@ -248,10 +248,7 @@ pub fn render(
let mut content_titles = vec![
Span::styled(
mode_label(mode),
Style::default()
.fg(theme.background())
.bg(mode_background)
.add_modifier(Modifier::BOLD),
crate::tui::widgets::status_badge_style(mode_background),
),
Span::raw(" "),
];
+12 -1
View File
@@ -6,7 +6,7 @@
use crate::config::theme::THEME;
use crossterm::event::KeyEvent;
use ratatui::{
style::Style,
style::{Color, Modifier, Style},
text::{Line, Span},
};
@@ -38,6 +38,17 @@ pub fn styled_title(title: &str, highlight: bool) -> Line<'_> {
}
}
pub(crate) fn status_badge_style(color: Color) -> Style {
Style::default()
.fg(THEME.as_ref().background())
.bg(color)
.add_modifier(Modifier::BOLD)
}
pub(crate) fn status_badge(label: impl Into<String>, color: Color) -> Span<'static> {
Span::styled(format!(" {} ", label.into()), status_badge_style(color))
}
pub trait WidgetKey {
fn handle_key(&mut self, key_event: &KeyEvent);
}
+1 -1
View File
@@ -495,7 +495,7 @@ fn render_java_picker(frame: &mut Frame, area: Rect, state: &mut State) {
Err(error) => crate::feedback::errors::push_message(tracing::Level::ERROR, error),
}
}
super::select_list::render(
super::select_list::render_styled(
state.java_picker.items(),
state.java_picker.selected,
list_area,
+19 -5
View File
@@ -1165,7 +1165,9 @@ fn render_choice_picker(frame: &mut Frame, area: Rect, state: &mut State) {
}
let items = match state.choice_picker {
Some(ChoicePicker::Java) => state.java_picker.items(),
Some(ChoicePicker::Resolution) => resolution_items(&state.resolution_choices()),
Some(ChoicePicker::Resolution) => {
resolution_items(&state.resolution_choices(), state.choice_index)
}
_ => state
.choice_values()
.iter()
@@ -1177,17 +1179,29 @@ fn render_choice_picker(frame: &mut Frame, area: Rect, state: &mut State) {
})
.collect(),
};
super::select_list::render(items, state.choice_index, list_area, frame.buffer_mut());
if matches!(
state.choice_picker,
Some(ChoicePicker::Java | ChoicePicker::Resolution)
) {
super::select_list::render_styled(items, state.choice_index, list_area, frame.buffer_mut());
} else {
super::select_list::render(items, state.choice_index, list_area, frame.buffer_mut());
}
}
fn resolution_items(choices: &[ResolutionChoice]) -> Vec<ListItem<'static>> {
fn resolution_items(choices: &[ResolutionChoice], selected: usize) -> Vec<ListItem<'static>> {
let theme = THEME.as_ref();
choices
.iter()
.map(|choice| {
.enumerate()
.map(|(index, choice)| {
let mut spans = vec![Span::styled(
choice.label(),
Style::default().fg(theme.text()),
Style::default().fg(if index == selected {
theme.accent()
} else {
theme.text()
}),
)];
match choice {
ResolutionChoice::Preset(_, _) => {}
+53
View File
@@ -39,3 +39,56 @@ pub(crate) fn render(items: Vec<ListItem<'_>>, selected: usize, area: Rect, buff
let mut state = ListState::default().with_selected(Some(selected));
StatefulWidget::render(list, area, buffer, &mut state);
}
pub(crate) fn render_styled(
items: Vec<ListItem<'_>>,
selected: usize,
area: Rect,
buffer: &mut Buffer,
) {
let theme = THEME.as_ref();
let items = items
.into_iter()
.enumerate()
.map(|(index, item)| {
if index == selected {
item.style(Style::default().bg(theme.stripe()))
} else {
item
}
})
.collect::<Vec<_>>();
let list = List::new(items)
.highlight_style(Style::default().add_modifier(Modifier::BOLD))
.highlight_symbol(Span::styled(
"▶ ",
Style::default()
.fg(theme.accent())
.add_modifier(Modifier::BOLD),
));
let mut state = ListState::default().with_selected(Some(selected));
StatefulWidget::render(list, area, buffer, &mut state);
}
#[cfg(test)]
mod tests {
use ratatui::text::Line;
use super::*;
use crate::tui::widgets::status_badge;
#[test]
fn styled_list_preserves_badges_on_the_selected_row() {
let theme = THEME.as_ref();
let area = Rect::new(0, 0, 30, 1);
let mut buffer = Buffer::empty(area);
let badge = status_badge("Auto", theme.success());
let items = vec![ListItem::new(Line::from(vec![Span::raw("Java "), badge]))];
render_styled(items, 0, area, &mut buffer);
let badge_cell = buffer.cell((8, 0)).unwrap();
assert_eq!(badge_cell.bg, theme.success());
assert_eq!(badge_cell.fg, theme.background());
}
}
+23 -11
View File
@@ -16,7 +16,9 @@ use ratatui::{
use ratatui_textarea::TextArea;
use crate::{
config::theme::THEME, instance::java::JavaInstallation, tui::widgets::popups::LoadState,
config::theme::THEME,
instance::java::JavaInstallation,
tui::widgets::{popups::LoadState, status_badge},
};
const MEMORY_STEPS: [&str; 12] = [
@@ -147,7 +149,8 @@ impl JavaPicker {
};
self.choices()
.into_iter()
.map(|choice| match choice {
.enumerate()
.map(|(index, choice)| match choice {
JavaChoice::Installation(path) => {
let installation = installations
.iter()
@@ -155,9 +158,24 @@ impl JavaPicker {
let version = installation
.and_then(|installation| installation.version.as_deref())
.map_or_else(|| "Java".to_owned(), |version| format!("Java {version}"));
let selected = index == self.selected;
let mut spans = vec![
Span::styled(version, Style::default().fg(theme.text())),
Span::styled(format!(" {path}"), Style::default().fg(theme.text_dim())),
Span::styled(
version,
Style::default().fg(if selected {
theme.accent()
} else {
theme.text()
}),
),
Span::styled(
format!(" {path}"),
Style::default().fg(if selected {
theme.accent()
} else {
theme.text_dim()
}),
),
];
if self.current.is_none() && path == self.detected {
spans.extend([Span::raw(" "), auto_label()]);
@@ -341,13 +359,7 @@ pub(crate) fn handle_text_area_input(input: &mut TextArea<'_>, key: &KeyEvent) {
}
pub(crate) fn auto_label() -> Span<'static> {
Span::styled(
" Auto ",
Style::default()
.fg(THEME.as_ref().text_bright())
.bg(THEME.as_ref().info())
.add_modifier(Modifier::BOLD),
)
status_badge("Auto", THEME.as_ref().success())
}
pub(crate) fn display_resolutions() -> Vec<DisplayResolution> {