refactor: share window workspace action refresh flow
Deduplicate the repeated action pattern used by revert, undo, and reset so workspace-changing window actions refresh the UI and toast through one boolean helper instead of open-coded branches.
This commit is contained in:
+35
-18
@@ -1366,15 +1366,15 @@ fn install_window_actions(
|
|||||||
let toast_overlay = toast_overlay.clone();
|
let toast_overlay = toast_overlay.clone();
|
||||||
let page_ctx = page_ctx.clone();
|
let page_ctx = page_ctx.clone();
|
||||||
revert_action.connect_activate(move |_, _| {
|
revert_action.connect_activate(move |_, _| {
|
||||||
if restore_saved_snapshot(&state) {
|
run_workspace_bool_action(
|
||||||
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
|
&state,
|
||||||
crate::ui::toast::show_toast(
|
&save_button,
|
||||||
&toast_overlay,
|
&toast_overlay,
|
||||||
|
&page_ctx,
|
||||||
|
|| restore_saved_snapshot(&state),
|
||||||
"Discarded unsaved changes and restored the last saved state",
|
"Discarded unsaved changes and restored the last saved state",
|
||||||
|
"No unsaved changes to revert",
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
crate::ui::toast::show_toast(&toast_overlay, "No unsaved changes to revert");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.add_action(&revert_action);
|
window.add_action(&revert_action);
|
||||||
@@ -1386,15 +1386,15 @@ fn install_window_actions(
|
|||||||
let toast_overlay = toast_overlay.clone();
|
let toast_overlay = toast_overlay.clone();
|
||||||
let page_ctx = page_ctx.clone();
|
let page_ctx = page_ctx.clone();
|
||||||
undo_action.connect_activate(move |_, _| {
|
undo_action.connect_activate(move |_, _| {
|
||||||
if restore_saved_snapshot(&state) {
|
run_workspace_bool_action(
|
||||||
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
|
&state,
|
||||||
crate::ui::toast::show_toast(
|
&save_button,
|
||||||
&toast_overlay,
|
&toast_overlay,
|
||||||
|
&page_ctx,
|
||||||
|
|| restore_saved_snapshot(&state),
|
||||||
"Discarded unsaved changes and restored the last saved state",
|
"Discarded unsaved changes and restored the last saved state",
|
||||||
|
"Nothing to undo",
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
crate::ui::toast::show_toast(&toast_overlay, "Nothing to undo");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.add_action(&undo_action);
|
window.add_action(&undo_action);
|
||||||
@@ -1536,15 +1536,15 @@ fn install_window_actions(
|
|||||||
let page_ctx = page_ctx.clone();
|
let page_ctx = page_ctx.clone();
|
||||||
let settings = settings.cloned();
|
let settings = settings.cloned();
|
||||||
reset_defaults_action.connect_activate(move |_, _| {
|
reset_defaults_action.connect_activate(move |_, _| {
|
||||||
if reset_config_to_defaults(&state, settings.as_ref()) {
|
run_workspace_bool_action(
|
||||||
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
|
&state,
|
||||||
crate::ui::toast::show_toast(
|
&save_button,
|
||||||
&toast_overlay,
|
&toast_overlay,
|
||||||
|
&page_ctx,
|
||||||
|
|| reset_config_to_defaults(&state, settings.as_ref()),
|
||||||
"Reset the current config and preview defaults",
|
"Reset the current config and preview defaults",
|
||||||
|
"Could not reset the current config",
|
||||||
);
|
);
|
||||||
} else {
|
|
||||||
crate::ui::toast::show_toast(&toast_overlay, "Could not reset the current config");
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
window.add_action(&reset_defaults_action);
|
window.add_action(&reset_defaults_action);
|
||||||
@@ -2783,6 +2783,23 @@ fn refresh_workspace_after_config_load(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_workspace_bool_action(
|
||||||
|
state: &Arc<Mutex<AppState>>,
|
||||||
|
save_button: &libadwaita::SplitButton,
|
||||||
|
toast_overlay: &libadwaita::ToastOverlay,
|
||||||
|
page_ctx: &PageBuildContext,
|
||||||
|
operation: impl FnOnce() -> bool,
|
||||||
|
success_message: &str,
|
||||||
|
failure_message: &str,
|
||||||
|
) {
|
||||||
|
if operation() {
|
||||||
|
refresh_workspace_after_config_change(state, save_button, page_ctx);
|
||||||
|
crate::ui::toast::show_toast(toast_overlay, success_message);
|
||||||
|
} else {
|
||||||
|
crate::ui::toast::show_toast(toast_overlay, failure_message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn reload_config_from_disk(
|
fn reload_config_from_disk(
|
||||||
state: &Arc<Mutex<AppState>>,
|
state: &Arc<Mutex<AppState>>,
|
||||||
save_button: &libadwaita::SplitButton,
|
save_button: &libadwaita::SplitButton,
|
||||||
|
|||||||
Reference in New Issue
Block a user