From f6db9748a9b8ebaeba87a49424d089dba4395af2 Mon Sep 17 00:00:00 2001 From: Heavenly Avenger Date: Fri, 28 Mar 2025 08:13:49 -0300 Subject: [PATCH] Delays file writes when saving all windows. Avoids truncating and rewriting the json file as many times as there are windows, calling the Save() function only when the list if fully built. --- src/WindowResizer.Configuration/ProfilesFactory.cs | 4 +++- src/WindowResizer/TrayContext.cs | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/WindowResizer.Configuration/ProfilesFactory.cs b/src/WindowResizer.Configuration/ProfilesFactory.cs index 420c8d3..98d96f1 100644 --- a/src/WindowResizer.Configuration/ProfilesFactory.cs +++ b/src/WindowResizer.Configuration/ProfilesFactory.cs @@ -13,6 +13,7 @@ public static class ProfilesFactory private static string _portableConfigPath = string.Empty; public static bool PortableMode; + public static bool PostponeSaving = false; public static string ConfigPath = string.Empty; public static void SetPath(string roamingPath, string portablePath) @@ -78,7 +79,8 @@ public static void Save() { var json = JsonConvert.SerializeObject(Profiles); new FileInfo(ConfigPath).Directory?.Create(); - File.WriteAllText(ConfigPath, json); + + if (!PostponeSaving) File.WriteAllText(ConfigPath, json); Profiles.Updated(); } diff --git a/src/WindowResizer/TrayContext.cs b/src/WindowResizer/TrayContext.cs index 723e364..ddaeba2 100644 --- a/src/WindowResizer/TrayContext.cs +++ b/src/WindowResizer/TrayContext.cs @@ -389,6 +389,7 @@ private void OnKeyPressed(object sender, KeyPressedEventArgs e) private void SaveAll() { var windows = Resizer.GetOpenWindows(); + ProfilesFactory.PostponeSaving = true; foreach (var window in windows) { if (Resizer.GetWindowState(window) != WindowState.Minimized) @@ -397,6 +398,9 @@ private void SaveAll() } } + ProfilesFactory.PostponeSaving = false; + ProfilesFactory.Save(); + if (ProfilesFactory.Current.NotifyOnSaved) { Toast.ShowToast(