Warn about unsaved changes when closing settings

This commit is contained in:
pizzaboxer 2024-08-27 12:42:16 +01:00
parent 0544f137d0
commit bac13eb507
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
9 changed files with 60 additions and 10 deletions

View File

@ -6,6 +6,8 @@ namespace Bloxstrap
{ {
public override string FileLocation => Path.Combine(Paths.Modifications, "ClientSettings\\ClientAppSettings.json"); public override string FileLocation => Path.Combine(Paths.Modifications, "ClientSettings\\ClientAppSettings.json");
public bool Changed => !OriginalProp.SequenceEqual(Prop);
public static IReadOnlyDictionary<string, string> PresetFlags = new Dictionary<string, string> public static IReadOnlyDictionary<string, string> PresetFlags = new Dictionary<string, string>
{ {
{ "Network.Log", "FLogNetwork" }, { "Network.Log", "FLogNetwork" },
@ -231,12 +233,18 @@ namespace Bloxstrap
Prop[pair.Key] = pair.Value.ToString()!; Prop[pair.Key] = pair.Value.ToString()!;
base.Save(); base.Save();
// clone the dictionary
OriginalProp = new(Prop);
} }
public override void Load() public override void Load()
{ {
base.Load(); base.Load();
// clone the dictionary
OriginalProp = new(Prop);
// TODO - remove when activity tracking has been revamped // TODO - remove when activity tracking has been revamped
if (GetPreset("Network.Log") != "7") if (GetPreset("Network.Log") != "7")
SetPreset("Network.Log", "7"); SetPreset("Network.Log", "7");

View File

@ -4,6 +4,8 @@ namespace Bloxstrap
{ {
public class JsonManager<T> where T : class, new() public class JsonManager<T> where T : class, new()
{ {
public T OriginalProp { get; set; } = new();
public T Prop { get; set; } = new(); public T Prop { get; set; } = new();
public virtual string FileLocation => Path.Combine(Paths.Base, $"{typeof(T).Name}.json"); public virtual string FileLocation => Path.Combine(Paths.Base, $"{typeof(T).Name}.json");

View File

@ -29,12 +29,16 @@ namespace Bloxstrap.Models.SettingTasks.Base
set set
{ {
App.PendingSettingTasks[Name] = this;
_newState = value; _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) { } public BoolBaseTask(string prefix, string name) : base(prefix, name) { }
} }

View File

@ -23,12 +23,16 @@
set set
{ {
App.PendingSettingTasks[Name] = this;
_newState = value; _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<T> Selections { get; private set; } public IEnumerable<T> Selections { get; private set; }
= Enum.GetValues(typeof(T)).Cast<T>().OrderBy(x => = Enum.GetValues(typeof(T)).Cast<T>().OrderBy(x =>

View File

@ -29,12 +29,16 @@ namespace Bloxstrap.Models.SettingTasks.Base
set set
{ {
App.PendingSettingTasks[Name] = this;
_newState = value; _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) { } public StringBaseTask(string prefix, string name) : base(prefix, name) { }
} }

View File

@ -2969,6 +2969,15 @@ namespace Bloxstrap.Resources {
} }
} }
/// <summary>
/// Looks up a localized string similar to You have unsaved changes. Are you sure you want to close without saving?.
/// </summary>
public static string Menu_UnsavedChanges {
get {
return ResourceManager.GetString("Menu.UnsavedChanges", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to They&apos;ll be kept where Bloxstrap was installed, and will automatically be restored on a reinstall.. /// Looks up a localized string similar to They&apos;ll be kept where Bloxstrap was installed, and will automatically be restored on a reinstall..
/// </summary> /// </summary>

View File

@ -1139,4 +1139,7 @@ If not, then please report this exception to the maintainers of this fork. Do NO
<data name="About.Translators.Title" xml:space="preserve"> <data name="About.Translators.Title" xml:space="preserve">
<value>Translators</value> <value>Translators</value>
</data> </data>
<data name="Menu.UnsavedChanges" xml:space="preserve">
<value>You have unsaved changes. Are you sure you want to close without saving?</value>
</data>
</root> </root>

View File

@ -15,7 +15,8 @@
Background="{ui:ThemeResource ApplicationBackgroundBrush}" Background="{ui:ThemeResource ApplicationBackgroundBrush}"
ExtendsContentIntoTitleBar="True" ExtendsContentIntoTitleBar="True"
WindowBackdropType="Mica" WindowBackdropType="Mica"
WindowStartupLocation="CenterScreen"> WindowStartupLocation="CenterScreen"
Closing="WpfUiWindow_Closing">
<Grid> <Grid>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto" /> <RowDefinition Height="Auto" />

View File

@ -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.Controls.Interfaces;
using Wpf.Ui.Mvvm.Contracts; using Wpf.Ui.Mvvm.Contracts;
using Bloxstrap.UI.ViewModels.Settings; using Bloxstrap.UI.ViewModels.Settings;
namespace Bloxstrap.UI.Elements.Settings namespace Bloxstrap.UI.Elements.Settings
@ -22,7 +26,7 @@ namespace Bloxstrap.UI.Elements.Settings
App.Logger.WriteLine("MainWindow::MainWindow", "Initializing menu"); App.Logger.WriteLine("MainWindow::MainWindow", "Initializing menu");
#if DEBUG // easier access #if DEBUG // easier access
EditorWarningNavItem.Visibility = System.Windows.Visibility.Visible; EditorWarningNavItem.Visibility = Visibility.Visible;
#endif #endif
if (showAlreadyRunningWarning) if (showAlreadyRunningWarning)
@ -50,5 +54,16 @@ namespace Bloxstrap.UI.Elements.Settings
public void CloseWindow() => Close(); public void CloseWindow() => Close();
#endregion INavigationWindow methods #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;
}
}
} }
} }