From 84ec724d4f31fd556ca5ff347aca689861671792 Mon Sep 17 00:00:00 2001 From: objz Date: Fri, 4 Sep 2026 13:28:39 +0200 Subject: [PATCH] fix: refine settings popup visuals --- src/tui/tests/flows.rs | 3 + src/tui/widgets/popups/instance_settings.rs | 274 +++++++++++++------- src/tui/widgets/popups/settings_controls.rs | 103 ++++---- 3 files changed, 230 insertions(+), 150 deletions(-) diff --git a/src/tui/tests/flows.rs b/src/tui/tests/flows.rs index c0c0eee..3d49b98 100644 --- a/src/tui/tests/flows.rs +++ b/src/tui/tests/flows.rs @@ -679,6 +679,7 @@ fn settings_use_java_memory_and_resolution_controls() { assert!(ui.screen().contains("Java Runtime")); assert!(ui.screen().contains("Automatic")); assert!(ui.screen().contains("Custom path")); + assert!(!ui.screen().contains("Manual")); ui.key(KeyCode::Esc); ui.key(KeyCode::Char('j')); @@ -701,6 +702,8 @@ fn settings_use_java_memory_and_resolution_controls() { ui.draw(); assert!(ui.screen().contains("Resolution")); assert!(ui.screen().contains("1920x1080")); + assert!(!ui.screen().contains("Preset")); + assert!(!ui.screen().contains("Inherit")); for _ in 0..20 { ui.key(KeyCode::Char('j')); } diff --git a/src/tui/widgets/popups/instance_settings.rs b/src/tui/widgets/popups/instance_settings.rs index 056d3c9..59b5b88 100644 --- a/src/tui/widgets/popups/instance_settings.rs +++ b/src/tui/widgets/popups/instance_settings.rs @@ -25,8 +25,8 @@ use crate::{ popups::{ LoadState, settings_controls::{ - DisplayResolution, JavaChoice, JavaPicker, adjust_memory, badge, - display_resolutions, memory_kib, render_memory_gauge, + DisplayResolution, JavaChoice, JavaPicker, adjust_memory, display_resolutions, + memory_kib, render_memory_gauge, }, }, search::SearchState, @@ -739,7 +739,9 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect { } else if state.choice_picker.is_some() { 10 } else { - 11 + u16::from(state.error.is_some()) + let form_width = (area.width * 58 / 100).saturating_sub(2); + 11 + jvm_row_count(state, form_width).saturating_sub(1) as u16 + + u16::from(state.error.is_some()) }; let width = match state.choice_picker { Some(ChoicePicker::Java) => 72, @@ -838,48 +840,73 @@ fn render_settings_list(frame: &mut Frame, area: Rect, state: &State) { "Resolution", "Desktop shortcut", ]; - let lines = labels - .iter() - .enumerate() - .map(|(index, label)| field_line(state, index, label)) - .chain(state.error.iter().map(|error| { - Line::from(Span::styled( - format!(" {error}"), - Style::default().fg(theme.error()), - )) - })) - .collect::>(); - frame.render_widget(Paragraph::new(lines), area); - - for field in [4, 5] { - let value = state.effective_memory(field); - let gauge_area = Rect { - x: area.x.saturating_add(20), - y: area.y.saturating_add(field as u16), - width: area.width.saturating_sub(21), - height: 1, + let mut y = area.y; + for (index, label) in labels.iter().enumerate() { + let lines = if index == 6 { + jvm_field_lines(state, area.width) + } else { + vec![field_line(state, index, label)] }; - render_memory_gauge( - frame, - gauge_area, - &value, - state.display_value(field), - state.selected == field, + let height = lines.len() as u16; + let row_area = Rect { y, height, ..area }; + frame.render_widget( + Paragraph::new(lines).style(Style::default().bg(if state.selected == index { + theme.stripe() + } else { + theme.surface() + })), + row_area, ); + + if matches!(index, 4 | 5) { + let value = state.effective_memory(index); + render_memory_gauge( + frame, + Rect { + x: area.x.saturating_add(20), + y, + width: area.width.saturating_sub(21), + height: 1, + }, + &value, + state.display_value(index), + state.selected == index, + ); + } + if state.selected == index + && let Some(editor) = state.editing.as_ref() + { + frame.render_widget( + editor, + Rect { + x: area.x.saturating_add(20), + y, + width: area.width.saturating_sub(20), + height: 1, + }, + ); + } + y = y.saturating_add(height); } - if let Some(editor) = state.editing.as_ref() { - let edit_area = Rect { - x: area.x.saturating_add(20), - y: area.y.saturating_add(state.selected as u16), - width: area.width.saturating_sub(20), - height: 1, - }; - frame.render_widget(editor, edit_area); + if let Some(error) = &state.error + && y < area.bottom() + { + frame.render_widget( + Paragraph::new(Span::styled( + format!(" {error}"), + Style::default().fg(theme.error()), + )), + Rect { + y, + height: 1, + ..area + }, + ); } } -fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { +fn field_line(state: &State, index: usize, label: &str) -> Line<'static> { let theme = THEME.as_ref(); let selected = index == state.selected; let editing = selected && state.editing.is_some(); @@ -889,7 +916,7 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { state.display_value(index) }; let dirty = field_dirty(state, index); - let mut spans = vec![ + Line::from(vec![ Span::styled( if selected { "▶ " } else { " " }, Style::default().fg(theme.accent()), @@ -918,37 +945,74 @@ fn field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { Modifier::empty() }), ), - ]; - if index == 6 && !editing { - if state.draft.jvm_args.is_empty() { - spans.push(Span::styled( - "no arguments", - Style::default().fg(theme.text_dim()), - )); - } else { - for (argument_index, argument) in state.draft.jvm_args.iter().enumerate() { - if argument_index > 0 { - spans.push(Span::raw(" ")); - } - spans.push(Span::styled( - format!(" {argument} "), - Style::default() - .fg(if selected { - theme.accent() - } else { - theme.text() - }) - .bg(theme.background()) - .add_modifier(Modifier::BOLD), - )); - } - } + ]) +} + +fn jvm_field_lines(state: &State, width: u16) -> Vec> { + let theme = THEME.as_ref(); + let selected = state.selected == 6; + let mut prefix = field_line(state, 6, "JVM args").spans; + if selected && state.editing.is_some() { + return vec![Line::from(prefix)]; } - Line::from(spans).style(Style::default().bg(if selected { - theme.stripe() - } else { - theme.surface() - })) + if state.draft.jvm_args.is_empty() { + prefix.push(Span::styled( + "no arguments", + Style::default().fg(theme.text_dim()), + )); + return vec![Line::from(prefix)]; + } + + let available = width.saturating_sub(20) as usize; + let mut lines = Vec::new(); + let mut spans = prefix; + let mut used = 0usize; + for argument in &state.draft.jvm_args { + let badge_width = argument.chars().count() + 2; + let separator = usize::from(used > 0); + if used > 0 && used + separator + badge_width > available { + lines.push(Line::from(spans)); + spans = vec![Span::raw(" ".repeat(20))]; + used = 0; + } + if used > 0 { + spans.push(Span::raw(" ")); + used += 1; + } + spans.push(Span::styled( + format!(" {argument} "), + Style::default() + .fg(if selected { + theme.accent() + } else { + theme.text() + }) + .bg(theme.background()) + .add_modifier(Modifier::BOLD), + )); + used += badge_width; + } + lines.push(Line::from(spans)); + lines +} + +fn jvm_row_count(state: &State, width: u16) -> usize { + if state.draft.jvm_args.is_empty() || state.selected == 6 && state.editing.is_some() { + return 1; + } + let available = width.saturating_sub(20) as usize; + let mut rows = 1; + let mut used = 0usize; + for argument in &state.draft.jvm_args { + let badge_width = argument.chars().count() + 2; + let separator = usize::from(used > 0); + if used > 0 && used + separator + badge_width > available { + rows += 1; + used = 0; + } + used += usize::from(used > 0) + badge_width; + } + rows } fn field_dirty(state: &State, index: usize) -> bool { @@ -1010,40 +1074,36 @@ fn resolution_items(choices: &[ResolutionChoice]) -> Vec> { Style::default().fg(theme.text()), )]; match choice { - ResolutionChoice::Default => { - spans.extend([Span::raw(" "), badge(" Inherit ", theme.info())]); - } + ResolutionChoice::Default | ResolutionChoice::Preset(_, _) => {} ResolutionChoice::Display(display) => { - spans.extend([ - Span::raw(" "), - badge( - if display.primary { - " Primary " - } else { - " Current " - }, - if display.primary { - theme.success() - } else { - theme.info() - }, - ), - ]); if !display.name.is_empty() { - spans.push(Span::styled( - format!(" {}", display.name), - Style::default().fg(theme.text_dim()), - )); + spans.extend([ + Span::raw(" "), + Span::styled( + format!(" {} ", display.name), + Style::default() + .fg(if display.primary { + theme.success() + } else { + theme.info() + }) + .bg(theme.stripe()), + ), + ]); } } - ResolutionChoice::Preset(_, _) => { - spans.extend([Span::raw(" "), badge(" Preset ", theme.info())]); - } ResolutionChoice::Configured(_, _) => { - spans.extend([Span::raw(" "), badge(" Configured ", theme.success())]); + spans.push(Span::styled( + " configured", + Style::default().fg(theme.text_dim()), + )); } ResolutionChoice::Custom => { - spans.extend([Span::raw(" "), badge(" Manual ", theme.warning())]); + spans.clear(); + spans.push(Span::styled( + choice.label(), + Style::default().fg(theme.warning()), + )); } } ListItem::new(Line::from(spans)) @@ -1348,4 +1408,26 @@ mod tests { 1 ); } + + #[test] + fn jvm_argument_badges_wrap_without_hiding_following_fields() { + let temp = tempfile::tempdir().unwrap(); + let mut config = instance(); + config.jvm_args = [ + "-Xmx4G", + "-XX:+UseG1GC", + "-Dexample.first=true", + "-Dexample.second=true", + ] + .into_iter() + .map(str::to_owned) + .collect(); + let state = State::new(&config, temp.path()); + + let lines = jvm_field_lines(&state, 48); + + assert!(lines.len() > 1); + assert_eq!(jvm_row_count(&state, 48), lines.len()); + assert!(lines.iter().any(|line| line.to_string().contains("second"))); + } } diff --git a/src/tui/widgets/popups/settings_controls.rs b/src/tui/widgets/popups/settings_controls.rs index 05f8d62..154ff1f 100644 --- a/src/tui/widgets/popups/settings_controls.rs +++ b/src/tui/widgets/popups/settings_controls.rs @@ -8,7 +8,7 @@ use std::sync::{Arc, Mutex}; use ratatui::{ Frame, layout::Rect, - style::{Color, Modifier, Style}, + style::{Modifier, Style}, text::{Line, Span}, widgets::{LineGauge, ListItem, Paragraph}, }; @@ -149,9 +149,12 @@ impl JavaPicker { .into_iter() .map(|choice| match choice { JavaChoice::Automatic => ListItem::new(Line::from(vec![ - Span::styled("Automatic", Style::default().fg(theme.text())), - Span::raw(" "), - badge(" Auto ", theme.info()), + Span::styled( + "Automatic", + Style::default() + .fg(theme.info()) + .add_modifier(Modifier::BOLD), + ), Span::styled( format!(" {}", self.detected), Style::default().fg(theme.text_dim()), @@ -164,26 +167,15 @@ impl JavaPicker { let version = installation .and_then(|installation| installation.version.as_deref()) .map_or_else(|| "Java".to_owned(), |version| format!("Java {version}")); - let current = self.current.as_deref() == Some(path.as_str()); ListItem::new(Line::from(vec![ Span::styled(version, Style::default().fg(theme.text())), - Span::raw(" "), - badge( - if current { " Current " } else { " Detected " }, - if current { - theme.success() - } else { - theme.info() - }, - ), Span::styled(format!(" {path}"), Style::default().fg(theme.text_dim())), ])) } - JavaChoice::Custom => ListItem::new(Line::from(vec![ - Span::styled("Custom path…", Style::default().fg(theme.text())), - Span::raw(" "), - badge(" Manual ", theme.warning()), - ])), + JavaChoice::Custom => ListItem::new(Line::from(Span::styled( + "Custom path…", + Style::default().fg(theme.warning()), + ))), }) .collect() } @@ -323,7 +315,11 @@ pub(crate) fn render_memory_gauge( let thumb_offset = ((line_area.width.saturating_sub(1)) as f64 * ratio).round() as u16; frame.render_widget( Paragraph::new(Span::styled( - "◆", + if thumb_offset + 1 < line_area.width { + "◆ " + } else { + "◆" + }, Style::default().fg(if selected { theme.accent() } else { @@ -332,7 +328,7 @@ pub(crate) fn render_memory_gauge( )), Rect { x: line_area.x.saturating_add(thumb_offset), - width: 1, + width: 2.min(line_area.width.saturating_sub(thumb_offset)), ..line_area }, ); @@ -343,43 +339,27 @@ pub(crate) fn display_resolutions() -> Vec { let Ok(displays) = display_info::DisplayInfo::all() else { return Vec::new(); }; - let mut resolutions = Vec::::new(); - for display in displays { - if display.width == 0 || display.height == 0 { - continue; - } - if let Some(existing) = resolutions.iter_mut().find(|resolution| { - resolution.width == display.width && resolution.height == display.height - }) { - existing.primary |= display.is_primary; - continue; - } - let name = if display.friendly_name.is_empty() { - display.name - } else { - display.friendly_name - }; - resolutions.push(DisplayResolution { - width: display.width, - height: display.height, - name, - primary: display.is_primary, - }); - } + let mut resolutions = displays + .into_iter() + .filter(|display| display.width > 0 && display.height > 0) + .map(|display| { + let name = if display.name.is_empty() { + display.friendly_name + } else { + display.name + }; + DisplayResolution { + width: display.width, + height: display.height, + name, + primary: display.is_primary, + } + }) + .collect::>(); resolutions.sort_by_key(|resolution| !resolution.primary); resolutions } -pub(crate) fn badge(text: &'static str, color: Color) -> Span<'static> { - Span::styled( - text, - Style::default() - .fg(THEME.as_ref().background()) - .bg(color) - .add_modifier(Modifier::BOLD), - ) -} - #[cfg(test)] mod tests { use super::*; @@ -407,4 +387,19 @@ mod tests { assert_eq!(picker.selected_choice(), JavaChoice::Custom); } + + #[test] + fn memory_thumb_clears_the_first_unfilled_cell() { + let backend = ratatui::backend::TestBackend::new(40, 1); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + terminal + .draw(|frame| render_memory_gauge(frame, frame.area(), "8G", "8G".to_owned(), true)) + .unwrap(); + let buffer = terminal.backend().buffer(); + let thumb = (0..40) + .find(|x| buffer[(*x, 0)].symbol() == "◆") + .expect("slider thumb"); + + assert_eq!(buffer[(thumb + 1, 0)].symbol(), " "); + } }