Skip to content

GlazeWM is a tiling window manager for macOS and Windows inspired by i3wm.

fix: keybindings do not fire if desktop has focus (#132)

lars-berger48175c538175
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
index 8f7cea79..3343f587 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/AttachContainerHandler.cs
@@ -19,7 +19,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
var targetParent = command.TargetParent;
var targetIndex = command.TargetIndex;
- if (childToAdd.Parent != null)
+ if (!childToAdd.IsDetached())
throw new Exception("Cannot attach an already attached container. This is a bug.");
childToAdd.Parent = targetParent;
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
index a4816bf4..d6aec785 100644
--- a/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
+++ b/GlazeWM.Domain/Containers/CommandHandlers/ReplaceContainerHandler.cs
@@ -21,7 +21,7 @@ namespace GlazeWM.Domain.Containers.CommandHandlers
var targetParent = command.TargetParent;
var targetIndex = command.TargetIndex;
- if (replacementContainer.Parent != null)
+ if (!replacementContainer.IsDetached())
throw new Exception(
"Cannot use an already attached container as replacement container. This is a bug."
);
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.cs
index d4106ac4..8884dde8 100644
--- a/GlazeWM.Domain/Containers/Container.cs
+++ b/GlazeWM.Domain/Containers/Container.cs
@@ -110,6 +110,11 @@ namespace GlazeWM.Domain.Containers
}
}
+ public bool IsDetached()
+ {
+ return Parent is null;
+ }
+
public bool HasChildren()
{
return Children.Count > 0;
diff --git a/GlazeWM.Domain/Containers/ContainerService.cs b/GlazeWM.Domain/Containers/ContainerService.cs
index a01aedcf..807df875 100644
--- a/GlazeWM.Domain/Containers/ContainerService.cs
+++ b/GlazeWM.Domain/Containers/ContainerService.cs
@@ -4,7 +4,6 @@ using System.Linq;
using GlazeWM.Domain.Common.Enums;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.Windows;
-using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
namespace GlazeWM.Domain.Containers
{
@@ -40,25 +39,6 @@ namespace GlazeWM.Domain.Containers
/// </summary>
public Container PendingFocusContainer;
- /// <summary>
- /// Whether the focused container of the WM is in sync with the OS. Mismatches between the
- /// focus state of the WM and the OS occur when ignored windows (eg. via user's window rules)
- /// or elevated windows are in focus.
- /// </summary>
- public bool IsFocusSynced
- {
- get
- {
- var foregroundHandle = GetForegroundWindow();
- var focusedContainer = FocusedContainer;
-
- if (focusedContainer is Window)
- return (focusedContainer as Window).Hwnd == foregroundHandle;
-
- return IntPtr.Zero == foregroundHandle;
- }
- }
-
private readonly UserConfigService _userConfigService;
public ContainerService(UserConfigService userConfigService)
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.cs
index 4eca82a2..395c7175 100644
--- a/GlazeWM.Domain/DependencyInjection.cs
+++ b/GlazeWM.Domain/DependencyInjection.cs
@@ -58,6 +58,7 @@ namespace GlazeWM.Domain
services.AddSingleton<ICommandHandler<AddWindowCommand>, AddWindowHandler>();
services.AddSingleton<ICommandHandler<CloseWindowCommand>, CloseWindowHandler>();
services.AddSingleton<ICommandHandler<FocusWindowCommand>, FocusWindowHandler>();
+ services.AddSingleton<ICommandHandler<IgnoreWindowCommand>, IgnoreWindowHandler>();
services.AddSingleton<ICommandHandler<MoveWindowCommand>, MoveWindowHandler>();
services.AddSingleton<ICommandHandler<RemoveWindowCommand>, RemoveWindowHandler>();
services.AddSingleton<ICommandHandler<ResizeWindowCommand>, ResizeWindowHandler>();
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
index fca00c26..9387c22a 100644
--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
@@ -1,8 +1,10 @@
using System.Linq;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.UserConfigs.Commands;
+using GlazeWM.Domain.Windows;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.WindowsApi;
+using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
namespace GlazeWM.Domain.UserConfigs.CommandHandlers
{
@@ -12,18 +14,20 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
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
- )
+ KeybindingService keybindingService,
+ WindowService windowService)
{
_bus = bus;
_commandParsingService = commandParsingService;
_containerService = containerService;
_keybindingService = keybindingService;
+ _windowService = windowService;
}
public CommandResponse Handle(RegisterKeybindingsCommand command)
@@ -41,9 +45,8 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
foreach (var binding in keybindingConfig.BindingList)
_keybindingService.AddGlobalKeybinding(binding, () =>
{
- // Avoid invoking keybinding if focus is not synced (eg. if an unmanaged window has
- // focus).
- if (!_containerService.IsFocusSynced)
+ // Avoid invoking keybinding if an ignored window currently has focus.
+ if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))
return;
// Invoke commands in sequence on keybinding press.
diff --git a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
index cb632351..fa02c2cc 100644
--- a/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandParsingService.cs
@@ -87,8 +87,9 @@ namespace GlazeWM.Domain.UserConfigs
ExtractProcessName(string.Join(" ", commandParts[1..])),
ExtractProcessArgs(string.Join(" ", commandParts[1..]))
),
- // TODO: Temporary hack to avoid errors during `ValidateCommand`.
- "ignore" => new NoopCommand(),
+ "ignore" => subjectContainer is Window
+ ? new IgnoreWindowCommand(subjectContainer as Window)
+ : new NoopCommand(),
_ => throw new ArgumentException(null, nameof(commandString)),
};
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
index 47c928fb..0e7882c5 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/AddWindowHandler.cs
@@ -61,16 +61,6 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
var targetWorkspace = WorkspaceService.GetWorkspaceFromChildContainer(targetParent);
var window = CreateWindow(windowHandle, targetWorkspace);
- var matchingWindowRules = _userConfigService.GetMatchingWindowRules(window);
-
- var commandStrings = matchingWindowRules
- .SelectMany(rule => rule.CommandList)
- .Select(commandString => CommandParsingService.FormatCommand(commandString));
-
- // Avoid managing a window if a window rule uses 'ignore' command.
- if (commandStrings.Contains("ignore"))
- return CommandResponse.Ok;
-
_logger.LogWindowEvent("New window managed", window);
if (window is IResizable)
@@ -89,9 +79,12 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
// isn't updated automatically.
_bus.Invoke(new SetFocusedDescendantCommand(window));
- // Set OS focus to the newly added window in case it's not already focused. This is also
- // necessary for window rule commands to run properly on startup with existing windows.
- _bus.Invoke(new FocusWindowCommand(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))
@@ -102,9 +95,16 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
foreach (var parsedCommand in parsedCommands)
_bus.Invoke((dynamic)parsedCommand);
+ // Window might be detached if 'ignore' command is invoked.
+ if (window.IsDetached())
+ return CommandResponse.Ok;
+
if (shouldRedraw)
_bus.Invoke(new RedrawContainersCommand());
+ // Set OS focus to the newly added window in case it's not already focused.
+ _bus.Invoke(new FocusWindowCommand(window));
+
return CommandResponse.Ok;
}
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/IgnoreWindowHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/IgnoreWindowHandler.cs
new file mode 100644
index 00000000..376c1be4
--- /dev/null
+++ b/GlazeWM.Domain/Windows/CommandHandlers/IgnoreWindowHandler.cs
@@ -0,0 +1,36 @@
+using GlazeWM.Domain.Containers;
+using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Windows.Commands;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Windows.CommandHandlers
+{
+ internal class IgnoreWindowHandler : ICommandHandler<IgnoreWindowCommand>
+ {
+ private readonly Bus _bus;
+ private readonly WindowService _windowService;
+
+ public IgnoreWindowHandler(Bus bus, WindowService windowService)
+ {
+ _bus = bus;
+ _windowService = windowService;
+ }
+
+ public CommandResponse Handle(IgnoreWindowCommand command)
+ {
+ var windowToIgnore = command.WindowToIgnore;
+
+ // Store handle to ignored window.
+ _windowService.IgnoredHandles.Add(windowToIgnore.Hwnd);
+
+ if (windowToIgnore is IResizable)
+ _bus.Invoke(new DetachAndResizeContainerCommand(windowToIgnore));
+ else
+ _bus.Invoke(new DetachContainerCommand(windowToIgnore));
+
+ _bus.Invoke(new RedrawContainersCommand());
+
+ return CommandResponse.Ok;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Windows/Commands/IgnoreWindowCommand.cs b/GlazeWM.Domain/Windows/Commands/IgnoreWindowCommand.cs
new file mode 100644
index 00000000..c65d4c34
--- /dev/null
+++ b/GlazeWM.Domain/Windows/Commands/IgnoreWindowCommand.cs
@@ -0,0 +1,14 @@
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Windows.Commands
+{
+ public class IgnoreWindowCommand : Command
+ {
+ public Window WindowToIgnore { get; }
+
+ public IgnoreWindowCommand(Window windowToIgnore)
+ {
+ WindowToIgnore = windowToIgnore;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Windows/WindowService.cs b/GlazeWM.Domain/Windows/WindowService.cs
index 875de90c..a008a79e 100644
--- a/GlazeWM.Domain/Windows/WindowService.cs
+++ b/GlazeWM.Domain/Windows/WindowService.cs
@@ -17,7 +17,12 @@ namespace GlazeWM.Domain.Windows
/// Window handles to appbars (application desktop toolbars). Positioning changes of appbars
/// can affect the working area of the parent monitor and requires windows to be redrawn.
/// </summary>
- public List<IntPtr> AppBarHandles { get; set; } = new List<IntPtr>();
+ public List<IntPtr> AppBarHandles { get; set; } = new();
+
+ /// <summary>
+ /// Window handles to ignored windows (ie. windows where 'ignore' command has been invoked).
+ /// </summary>
+ public List<IntPtr> IgnoredHandles { get; set; } = new();
private readonly ContainerService _containerService;
private readonly MonitorService _monitorService;