GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.
refactor: separate command for running window rules (#133)
lars-berger3f9e648f2534
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.csindex 395c7175..430004b1 100644--- a/GlazeWM.Domain/DependencyInjection.cs+++ b/GlazeWM.Domain/DependencyInjection.cs@@ -62,6 +62,7 @@ namespace GlazeWM.Domainservices.AddSingleton<ICommandHandler<MoveWindowCommand>, MoveWindowHandler>();services.AddSingleton<ICommandHandler<RemoveWindowCommand>, RemoveWindowHandler>();services.AddSingleton<ICommandHandler<ResizeWindowCommand>, ResizeWindowHandler>();+ services.AddSingleton<ICommandHandler<RunWindowRulesCommand>, RunWindowRulesHandler>();services.AddSingleton<ICommandHandler<ResizeWindowBordersCommand>, ResizeWindowBordersHandler>();services.AddSingleton<ICommandHandler<SetFloatingCommand>, SetFloatingHandler>();services.AddSingleton<ICommandHandler<SetMinimizedCommand>, SetMinimizedHandler>();diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.csindex 629de73d..261d00b0 100644--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/ReloadUserConfigHandler.cs@@ -1,9 +1,9 @@-using System.Linq;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;using GlazeWM.Domain.UserConfigs.Commands;using GlazeWM.Domain.UserConfigs.Events;using GlazeWM.Domain.Windows;+using GlazeWM.Domain.Windows.Commands;using GlazeWM.Domain.Workspaces.Commands;using GlazeWM.Infrastructure.Bussing;@@ -12,20 +12,17 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersinternal class ReloadUserConfigHandler : ICommandHandler<ReloadUserConfigCommand>{private readonly Bus _bus;- private readonly CommandParsingService _commandParsingService;private readonly ContainerService _containerService;private readonly UserConfigService _userConfigService;private readonly WindowService _windowService;public ReloadUserConfigHandler(Bus bus,- CommandParsingService commandParsingService,ContainerService containerService,UserConfigService userConfigService,WindowService windowService){_bus = bus;- _commandParsingService = commandParsingService;_containerService = containerService;_userConfigService = userConfigService;_windowService = windowService;@@ -40,21 +37,8 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlersforeach (var window in _windowService.GetWindows()){- // TODO: Create `RunWindowRulesCommand(Window window, List<WindowRule> windowRules).- var matchingWindowRules = _userConfigService.GetMatchingWindowRules(window);-- var commandStrings = matchingWindowRules- .SelectMany(rule => rule.CommandList)- .Select(commandString => CommandParsingService.FormatCommand(commandString));-- var parsedCommands = commandStrings- .Select(commandString => _commandParsingService.ParseCommand(commandString, window))- .ToList();-- // Invoke commands in the matching window rules. Use `dynamic` to resolve the command type- // at runtime and allow multiple dispatch.- foreach (var parsedCommand in parsedCommands)- _bus.Invoke((dynamic)parsedCommand);+ var windowRules = _userConfigService.GetMatchingWindowRules(window);+ _bus.Invoke(new RunWindowRulesCommand(window, windowRules));}// Redraw full container tree.diff --git a/GlazeWM.Domain/UserConfigs/UserConfigService.cs b/GlazeWM.Domain/UserConfigs/UserConfigService.csindex 311f81bb..0c06e391 100644--- a/GlazeWM.Domain/UserConfigs/UserConfigService.cs+++ b/GlazeWM.Domain/UserConfigs/UserConfigService.cs@@ -81,14 +81,14 @@ namespace GlazeWM.Domain.UserConfigs);}- public IEnumerable<WindowRuleConfig> GetMatchingWindowRules(Window window)+ public List<WindowRuleConfig> GetMatchingWindowRules(Window window){return UserConfig.WindowRules.Where(rule =>{- return rule.ProcessNameRegex?.IsMatch(window.ProcessName) != false- && rule.ClassNameRegex?.IsMatch(window.ClassName) != false- && (rule.TitleRegex?.IsMatch(window.Title)) != false;- });+ return rule.ProcessNameRegex?.IsMatch(window.ProcessName) != false &&+ rule.ClassNameRegex?.IsMatch(window.ClassName) != false &&+ rule.TitleRegex?.IsMatch(window.Title) != false;+ }).ToList();}}}diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.csindex 0e7882c5..e93d1e00 100644--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs@@ -1,5 +1,4 @@using System;-using System.Linq;using GlazeWM.Domain.Common.Utils;using GlazeWM.Domain.Containers;using GlazeWM.Domain.Containers.Commands;@@ -18,32 +17,22 @@ namespace GlazeWM.Domain.Windows.CommandHandlers{private readonly Bus _bus;private readonly ContainerService _containerService;- private readonly UserConfigService _userConfigService;- private readonly CommandParsingService _commandParsingService;- private readonly WindowService _windowService;- private readonly MonitorService _monitorService;- private readonly WorkspaceService _workspaceService;private readonly ILogger<AddWindowHandler> _logger;+ private readonly MonitorService _monitorService;+ private readonly UserConfigService _userConfigService;public AddWindowHandler(Bus bus,ContainerService containerService,- UserConfigService userConfigService,- CommandParsingService commandParsingService,- WindowService windowService,+ ILogger<AddWindowHandler> logger,MonitorService monitorService,- WorkspaceService workspaceService,- ILogger<AddWindowHandler> logger- )+ UserConfigService userConfigService){_bus = bus;_containerService = containerService;- _userConfigService = userConfigService;- _commandParsingService = commandParsingService;- _windowService = windowService;- _monitorService = monitorService;- _workspaceService = workspaceService;_logger = logger;+ _monitorService = monitorService;+ _userConfigService = userConfigService;}public CommandResponse Handle(AddWindowCommand command)@@ -58,8 +47,7 @@ namespace GlazeWM.Domain.Windows.CommandHandlers: GetInsertionTarget();// Create the window instance.- var targetWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(targetParent);- var window = CreateWindow(windowHandle, targetWorkspace);+ var window = CreateWindow(windowHandle, targetParent);_logger.LogWindowEvent("New window managed", window);@@ -74,28 +62,15 @@ namespace GlazeWM.Domain.Windows.CommandHandlersif (MonitorService.HasDpiDifference(monitor, window.Parent))window.HasPendingDpiAdjustment = true;- // Set the newly added window as focus descendant. This is necessary because- // `EVENT_SYSTEM_FOREGROUND` is emitted before `EVENT_OBJECT_SHOW` and thus, focus state- // isn't updated automatically.+ // 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));- // TODO: Create separate command.- var matchingWindowRules = _userConfigService.GetMatchingWindowRules(window);-- var commandStrings = matchingWindowRules- .SelectMany(rule => rule.CommandList)- .Select(commandString => CommandParsingService.FormatCommand(commandString));-- var parsedCommands = commandStrings- .Select(commandString => _commandParsingService.ParseCommand(commandString, window))- .ToList();-- // Invoke commands in the matching window rules. Use `dynamic` to resolve the command type- // at runtime and allow multiple dispatch.- foreach (var parsedCommand in parsedCommands)- _bus.Invoke((dynamic)parsedCommand);+ // Run matching window rules.+ var windowRules = _userConfigService.GetMatchingWindowRules(window);+ _bus.Invoke(new RunWindowRulesCommand(window, windowRules));- // Window might be detached if 'ignore' command is invoked.+ // Window might be detached if 'ignore' command has been invoked.if (window.IsDetached())return CommandResponse.Ok;@@ -108,11 +83,13 @@ namespace GlazeWM.Domain.Windows.CommandHandlersreturn CommandResponse.Ok;}- private static Window CreateWindow(IntPtr windowHandle, Workspace targetWorkspace)+ private static Window CreateWindow(IntPtr windowHandle, Container targetParent){+ var originalPlacement = WindowService.GetPlacementOfHandle(windowHandle).NormalPosition;+// Calculate where window should be placed when floating is enabled. Use the original// width/height of the window, but position it in the center of the workspace.- var originalPlacement = WindowService.GetPlacementOfHandle(windowHandle).NormalPosition;+ var targetWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(targetParent);var floatingPlacement = originalPlacement.TranslateToCenter(targetWorkspace.ToRectangle());var defaultBorderDelta = new RectDelta(7, 0, 7, 7);diff --git a/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.csnew file mode 100644index 00000000..e057b08c--- /dev/null+++ b/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs@@ -0,0 +1,40 @@+using System.Linq;+using GlazeWM.Domain.UserConfigs;+using GlazeWM.Domain.Windows.Commands;+using GlazeWM.Infrastructure.Bussing;++namespace GlazeWM.Domain.Windows.CommandHandlers+{+ internal class RunWindowRulesHandler : ICommandHandler<RunWindowRulesCommand>+ {+ private readonly Bus _bus;+ private readonly CommandParsingService _commandParsingService;++ public RunWindowRulesHandler(+ Bus bus,+ CommandParsingService commandParsingService)+ {+ _bus = bus;+ _commandParsingService = commandParsingService;+ }++ public CommandResponse Handle(RunWindowRulesCommand command)+ {+ var window = command.Window;+ var windowRules = command.WindowRules;++ var parsedCommands = windowRules+ .SelectMany(rule => rule.CommandList)+ .Select(commandString => CommandParsingService.FormatCommand(commandString))+ .Select(commandString => _commandParsingService.ParseCommand(commandString, window))+ .ToList();++ // Invoke commands in the matching window rules. Use `dynamic` to resolve the command type+ // at runtime and allow multiple dispatch.+ foreach (var parsedCommand in parsedCommands)+ _bus.Invoke((dynamic)parsedCommand);++ return CommandResponse.Ok;+ }+ }+}diff --git a/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.cs b/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.csnew file mode 100644index 00000000..237f4c66--- /dev/null+++ b/GlazeWM.Domain/Windows/Commands/RunWindowRulesCommand.cs@@ -0,0 +1,18 @@+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;+ }+ }+}