GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
fix: multi-threading issue with many keybinding commands at once (#381)
lars-berger023384b2c2ba
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex f4e2a77a..293d0c60 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -60,12 +60,12 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<EvaluateUserConfigCommand>, EvaluateUserConfigHandler>();services.AddSingleton<ICommandHandler<RegisterKeybindingsCommand>, RegisterKeybindingsHandler>();services.AddSingleton<ICommandHandler<ReloadUserConfigCommand>, ReloadUserConfigHandler>();+ services.AddSingleton<ICommandHandler<RunWithSubjectContainerCommand>, RunWithSubjectContainerHandler>();services.AddSingleton<ICommandHandler<CloseWindowCommand>, CloseWindowHandler>();services.AddSingleton<ICommandHandler<IgnoreWindowCommand>, IgnoreWindowHandler>();services.AddSingleton<ICommandHandler<ManageWindowCommand>, ManageWindowHandler>();services.AddSingleton<ICommandHandler<MoveWindowCommand>, MoveWindowHandler>();services.AddSingleton<ICommandHandler<ResizeWindowCommand>, ResizeWindowHandler>();- services.AddSingleton<ICommandHandler<RunWindowRulesCommand>, RunWindowRulesHandler>();services.AddSingleton<ICommandHandler<ResizeWindowBordersCommand>, ResizeWindowBordersHandler>();services.AddSingleton<ICommandHandler<SetFloatingCommand>, SetFloatingHandler>();services.AddSingleton<ICommandHandler<SetMaximizedCommand>, SetMaximizedHandler>();diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.csindex 737ccc49..0090d8f0 100644--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs@@ -14,21 +14,15 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersinternal sealed class RegisterKeybindingsHandler : ICommandHandler<RegisterKeybindingsCommand>{private readonly Bus _bus;- private readonly CommandParsingService _commandParsingService;- private readonly ContainerService _containerService;private readonly KeybindingService _keybindingService;private readonly WindowService _windowService;public RegisterKeybindingsHandler(Bus bus,- CommandParsingService commandParsingService,- ContainerService containerService,KeybindingService keybindingService,WindowService windowService){_bus = bus;- _commandParsingService = commandParsingService;- _containerService = containerService;_keybindingService = keybindingService;_windowService = windowService;}@@ -40,8 +34,8 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersforeach (var keybindingConfig in command.Keybindings){// Format command strings defined in keybinding config.- var formattedCommandStrings = keybindingConfig.CommandList.Select(- commandString => CommandParsingService.FormatCommand(commandString)+ var commandStrings = keybindingConfig.CommandList.Select(+ CommandParsingService.FormatCommand);// Register all keybindings for a command sequence.@@ -56,29 +50,7 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersif (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))return;- var subjectContainer = _containerService.FocusedContainer;- var subjectContainerId = subjectContainer.Id;-- // Invoke commands in sequence on keybinding press.- foreach (var commandString in formattedCommandStrings)- {- // Avoid calling command if container gets detached. This is to prevent crashes- // for edge cases like ["close", "move to workspace X"].- if (subjectContainer.IsDetached())- return;-- var parsedCommand = _commandParsingService.ParseCommand(- commandString,- subjectContainer- );-- // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.- _bus.Invoke((dynamic)parsedCommand);-- // Update subject container in case the reference changes (eg. when going from a tiling to a- // floating window).- subjectContainer = _containerService.GetContainerById(subjectContainerId);- }+ _bus.Invoke(new RunWithSubjectContainerCommand(commandStrings));}catch (Exception e){diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.csindex 21330a76..ba285873 100644--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs@@ -1,3 +1,4 @@+using System.Linq;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.UserConfigs.Commands;@@ -38,7 +39,12 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersforeach (var window in _windowService.GetWindows()){var windowRules = _userConfigService.GetMatchingWindowRules(window);- _bus.Invoke(new RunWindowRulesCommand(window, windowRules));+ var windowRuleCommands = windowRules+ .SelectMany(rule => rule.CommandList)+ .Select(CommandParsingService.FormatCommand);++ // Run matching window rules.+ _bus.Invoke(new RunWithSubjectContainerCommand(windowRuleCommands, window));}// Redraw full container tree.diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RunWithSubjectContainerHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RunWithSubjectContainerHandler.csnew file mode 100644index 00000000..c1ce148b--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RunWithSubjectContainerHandler.cs@@ -0,0 +1,62 @@+using System.Linq;+using GlazeWM.Domain.Containers;+using GlazeWM.Domain.UserConfigs.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.UserConfigs.CommandHandlers+{+ internal sealed class RunWithSubjectContainerHandler+ : ICommandHandler<RunWithSubjectContainerCommand>+ {+ private readonly Bus _bus;+ private readonly CommandParsingService _commandParsingService;+ private readonly ContainerService _containerService;++ public RunWithSubjectContainerHandler(+ Bus bus,+ CommandParsingService commandParsingService,+ ContainerService containerService)+ {+ _bus = bus;+ _commandParsingService = commandParsingService;+ _containerService = containerService;+ }++ public CommandResponse Handle(RunWithSubjectContainerCommand command)+ {+ var commandStrings = command.CommandStrings;+ var subjectContainer =+ command.SubjectContainer ?? _containerService.FocusedContainer;++ var subjectContainerId = subjectContainer.Id;++ // Return early if any of the commands is an ignore command.+ if (commandStrings.Any(command => command == "ignore"))+ return CommandResponse.Ok;++ // Invoke commands in sequence.+ foreach (var commandString in commandStrings)+ {+ // Avoid calling command if container gets detached. This is to prevent crashes+ // for edge cases like ["close", "move to workspace X"].+ if (subjectContainer?.IsDetached() != false)+ return CommandResponse.Ok;++ var parsedCommand = _commandParsingService.ParseCommand(+ commandString,+ subjectContainer+ );++ // Use `dynamic` to resolve the command type at runtime and allow multiple+ // dispatch.+ _bus.Invoke((dynamic)parsedCommand);++ // Update subject container in case the reference changes (eg. when going from a+ // tiling to a floating window).+ subjectContainer = _containerService.GetContainerById(subjectContainerId);+ }++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/UserConfigs/Commands/RunWithSubjectContainerCommand.cs b/GlazeWM.Domain/UserConfigs/Commands/RunWithSubjectContainerCommand.csnew file mode 100644index 00000000..b1592302--- /dev/null+++ b/GlazeWM.Domain/UserConfigs/Commands/RunWithSubjectContainerCommand.cs@@ -0,0 +1,25 @@+using System.Collections.Generic;+using GlazeWM.Domain.Containers;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.UserConfigs.Commands+{+ public class RunWithSubjectContainerCommand : Command+ {+ public IEnumerable<string> CommandStrings { get; }+ public Container SubjectContainer { get; }++ public RunWithSubjectContainerCommand(+ IEnumerable<string> commandStrings,+ Container subjectContainer)+ {+ CommandStrings = commandStrings;+ SubjectContainer = subjectContainer;+ }++ public RunWithSubjectContainerCommand(IEnumerable<string> commandStrings)+ {+ CommandStrings = commandStrings;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.csindex 24c085cf..11e8721b 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/ManageWindowHandler.cs@@ -1,9 +1,11 @@using System;+using System.Linq;using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.Monitors;using GlazeWM.Domain.UserConfigs;+using GlazeWM.Domain.UserConfigs.Commands;using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Windows.Events;using GlazeWM.Domain.Workspaces;@@ -64,13 +66,15 @@ namespace GlazeWM.Domain.Windows.CommandHandlersif (MonitorService.HasDpiDifference(monitor, window.Parent))window.HasPendingDpiAdjustment = true;+ var windowRules = _userConfigService.GetMatchingWindowRules(window);+ var windowRuleCommands = windowRules+ .SelectMany(rule => rule.CommandList)+ .Select(CommandParsingService.FormatCommand);+// Set the newly added window as focus descendant. This means the window rules will be run as// if the window is focused._bus.Invoke(new SetFocusedDescendantCommand(window));-- // Run matching window rules.- var windowRules = _userConfigService.GetMatchingWindowRules(window);- _bus.Invoke(new RunWindowRulesCommand(window, windowRules));+ _bus.Invoke(new RunWithSubjectContainerCommand(windowRuleCommands, window));// Update window in case the reference changes.window = _windowService.GetWindowByHandle(window.Handle);@@ -82,12 +86,12 @@ namespace GlazeWM.Domain.Windows.CommandHandlersif (shouldRedraw)_bus.Invoke(new RedrawContainersCommand());- // Set OS focus to the newly added window in case it's not already focused.- _bus.Invoke(new SetNativeFocusCommand(window));-_logger.LogWindowEvent("New window managed", window);_bus.Emit(new WindowManagedEvent(window));+ // Set OS focus to the newly added window in case it's not already focused.+ _bus.Invoke(new SetNativeFocusCommand(window));+return CommandResponse.Ok;}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.csdeleted file mode 100644index db91765a..00000000--- a/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs+++ /dev/null@@ -1,57 +0,0 @@-using System.Linq;-using GlazeWM.Domain.UserConfigs;-using GlazeWM.Domain.Windows.Commands;-using GlazeWM.Infrastructure.Bussing;--namespace GlazeWM.Domain.Windows.CommandHandlers-{- internal sealed class RunWindowRulesHandler : ICommandHandler<RunWindowRulesCommand>- {- private readonly Bus _bus;- private readonly CommandParsingService _commandParsingService;- private readonly WindowService _windowService;-- public RunWindowRulesHandler(- Bus bus,- CommandParsingService commandParsingService,- WindowService windowService)- {- _bus = bus;- _commandParsingService = commandParsingService;- _windowService = windowService;- }-- public CommandResponse Handle(RunWindowRulesCommand command)- {- var window = command.Window;- var windowHandle = window.Handle;- var windowRules = command.WindowRules;-- var commandStrings = windowRules- .SelectMany(rule => rule.CommandList)- .Select(commandString => CommandParsingService.FormatCommand(commandString));-- // Window to use as subject container when invoking commands.- var subjectWindow = window;-- foreach (var commandString in commandStrings)- {- // Subject window might be null if "ignore" rule has run.- if (subjectWindow?.IsDetached() != false)- return CommandResponse.Ok;-- var parsedCommand = _commandParsingService.ParseCommand(commandString, subjectWindow);-- // Invoke commands in the matching window rules. Use `dynamic` to resolve the command type- // at runtime and allow multiple dispatch.- _bus.Invoke((dynamic)parsedCommand);-- // Update subject window in case the reference changes (eg. when going from a tiling to a- // floating window).- subjectWindow = _windowService.GetWindowByHandle(windowHandle);- }-- return CommandResponse.Ok;- }- }-}diff --git a/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.cs b/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.csdeleted file mode 100644index 24669240..00000000--- a/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.cs+++ /dev/null@@ -1,18 +0,0 @@-using System.Collections.Generic;-using GlazeWM.Domain.UserConfigs;-using GlazeWM.Infrastructure.Bussing;--namespace GlazeWM.Domain.Windows.Commands-{- public class RunWindowRulesCommand : Command- {- public Window Window { get; }- public List<WindowRuleConfig> WindowRules { get; }-- public RunWindowRulesCommand(Window window, List<WindowRuleConfig> windowRules)- {- Window = window;- WindowRules = windowRules;- }- }-}