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 bool Changed => !OriginalProp.SequenceEqual(Prop);
public static IReadOnlyDictionary<string, string> PresetFlags = new Dictionary<string, string>
{
{ "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");

View File

@ -4,6 +4,8 @@ namespace Bloxstrap
{
public class JsonManager<T> 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");

View File

@ -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) { }
}

View File

@ -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<T> Selections { get; private set; }
= Enum.GetValues(typeof(T)).Cast<T>().OrderBy(x =>

View File

@ -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) { }
}

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>
/// 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>

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">
<value>Translators</value>
</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>

View File

@ -15,7 +15,8 @@
Background="{ui:ThemeResource ApplicationBackgroundBrush}"
ExtendsContentIntoTitleBar="True"
WindowBackdropType="Mica"
WindowStartupLocation="CenterScreen">
WindowStartupLocation="CenterScreen"
Closing="WpfUiWindow_Closing">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
@ -93,7 +94,7 @@
<ui:Button Content="{x:Static resources:Strings.Menu_Save}" Appearance="Primary" Command="{Binding SaveSettingsCommand, Mode=OneWay}" />
</StatusBarItem>
<StatusBarItem Grid.Column="2" Padding="4,0,0,0">
<ui:Button Content="{x:Static resources:Strings.Common_Close}" IsCancel="True" />
<ui:Button Content="{x:Static resources:Strings.Common_Close}" IsCancel="True" />
</StatusBarItem>
</StatusBar>
</Grid>

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.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;
}
}
}
}