GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
feat(boot,infra): show error dialog on any fatal errors, not just ones thrown via `Bus`
Lars Berger476f0255eb9d
diff --git a/GlazeWM.Bootstrapper/Program.cs b/GlazeWM.Bootstrapper/Program.csindex 7edce4e8..8eb459b7 100644--- a/GlazeWM.Bootstrapper/Program.cs+++ b/GlazeWM.Bootstrapper/Program.cs@@ -6,7 +6,6 @@ using System;using System.Diagnostics;using System.Threading;using System.Windows.Forms;-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;namespace GlazeWM.Bootstrapper{@@ -33,21 +32,23 @@ namespace GlazeWM.Bootstrapperreturn;}- // Set the process-default DPI awareness.- SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);-- var serviceCollection = new ServiceCollection();- serviceCollection.AddInfrastructureServices();- serviceCollection.AddDomainServices();- serviceCollection.AddBarServices();- serviceCollection.AddSingleton<Startup>();-- ServiceLocator.Provider = serviceCollection.BuildServiceProvider();+ ServiceLocator.Provider = BuildServiceProvider();var startup = ServiceLocator.Provider.GetRequiredService<Startup>();- startup.Init();+ startup.Run();Application.Run();}}++ private static ServiceProvider BuildServiceProvider()+ {+ var serviceCollection = new ServiceCollection();+ serviceCollection.AddInfrastructureServices();+ serviceCollection.AddDomainServices();+ serviceCollection.AddBarServices();+ serviceCollection.AddSingleton<Startup>();++ return serviceCollection.BuildServiceProvider();+ }}}diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.csindex 8b7e83a5..c0e5461a 100644--- a/GlazeWM.Bootstrapper/Startup.cs+++ b/GlazeWM.Bootstrapper/Startup.cs@@ -13,6 +13,7 @@ using GlazeWM.Infrastructure.Bussing;using GlazeWM.Infrastructure.WindowsApi;using GlazeWM.Infrastructure.WindowsApi.Events;using System;+using System.IO;using System.Linq;using System.Reactive.Linq;using System.Windows.Forms;@@ -55,29 +56,46 @@ namespace GlazeWM.Bootstrapper_systemEventService = systemEventService;}- public void Init()+ public void Run(){- // Launch bar WPF application. Spawns bar window when monitors are added, so the service needs- // to be initialized before populating initial state.- _barService.StartApp();+ try+ {+ // Set the process-default DPI awareness.+ SetProcessDpiAwarenessContext(DpiAwarenessContext.Context_PerMonitorAwareV2);++ // Launch bar WPF application. Spawns bar window when monitors are added, so the service needs+ // to be initialized before populating initial state.+ _barService.StartApp();- // Populate initial monitors, windows, workspaces and user config.- PopulateInitialState();+ // Populate initial monitors, windows, workspaces and user config.+ PopulateInitialState();- // Listen on registered keybindings.- _keybindingService.Start();+ // Listen on registered keybindings.+ _keybindingService.Start();- // Listen for window events (eg. close, focus).- _windowEventService.Start();+ // Listen for window events (eg. close, focus).+ _windowEventService.Start();- // Listen for system-related events (eg. changes to display settings).- _systemEventService.Start();+ // Listen for system-related events (eg. changes to display settings).+ _systemEventService.Start();- // Add application to system tray.- _systemTrayService.AddToSystemTray();+ // Add application to system tray.+ _systemTrayService.AddToSystemTray();- _bus.Events.Where(@event => @event is ApplicationExitingEvent)- .Subscribe((@event) => OnApplicationExit());+ _bus.Events.Where(@event => @event is ApplicationExitingEvent)+ .Subscribe((@event) => OnApplicationExit());+ }+ catch (Exception error)+ {+ // Alert the user of the error.+ // TODO: This throws duplicate errors if a command errors and it was invoked by another+ // handler.+ if (error is FatalUserException)+ MessageBox.Show(error.Message);++ File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");+ throw error;+ }}/// <summary>diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.csindex 6dc21ebf..505a6613 100644--- a/GlazeWM.Infrastructure/Bussing/Bus.cs+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs@@ -2,9 +2,7 @@using System;using System.Collections.Generic;using System.Diagnostics;-using System.IO;using System.Reactive.Subjects;-using System.Windows;namespace GlazeWM.Infrastructure.Bussing{@@ -21,28 +19,17 @@ namespace GlazeWM.Infrastructure.Bussing/// </summary>public CommandResponse Invoke<T>(T command) where T : Command{+ Debug.WriteLine($"Command {command.Name} invoked.");+// Create a `Type` object representing the constructed `ICommandHandler` generic.var handlerType = typeof(ICommandHandler<>).MakeGenericType(command.GetType());- Debug.WriteLine($"Command {command.Name} invoked.");+ var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType)+ as ICommandHandler<T>;- try+ lock (_lockObj){- var handlerInstance = ServiceLocator.Provider.GetRequiredService(handlerType) as ICommandHandler<T>;- lock (_lockObj)- {- return handlerInstance.Handle(command);- }- }- catch (Exception error)- {- // Alert the user of the error.- // TODO: This throws duplicate errors if a command errors and it was invoked by another handler.- if (error is FatalUserException)- MessageBox.Show(error.Message);-- File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");- throw error;+ return handlerInstance.Handle(command);}}@@ -51,35 +38,24 @@ namespace GlazeWM.Infrastructure.Bussing/// </summary>public void RaiseEvent<T>(T @event) where T : Event{+ Debug.WriteLine($"Event {@event.Name} emitted.");+// Create a `Type` object representing the constructed `IEventHandler` generic.var handlerType = typeof(IEventHandler<>).MakeGenericType(@event.GetType());- Debug.WriteLine($"Event {@event.Name} emitted.");+ var handlerInstances = ServiceLocator.Provider.GetServices(handlerType)+ as IEnumerable<IEventHandler<T>>;- try+ foreach (var handler in handlerInstances){- var handlerInstances = ServiceLocator.Provider.GetServices(handlerType) as IEnumerable<IEventHandler<T>>;-- foreach (var handler in handlerInstances)+ lock (_lockObj){- lock (_lockObj)- {- handler.Handle(@event);- }+ handler.Handle(@event);}-- // Emit event through subject.- Events.OnNext(@event);}- catch (Exception error)- {- // Alert the user of the error.- if (error is FatalUserException)- MessageBox.Show(error.Message);- File.AppendAllText("./errors.log", $"\n\n{error.Message + error.StackTrace}");- throw error;- }+ // Emit event through subject.+ Events.OnNext(@event);}}}