refactor: Consolidate ShellBootstrapper and base class into one

This commit is contained in:
John Colvin committed 2026-01-04 21:19:00 +00:00
1 parent db0e0d4311
commit cf4158b34c
5 files changed
+42 -63

No files matched your search

+2 -1
View File
@@ -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) {
+37 -6
View File
@@ -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.
/// </summary>
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<bts.config.ShellRenderMode>().SoftwareOnly) {
System.Windows.Media.RenderOptions.ProcessRenderMode = System.Windows.Interop.RenderMode.SoftwareOnly;
}
var launcher = model.create<ILauncher>();
var launcher = _model.create<ILauncher>();
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();
}
}
}
@@ -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();
}
}
}
@@ -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();
@@ -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)