feat: autosave settings changes
This commit is contained in:
1 parent
6ce48b640a
commit
fa6220649f
5 files changed
+130
-159
No files matched your search
+20
-40
@@ -268,18 +268,10 @@ impl App {
|
|||||||
.as_mut()
|
.as_mut()
|
||||||
.and_then(|state| state.confirmed_save());
|
.and_then(|state| state.confirmed_save());
|
||||||
if let Some((updated, desktop)) = confirmed {
|
if let Some((updated, desktop)) = confirmed {
|
||||||
self.apply_instance_settings(*updated, desktop);
|
self.apply_instance_settings(*updated, desktop, true);
|
||||||
}
|
}
|
||||||
self.focused
|
self.focused
|
||||||
}
|
}
|
||||||
Some(confirm_popup::ConfirmTarget::DiscardInstanceSettings) => {
|
|
||||||
self.instance_settings = None;
|
|
||||||
self.pre_overlay_focused
|
|
||||||
}
|
|
||||||
Some(confirm_popup::ConfirmTarget::DiscardLauncherSettings) => {
|
|
||||||
self.global_settings = None;
|
|
||||||
self.pre_overlay_focused
|
|
||||||
}
|
|
||||||
None => FocusedArea::Instances,
|
None => FocusedArea::Instances,
|
||||||
};
|
};
|
||||||
confirm_popup::clear_pending();
|
confirm_popup::clear_pending();
|
||||||
@@ -296,13 +288,12 @@ impl App {
|
|||||||
Some(confirm_popup::ConfirmTarget::ConfigProfile { .. }) => {
|
Some(confirm_popup::ConfirmTarget::ConfigProfile { .. }) => {
|
||||||
FocusedArea::Settings
|
FocusedArea::Settings
|
||||||
}
|
}
|
||||||
Some(confirm_popup::ConfirmTarget::InstanceRuntime { .. })
|
Some(confirm_popup::ConfirmTarget::InstanceRuntime { .. }) => {
|
||||||
| Some(confirm_popup::ConfirmTarget::DiscardInstanceSettings) => {
|
if let Some(state) = self.instance_settings.as_mut() {
|
||||||
|
state.cancel_runtime_change();
|
||||||
|
}
|
||||||
FocusedArea::InstanceSettings
|
FocusedArea::InstanceSettings
|
||||||
}
|
}
|
||||||
Some(confirm_popup::ConfirmTarget::DiscardLauncherSettings) => {
|
|
||||||
FocusedArea::GlobalSettings
|
|
||||||
}
|
|
||||||
_ => FocusedArea::Instances,
|
_ => FocusedArea::Instances,
|
||||||
};
|
};
|
||||||
confirm_popup::clear_pending();
|
confirm_popup::clear_pending();
|
||||||
@@ -675,12 +666,6 @@ impl App {
|
|||||||
self.global_settings = None;
|
self.global_settings = None;
|
||||||
self.focused = self.pre_overlay_focused;
|
self.focused = self.pre_overlay_focused;
|
||||||
}
|
}
|
||||||
widgets::popups::global_settings::Action::ConfirmClose => {
|
|
||||||
confirm_popup::set_pending(
|
|
||||||
confirm_popup::ConfirmTarget::DiscardLauncherSettings,
|
|
||||||
);
|
|
||||||
self.focused = FocusedArea::ConfirmDelete;
|
|
||||||
}
|
|
||||||
widgets::popups::global_settings::Action::OpenRaw(path) => {
|
widgets::popups::global_settings::Action::OpenRaw(path) => {
|
||||||
self.pending_editor = Some(path);
|
self.pending_editor = Some(path);
|
||||||
self.global_settings = None;
|
self.global_settings = None;
|
||||||
@@ -692,8 +677,7 @@ impl App {
|
|||||||
.and_then(|()| crate::config::theme::apply_theme(theme, border));
|
.and_then(|()| crate::config::theme::apply_theme(theme, border));
|
||||||
match result {
|
match result {
|
||||||
Ok(()) => {
|
Ok(()) => {
|
||||||
self.global_settings = None;
|
crate::feedback::request_redraw();
|
||||||
self.focused = self.pre_overlay_focused;
|
|
||||||
}
|
}
|
||||||
Err(error) => error_buffer::push_error(error_buffer::ErrorEvent {
|
Err(error) => error_buffer::push_error(error_buffer::ErrorEvent {
|
||||||
id: 0,
|
id: 0,
|
||||||
@@ -732,7 +716,7 @@ impl App {
|
|||||||
self.focused = self.pre_overlay_focused;
|
self.focused = self.pre_overlay_focused;
|
||||||
}
|
}
|
||||||
widgets::popups::instance_settings::Action::Save(updated, desktop) => {
|
widgets::popups::instance_settings::Action::Save(updated, desktop) => {
|
||||||
self.apply_instance_settings(*updated, desktop);
|
self.apply_instance_settings(*updated, desktop, false);
|
||||||
}
|
}
|
||||||
widgets::popups::instance_settings::Action::ConfirmRuntime { name, from, to } => {
|
widgets::popups::instance_settings::Action::ConfirmRuntime { name, from, to } => {
|
||||||
confirm_popup::set_pending(confirm_popup::ConfirmTarget::InstanceRuntime {
|
confirm_popup::set_pending(confirm_popup::ConfirmTarget::InstanceRuntime {
|
||||||
@@ -742,12 +726,6 @@ impl App {
|
|||||||
});
|
});
|
||||||
self.focused = FocusedArea::ConfirmDelete;
|
self.focused = FocusedArea::ConfirmDelete;
|
||||||
}
|
}
|
||||||
widgets::popups::instance_settings::Action::ConfirmClose => {
|
|
||||||
confirm_popup::set_pending(
|
|
||||||
confirm_popup::ConfirmTarget::DiscardInstanceSettings,
|
|
||||||
);
|
|
||||||
self.focused = FocusedArea::ConfirmDelete;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
@@ -2050,6 +2028,7 @@ impl App {
|
|||||||
&mut self,
|
&mut self,
|
||||||
updated: crate::instance::models::InstanceConfig,
|
updated: crate::instance::models::InstanceConfig,
|
||||||
desktop: bool,
|
desktop: bool,
|
||||||
|
close: bool,
|
||||||
) {
|
) {
|
||||||
let Some(previous) = self.instances_state.selected_instance().cloned() else {
|
let Some(previous) = self.instances_state.selected_instance().cloned() else {
|
||||||
return;
|
return;
|
||||||
@@ -2068,8 +2047,10 @@ impl App {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
self.spawn_instance_settings_update(previous, updated, desktop);
|
self.spawn_instance_settings_update(previous, updated, desktop);
|
||||||
self.instance_settings = None;
|
if close {
|
||||||
self.focused = self.pre_overlay_focused;
|
self.instance_settings = None;
|
||||||
|
self.focused = self.pre_overlay_focused;
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2089,15 +2070,14 @@ impl App {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
self.instances_state
|
self.instances_state
|
||||||
.replace_instance(&previous.name, updated);
|
.replace_instance(&previous.name, updated.clone());
|
||||||
error_buffer::push_error(error_buffer::ErrorEvent {
|
if let Some(state) = self.instance_settings.as_mut() {
|
||||||
id: 0,
|
state.mark_saved(&updated, desktop);
|
||||||
level: tracing::Level::INFO,
|
}
|
||||||
message: format!("Updated instance '{}'", previous.name),
|
if close {
|
||||||
pushed_at: std::time::Instant::now(),
|
self.instance_settings = None;
|
||||||
});
|
self.focused = self.pre_overlay_focused;
|
||||||
self.instance_settings = None;
|
}
|
||||||
self.focused = self.pre_overlay_focused;
|
|
||||||
}
|
}
|
||||||
Err(error) => error_buffer::push_error(error_buffer::ErrorEvent {
|
Err(error) => error_buffer::push_error(error_buffer::ErrorEvent {
|
||||||
id: 0,
|
id: 0,
|
||||||
|
|||||||
+19
-12
@@ -607,11 +607,12 @@ fn settings_panel_routes_legacy_edit_keys_to_tui_popups() {
|
|||||||
ui.draw();
|
ui.draw();
|
||||||
assert!(ui.screen().contains("-Xfoo"));
|
assert!(ui.screen().contains("-Xfoo"));
|
||||||
ui.key(KeyCode::Enter);
|
ui.key(KeyCode::Enter);
|
||||||
|
assert_eq!(ui.app.focused, FocusedArea::InstanceSettings);
|
||||||
|
assert_eq!(
|
||||||
|
ui.app.instances_state.selected_instance().unwrap().jvm_args,
|
||||||
|
["-Xfoo"]
|
||||||
|
);
|
||||||
ui.key(KeyCode::Esc);
|
ui.key(KeyCode::Esc);
|
||||||
assert_eq!(ui.app.focused, FocusedArea::ConfirmDelete);
|
|
||||||
ui.draw();
|
|
||||||
assert!(ui.screen().contains("Discard changes"));
|
|
||||||
ui.key(KeyCode::Enter);
|
|
||||||
assert_eq!(ui.app.focused, FocusedArea::Settings);
|
assert_eq!(ui.app.focused, FocusedArea::Settings);
|
||||||
|
|
||||||
ui.key(KeyCode::Char('g'));
|
ui.key(KeyCode::Char('g'));
|
||||||
@@ -644,7 +645,10 @@ fn runtime_settings_use_the_shared_confirmation_popup() {
|
|||||||
.draft
|
.draft
|
||||||
.game_version = "1.21.2".to_owned();
|
.game_version = "1.21.2".to_owned();
|
||||||
|
|
||||||
ui.key(KeyCode::Char('s'));
|
for _ in 0..4 {
|
||||||
|
ui.key(KeyCode::Char('j'));
|
||||||
|
}
|
||||||
|
ui.key(KeyCode::Char('l'));
|
||||||
|
|
||||||
assert_eq!(ui.app.focused, FocusedArea::ConfirmDelete);
|
assert_eq!(ui.app.focused, FocusedArea::ConfirmDelete);
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
@@ -653,17 +657,12 @@ fn runtime_settings_use_the_shared_confirmation_popup() {
|
|||||||
));
|
));
|
||||||
ui.draw();
|
ui.draw();
|
||||||
assert!(ui.screen().contains("Change runtime"));
|
assert!(ui.screen().contains("Change runtime"));
|
||||||
assert!(ui.screen().contains("Runtime files will be downloaded"));
|
assert!(ui.screen().contains("downloaded before applying"));
|
||||||
assert!(ui.screen().contains("Installed mods may not load"));
|
assert!(ui.screen().contains("Installed mods may not load"));
|
||||||
|
|
||||||
ui.key(KeyCode::Esc);
|
ui.key(KeyCode::Esc);
|
||||||
assert_eq!(ui.app.focused, FocusedArea::InstanceSettings);
|
assert_eq!(ui.app.focused, FocusedArea::InstanceSettings);
|
||||||
ui.key(KeyCode::Esc);
|
ui.key(KeyCode::Esc);
|
||||||
assert!(matches!(
|
|
||||||
confirm::pending_target(),
|
|
||||||
Some(confirm::ConfirmTarget::DiscardInstanceSettings)
|
|
||||||
));
|
|
||||||
ui.key(KeyCode::Enter);
|
|
||||||
assert_eq!(ui.app.focused, FocusedArea::Settings);
|
assert_eq!(ui.app.focused, FocusedArea::Settings);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -698,6 +697,15 @@ fn settings_use_java_memory_and_resolution_controls() {
|
|||||||
.as_deref(),
|
.as_deref(),
|
||||||
Some("1G")
|
Some("1G")
|
||||||
);
|
);
|
||||||
|
assert_eq!(
|
||||||
|
ui.app
|
||||||
|
.instances_state
|
||||||
|
.selected_instance()
|
||||||
|
.unwrap()
|
||||||
|
.memory_min
|
||||||
|
.as_deref(),
|
||||||
|
Some("1G")
|
||||||
|
);
|
||||||
|
|
||||||
for _ in 0..3 {
|
for _ in 0..3 {
|
||||||
ui.key(KeyCode::Char('j'));
|
ui.key(KeyCode::Char('j'));
|
||||||
@@ -711,7 +719,6 @@ fn settings_use_java_memory_and_resolution_controls() {
|
|||||||
assert!(ui.screen().contains("custom"));
|
assert!(ui.screen().contains("custom"));
|
||||||
ui.key(KeyCode::Esc);
|
ui.key(KeyCode::Esc);
|
||||||
ui.key(KeyCode::Esc);
|
ui.key(KeyCode::Esc);
|
||||||
ui.key(KeyCode::Enter);
|
|
||||||
|
|
||||||
ui.key(KeyCode::Char('G'));
|
ui.key(KeyCode::Char('G'));
|
||||||
for _ in 0..4 {
|
for _ in 0..4 {
|
||||||
|
|||||||
@@ -50,8 +50,6 @@ pub enum ConfirmTarget {
|
|||||||
from: String,
|
from: String,
|
||||||
to: String,
|
to: String,
|
||||||
},
|
},
|
||||||
DiscardInstanceSettings,
|
|
||||||
DiscardLauncherSettings,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfirmTarget {
|
impl ConfirmTarget {
|
||||||
@@ -59,9 +57,6 @@ impl ConfirmTarget {
|
|||||||
match self {
|
match self {
|
||||||
Self::OrphanDependencies { .. } => " Remove unused dependencies ".to_owned(),
|
Self::OrphanDependencies { .. } => " Remove unused dependencies ".to_owned(),
|
||||||
Self::InstanceRuntime { .. } => " Change runtime ".to_owned(),
|
Self::InstanceRuntime { .. } => " Change runtime ".to_owned(),
|
||||||
Self::DiscardInstanceSettings | Self::DiscardLauncherSettings => {
|
|
||||||
" Discard changes ".to_owned()
|
|
||||||
}
|
|
||||||
_ => format!(" Delete '{}' ", self.name()),
|
_ => format!(" Delete '{}' ", self.name()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -101,14 +96,8 @@ impl ConfirmTarget {
|
|||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("\n"),
|
.join("\n"),
|
||||||
ConfirmTarget::InstanceRuntime { from, to, .. } => format!(
|
ConfirmTarget::InstanceRuntime { from, to, .. } => format!(
|
||||||
"{from} → {to}\nRuntime files will be downloaded before saving.\n! Installed mods may not load and can be incompatible with the new runtime."
|
"{from} → {to}\nRuntime files will be downloaded before applying the change.\n! Installed mods may not load and can be incompatible with the new runtime."
|
||||||
),
|
),
|
||||||
ConfirmTarget::DiscardInstanceSettings => {
|
|
||||||
"Unsaved instance settings will be lost.".to_owned()
|
|
||||||
}
|
|
||||||
ConfirmTarget::DiscardLauncherSettings => {
|
|
||||||
"Unsaved launcher settings will be lost.".to_owned()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,8 +109,6 @@ impl ConfirmTarget {
|
|||||||
ConfirmTarget::Content { name, .. } => name,
|
ConfirmTarget::Content { name, .. } => name,
|
||||||
ConfirmTarget::OrphanDependencies { .. } => "unused dependencies",
|
ConfirmTarget::OrphanDependencies { .. } => "unused dependencies",
|
||||||
ConfirmTarget::InstanceRuntime { name, .. } => name,
|
ConfirmTarget::InstanceRuntime { name, .. } => name,
|
||||||
ConfirmTarget::DiscardInstanceSettings => "instance settings",
|
|
||||||
ConfirmTarget::DiscardLauncherSettings => "launcher settings",
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,7 +117,6 @@ impl ConfirmTarget {
|
|||||||
Self::Content { dependents, .. } if !dependents.is_empty() => " delete anyway",
|
Self::Content { dependents, .. } if !dependents.is_empty() => " delete anyway",
|
||||||
Self::OrphanDependencies { .. } => " remove all",
|
Self::OrphanDependencies { .. } => " remove all",
|
||||||
Self::InstanceRuntime { .. } => " change",
|
Self::InstanceRuntime { .. } => " change",
|
||||||
Self::DiscardInstanceSettings | Self::DiscardLauncherSettings => " discard",
|
|
||||||
_ => " confirm",
|
_ => " confirm",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ pub struct State {
|
|||||||
selected: usize,
|
selected: usize,
|
||||||
editing: Option<TextArea<'static>>,
|
editing: Option<TextArea<'static>>,
|
||||||
error: Option<String>,
|
error: Option<String>,
|
||||||
config_dirty: bool,
|
save_pending: bool,
|
||||||
themes: Vec<String>,
|
themes: Vec<String>,
|
||||||
theme_picker: bool,
|
theme_picker: bool,
|
||||||
theme_index: usize,
|
theme_index: usize,
|
||||||
@@ -42,7 +42,6 @@ pub struct State {
|
|||||||
pub enum Action {
|
pub enum Action {
|
||||||
None,
|
None,
|
||||||
Save(Box<Config>, String, BorderStyle),
|
Save(Box<Config>, String, BorderStyle),
|
||||||
ConfirmClose,
|
|
||||||
OpenRaw(std::path::PathBuf),
|
OpenRaw(std::path::PathBuf),
|
||||||
Close,
|
Close,
|
||||||
}
|
}
|
||||||
@@ -61,7 +60,7 @@ impl State {
|
|||||||
selected: 0,
|
selected: 0,
|
||||||
editing: None,
|
editing: None,
|
||||||
error: None,
|
error: None,
|
||||||
config_dirty: false,
|
save_pending: false,
|
||||||
themes,
|
themes,
|
||||||
theme_picker: false,
|
theme_picker: false,
|
||||||
theme_index,
|
theme_index,
|
||||||
@@ -119,7 +118,7 @@ impl State {
|
|||||||
if memory_kib(&value) > memory_kib(&self.config.defaults.memory_max) {
|
if memory_kib(&value) > memory_kib(&self.config.defaults.memory_max) {
|
||||||
self.config.defaults.memory_max = value;
|
self.config.defaults.memory_max = value;
|
||||||
}
|
}
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
3 => {
|
3 => {
|
||||||
let value = normalize_memory_value(value).unwrap();
|
let value = normalize_memory_value(value).unwrap();
|
||||||
@@ -127,11 +126,11 @@ impl State {
|
|||||||
if memory_kib(&value) < memory_kib(&self.config.defaults.memory_min) {
|
if memory_kib(&value) < memory_kib(&self.config.defaults.memory_min) {
|
||||||
self.config.defaults.memory_min = value;
|
self.config.defaults.memory_min = value;
|
||||||
}
|
}
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
4 => {
|
4 => {
|
||||||
self.config.paths.java_path = (!value.is_empty()).then(|| value.to_owned());
|
self.config.paths.java_path = (!value.is_empty()).then(|| value.to_owned());
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -147,6 +146,8 @@ impl State {
|
|||||||
) {
|
) {
|
||||||
self.error = Some(error.to_string());
|
self.error = Some(error.to_string());
|
||||||
self.theme.theme = previous;
|
self.theme.theme = previous;
|
||||||
|
} else if self.theme.theme != previous {
|
||||||
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +185,8 @@ impl State {
|
|||||||
) {
|
) {
|
||||||
self.error = Some(error.to_string());
|
self.error = Some(error.to_string());
|
||||||
self.theme.border_style = previous;
|
self.theme.border_style = previous;
|
||||||
|
} else if self.theme.border_style != previous {
|
||||||
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -201,7 +204,7 @@ impl State {
|
|||||||
KeyCode::Esc | KeyCode::Char('h') | KeyCode::Left => self.java_picker_open = false,
|
KeyCode::Esc | KeyCode::Char('h') | KeyCode::Left => self.java_picker_open = false,
|
||||||
KeyCode::Char('a') => {
|
KeyCode::Char('a') => {
|
||||||
if self.config.paths.java_path.take().is_some() {
|
if self.config.paths.java_path.take().is_some() {
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
self.java_picker_open = false;
|
self.java_picker_open = false;
|
||||||
}
|
}
|
||||||
@@ -220,7 +223,7 @@ impl State {
|
|||||||
JavaChoice::Installation(path) => {
|
JavaChoice::Installation(path) => {
|
||||||
if self.config.paths.java_path.as_deref() != Some(&path) {
|
if self.config.paths.java_path.as_deref() != Some(&path) {
|
||||||
self.config.paths.java_path = Some(path);
|
self.config.paths.java_path = Some(path);
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -248,21 +251,24 @@ impl State {
|
|||||||
self.config.defaults.memory_min = value;
|
self.config.defaults.memory_min = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
self.error = None;
|
self.error = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn validate_before_save(&mut self) -> bool {
|
|
||||||
self.error = None;
|
|
||||||
let min = memory_kib(&self.config.defaults.memory_min);
|
|
||||||
let max = memory_kib(&self.config.defaults.memory_max);
|
|
||||||
if min.zip(max).is_some_and(|(min, max)| min > max) {
|
|
||||||
self.error = Some("minimum memory cannot exceed maximum memory".to_owned());
|
|
||||||
}
|
|
||||||
self.error.is_none()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn handle_key(&mut self, key: &KeyEvent) -> Action {
|
pub fn handle_key(&mut self, key: &KeyEvent) -> Action {
|
||||||
|
let action = self.handle_key_inner(key);
|
||||||
|
if matches!(action, Action::None) && self.save_pending && self.editing.is_none() {
|
||||||
|
self.save_pending = false;
|
||||||
|
return Action::Save(
|
||||||
|
Box::new(self.config.clone()),
|
||||||
|
self.theme.theme.clone(),
|
||||||
|
self.theme.border_style.clone(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
action
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_key_inner(&mut self, key: &KeyEvent) -> Action {
|
||||||
if self.java_picker_open {
|
if self.java_picker_open {
|
||||||
self.handle_java_picker_key(key);
|
self.handle_java_picker_key(key);
|
||||||
return Action::None;
|
return Action::None;
|
||||||
@@ -301,26 +307,12 @@ impl State {
|
|||||||
},
|
},
|
||||||
KeyCode::Char('a') if self.selected == 4 => {
|
KeyCode::Char('a') if self.selected == 4 => {
|
||||||
if self.config.paths.java_path.take().is_some() {
|
if self.config.paths.java_path.take().is_some() {
|
||||||
self.config_dirty = true;
|
self.save_pending = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Char('c') if self.selected == 4 => {
|
KeyCode::Char('c') if self.selected == 4 => {
|
||||||
self.editing = Some(new_text_area(vec![self.value(4)]));
|
self.editing = Some(new_text_area(vec![self.value(4)]));
|
||||||
}
|
}
|
||||||
KeyCode::Char('s') => {
|
|
||||||
if self.validate_before_save() {
|
|
||||||
return Action::Save(
|
|
||||||
Box::new(self.config.clone()),
|
|
||||||
self.theme.theme.clone(),
|
|
||||||
self.theme.border_style.clone(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Char('E') if self.config_dirty => {
|
|
||||||
self.error = Some(
|
|
||||||
"save or discard launcher defaults before opening the raw file".to_owned(),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
KeyCode::Char('E') => {
|
KeyCode::Char('E') => {
|
||||||
let file = if self.selected <= 1 {
|
let file = if self.selected <= 1 {
|
||||||
"theme.toml"
|
"theme.toml"
|
||||||
@@ -329,7 +321,6 @@ impl State {
|
|||||||
};
|
};
|
||||||
return Action::OpenRaw(crate::config::get_config_path().join(file));
|
return Action::OpenRaw(crate::config::get_config_path().join(file));
|
||||||
}
|
}
|
||||||
KeyCode::Esc if self.config_dirty => return Action::ConfirmClose,
|
|
||||||
KeyCode::Esc => return Action::Close,
|
KeyCode::Esc => return Action::Close,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -413,25 +404,18 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
|
|||||||
} else if state.theme_picker {
|
} else if state.theme_picker {
|
||||||
super::keybind_line(&[("h", " back"), ("Enter", " select")])
|
super::keybind_line(&[("h", " back"), ("Enter", " select")])
|
||||||
} else if matches!(state.selected, 2 | 3) {
|
} else if matches!(state.selected, 2 | 3) {
|
||||||
super::keybind_line(&[
|
super::keybind_line(&[("h/l", " adjust"), ("Enter", " exact"), ("Esc", " back")])
|
||||||
("h/l", " adjust"),
|
|
||||||
("Enter", " exact"),
|
|
||||||
("s", " save"),
|
|
||||||
("Esc", " back"),
|
|
||||||
])
|
|
||||||
} else if state.selected == 4 {
|
} else if state.selected == 4 {
|
||||||
super::keybind_line(&[
|
super::keybind_line(&[
|
||||||
("Enter", " runtimes"),
|
("Enter", " runtimes"),
|
||||||
("a", " auto"),
|
("a", " auto"),
|
||||||
("c", " custom"),
|
("c", " custom"),
|
||||||
("s", " save"),
|
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
} else {
|
} else {
|
||||||
super::keybind_line(&[
|
super::keybind_line(&[
|
||||||
("j/k", ""),
|
("j/k", ""),
|
||||||
("Enter", " edit"),
|
("Enter", " edit"),
|
||||||
("s", " save"),
|
|
||||||
("E", " raw"),
|
("E", " raw"),
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
@@ -594,7 +578,10 @@ mod tests {
|
|||||||
let mut state = State::new();
|
let mut state = State::new();
|
||||||
state.selected = 2;
|
state.selected = 2;
|
||||||
let original = state.config.defaults.memory_min.clone();
|
let original = state.config.defaults.memory_min.clone();
|
||||||
state.handle_key(&KeyEvent::from(KeyCode::Char('l')));
|
assert!(matches!(
|
||||||
|
state.handle_key(&KeyEvent::from(KeyCode::Char('l'))),
|
||||||
|
Action::Save(..)
|
||||||
|
));
|
||||||
assert_ne!(state.config.defaults.memory_min, original);
|
assert_ne!(state.config.defaults.memory_min, original);
|
||||||
assert!(state.editing.is_none());
|
assert!(state.editing.is_none());
|
||||||
|
|
||||||
|
|||||||
@@ -110,7 +110,6 @@ pub enum Action {
|
|||||||
from: String,
|
from: String,
|
||||||
to: String,
|
to: String,
|
||||||
},
|
},
|
||||||
ConfirmClose,
|
|
||||||
OpenRaw,
|
OpenRaw,
|
||||||
Close,
|
Close,
|
||||||
}
|
}
|
||||||
@@ -617,6 +616,42 @@ impl State {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_key(&mut self, key: &KeyEvent) -> Action {
|
pub fn handle_key(&mut self, key: &KeyEvent) -> Action {
|
||||||
|
let before = self.draft.clone();
|
||||||
|
let desktop_before = self.desktop;
|
||||||
|
let action = self.handle_key_inner(key);
|
||||||
|
if !matches!(action, Action::None)
|
||||||
|
|| before == self.draft && desktop_before == self.desktop
|
||||||
|
|| !self.dirty()
|
||||||
|
{
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
if self.runtime_changed() {
|
||||||
|
if self.draft.loader != ModLoader::Vanilla
|
||||||
|
&& self
|
||||||
|
.draft
|
||||||
|
.loader_version
|
||||||
|
.as_deref()
|
||||||
|
.is_none_or(str::is_empty)
|
||||||
|
{
|
||||||
|
return Action::None;
|
||||||
|
}
|
||||||
|
if !self.validate_before_save() {
|
||||||
|
return Action::None;
|
||||||
|
}
|
||||||
|
return Action::ConfirmRuntime {
|
||||||
|
name: self.draft.name.clone(),
|
||||||
|
from: runtime_label(&self.original),
|
||||||
|
to: runtime_label(&self.draft),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if self.validate_before_save() {
|
||||||
|
Action::Save(Box::new(self.draft.clone()), self.desktop)
|
||||||
|
} else {
|
||||||
|
Action::None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_key_inner(&mut self, key: &KeyEvent) -> Action {
|
||||||
if self.choice_picker.is_some() {
|
if self.choice_picker.is_some() {
|
||||||
self.handle_choice_key(key);
|
self.handle_choice_key(key);
|
||||||
return Action::None;
|
return Action::None;
|
||||||
@@ -657,25 +692,7 @@ impl State {
|
|||||||
KeyCode::Char('c') if self.selected == 3 => {
|
KeyCode::Char('c') if self.selected == 3 => {
|
||||||
self.editing = Some(new_text_area(vec![self.value(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() {
|
|
||||||
return Action::ConfirmRuntime {
|
|
||||||
name: self.draft.name.clone(),
|
|
||||||
from: runtime_label(&self.original),
|
|
||||||
to: runtime_label(&self.draft),
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
return Action::Save(Box::new(self.draft.clone()), self.desktop);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
KeyCode::Char('E') if self.dirty() => {
|
|
||||||
self.error =
|
|
||||||
Some("save or discard draft changes before opening the raw file".to_owned());
|
|
||||||
}
|
|
||||||
KeyCode::Char('E') => return Action::OpenRaw,
|
KeyCode::Char('E') => return Action::OpenRaw,
|
||||||
KeyCode::Esc if self.dirty() => return Action::ConfirmClose,
|
|
||||||
KeyCode::Esc => return Action::Close,
|
KeyCode::Esc => return Action::Close,
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
@@ -686,6 +703,21 @@ impl State {
|
|||||||
self.validate_before_save()
|
self.validate_before_save()
|
||||||
.then(|| (Box::new(self.draft.clone()), self.desktop))
|
.then(|| (Box::new(self.draft.clone()), self.desktop))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn mark_saved(&mut self, saved: &InstanceConfig, desktop: bool) {
|
||||||
|
self.original = saved.clone();
|
||||||
|
self.draft = saved.clone();
|
||||||
|
self.original_desktop = desktop;
|
||||||
|
self.desktop = desktop;
|
||||||
|
self.error = None;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn cancel_runtime_change(&mut self) {
|
||||||
|
self.draft.game_version = self.original.game_version.clone();
|
||||||
|
self.draft.loader = self.original.loader;
|
||||||
|
self.draft.loader_version = self.original.loader_version.clone();
|
||||||
|
self.error = None;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn runtime_label(config: &InstanceConfig) -> String {
|
fn runtime_label(config: &InstanceConfig) -> String {
|
||||||
@@ -816,7 +848,6 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
|
|||||||
("h/l", " adjust"),
|
("h/l", " adjust"),
|
||||||
("Enter", " exact"),
|
("Enter", " exact"),
|
||||||
("d", " default"),
|
("d", " default"),
|
||||||
("s", " save"),
|
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
} else if state.selected == 3 {
|
} else if state.selected == 3 {
|
||||||
@@ -824,7 +855,6 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
|
|||||||
("Enter", " runtimes"),
|
("Enter", " runtimes"),
|
||||||
("a", " auto"),
|
("a", " auto"),
|
||||||
("c", " custom"),
|
("c", " custom"),
|
||||||
("s", " save"),
|
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
} else if state.selected == 7 {
|
} else if state.selected == 7 {
|
||||||
@@ -832,14 +862,12 @@ pub fn render(frame: &mut Frame, area: Rect, state: &mut State) {
|
|||||||
("l", " presets"),
|
("l", " presets"),
|
||||||
("Enter", " custom"),
|
("Enter", " custom"),
|
||||||
("d", " default"),
|
("d", " default"),
|
||||||
("s", " save"),
|
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
} else {
|
} else {
|
||||||
super::keybind_line(&[
|
super::keybind_line(&[
|
||||||
("j/k", ""),
|
("j/k", ""),
|
||||||
("Enter", " edit"),
|
("Enter", " edit"),
|
||||||
("s", " save"),
|
|
||||||
("E", " raw"),
|
("E", " raw"),
|
||||||
("Esc", " back"),
|
("Esc", " back"),
|
||||||
])
|
])
|
||||||
@@ -974,20 +1002,15 @@ fn field_line(state: &State, index: usize, label: &str) -> Line<'static> {
|
|||||||
} else {
|
} else {
|
||||||
state.display_value(index)
|
state.display_value(index)
|
||||||
};
|
};
|
||||||
let dirty = field_dirty(state, index);
|
|
||||||
let mut spans = vec![
|
let mut spans = vec![
|
||||||
Span::styled(
|
Span::styled(
|
||||||
if selected { "▶ " } else { " " },
|
if selected { "▶ " } else { " " },
|
||||||
Style::default().fg(theme.accent()),
|
Style::default().fg(theme.accent()),
|
||||||
),
|
),
|
||||||
Span::styled(
|
Span::styled(
|
||||||
format!("{label:<16}"),
|
format!("{label:<18}"),
|
||||||
Style::default().fg(theme.text_dim()),
|
Style::default().fg(theme.text_dim()),
|
||||||
),
|
),
|
||||||
Span::styled(
|
|
||||||
if dirty { "* " } else { " " },
|
|
||||||
Style::default().fg(theme.accent()),
|
|
||||||
),
|
|
||||||
Span::styled(
|
Span::styled(
|
||||||
value,
|
value,
|
||||||
Style::default()
|
Style::default()
|
||||||
@@ -1083,21 +1106,6 @@ fn jvm_row_count(state: &State, width: u16) -> usize {
|
|||||||
rows
|
rows
|
||||||
}
|
}
|
||||||
|
|
||||||
fn field_dirty(state: &State, index: usize) -> bool {
|
|
||||||
match index {
|
|
||||||
0 => state.draft.game_version != state.original.game_version,
|
|
||||||
1 => state.draft.loader != state.original.loader,
|
|
||||||
2 => state.draft.loader_version != state.original.loader_version,
|
|
||||||
3 => state.draft.java_path != state.original.java_path,
|
|
||||||
4 => state.draft.memory_min != state.original.memory_min,
|
|
||||||
5 => state.draft.memory_max != state.original.memory_max,
|
|
||||||
6 => state.draft.jvm_args != state.original.jvm_args,
|
|
||||||
7 => state.draft.resolution != state.original.resolution,
|
|
||||||
8 => state.desktop != state.original_desktop,
|
|
||||||
_ => false,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) {
|
fn render_choice_picker(frame: &mut Frame, area: Rect, state: &State) {
|
||||||
let theme = THEME.as_ref();
|
let theme = THEME.as_ref();
|
||||||
let mut list_area = area;
|
let mut list_area = area;
|
||||||
@@ -1310,10 +1318,13 @@ mod tests {
|
|||||||
fn runtime_changes_require_confirmation_before_save() {
|
fn runtime_changes_require_confirmation_before_save() {
|
||||||
let temp = tempfile::tempdir().unwrap();
|
let temp = tempfile::tempdir().unwrap();
|
||||||
let mut state = State::new(&instance(), temp.path());
|
let mut state = State::new(&instance(), temp.path());
|
||||||
state.draft.game_version = "1.21.2".to_owned();
|
*state.loader_versions.lock().unwrap() = LoadState::Loaded(vec!["0.16.15".to_owned()]);
|
||||||
|
state.picker = Some(VersionPicker::Loader);
|
||||||
|
state.picker_initialized = true;
|
||||||
|
state.picker_index = 0;
|
||||||
|
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
state.handle_key(&KeyEvent::from(KeyCode::Char('s'))),
|
state.handle_key(&KeyEvent::from(KeyCode::Enter)),
|
||||||
Action::ConfirmRuntime { .. }
|
Action::ConfirmRuntime { .. }
|
||||||
));
|
));
|
||||||
assert!(state.confirmed_save().is_some());
|
assert!(state.confirmed_save().is_some());
|
||||||
@@ -1349,9 +1360,9 @@ mod tests {
|
|||||||
let mut config = instance();
|
let mut config = instance();
|
||||||
config.config_sync_profile = Some("shared".to_owned());
|
config.config_sync_profile = Some("shared".to_owned());
|
||||||
let mut state = State::new(&config, temp.path());
|
let mut state = State::new(&config, temp.path());
|
||||||
state.desktop = !state.desktop;
|
state.selected = 8;
|
||||||
|
|
||||||
let Action::Save(config, _) = state.handle_key(&KeyEvent::from(KeyCode::Char('s'))) else {
|
let Action::Save(config, _) = state.handle_key(&KeyEvent::from(KeyCode::Enter)) else {
|
||||||
panic!("expected settings save");
|
panic!("expected settings save");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user