From bac13eb507961e52124f8df541bd2463e56f732e Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Tue, 27 Aug 2024 12:42:16 +0100 Subject: [PATCH] Warn about unsaved changes when closing settings --- Bloxstrap/FastFlagManager.cs | 8 ++++++++ Bloxstrap/JsonManager.cs | 2 ++ .../Models/SettingTasks/Base/BoolBaseTask.cs | 8 ++++++-- .../Models/SettingTasks/Base/EnumBaseTask.cs | 8 ++++++-- .../SettingTasks/Base/StringBaseTask.cs | 8 ++++++-- Bloxstrap/Resources/Strings.Designer.cs | 9 +++++++++ Bloxstrap/Resources/Strings.resx | 3 +++ .../UI/Elements/Settings/MainWindow.xaml | 5 +++-- .../UI/Elements/Settings/MainWindow.xaml.cs | 19 +++++++++++++++++-- 9 files changed, 60 insertions(+), 10 deletions(-) diff --git a/Bloxstrap/FastFlagManager.cs b/Bloxstrap/FastFlagManager.cs index d702c22..f211752 100644 --- a/Bloxstrap/FastFlagManager.cs +++ b/Bloxstrap/FastFlagManager.cs @@ -6,6 +6,8 @@ namespace Bloxstrap { public override string FileLocation => Path.Combine(Paths.Modifications, "ClientSettings\\ClientAppSettings.json"); + public bool Changed => !OriginalProp.SequenceEqual(Prop); + public static IReadOnlyDictionary PresetFlags = new Dictionary { { "Network.Log", "FLogNetwork" }, @@ -231,12 +233,18 @@ namespace Bloxstrap Prop[pair.Key] = pair.Value.ToString()!; base.Save(); + + // clone the dictionary + OriginalProp = new(Prop); } public override void Load() { base.Load(); + // clone the dictionary + OriginalProp = new(Prop); + // TODO - remove when activity tracking has been revamped if (GetPreset("Network.Log") != "7") SetPreset("Network.Log", "7"); diff --git a/Bloxstrap/JsonManager.cs b/Bloxstrap/JsonManager.cs index 1a91949..a8e8c3e 100644 --- a/Bloxstrap/JsonManager.cs +++ b/Bloxstrap/JsonManager.cs @@ -4,6 +4,8 @@ namespace Bloxstrap { public class JsonManager where T : class, new() { + public T OriginalProp { get; set; } = new(); + public T Prop { get; set; } = new(); public virtual string FileLocation => Path.Combine(Paths.Base, $"{typeof(T).Name}.json"); diff --git a/Bloxstrap/Models/SettingTasks/Base/BoolBaseTask.cs b/Bloxstrap/Models/SettingTasks/Base/BoolBaseTask.cs index b22b067..5b5adce 100644 --- a/Bloxstrap/Models/SettingTasks/Base/BoolBaseTask.cs +++ b/Bloxstrap/Models/SettingTasks/Base/BoolBaseTask.cs @@ -29,12 +29,16 @@ namespace Bloxstrap.Models.SettingTasks.Base set { - App.PendingSettingTasks[Name] = this; _newState = value; + + if (Changed) + App.PendingSettingTasks[Name] = this; + else + App.PendingSettingTasks.Remove(Name); } } - public override bool Changed => NewState != OriginalState; + public override bool Changed => _newState != OriginalState; public BoolBaseTask(string prefix, string name) : base(prefix, name) { } } diff --git a/Bloxstrap/Models/SettingTasks/Base/EnumBaseTask.cs b/Bloxstrap/Models/SettingTasks/Base/EnumBaseTask.cs index 56a8c47..c64e0a0 100644 --- a/Bloxstrap/Models/SettingTasks/Base/EnumBaseTask.cs +++ b/Bloxstrap/Models/SettingTasks/Base/EnumBaseTask.cs @@ -23,12 +23,16 @@ set { - App.PendingSettingTasks[Name] = this; _newState = value; + + if (Changed) + App.PendingSettingTasks[Name] = this; + else + App.PendingSettingTasks.Remove(Name); } } - public override bool Changed => !NewState.Equals(OriginalState); + public override bool Changed => !_newState.Equals(OriginalState); public IEnumerable Selections { get; private set; } = Enum.GetValues(typeof(T)).Cast().OrderBy(x => diff --git a/Bloxstrap/Models/SettingTasks/Base/StringBaseTask.cs b/Bloxstrap/Models/SettingTasks/Base/StringBaseTask.cs index dd4a07f..6b3ce13 100644 --- a/Bloxstrap/Models/SettingTasks/Base/StringBaseTask.cs +++ b/Bloxstrap/Models/SettingTasks/Base/StringBaseTask.cs @@ -29,12 +29,16 @@ namespace Bloxstrap.Models.SettingTasks.Base set { - App.PendingSettingTasks[Name] = this; _newState = value; + + if (Changed) + App.PendingSettingTasks[Name] = this; + else + App.PendingSettingTasks.Remove(Name); } } - public override bool Changed => NewState != OriginalState; + public override bool Changed => _newState != OriginalState; public StringBaseTask(string prefix, string name) : base(prefix, name) { } } diff --git a/Bloxstrap/Resources/Strings.Designer.cs b/Bloxstrap/Resources/Strings.Designer.cs index cbfba62..8a77c15 100644 --- a/Bloxstrap/Resources/Strings.Designer.cs +++ b/Bloxstrap/Resources/Strings.Designer.cs @@ -2969,6 +2969,15 @@ namespace Bloxstrap.Resources { } } + /// + /// Looks up a localized string similar to You have unsaved changes. Are you sure you want to close without saving?. + /// + public static string Menu_UnsavedChanges { + get { + return ResourceManager.GetString("Menu.UnsavedChanges", resourceCulture); + } + } + /// /// Looks up a localized string similar to They'll be kept where Bloxstrap was installed, and will automatically be restored on a reinstall.. /// diff --git a/Bloxstrap/Resources/Strings.resx b/Bloxstrap/Resources/Strings.resx index 09587cb..0b01859 100644 --- a/Bloxstrap/Resources/Strings.resx +++ b/Bloxstrap/Resources/Strings.resx @@ -1139,4 +1139,7 @@ If not, then please report this exception to the maintainers of this fork. Do NO Translators + + You have unsaved changes. Are you sure you want to close without saving? + \ No newline at end of file diff --git a/Bloxstrap/UI/Elements/Settings/MainWindow.xaml b/Bloxstrap/UI/Elements/Settings/MainWindow.xaml index 4d6fc63..7012567 100644 --- a/Bloxstrap/UI/Elements/Settings/MainWindow.xaml +++ b/Bloxstrap/UI/Elements/Settings/MainWindow.xaml @@ -15,7 +15,8 @@ Background="{ui:ThemeResource ApplicationBackgroundBrush}" ExtendsContentIntoTitleBar="True" WindowBackdropType="Mica" - WindowStartupLocation="CenterScreen"> + WindowStartupLocation="CenterScreen" + Closing="WpfUiWindow_Closing"> @@ -93,7 +94,7 @@ - + diff --git a/Bloxstrap/UI/Elements/Settings/MainWindow.xaml.cs b/Bloxstrap/UI/Elements/Settings/MainWindow.xaml.cs index c877bae..c22a732 100644 --- a/Bloxstrap/UI/Elements/Settings/MainWindow.xaml.cs +++ b/Bloxstrap/UI/Elements/Settings/MainWindow.xaml.cs @@ -1,6 +1,10 @@ -using System.Windows.Controls; +using System.ComponentModel; +using System.Windows; +using System.Windows.Controls; + using Wpf.Ui.Controls.Interfaces; using Wpf.Ui.Mvvm.Contracts; + using Bloxstrap.UI.ViewModels.Settings; namespace Bloxstrap.UI.Elements.Settings @@ -22,7 +26,7 @@ namespace Bloxstrap.UI.Elements.Settings App.Logger.WriteLine("MainWindow::MainWindow", "Initializing menu"); #if DEBUG // easier access - EditorWarningNavItem.Visibility = System.Windows.Visibility.Visible; + EditorWarningNavItem.Visibility = Visibility.Visible; #endif if (showAlreadyRunningWarning) @@ -50,5 +54,16 @@ namespace Bloxstrap.UI.Elements.Settings public void CloseWindow() => Close(); #endregion INavigationWindow methods + + private void WpfUiWindow_Closing(object sender, CancelEventArgs e) + { + if (App.FastFlags.Changed || App.PendingSettingTasks.Any()) + { + var result = Frontend.ShowMessageBox(Strings.Menu_UnsavedChanges, MessageBoxImage.Warning, MessageBoxButton.YesNo); + + if (result != MessageBoxResult.Yes) + e.Cancel = true; + } + } } }