From 110a9ae8187013356506de9972878bc7459a72af Mon Sep 17 00:00:00 2001 From: Andy McFadden Date: Sat, 2 Mar 2024 14:48:06 -0800 Subject: [PATCH] Change the way app settings Apply button works We were passing a reference to MainController in, and calling a method on it, which is a little convoluted. Now the main controller subscribes to a "settings applied" event, and handles the update with an event handler. No change in behavior. --- SourceGen/MainController.cs | 34 ++++++++++-------------- SourceGen/WpfGui/EditAppSettings.xaml.cs | 31 ++++++++++++++------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/SourceGen/MainController.cs b/SourceGen/MainController.cs index 0ae9c2d..c249d29 100644 --- a/SourceGen/MainController.cs +++ b/SourceGen/MainController.cs @@ -453,20 +453,6 @@ namespace SourceGen { } } - /// - /// Replaces the contents of the global settings object with the new settings, - /// then applies them to the project. - /// - /// New settings. - public void SetAppSettings(AppSettings settings) { - AppSettings.Global.ReplaceSettings(settings); - ApplyAppSettings(); - - // We get called whenever Apply or OK is hit in the settings editor, so it's - // a pretty good time to save the settings out. - SaveAppSettings(); - } - /// /// Sets the app window's location and size. This should be called before the window has /// finished initialization. @@ -1519,22 +1505,30 @@ namespace SourceGen { } /// - /// Opens the application settings dialog. All changes to settings are made directly - /// to the AppSettings.Global object. + /// Handles Edit > App Settings. /// public void EditAppSettings() { ShowAppSettings(mMainWin, WpfGui.EditAppSettings.Tab.Unknown, AsmGen.AssemblerInfo.Id.Unknown); } + /// + /// Opens the application settings dialog. All changes to settings are made directly + /// to the AppSettings.Global object. + /// public void ShowAppSettings(Window owner, EditAppSettings.Tab initialTab, AsmGen.AssemblerInfo.Id initialAsmId) { - // TODO: this can be done in a less-awkward way by subscribing to an event - EditAppSettings dlg = new EditAppSettings(owner, mMainWin, this, - initialTab, initialAsmId); + EditAppSettings dlg = new EditAppSettings(owner, mMainWin, initialTab, initialAsmId); + dlg.SettingsApplied += SetAppSettings; // called when "Apply" is clicked dlg.ShowDialog(); + } - // The settings code calls SetAppSettings() directly whenever "Apply" is hit. + /// + /// Applies settings to the project, and saves them to the settings files. + /// + private void SetAppSettings() { + ApplyAppSettings(); + SaveAppSettings(); } public void HandleCodeListDoubleClick(int row, int col) { diff --git a/SourceGen/WpfGui/EditAppSettings.xaml.cs b/SourceGen/WpfGui/EditAppSettings.xaml.cs index 1b1da77..c7fe5f7 100644 --- a/SourceGen/WpfGui/EditAppSettings.xaml.cs +++ b/SourceGen/WpfGui/EditAppSettings.xaml.cs @@ -37,18 +37,19 @@ namespace SourceGen.WpfGui { /// Application settings dialog. /// public partial class EditAppSettings : Window, INotifyPropertyChanged { + /// + /// Event that the controller can subscribe to if it wants to be notified when the + /// "Apply" or "OK" button is hit. + /// + public event SettingsAppliedHandler SettingsApplied; + public delegate void SettingsAppliedHandler(); + /// /// Reference to main window. Needed for examination of the code list font and /// column widths. /// private MainWindow mMainWin; - /// - /// Reference to main controller. Needed to push settings out when Apply/OK is clicked. - /// TODO: use an Event instead? - /// - private MainController mMainCtrl; - /// /// Copy of settings that we make changes to. On "Apply" or "OK", this is pushed /// into the global settings object, and applied to the ProjectView. @@ -103,14 +104,13 @@ namespace SourceGen.WpfGui { } - public EditAppSettings(Window owner, MainWindow mainWin, MainController mainCtrl, - Tab initialTab, AssemblerInfo.Id initialAsmId) { + public EditAppSettings(Window owner, MainWindow mainWin, Tab initialTab, + AssemblerInfo.Id initialAsmId) { InitializeComponent(); Owner = owner; DataContext = this; mMainWin = mainWin; - mMainCtrl = mainCtrl; mInitialTab = initialTab; mInitialAsmId = initialAsmId; @@ -204,7 +204,8 @@ namespace SourceGen.WpfGui { // QueryVersions() can sometimes be slow under Win10 (mid 2019), possibly // because of the built-in malware detection, so pop up a wait cursor. Mouse.OverrideCursor = Cursors.Wait; - mMainCtrl.SetAppSettings(mSettings); + AppSettings.Global.ReplaceSettings(mSettings); + OnSettingsApplied(); AsmGen.AssemblerVersionCache.QueryVersions(); } finally { Mouse.OverrideCursor = null; @@ -213,6 +214,16 @@ namespace SourceGen.WpfGui { IsDirty = false; } + /// + /// Raises the "settings applied" event. + /// + private void OnSettingsApplied() { + SettingsAppliedHandler handler = SettingsApplied; + if (handler != null) { + handler(); + } + } + #region Code View