From cf4158b34cf2a17edef8087ed4e4a4aba0ee814e Mon Sep 17 00:00:00 2001 From: John Colvin Date: Sat, 2 Aug 2025 01:09:08 -0400 Subject: [PATCH] refactor: Consolidate ShellBootstrapper and base class into one --- src/client/spark/Notifications/INotifier.cs | 3 +- src/client/spark/ShellBootstrapper.cs | 43 ++++++++++++--- .../Utility/BTSApplicationBootstrapper.cs | 54 ------------------- .../Shell/ViewModels/ShellThreadFactory.cs | 2 +- .../view/Utility/ThreadedWindowLauncher.cs | 3 +- 5 files changed, 42 insertions(+), 63 deletions(-) delete mode 100644 src/client/spark/core/Utility/BTSApplicationBootstrapper.cs diff --git a/src/client/spark/Notifications/INotifier.cs b/src/client/spark/Notifications/INotifier.cs index 1dccc8694..f2caf4bcc 100644 --- a/src/client/spark/Notifications/INotifier.cs +++ b/src/client/spark/Notifications/INotifier.cs @@ -8,6 +8,7 @@ using mbts.config; using mbts.persist; using mbts.spark; using mbts.ui.extensions; +using mbts.ui.shell; using mbts.ui.utility; using Spark.Core.Disposables; @@ -155,7 +156,7 @@ namespace mbts.ui { } static void _on_dispatcher_UnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e) { - BTSApplicationBootstrapper.CurrentDispatcher_HandleClipboardError(sender, e); + ShellBootstrapper.CurrentDispatcher_HandleClipboardError(sender, e); if (!e.Handled) { Log.Ui.error($"Unhandled Exception in ToastNotification dispatcher: {e.Exception}"); if (!e.Dispatcher.HasShutdownStarted) { diff --git a/src/client/spark/ShellBootstrapper.cs b/src/client/spark/ShellBootstrapper.cs index 95908b210..bc6bed434 100644 --- a/src/client/spark/ShellBootstrapper.cs +++ b/src/client/spark/ShellBootstrapper.cs @@ -1,12 +1,14 @@ namespace mbts.ui.shell { + using bts; + using mbts.config; using mbts.ui.extensions; using Spark.Core.Disposables; using System; - using System.Collections.Generic; using System.Collections.Specialized; + using System.Runtime.InteropServices; using System.Threading; using System.Windows; using System.Windows.Threading; @@ -16,15 +18,38 @@ /// for creating the main objects which make the up interface (the launcher /// and the collection of open windows) and displaying them. /// - class ShellBootstrapper : BTSApplicationBootstrapper { + class ShellBootstrapper : IDisposable { + readonly string _config_source; + ShellModel _model; Window _login; + static ShellBootstrapper() { + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; + Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_HandleClipboardError; + } + + public static void CurrentDispatcher_HandleClipboardError(object sender, DispatcherUnhandledExceptionEventArgs e) { + // Dont' allow clipboard failure to bring down the shell + if (e.Exception is COMException com_exeption && com_exeption.ErrorCode == -2147221040) { + e.Handled = true; + } + } + + public static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { + var exception = e.ExceptionObject as Exception; + Log.Ui.error($"Unhandled Exception: {e.ExceptionObject} {exception?.StackTrace}"); + if (e.IsTerminating) { + Log.system_is_crashing("Unhandled exception", e.ExceptionObject); + } + } + public ShellBootstrapper( string user = null, string password = null, string config_source = null, bool? restore_workspace = null - ) : base(config_source) { + ) { + _config_source = config_source; // Get the LoginModel instance on this thread to make sure it performs syncronous config initialization. var login_model = LoginModel.init(config_source); // Show the dialog on a background thread so animations aren't blocked during the main application startup @@ -95,12 +120,14 @@ } } - public override void CreateShell(ShellModel model) { + public void Run() { + _model = new ShellModel(); + _model.Initialize(_config_source); if (Configuration.get().SoftwareOnly) { System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly; } - var launcher = model.create(); + var launcher = _model.create(); if (_login != null) { launcher.shown += () => _login.Dispatcher.InvokeAsync(destroy_login); @@ -111,7 +138,11 @@ } launcher.show(); - model.notify_delayed_load(); + _model.notify_delayed_load(); + } + + public void Dispose() { + _model.Dispose(); } } } diff --git a/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs b/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs deleted file mode 100644 index a4e5c30e5..000000000 --- a/src/client/spark/core/Utility/BTSApplicationBootstrapper.cs +++ /dev/null @@ -1,54 +0,0 @@ -using bts; - -using mbts.ui.native; - -using System; -using System.Collections.Generic; -using System.Windows; -using System.Windows.Threading; - -namespace mbts.ui { - public abstract class BTSApplicationBootstrapper : IDisposable { - readonly string _config_source; - - private ShellModel _model; - - static BTSApplicationBootstrapper() { - AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; - Dispatcher.CurrentDispatcher.UnhandledException += CurrentDispatcher_HandleClipboardError; - } - - protected BTSApplicationBootstrapper(string config_source = null) { - _config_source = config_source; - } - - public static void CurrentDispatcher_HandleClipboardError(object sender, DispatcherUnhandledExceptionEventArgs e) { - // Dont' allow clipboard failure to bring down the shell - var com_exeption = e.Exception as System.Runtime.InteropServices.COMException; - if (com_exeption != null && com_exeption.ErrorCode == -2147221040) { - e.Handled = true; - } - } - - static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { - var exception = e.ExceptionObject as Exception; - Log.Ui.error($"Unhandled Exception: {e.ExceptionObject} {exception?.StackTrace}"); - if (e.IsTerminating) { - Log.system_is_crashing("Unhandled exception", e.ExceptionObject); - } - } - - // Should create and show the shell - public abstract void CreateShell(ShellModel model); - - public void Run() { - _model = new ShellModel(); - _model.Initialize(_config_source); - CreateShell(_model); - } - - public void Dispose() { - _model.Dispose(); - } - } -} diff --git a/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs b/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs index e5d0b4197..7ed9d4bc2 100644 --- a/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs +++ b/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs @@ -40,7 +40,7 @@ namespace mbts.ui.shell { uid = new UIThreadData(); uid.thread = new Thread(() => { uid.dispatcher = Dispatcher.CurrentDispatcher; - uid.dispatcher.UnhandledException += BTSApplicationBootstrapper.CurrentDispatcher_HandleClipboardError; + uid.dispatcher.UnhandledException += ShellBootstrapper.CurrentDispatcher_HandleClipboardError; ComponentDispatcher.ThreadFilterMessage += (ref MSG m, ref bool h) => ComponentDispatcher_ThreadFilterMessage(uid, ref m, ref h); try { Dispatcher.Run(); diff --git a/src/client/spark/view/Utility/ThreadedWindowLauncher.cs b/src/client/spark/view/Utility/ThreadedWindowLauncher.cs index d7ec8c22c..86d3ea848 100644 --- a/src/client/spark/view/Utility/ThreadedWindowLauncher.cs +++ b/src/client/spark/view/Utility/ThreadedWindowLauncher.cs @@ -1,5 +1,6 @@ using bts; +using mbts.ui.shell; using mbts.utils; using System; @@ -94,7 +95,7 @@ namespace mbts.ui.utilities { dispose_core(); } void on_dispatcher_unhandled_exception(object sender, DispatcherUnhandledExceptionEventArgs e) { - BTSApplicationBootstrapper.CurrentDispatcher_HandleClipboardError(sender, e); + ShellBootstrapper.CurrentDispatcher_HandleClipboardError(sender, e); if (!e.Handled) { _log.error($"unhandled exception in dispatcher: {e.Exception}"); if (!e.Dispatcher.HasShutdownStarted)