From e04bdad16c90f43dcb0be324f7e2532a9bd44060 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Wed, 18 Oct 2023 02:59:41 -0400 Subject: [PATCH] Add first-run Options title, fix saving bug --- CHANGELIST.md | 1 + MPF.Core/UI/ViewModels/MainViewModel.cs | 12 ++++-- MPF.Core/UI/ViewModels/OptionsViewModel.cs | 47 +++++++++++++++++++++- MPF.UI.Core/Windows/MainWindow.xaml.cs | 29 ++++++++----- MPF.UI.Core/Windows/OptionsWindow.xaml | 9 ++++- MPF.UI.Core/Windows/OptionsWindow.xaml.cs | 11 +++++ 6 files changed, 92 insertions(+), 17 deletions(-) diff --git a/CHANGELIST.md b/CHANGELIST.md index b3248acc..adce6b7f 100644 --- a/CHANGELIST.md +++ b/CHANGELIST.md @@ -6,6 +6,7 @@ - Create skeleton for first-run - Show options window on first run - Fix drive letter issue in UI +- Add first-run Options title, fix saving bug ### 2.7.2 (2023-10-17) diff --git a/MPF.Core/UI/ViewModels/MainViewModel.cs b/MPF.Core/UI/ViewModels/MainViewModel.cs index ac3f0e6e..278d1b2b 100644 --- a/MPF.Core/UI/ViewModels/MainViewModel.cs +++ b/MPF.Core/UI/ViewModels/MainViewModel.cs @@ -988,11 +988,17 @@ namespace MPF.Core.UI.ViewModels public void UpdateOptions(bool savedSettings, Data.Options? newOptions) #endif { + // Get which options to save + var optionsToSave = savedSettings ? newOptions : Options; + + // Ensure the first run flag is unset + var continuingOptions = new Data.Options(optionsToSave); + continuingOptions.FirstRun = false; + this.Options = continuingOptions; + + // If settings were changed, reinitialize the UI if (savedSettings) - { - this.Options = new Data.Options(newOptions); InitializeUIValues(removeEventHandlers: true, rescanDrives: true); - } } #endregion diff --git a/MPF.Core/UI/ViewModels/OptionsViewModel.cs b/MPF.Core/UI/ViewModels/OptionsViewModel.cs index a1314922..650475b5 100644 --- a/MPF.Core/UI/ViewModels/OptionsViewModel.cs +++ b/MPF.Core/UI/ViewModels/OptionsViewModel.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using MPF.Core.Data; @@ -7,10 +8,32 @@ using SabreTools.RedumpLib.Web; namespace MPF.Core.UI.ViewModels { - public class OptionsViewModel + public class OptionsViewModel : INotifyPropertyChanged { #region Fields + /// + /// Title for the window + /// +#if NET48 + public string Title +#else + public string? Title +#endif + { + get => _title; + set + { + _title = value; + TriggerPropertyChanged(nameof(Title)); + } + } +#if NET48 + private string _title; +#else + private string? _title; +#endif + /// /// Current set of options /// @@ -21,7 +44,14 @@ namespace MPF.Core.UI.ViewModels /// public bool SavedSettings { get; set; } - #endregion + /// +#if NET48 + public event PropertyChangedEventHandler PropertyChanged; +#else + public event PropertyChangedEventHandler? PropertyChanged; +#endif + +#endregion #region Lists @@ -77,5 +107,18 @@ namespace MPF.Core.UI.ViewModels } #endregion + + #region Property Updates + + /// + /// Trigger a property changed event + /// + private void TriggerPropertyChanged(string propertyName) + { + // If the property change event is initialized + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + + #endregion } } diff --git a/MPF.UI.Core/Windows/MainWindow.xaml.cs b/MPF.UI.Core/Windows/MainWindow.xaml.cs index 02018995..31e1cd87 100644 --- a/MPF.UI.Core/Windows/MainWindow.xaml.cs +++ b/MPF.UI.Core/Windows/MainWindow.xaml.cs @@ -58,12 +58,7 @@ namespace MPF.UI.Core.Windows if (MainViewModel.Options.FirstRun) { // Show the options window - ShowOptionsWindow(); - - // Ensure the first run flag is unset - var continuingOptions = new MPF.Core.Data.Options(MainViewModel.Options); - continuingOptions.FirstRun = false; - MainViewModel.Options = continuingOptions; + ShowOptionsWindow("Welcome to MPF, Explore the Options"); } } @@ -255,7 +250,11 @@ namespace MPF.UI.Core.Windows /// /// Show the Options window /// - public void ShowOptionsWindow() +#if NET48 + public void ShowOptionsWindow(string title = null) +#else + public void ShowOptionsWindow(string? title = null) +#endif { var optionsWindow = new OptionsWindow(MainViewModel.Options) { @@ -263,8 +262,10 @@ namespace MPF.UI.Core.Windows Owner = this, ShowActivated = true, ShowInTaskbar = true, + Title = title ?? "Options", WindowStartupLocation = WindowStartupLocation.CenterOwner, }; + optionsWindow.Closed += OnOptionsUpdated; optionsWindow.Show(); } @@ -287,7 +288,7 @@ namespace MPF.UI.Core.Windows theme.Apply(); } - #endregion +#endregion #region Event Handlers @@ -300,9 +301,17 @@ namespace MPF.UI.Core.Windows public void OnOptionsUpdated(object? sender, EventArgs e) #endif { - bool savedSettings = (sender as OptionsWindow)?.OptionsViewModel?.SavedSettings ?? false; - var options = (sender as OptionsWindow)?.OptionsViewModel?.Options; + // Get the options window + var optionsWindow = (sender as OptionsWindow); + if (optionsWindow?.OptionsViewModel == null) + return; + + bool savedSettings = optionsWindow.OptionsViewModel.SavedSettings; + var options = optionsWindow.OptionsViewModel.Options; MainViewModel.UpdateOptions(savedSettings, options); + + // Force the UI to rerender + OnContentRendered(new EventArgs()); } #region Menu Bar diff --git a/MPF.UI.Core/Windows/OptionsWindow.xaml b/MPF.UI.Core/Windows/OptionsWindow.xaml index d585dd48..a8fd68cf 100644 --- a/MPF.UI.Core/Windows/OptionsWindow.xaml +++ b/MPF.UI.Core/Windows/OptionsWindow.xaml @@ -6,7 +6,7 @@ xmlns:core="clr-namespace:MPF.UI.Core" xmlns:coreWindows="clr-namespace:MPF.UI.Core.Windows" mc:Ignorable="d" - Title="Options" Width="515.132" WindowStyle="None" + Width="515.132" WindowStyle="None" WindowStartupLocation="CenterOwner" ResizeMode="CanMinimize" SizeToContent="Height" BorderBrush="DarkGray" BorderThickness="2"> @@ -32,7 +32,12 @@