feat: confirm automatic Java changes

This commit is contained in:
objz committed 2026-09-04 16:16:15 +02:00
1 parent 3566d6b50d
commit 59407ee01d
6 files changed
+314 -47

No files matched your search

+65 -14
View File
@@ -283,6 +283,32 @@ impl App {
}
self.focused
}
Some(confirm_popup::ConfirmTarget::JavaAuto { instance, .. }) => {
if instance.is_some() {
self.focused = FocusedArea::InstanceSettings;
let confirmed = self.instance_settings.as_mut().and_then(|state| {
state.enable_auto_java();
state.confirmed_save()
});
if let Some((updated, desktop)) = confirmed {
self.apply_instance_settings(*updated, desktop, false);
}
} else {
self.focused = FocusedArea::GlobalSettings;
let action = self.global_settings.as_mut().map(
widgets::popups::global_settings::State::confirm_auto_java,
);
if let Some(widgets::popups::global_settings::Action::Save(
config,
theme,
border,
)) = action
{
self.apply_global_settings(*config, theme, border);
}
}
self.focused
}
None => FocusedArea::Instances,
};
confirm_popup::clear_pending();
@@ -308,6 +334,13 @@ impl App {
Some(confirm_popup::ConfirmTarget::JvmArguments { .. }) => {
FocusedArea::InstanceSettings
}
Some(confirm_popup::ConfirmTarget::JavaAuto { instance, .. }) => {
if instance.is_some() {
FocusedArea::InstanceSettings
} else {
FocusedArea::GlobalSettings
}
}
_ => FocusedArea::Instances,
};
confirm_popup::clear_pending();
@@ -684,6 +717,14 @@ impl App {
pushed_at: std::time::Instant::now(),
});
}
widgets::popups::global_settings::Action::ConfirmJavaAuto { from, to } => {
confirm_popup::set_pending(confirm_popup::ConfirmTarget::JavaAuto {
instance: None,
from,
to,
});
self.focused = FocusedArea::ConfirmDelete;
}
widgets::popups::global_settings::Action::Close => {
self.global_settings = None;
self.focused = self.pre_overlay_focused;
@@ -694,20 +735,7 @@ impl App {
self.focused = self.pre_overlay_focused;
}
widgets::popups::global_settings::Action::Save(config, theme, border) => {
let result = crate::config::SETTINGS
.save_launcher_settings(*config)
.and_then(|()| crate::config::theme::apply_theme(theme, border));
match result {
Ok(()) => {
crate::feedback::request_redraw();
}
Err(error) => error_buffer::push_error(error_buffer::ErrorEvent {
id: 0,
level: tracing::Level::ERROR,
message: error.to_string(),
pushed_at: std::time::Instant::now(),
}),
}
self.apply_global_settings(*config, theme, border);
}
}
return Ok(());
@@ -762,6 +790,14 @@ impl App {
confirm_popup::set_pending(confirm_popup::ConfirmTarget::JvmArguments { name });
self.focused = FocusedArea::ConfirmDelete;
}
widgets::popups::instance_settings::Action::ConfirmJavaAuto { name, from, to } => {
confirm_popup::set_pending(confirm_popup::ConfirmTarget::JavaAuto {
instance: Some(name),
from,
to,
});
self.focused = FocusedArea::ConfirmDelete;
}
}
return Ok(());
}
@@ -2060,6 +2096,21 @@ impl App {
}
}
fn apply_global_settings(
&mut self,
config: crate::config::Config,
theme: String,
border: crate::config::theme::BorderStyle,
) {
let result = crate::config::SETTINGS
.save_launcher_settings(config)
.and_then(|()| crate::config::theme::apply_theme(theme, border));
match result {
Ok(()) => crate::feedback::request_redraw(),
Err(error) => error_buffer::push_message(tracing::Level::ERROR, error.to_string()),
}
}
fn apply_instance_settings(
&mut self,
updated: crate::instance::models::InstanceConfig,
+53 -2
View File
@@ -723,6 +723,57 @@ fn clearing_jvm_arguments_requires_confirmation_and_autosaves() {
);
}
#[test]
fn enabling_a_different_automatic_java_requires_confirmation() {
let mut ui = UiHarness::new();
ui.add_instance("java-auto-test");
ui.key(KeyCode::Char('E'));
ui.app.instance_settings.as_mut().unwrap().draft.java_path = Some("/custom/java".to_owned());
for _ in 0..3 {
ui.key(KeyCode::Char('j'));
}
ui.key(KeyCode::Char('a'));
assert_eq!(ui.app.focused, FocusedArea::ConfirmDelete);
assert!(matches!(
confirm::pending_target(),
Some(confirm::ConfirmTarget::JavaAuto {
instance: Some(name),
..
}) if name == "java-auto-test"
));
ui.draw();
assert!(ui.screen().contains("Enable automatic Java"));
assert!(ui.screen().contains("Java runtime will change"));
ui.key(KeyCode::Esc);
assert_eq!(ui.app.focused, FocusedArea::InstanceSettings);
assert_eq!(
ui.app
.instance_settings
.as_ref()
.unwrap()
.draft
.java_path
.as_deref(),
Some("/custom/java")
);
ui.key(KeyCode::Char('a'));
ui.key(KeyCode::Enter);
assert_eq!(ui.app.focused, FocusedArea::InstanceSettings);
assert_eq!(
ui.app
.instances_state
.selected_instance()
.unwrap()
.java_path,
None
);
}
#[test]
fn instance_settings_validation_errors_use_the_toast_buffer() {
let mut ui = UiHarness::new();
@@ -757,7 +808,7 @@ fn settings_use_java_memory_and_resolution_controls() {
ui.key(KeyCode::Enter);
ui.draw();
assert!(ui.screen().contains("Java Runtime"));
assert!(ui.screen().contains("auto"));
assert!(!ui.screen().contains("auto"));
assert!(!ui.screen().contains("custom"));
assert!(!ui.screen().contains("Automatic"));
assert!(!ui.screen().contains("Custom path"));
@@ -806,7 +857,7 @@ fn settings_use_java_memory_and_resolution_controls() {
ui.key(KeyCode::Enter);
ui.draw();
assert!(ui.screen().contains("Java Runtime"));
assert!(ui.screen().contains("auto"));
assert!(!ui.screen().contains("auto"));
assert!(!ui.screen().contains("Automatic"));
ui.key(KeyCode::Esc);
ui.key(KeyCode::Esc);
+11
View File
@@ -51,6 +51,11 @@ pub enum ConfirmTarget {
JvmArguments {
name: String,
},
JavaAuto {
instance: Option<String>,
from: String,
to: String,
},
}
impl ConfirmTarget {
@@ -59,6 +64,7 @@ impl ConfirmTarget {
Self::OrphanDependencies { .. } => " Remove unused dependencies ".to_owned(),
Self::InstanceRuntime { .. } => " Change runtime ".to_owned(),
Self::JvmArguments { .. } => " Clear JVM arguments ".to_owned(),
Self::JavaAuto { .. } => " Enable automatic Java ".to_owned(),
_ => format!(" Delete '{}' ", self.name()),
}
}
@@ -99,6 +105,9 @@ impl ConfirmTarget {
.join("\n"),
ConfirmTarget::InstanceRuntime { .. } => "Apply this runtime change".to_owned(),
ConfirmTarget::JvmArguments { .. } => "Remove all JVM arguments".to_owned(),
ConfirmTarget::JavaAuto { from, to, .. } => {
format!("Java runtime will change:\n{from} → {to}")
}
}
}
@@ -111,6 +120,7 @@ impl ConfirmTarget {
ConfirmTarget::OrphanDependencies { .. } => "unused dependencies",
ConfirmTarget::InstanceRuntime { name, .. } => name,
ConfirmTarget::JvmArguments { name, .. } => name,
ConfirmTarget::JavaAuto { instance, .. } => instance.as_deref().unwrap_or("launcher"),
}
}
@@ -120,6 +130,7 @@ impl ConfirmTarget {
Self::OrphanDependencies { .. } => " remove all",
Self::InstanceRuntime { .. } => " change",
Self::JvmArguments { .. } => " clear",
Self::JavaAuto { .. } => " enable",
_ => " confirm",
}
}
+62 -15
View File
@@ -43,6 +43,7 @@ pub enum Action {
None,
Save(Box<Config>, String, BorderStyle),
Error(String),
ConfirmJavaAuto { from: String, to: String },
OpenRaw(std::path::PathBuf),
Close,
}
@@ -198,25 +199,41 @@ impl State {
self.java_picker_open = true;
}
fn toggle_auto_java(&mut self) {
self.config.paths.java_path = if self.config.paths.java_path.is_none() {
Some(self.java_picker.detected_path().to_owned())
} else {
None
fn toggle_auto_java(&mut self) -> Action {
let Some(current) = self.config.paths.java_path.as_deref() else {
self.config.paths.java_path = Some(self.java_picker.detected_path().to_owned());
self.save_pending = true;
self.error = None;
return Action::None;
};
if let Some((from, to)) = self.java_picker.automatic_change(current) {
return Action::ConfirmJavaAuto { from, to };
}
self.enable_auto_java();
Action::None
}
fn enable_auto_java(&mut self) {
self.config.paths.java_path = None;
self.save_pending = true;
self.error = None;
}
pub fn confirm_auto_java(&mut self) -> Action {
self.enable_auto_java();
self.save_pending = false;
Action::Save(
Box::new(self.config.clone()),
self.theme.theme.clone(),
self.theme.border_style.clone(),
)
}
fn handle_java_picker_key(&mut self, key: &KeyEvent) {
self.java_picker.initialize();
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') => {
self.toggle_auto_java();
self.java_picker_open = false;
}
KeyCode::Char('j') | KeyCode::Down if count > 0 => {
self.java_picker.selected = (self.java_picker.selected + 1).min(count - 1);
}
@@ -316,9 +333,7 @@ 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 => {
self.toggle_auto_java();
}
KeyCode::Char('a') if self.selected == 4 => return self.toggle_auto_java(),
KeyCode::Char('c') if self.selected == 4 => {
self.editing = Some(new_text_area(vec![self.value(4)]));
}
@@ -403,9 +418,7 @@ 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.java_picker_open {
super::keybind_line(&[("a", " auto"), ("h", " back"), ("Enter", " select")])
} else if state.theme_picker {
} else if state.java_picker_open || state.theme_picker {
super::keybind_line(&[("h", " back"), ("Enter", " select")])
} else if matches!(state.selected, 2 | 3) {
super::keybind_line(&[("h/l", " adjust"), ("Enter", " exact"), ("Esc", " back")])
@@ -627,4 +640,38 @@ mod tests {
));
assert_eq!(state.config.paths.java_path, None);
}
#[test]
fn java_picker_does_not_toggle_auto_mode() {
let mut state = State::new();
state.selected = 4;
state.config.paths.java_path = Some("/custom/java".to_owned());
state.open_java_picker();
assert!(matches!(
state.handle_key(&KeyEvent::from(KeyCode::Char('a'))),
Action::None
));
assert!(state.java_picker_open);
assert_eq!(
state.config.paths.java_path.as_deref(),
Some("/custom/java")
);
}
#[test]
fn changing_runtime_to_auto_requests_confirmation() {
let mut state = State::new();
state.selected = 4;
state.config.paths.java_path = Some("/custom/java".to_owned());
assert!(matches!(
state.handle_key(&KeyEvent::from(KeyCode::Char('a'))),
Action::ConfirmJavaAuto { .. }
));
assert_eq!(
state.config.paths.java_path.as_deref(),
Some("/custom/java")
);
}
}
+55 -15
View File
@@ -106,14 +106,29 @@ pub enum Action {
None,
Save(Box<InstanceConfig>, bool),
Error(String),
ConfirmRuntime { name: String },
ConfirmClearJvmArgs { name: String },
ConfirmRuntime {
name: String,
},
ConfirmClearJvmArgs {
name: String,
},
ConfirmJavaAuto {
name: String,
from: String,
to: String,
},
OpenRaw,
Close,
}
impl State {
pub fn new(instance: &InstanceConfig, _meta_dir: &std::path::Path) -> Self {
let auto_java_path = SETTINGS
.read()
.paths
.effective_java_path()
.map(str::to_owned)
.unwrap_or_else(crate::instance::java::detect_java_path);
Self {
original: instance.clone(),
draft: instance.clone(),
@@ -131,7 +146,7 @@ impl State {
loader_versions: Arc::new(Mutex::new(LoadState::Idle)),
choice_picker: None,
choice_index: 0,
java_picker: JavaPicker::new(),
java_picker: JavaPicker::with_auto_path(auto_java_path),
display_resolutions: display_resolutions(),
}
}
@@ -283,10 +298,6 @@ 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.toggle_auto_java();
self.choice_picker = None;
}
KeyCode::Char('d') if self.choice_picker == Some(ChoicePicker::Resolution) => {
self.apply_default_resolution();
self.choice_picker = None;
@@ -538,12 +549,25 @@ impl State {
}
}
fn toggle_auto_java(&mut self) {
self.draft.java_path = if self.draft.java_path.is_none() {
Some(self.java_picker.detected_path().to_owned())
} else {
None
fn toggle_auto_java(&mut self) -> Action {
let Some(current) = self.draft.java_path.as_deref() else {
self.draft.java_path = Some(self.java_picker.detected_path().to_owned());
self.error = None;
return Action::None;
};
if let Some((from, to)) = self.java_picker.automatic_change(current) {
return Action::ConfirmJavaAuto {
name: self.draft.name.clone(),
from,
to,
};
}
self.enable_auto_java();
Action::None
}
pub fn enable_auto_java(&mut self) {
self.draft.java_path = None;
self.error = None;
}
@@ -755,7 +779,7 @@ impl State {
KeyCode::Char('c') if self.selected == 7 => {
self.editing = Some(new_text_area(vec![self.value(7)]));
}
KeyCode::Char('a') if self.selected == 3 => self.toggle_auto_java(),
KeyCode::Char('a') if self.selected == 3 => return self.toggle_auto_java(),
KeyCode::Char('c') if self.selected == 3 => {
self.editing = Some(new_text_area(vec![self.value(3)]));
}
@@ -888,7 +912,7 @@ 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"), ("h", " back"), ("Enter", " select")])
super::keybind_line(&[("h", " back"), ("Enter", " select")])
} else if state.choice_picker == Some(ChoicePicker::Resolution) {
super::keybind_line(&[("d", " default"), ("h", " back"), ("Enter", " select")])
} else if state.choice_picker.is_some() {
@@ -1530,6 +1554,10 @@ mod tests {
state.selected = 3;
state.begin_edit();
assert_eq!(state.choice_picker, Some(ChoicePicker::Java));
let original_java = state.draft.java_path.clone();
state.handle_choice_key(&KeyEvent::from(KeyCode::Char('a')));
assert_eq!(state.choice_picker, Some(ChoicePicker::Java));
assert_eq!(state.draft.java_path, original_java);
state.handle_choice_key(&KeyEvent::from(KeyCode::Char('c')));
assert_eq!(state.choice_picker, Some(ChoicePicker::Java));
assert!(state.editing.is_none());
@@ -1545,13 +1573,25 @@ mod tests {
state.selected = 3;
state.draft.java_path = Some("/custom/java".to_owned());
state.handle_key(&KeyEvent::from(KeyCode::Char('a')));
assert!(matches!(
state.handle_key(&KeyEvent::from(KeyCode::Char('a'))),
Action::ConfirmJavaAuto { .. }
));
assert_eq!(state.draft.java_path.as_deref(), Some("/custom/java"));
state.enable_auto_java();
assert_eq!(state.draft.java_path, None);
state.handle_key(&KeyEvent::from(KeyCode::Char('a')));
assert_eq!(
state.draft.java_path.as_deref(),
Some(state.java_picker.detected_path())
);
let saved = state.draft.clone();
state.mark_saved(&saved, state.desktop);
assert!(matches!(
state.handle_key(&KeyEvent::from(KeyCode::Char('a'))),
Action::Save(..)
));
assert_eq!(state.draft.java_path, None);
state.selected = 7;
state.display_resolutions = vec![DisplayResolution {
+68 -1
View File
@@ -49,10 +49,14 @@ pub(crate) struct DisplayResolution {
impl JavaPicker {
pub(crate) fn new() -> Self {
Self::with_auto_path(crate::instance::java::detect_java_path())
}
pub(crate) fn with_auto_path(detected: String) -> Self {
Self {
load: Arc::new(Mutex::new(LoadState::Idle)),
current: None,
detected: crate::instance::java::detect_java_path(),
detected,
selected: 0,
previous_choices: Vec::new(),
}
@@ -240,6 +244,33 @@ impl JavaPicker {
pub(crate) fn detected_path(&self) -> &str {
&self.detected
}
pub(crate) fn automatic_change(&self, current: &str) -> Option<(String, String)> {
if same_executable(current, &self.detected) {
return None;
}
let load = self
.load
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let installations = match &*load {
LoadState::Loaded(installations) => installations,
_ => return Some((current.to_owned(), self.detected.clone())),
};
let version_for = |path: &str| {
installations
.iter()
.find(|installation| same_executable(&installation.path.to_string_lossy(), path))
.and_then(|installation| installation.version.clone())
};
let current_version = version_for(current);
let detected_version = version_for(&self.detected);
Some((
java_runtime_label(current, current_version.as_deref()),
java_runtime_label(&self.detected, detected_version.as_deref()),
))
}
}
impl Default for JavaPicker {
@@ -248,6 +279,23 @@ impl Default for JavaPicker {
}
}
fn same_executable(left: &str, right: &str) -> bool {
if left == right {
return true;
}
std::fs::canonicalize(left)
.ok()
.zip(std::fs::canonicalize(right).ok())
.is_some_and(|(left, right)| left == right)
}
fn java_runtime_label(path: &str, version: Option<&str>) -> String {
version.map_or_else(
|| path.to_owned(),
|version| format!("Java {version} {path}"),
)
}
pub(crate) fn memory_kib(value: &str) -> Option<u64> {
let normalized = crate::instance::models::normalize_memory_value(value)?;
let (number, suffix) = normalized.split_at(normalized.len().saturating_sub(1));
@@ -417,6 +465,25 @@ mod tests {
);
}
#[test]
fn automatic_java_compares_selected_executables() {
let picker = JavaPicker::with_auto_path("/auto/java".to_owned());
*picker.load.lock().unwrap() = LoadState::Loaded(vec![
JavaInstallation {
path: "/current/java".into(),
version: Some("21.0.8".to_owned()),
},
JavaInstallation {
path: "/auto/java".into(),
version: Some("21.0.8".to_owned()),
},
]);
assert!(picker.automatic_change("/auto/java").is_none());
assert!(picker.automatic_change("/current/java").is_some());
assert!(picker.automatic_change("/other/java").is_some());
}
#[test]
fn settings_text_input_deletes_the_previous_word() {
let mut input = TextArea::from(["one two"]);