feat: confirm automatic Java changes
This commit is contained in:
1 parent
3566d6b50d
commit
59407ee01d
6 files changed
+314
-47
No files matched your search
@@ -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",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"]);
|
||||
|
||||
Reference in new issue
Block a user