From 6ce48b640a16ab1389398cad6873fb4dad1780d1 Mon Sep 17 00:00:00 2001 From: objz Date: Fri, 4 Sep 2026 14:18:52 +0200 Subject: [PATCH] fix: refine settings interactions --- src/tui/tests/flows.rs | 21 +- src/tui/widgets/popups/confirm.rs | 2 +- src/tui/widgets/popups/global_settings.rs | 65 ++++-- src/tui/widgets/popups/instance_settings.rs | 213 +++++++++++++++----- src/tui/widgets/popups/settings_controls.rs | 127 ++++++++---- 5 files changed, 307 insertions(+), 121 deletions(-) diff --git a/src/tui/tests/flows.rs b/src/tui/tests/flows.rs index 3d49b98..c12db6e 100644 --- a/src/tui/tests/flows.rs +++ b/src/tui/tests/flows.rs @@ -581,6 +581,7 @@ fn settings_panel_routes_legacy_edit_keys_to_tui_popups() { assert_eq!(ui.app.focused, FocusedArea::InstanceSettings); ui.draw(); assert!(ui.screen().contains("Instance Settings")); + assert!(!ui.screen().contains("Instance Settings *")); assert!(ui.screen().contains("settings-test")); assert!(ui.screen().contains("Game version")); assert!(ui.screen().contains("Memory min")); @@ -623,7 +624,7 @@ fn settings_panel_routes_legacy_edit_keys_to_tui_popups() { assert!(!ui.screen().contains('▰')); ui.key(KeyCode::Enter); ui.draw(); - assert!(ui.screen().contains("green")); + assert!(ui.screen().contains("Theme")); ui.key(KeyCode::Esc); ui.key(KeyCode::Esc); assert_eq!(ui.app.focused, FocusedArea::Settings); @@ -653,6 +654,7 @@ fn runtime_settings_use_the_shared_confirmation_popup() { ui.draw(); assert!(ui.screen().contains("Change runtime")); assert!(ui.screen().contains("Runtime files will be downloaded")); + assert!(ui.screen().contains("Installed mods may not load")); ui.key(KeyCode::Esc); assert_eq!(ui.app.focused, FocusedArea::InstanceSettings); @@ -677,8 +679,10 @@ fn settings_use_java_memory_and_resolution_controls() { ui.key(KeyCode::Enter); ui.draw(); assert!(ui.screen().contains("Java Runtime")); - assert!(ui.screen().contains("Automatic")); - assert!(ui.screen().contains("Custom path")); + assert!(ui.screen().contains("auto")); + assert!(ui.screen().contains("custom")); + assert!(!ui.screen().contains("Automatic")); + assert!(!ui.screen().contains("Custom path")); assert!(!ui.screen().contains("Manual")); ui.key(KeyCode::Esc); @@ -698,17 +702,13 @@ fn settings_use_java_memory_and_resolution_controls() { for _ in 0..3 { ui.key(KeyCode::Char('j')); } - ui.key(KeyCode::Enter); + ui.key(KeyCode::Char('l')); 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')); - } - ui.draw(); - assert!(ui.screen().contains("Custom")); + assert!(ui.screen().contains("custom")); ui.key(KeyCode::Esc); ui.key(KeyCode::Esc); ui.key(KeyCode::Enter); @@ -720,7 +720,8 @@ fn settings_use_java_memory_and_resolution_controls() { ui.key(KeyCode::Enter); ui.draw(); assert!(ui.screen().contains("Java Runtime")); - assert!(ui.screen().contains("Automatic")); + assert!(ui.screen().contains("auto")); + assert!(!ui.screen().contains("Automatic")); ui.key(KeyCode::Esc); ui.key(KeyCode::Esc); } diff --git a/src/tui/widgets/popups/confirm.rs b/src/tui/widgets/popups/confirm.rs index f5651ac..9aa042f 100644 --- a/src/tui/widgets/popups/confirm.rs +++ b/src/tui/widgets/popups/confirm.rs @@ -101,7 +101,7 @@ impl ConfirmTarget { .collect::>() .join("\n"), ConfirmTarget::InstanceRuntime { from, to, .. } => format!( - "{from} → {to}\nRuntime files will be downloaded before saving.\n! Existing mods may be incompatible." + "{from} → {to}\nRuntime files will be downloaded before saving.\n! Installed mods may not load and can be incompatible with the new runtime." ), ConfirmTarget::DiscardInstanceSettings => { "Unsaved instance settings will be lost.".to_owned() diff --git a/src/tui/widgets/popups/global_settings.rs b/src/tui/widgets/popups/global_settings.rs index bbe15b4..00cc7c7 100644 --- a/src/tui/widgets/popups/global_settings.rs +++ b/src/tui/widgets/popups/global_settings.rs @@ -20,7 +20,8 @@ use crate::{ }, instance::models::normalize_memory_value, tui::widgets::popups::settings_controls::{ - JavaChoice, JavaPicker, adjust_memory, memory_kib, render_memory_gauge, + JavaChoice, JavaPicker, adjust_memory, handle_text_area_input, memory_kib, + render_memory_gauge, subtle_tag, }, }; @@ -90,7 +91,7 @@ impl State { .as_deref() .is_none_or(str::is_empty) => { - "auto-detect".to_owned() + self.java_picker.detected_path().to_owned() } _ => self.value(field), } @@ -198,6 +199,16 @@ impl State { let count = self.java_picker.labels().len(); match key.code { KeyCode::Esc | KeyCode::Char('h') | KeyCode::Left => self.java_picker_open = false, + KeyCode::Char('a') => { + if self.config.paths.java_path.take().is_some() { + self.config_dirty = true; + } + self.java_picker_open = false; + } + KeyCode::Char('c') => { + self.java_picker_open = false; + self.editing = Some(new_text_area(vec![self.value(4)])); + } KeyCode::Char('j') | KeyCode::Down if count > 0 => { self.java_picker.selected = (self.java_picker.selected + 1).min(count - 1); } @@ -206,20 +217,12 @@ impl State { } KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => { match self.java_picker.selected_choice() { - JavaChoice::Automatic => { - if self.config.paths.java_path.take().is_some() { - self.config_dirty = true; - } - } JavaChoice::Installation(path) => { if self.config.paths.java_path.as_deref() != Some(&path) { self.config.paths.java_path = Some(path); self.config_dirty = true; } } - JavaChoice::Custom => { - self.editing = Some(new_text_area(vec![self.value(4)])); - } } self.java_picker_open = false; } @@ -272,9 +275,7 @@ impl State { match key.code { KeyCode::Enter => self.commit_edit(), KeyCode::Esc => self.editing = None, - _ => { - input.input(*key); - } + _ => handle_text_area_input(input, key), } return Action::None; } @@ -298,6 +299,14 @@ impl State { 4 => self.open_java_picker(), field => self.editing = Some(new_text_area(vec![self.value(field)])), }, + KeyCode::Char('a') if self.selected == 4 => { + if self.config.paths.java_path.take().is_some() { + self.config_dirty = true; + } + } + KeyCode::Char('c') if self.selected == 4 => { + self.editing = Some(new_text_area(vec![self.value(4)])); + } KeyCode::Char('s') => { if self.validate_before_save() { return Action::Save( @@ -394,7 +403,14 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { frame.render_widget(Clear, area); let keybinds = if state.editing.is_some() { super::keybind_line(&[("Enter", " apply"), ("Esc", " cancel")]) - } else if state.theme_picker || state.java_picker_open { + } else if state.java_picker_open { + super::keybind_line(&[ + ("a", " auto"), + ("c", " custom"), + ("h", " back"), + ("Enter", " select"), + ]) + } else if state.theme_picker { super::keybind_line(&[("h", " back"), ("Enter", " select")]) } else if matches!(state.selected, 2 | 3) { super::keybind_line(&[ @@ -403,6 +419,14 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { ("s", " save"), ("Esc", " back"), ]) + } else if state.selected == 4 { + super::keybind_line(&[ + ("Enter", " runtimes"), + ("a", " auto"), + ("c", " custom"), + ("s", " save"), + ("Esc", " back"), + ]) } else { super::keybind_line(&[ ("j/k", ""), @@ -416,8 +440,6 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { " Theme " } else if state.java_picker_open { " Java Runtime " - } else if state.config_dirty { - " Launcher Settings * " } else { " Launcher Settings " }; @@ -521,11 +543,11 @@ fn render_settings_list(frame: &mut Frame, area: Rect, state: &State) { } } -fn global_field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a> { +fn global_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(); - Line::from(vec![ + let mut spans = vec![ Span::styled( if selected { "▶ " } else { " " }, Style::default().fg(theme.accent()), @@ -552,8 +574,11 @@ fn global_field_line<'a>(state: &'a State, index: usize, label: &str) -> Line<'a Modifier::empty() }), ), - ]) - .style(Style::default().bg(if selected { + ]; + if index == 4 && state.config.paths.java_path.is_none() && !editing { + spans.extend([Span::raw(" "), subtle_tag("Auto", theme.info())]); + } + Line::from(spans).style(Style::default().bg(if selected { theme.stripe() } else { theme.surface() diff --git a/src/tui/widgets/popups/instance_settings.rs b/src/tui/widgets/popups/instance_settings.rs index 59b5b88..ac1c33e 100644 --- a/src/tui/widgets/popups/instance_settings.rs +++ b/src/tui/widgets/popups/instance_settings.rs @@ -26,7 +26,7 @@ use crate::{ LoadState, settings_controls::{ DisplayResolution, JavaChoice, JavaPicker, adjust_memory, display_resolutions, - memory_kib, render_memory_gauge, + handle_text_area_input, memory_kib, render_memory_gauge, subtle_tag, }, }, search::SearchState, @@ -51,11 +51,9 @@ enum ChoicePicker { #[derive(Debug, Clone, PartialEq, Eq)] enum ResolutionChoice { - Default, Display(DisplayResolution), Preset(u32, u32), Configured(u32, u32), - Custom, } impl ResolutionChoice { @@ -65,19 +63,13 @@ impl ResolutionChoice { Self::Preset(width, height) | Self::Configured(width, height) => { Some((*width, *height)) } - Self::Default | Self::Custom => None, } } fn label(&self) -> String { - self.resolution().map_or_else( - || match self { - Self::Default => "Default".to_owned(), - Self::Custom => "Custom…".to_owned(), - _ => String::new(), - }, - |(width, height)| format!("{width}x{height}"), - ) + self.resolution() + .map(|(width, height)| format!("{width}x{height}")) + .unwrap_or_default() } } @@ -212,7 +204,7 @@ impl State { fn display_value(&self, field: usize) -> String { match field { 2 if self.draft.loader == ModLoader::Vanilla => "not applicable".to_owned(), - 3 if self.draft.java_path.is_none() => "auto-detect".to_owned(), + 3 if self.draft.java_path.is_none() => self.java_picker.detected_path().to_owned(), 4 if self.draft.memory_min.is_none() => { format!("default ({})", SETTINGS.read().defaults.memory_min) } @@ -221,7 +213,10 @@ impl State { } 6 if self.draft.jvm_args.is_empty() => "no arguments".to_owned(), 6 => self.draft.jvm_args.join(" "), - 7 if self.draft.resolution.is_none() => "default".to_owned(), + 7 if self.draft.resolution.is_none() => self.default_resolution().map_or_else( + || "default".to_owned(), + |(width, height)| format!("{width}x{height}"), + ), 8 if self.desktop => "enabled".to_owned(), 8 => "disabled".to_owned(), _ => self.value(field).replace('\n', " ↵ "), @@ -241,7 +236,7 @@ impl State { self.editing = Some(new_text_area(vec![self.effective_memory(self.selected)])); } 6 => self.editing = Some(new_text_area(vec![self.value(self.selected)])), - 7 => self.open_choice_picker(ChoicePicker::Resolution), + 7 => self.editing = Some(new_text_area(vec![self.value(7)])), 8 => self.desktop = !self.desktop, field => self.editing = Some(new_text_area(vec![self.value(field)])), } @@ -295,6 +290,22 @@ impl State { let count = self.choice_values().len(); match key.code { KeyCode::Esc | KeyCode::Char('h') | KeyCode::Left => self.choice_picker = None, + KeyCode::Char('a') if self.choice_picker == Some(ChoicePicker::Java) => { + self.draft.java_path = None; + self.choice_picker = None; + } + KeyCode::Char('c') if self.choice_picker == Some(ChoicePicker::Java) => { + self.choice_picker = None; + self.editing = Some(new_text_area(vec![self.value(3)])); + } + KeyCode::Char('c') if self.choice_picker == Some(ChoicePicker::Resolution) => { + self.choice_picker = None; + self.editing = Some(new_text_area(vec![self.value(7)])); + } + KeyCode::Char('d') if self.choice_picker == Some(ChoicePicker::Resolution) => { + self.draft.resolution = None; + self.choice_picker = None; + } KeyCode::Char('j') | KeyCode::Down | KeyCode::Right if count > 0 => { self.choice_index = (self.choice_index + 1).min(count - 1); } @@ -324,11 +335,7 @@ impl State { Some(ChoicePicker::Java) => { self.java_picker.selected = self.choice_index; match self.java_picker.selected_choice() { - JavaChoice::Automatic => self.draft.java_path = None, JavaChoice::Installation(path) => self.draft.java_path = Some(path), - JavaChoice::Custom => { - self.editing = Some(new_text_area(vec![self.value(3)])); - } } } Some(ChoicePicker::Resolution) => { @@ -336,13 +343,9 @@ impl State { .resolution_choices() .get(self.choice_index) .cloned() - .unwrap_or(ResolutionChoice::Default); - match selected { - ResolutionChoice::Default => self.draft.resolution = None, - ResolutionChoice::Custom => { - self.editing = Some(new_text_area(vec![self.value(7)])); - } - choice => self.draft.resolution = choice.resolution(), + .and_then(|choice| choice.resolution()); + if let Some(resolution) = selected { + self.draft.resolution = Some(resolution); } } None => {} @@ -504,6 +507,12 @@ impl State { resolution_choices(self.draft.resolution, &self.display_resolutions) } + fn default_resolution(&self) -> Option<(u32, u32)> { + self.display_resolutions + .first() + .map(|display| (display.width, display.height)) + } + fn handle_picker_key(&mut self, key: &KeyEvent) { self.initialize_picker_index(); if self.picker_search.active { @@ -620,9 +629,7 @@ impl State { match key.code { KeyCode::Enter => self.commit_edit(), KeyCode::Esc => self.editing = None, - _ => { - input.input(*key); - } + _ => handle_text_area_input(input, key), } return Action::None; } @@ -638,10 +645,18 @@ impl State { KeyCode::Char('l') | KeyCode::Right if matches!(self.selected, 4 | 5) => { self.adjust_selected_memory(true); } + KeyCode::Char('l') | KeyCode::Right if self.selected == 7 => { + self.open_choice_picker(ChoicePicker::Resolution); + } KeyCode::Enter => self.begin_edit(), - KeyCode::Char('r') if matches!(self.selected, 4 | 5) => { + KeyCode::Char('d') if matches!(self.selected, 4 | 5) => { self.set_memory(self.selected, None); } + KeyCode::Char('d') if self.selected == 7 => self.draft.resolution = None, + KeyCode::Char('a') if self.selected == 3 => self.draft.java_path = None, + KeyCode::Char('c') if self.selected == 3 => { + self.editing = Some(new_text_area(vec![self.value(3)])); + } KeyCode::Char('s') if self.dirty() => { if self.validate_before_save() { if self.runtime_changed() { @@ -690,7 +705,7 @@ fn resolution_choices( current: Option<(u32, u32)>, displays: &[DisplayResolution], ) -> Vec { - let mut choices = vec![ResolutionChoice::Default]; + let mut choices = Vec::new(); choices.extend(displays.iter().cloned().map(ResolutionChoice::Display)); for (width, height) in [ (854, 480), @@ -714,7 +729,6 @@ fn resolution_choices( { choices.push(ResolutionChoice::Configured(width, height)); } - choices.push(ResolutionChoice::Custom); choices } @@ -741,6 +755,7 @@ pub fn popup_rect(area: Rect, state: &State) -> Rect { } else { 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.runtime_changed()) + u16::from(state.error.is_some()) }; let width = match state.choice_picker { @@ -767,7 +782,6 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { (_, Some(ChoicePicker::Loader)) => " Mod Loader ", (_, Some(ChoicePicker::Java)) => " Java Runtime ", (_, Some(ChoicePicker::Resolution)) => " Resolution ", - _ if state.dirty() => " Instance Settings * ", _ => " Instance Settings ", }; let keybinds = if state.editing.is_some() { @@ -781,13 +795,43 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) { ]) } else if state.picker.is_some() { super::keybind_line(&[("/", " search"), ("h", " back"), ("Enter", " select")]) + } else if state.choice_picker == Some(ChoicePicker::Java) { + super::keybind_line(&[ + ("a", " auto"), + ("c", " custom"), + ("h", " back"), + ("Enter", " select"), + ]) + } else if state.choice_picker == Some(ChoicePicker::Resolution) { + super::keybind_line(&[ + ("d", " default"), + ("c", " custom"), + ("h", " back"), + ("Enter", " select"), + ]) } else if state.choice_picker.is_some() { super::keybind_line(&[("h", " back"), ("Enter", " select")]) } else if matches!(state.selected, 4 | 5) { super::keybind_line(&[ ("h/l", " adjust"), ("Enter", " exact"), - ("r", " default"), + ("d", " default"), + ("s", " save"), + ("Esc", " back"), + ]) + } else if state.selected == 3 { + super::keybind_line(&[ + ("Enter", " runtimes"), + ("a", " auto"), + ("c", " custom"), + ("s", " save"), + ("Esc", " back"), + ]) + } else if state.selected == 7 { + super::keybind_line(&[ + ("l", " presets"), + ("Enter", " custom"), + ("d", " default"), ("s", " save"), ("Esc", " back"), ]) @@ -889,6 +933,21 @@ fn render_settings_list(frame: &mut Frame, area: Rect, state: &State) { y = y.saturating_add(height); } + if state.runtime_changed() && y < area.bottom() { + frame.render_widget( + Paragraph::new(Span::styled( + " ! Runtime change may break installed mods.", + Style::default().fg(theme.warning()), + )), + Rect { + y, + height: 1, + ..area + }, + ); + y = y.saturating_add(1); + } + if let Some(error) = &state.error && y < area.bottom() { @@ -916,7 +975,7 @@ fn field_line(state: &State, index: usize, label: &str) -> Line<'static> { state.display_value(index) }; let dirty = field_dirty(state, index); - Line::from(vec![ + let mut spans = vec![ Span::styled( if selected { "▶ " } else { " " }, Style::default().fg(theme.accent()), @@ -945,7 +1004,16 @@ fn field_line(state: &State, index: usize, label: &str) -> Line<'static> { Modifier::empty() }), ), - ]) + ]; + if index == 3 && state.draft.java_path.is_none() && !editing { + spans.extend([Span::raw(" "), subtle_tag("Auto", theme.info())]); + } else if index == 7 && state.draft.resolution.is_none() && !editing { + spans.push(Span::styled( + " default", + Style::default().fg(theme.text_dim()), + )); + } + Line::from(spans) } fn jvm_field_lines(state: &State, width: u16) -> Vec> { @@ -1074,7 +1142,7 @@ fn resolution_items(choices: &[ResolutionChoice]) -> Vec> { Style::default().fg(theme.text()), )]; match choice { - ResolutionChoice::Default | ResolutionChoice::Preset(_, _) => {} + ResolutionChoice::Preset(_, _) => {} ResolutionChoice::Display(display) => { if !display.name.is_empty() { spans.extend([ @@ -1098,13 +1166,6 @@ fn resolution_items(choices: &[ResolutionChoice]) -> Vec> { Style::default().fg(theme.text_dim()), )); } - ResolutionChoice::Custom => { - spans.clear(); - spans.push(Span::styled( - choice.label(), - Style::default().fg(theme.warning()), - )); - } } ListItem::new(Line::from(spans)) }) @@ -1354,6 +1415,9 @@ mod tests { state.selected = 7; state.begin_edit(); + assert!(state.editing.is_some()); + state.editing = None; + state.handle_key(&KeyEvent::from(KeyCode::Char('l'))); assert_eq!(state.choice_picker, Some(ChoicePicker::Resolution)); state.choice_index = state .resolution_choices() @@ -1366,11 +1430,29 @@ mod tests { state.selected = 3; state.begin_edit(); assert_eq!(state.choice_picker, Some(ChoicePicker::Java)); - state.choice_index = state.choice_values().len() - 1; - state.apply_choice(); + state.handle_choice_key(&KeyEvent::from(KeyCode::Char('c'))); assert!(state.editing.is_some()); } + #[test] + fn java_and_resolution_defaults_are_direct_actions_not_list_rows() { + let temp = tempfile::tempdir().unwrap(); + let mut state = State::new(&instance(), temp.path()); + + state.selected = 3; + state.draft.java_path = Some("/custom/java".to_owned()); + state.handle_key(&KeyEvent::from(KeyCode::Char('a'))); + assert_eq!(state.draft.java_path, None); + + state.selected = 7; + state.draft.resolution = Some((1920, 1080)); + state.handle_key(&KeyEvent::from(KeyCode::Char('d'))); + assert_eq!(state.draft.resolution, None); + state.handle_key(&KeyEvent::from(KeyCode::Char('l'))); + assert!(!state.choice_values().iter().any(|value| value == "Default")); + assert!(!state.choice_values().iter().any(|value| value == "Custom…")); + } + #[test] fn memory_slider_keeps_bounds_linked_and_desktop_has_no_dot() { let temp = tempfile::tempdir().unwrap(); @@ -1399,7 +1481,7 @@ mod tests { let choices = resolution_choices(None, &displays); - assert!(matches!(choices[1], ResolutionChoice::Display(_))); + assert!(matches!(choices[0], ResolutionChoice::Display(_))); assert_eq!( choices .iter() @@ -1409,6 +1491,43 @@ mod tests { ); } + #[test] + fn inherited_resolution_displays_the_detected_primary_size() { + let temp = tempfile::tempdir().unwrap(); + let mut state = State::new(&instance(), temp.path()); + state.display_resolutions = vec![DisplayResolution { + width: 2560, + height: 1440, + name: "DP-4".to_owned(), + primary: true, + }]; + + assert_eq!(state.display_value(7), "2560x1440"); + } + + #[test] + fn runtime_changes_show_an_inline_compatibility_warning() { + let temp = tempfile::tempdir().unwrap(); + let mut state = State::new(&instance(), temp.path()); + state.draft.game_version = "1.21.2".to_owned(); + let backend = ratatui::backend::TestBackend::new(80, 20); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + + terminal + .draw(|frame| { + let area = popup_rect(frame.area(), &state); + render(frame, area, &mut state); + }) + .unwrap(); + + assert!( + terminal + .backend() + .to_string() + .contains("Runtime change may break installed mods") + ); + } + #[test] fn jvm_argument_badges_wrap_without_hiding_following_fields() { let temp = tempfile::tempdir().unwrap(); diff --git a/src/tui/widgets/popups/settings_controls.rs b/src/tui/widgets/popups/settings_controls.rs index 154ff1f..dc52094 100644 --- a/src/tui/widgets/popups/settings_controls.rs +++ b/src/tui/widgets/popups/settings_controls.rs @@ -5,6 +5,7 @@ use std::sync::{Arc, Mutex}; +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use ratatui::{ Frame, layout::Rect, @@ -12,6 +13,7 @@ use ratatui::{ text::{Line, Span}, widgets::{LineGauge, ListItem, Paragraph}, }; +use ratatui_textarea::TextArea; use crate::{ config::theme::THEME, instance::java::JavaInstallation, tui::widgets::popups::LoadState, @@ -32,9 +34,7 @@ pub(crate) struct JavaPicker { #[derive(Debug, Clone, PartialEq, Eq)] pub(crate) enum JavaChoice { - Automatic, Installation(String), - Custom, } #[derive(Debug, Clone, PartialEq, Eq)] @@ -88,25 +88,27 @@ impl JavaPicker { } pub(crate) fn choices(&self) -> Vec { - let mut choices = vec![JavaChoice::Automatic]; + let mut paths = Vec::new(); if let LoadState::Loaded(installations) = &*self .load .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) { - choices.extend(installations.iter().map(|installation| { - JavaChoice::Installation(installation.path.to_string_lossy().into_owned()) - })); + paths.extend( + installations + .iter() + .map(|installation| installation.path.to_string_lossy().into_owned()), + ); + } + if !paths.contains(&self.detected) { + paths.insert(0, self.detected.clone()); } if let Some(current) = &self.current - && !choices - .iter() - .any(|choice| matches!(choice, JavaChoice::Installation(path) if path == current)) + && !paths.contains(current) { - choices.push(JavaChoice::Installation(current.clone())); + paths.push(current.clone()); } - choices.push(JavaChoice::Custom); - choices + paths.into_iter().map(JavaChoice::Installation).collect() } pub(crate) fn labels(&self) -> Vec { @@ -121,7 +123,6 @@ impl JavaPicker { self.choices() .into_iter() .map(|choice| match choice { - JavaChoice::Automatic => format!("Automatic {}", self.detected), JavaChoice::Installation(path) => installations .as_ref() .and_then(|items| { @@ -130,7 +131,6 @@ impl JavaPicker { .find(|item| item.path.to_string_lossy() == path) }) .map_or_else(|| format!("Java {path}"), JavaInstallation::label), - JavaChoice::Custom => "Custom path…".to_owned(), }) .collect() } @@ -148,18 +148,6 @@ impl JavaPicker { self.choices() .into_iter() .map(|choice| match choice { - JavaChoice::Automatic => ListItem::new(Line::from(vec![ - Span::styled( - "Automatic", - Style::default() - .fg(theme.info()) - .add_modifier(Modifier::BOLD), - ), - Span::styled( - format!(" {}", self.detected), - Style::default().fg(theme.text_dim()), - ), - ])), JavaChoice::Installation(path) => { let installation = installations .iter() @@ -167,15 +155,15 @@ impl JavaPicker { let version = installation .and_then(|installation| installation.version.as_deref()) .map_or_else(|| "Java".to_owned(), |version| format!("Java {version}")); - ListItem::new(Line::from(vec![ + let mut spans = vec![ Span::styled(version, Style::default().fg(theme.text())), Span::styled(format!(" {path}"), Style::default().fg(theme.text_dim())), - ])) + ]; + if self.current.is_none() && path == self.detected { + spans.extend([Span::raw(" "), subtle_tag("Auto", theme.info())]); + } + ListItem::new(Line::from(spans)) } - JavaChoice::Custom => ListItem::new(Line::from(Span::styled( - "Custom path…", - Style::default().fg(theme.warning()), - ))), }) .collect() } @@ -206,11 +194,13 @@ impl JavaPicker { .get(self.selected) .cloned() .or_else(|| { - self.current - .as_ref() - .map(|current| JavaChoice::Installation(current.clone())) + Some(JavaChoice::Installation( + self.current + .clone() + .unwrap_or_else(|| self.detected.clone()), + )) }) - .unwrap_or(JavaChoice::Automatic); + .unwrap_or_else(|| JavaChoice::Installation(self.detected.clone())); self.selected = choices .iter() .position(|choice| choice == &selected) @@ -222,7 +212,11 @@ impl JavaPicker { self.choices() .get(self.selected) .cloned() - .unwrap_or(JavaChoice::Automatic) + .unwrap_or_else(|| JavaChoice::Installation(self.detected.clone())) + } + + pub(crate) fn detected_path(&self) -> &str { + &self.detected } } @@ -288,11 +282,10 @@ pub(crate) fn render_memory_gauge( }; let value_style = if selected { Style::default() - .fg(theme.background()) - .bg(theme.accent()) + .fg(theme.accent()) .add_modifier(Modifier::BOLD) } else { - Style::default().fg(theme.text()).bg(theme.background()) + Style::default().fg(theme.text()) }; frame.render_widget( Paragraph::new(Line::from(Span::styled(format!(" {label} "), value_style))), @@ -335,6 +328,21 @@ pub(crate) fn render_memory_gauge( } } +pub(crate) fn handle_text_area_input(input: &mut TextArea<'_>, key: &KeyEvent) { + if key.code == KeyCode::Backspace && key.modifiers.contains(KeyModifiers::CONTROL) { + input.delete_word(); + } else { + input.input(*key); + } +} + +pub(crate) fn subtle_tag(label: impl Into, color: ratatui::style::Color) -> Span<'static> { + Span::styled( + format!(" {} ", label.into()), + Style::default().fg(color).bg(THEME.as_ref().stripe()), + ) +} + pub(crate) fn display_resolutions() -> Vec { let Ok(displays) = display_info::DisplayInfo::all() else { return Vec::new(); @@ -375,9 +383,8 @@ mod tests { #[test] fn java_picker_preserves_semantic_selection_when_results_arrive() { let mut picker = JavaPicker::new(); - picker.current = None; + picker.current = Some("/opt/jdk/bin/java".to_owned()); picker.initialize(); - picker.selected = picker.choices().len() - 1; *picker.load.lock().unwrap() = LoadState::Loaded(vec![JavaInstallation { path: "/opt/jdk/bin/java".into(), version: Some("21".to_owned()), @@ -385,7 +392,23 @@ mod tests { picker.initialize(); - assert_eq!(picker.selected_choice(), JavaChoice::Custom); + assert_eq!( + picker.selected_choice(), + JavaChoice::Installation("/opt/jdk/bin/java".to_owned()) + ); + } + + #[test] + fn settings_text_input_deletes_the_previous_word() { + let mut input = TextArea::from(["one two"]); + input.move_cursor(ratatui_textarea::CursorMove::End); + + handle_text_area_input( + &mut input, + &KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL), + ); + + assert_eq!(input.lines(), ["one "]); } #[test] @@ -402,4 +425,22 @@ mod tests { assert_eq!(buffer[(thumb + 1, 0)].symbol(), " "); } + + #[test] + fn memory_value_uses_the_existing_row_background() { + let backend = ratatui::backend::TestBackend::new(40, 1); + let mut terminal = ratatui::Terminal::new(backend).unwrap(); + let surface = THEME.as_ref().surface(); + terminal + .draw(|frame| { + frame.render_widget( + ratatui::widgets::Block::default().style(Style::default().bg(surface)), + frame.area(), + ); + render_memory_gauge(frame, frame.area(), "6G", "6G".to_owned(), false); + }) + .unwrap(); + + assert_eq!(terminal.backend().buffer()[(1, 0)].bg, surface); + } }