From 7de8224e67340e435697b39b6b55dbc338480492 Mon Sep 17 00:00:00 2001 From: 44r0n7 <44r0n7+gitea@pm.me> Date: Tue, 31 Mar 2026 19:09:20 -0400 Subject: [PATCH] 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. --- src/window.rs | 71 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 27 deletions(-) diff --git a/src/window.rs b/src/window.rs index 4fdc970..585736a 100644 --- a/src/window.rs +++ b/src/window.rs @@ -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>, + 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>, save_button: &libadwaita::SplitButton,