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:
2026-03-31 19:09:20 -04:00
parent c26d9531f7
commit 7de8224e67
+44 -27
View File
@@ -1366,15 +1366,15 @@ fn install_window_actions(
let toast_overlay = toast_overlay.clone();
let page_ctx = page_ctx.clone();
revert_action.connect_activate(move |_, _| {
if restore_saved_snapshot(&state) {
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
crate::ui::toast::show_toast(
&toast_overlay,
"Discarded unsaved changes and restored the last saved state",
);
} else {
crate::ui::toast::show_toast(&toast_overlay, "No unsaved changes to revert");
}
run_workspace_bool_action(
&state,
&save_button,
&toast_overlay,
&page_ctx,
|| restore_saved_snapshot(&state),
"Discarded unsaved changes and restored the last saved state",
"No unsaved changes to revert",
);
});
}
window.add_action(&revert_action);
@@ -1386,15 +1386,15 @@ fn install_window_actions(
let toast_overlay = toast_overlay.clone();
let page_ctx = page_ctx.clone();
undo_action.connect_activate(move |_, _| {
if restore_saved_snapshot(&state) {
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
crate::ui::toast::show_toast(
&toast_overlay,
"Discarded unsaved changes and restored the last saved state",
);
} else {
crate::ui::toast::show_toast(&toast_overlay, "Nothing to undo");
}
run_workspace_bool_action(
&state,
&save_button,
&toast_overlay,
&page_ctx,
|| restore_saved_snapshot(&state),
"Discarded unsaved changes and restored the last saved state",
"Nothing to undo",
);
});
}
window.add_action(&undo_action);
@@ -1536,15 +1536,15 @@ fn install_window_actions(
let page_ctx = page_ctx.clone();
let settings = settings.cloned();
reset_defaults_action.connect_activate(move |_, _| {
if reset_config_to_defaults(&state, settings.as_ref()) {
refresh_workspace_after_config_change(&state, &save_button, &page_ctx);
crate::ui::toast::show_toast(
&toast_overlay,
"Reset the current config and preview defaults",
);
} else {
crate::ui::toast::show_toast(&toast_overlay, "Could not reset the current config");
}
run_workspace_bool_action(
&state,
&save_button,
&toast_overlay,
&page_ctx,
|| reset_config_to_defaults(&state, settings.as_ref()),
"Reset the current config and preview defaults",
"Could not reset the current config",
);
});
}
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(
state: &Arc<Mutex<AppState>>,
save_button: &libadwaita::SplitButton,