Skip to content

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

fix: invalid reference when running multiple commands (#153)

lars-berger4746641ed67d
diff --git a/GlazeWM.Domain/Containers/Container.cs b/GlazeWM.Domain/Containers/Container.cs
index 81804569..cff503e1 100644
--- a/GlazeWM.Domain/Containers/Container.cs
+++ b/GlazeWM.Domain/Containers/Container.cs
@@ -7,6 +7,11 @@ namespace GlazeWM.Domain.Containers
{
public abstract class Container
{
+ /// <summary>
+ /// A unique identifier for the particular container. Implementation varies between container
+ /// types (eg. windows use their handle, whereas monitors use device name).
+ /// </summary>
+ public abstract string Id { get; init; }
public virtual int Height { get; set; }
public virtual int Width { get; set; }
public virtual int X { get; set; }
diff --git a/GlazeWM.Domain/Containers/ContainerService.cs b/GlazeWM.Domain/Containers/ContainerService.cs
index 66ec7f48..d9fbebce 100644
--- a/GlazeWM.Domain/Containers/ContainerService.cs
+++ b/GlazeWM.Domain/Containers/ContainerService.cs
@@ -40,6 +40,11 @@ namespace GlazeWM.Domain.Containers
_userConfigService = userConfigService;
}
+ public Container GetContainerById(string id)
+ {
+ return ContainerTree.SelfAndDescendants.FirstOrDefault(container => container.Id == id);
+ }
+
/// <summary>
/// Calculates the width of a container that can be resized programatically. This
/// calculation is shared by windows and split containers.
diff --git a/GlazeWM.Domain/Containers/RootContainer.cs b/GlazeWM.Domain/Containers/RootContainer.cs
index 3281e06a..63e54a38 100644
--- a/GlazeWM.Domain/Containers/RootContainer.cs
+++ b/GlazeWM.Domain/Containers/RootContainer.cs
@@ -1,6 +1,9 @@
-namespace GlazeWM.Domain.Containers
+using System;
+
+namespace GlazeWM.Domain.Containers
{
public sealed class RootContainer : Container
{
+ public override string Id { get; init; } = $"ROOT/{new Guid().ToString()}";
}
}
diff --git a/GlazeWM.Domain/Containers/SplitContainer.cs b/GlazeWM.Domain/Containers/SplitContainer.cs
index a601952d..9753dedc 100644
--- a/GlazeWM.Domain/Containers/SplitContainer.cs
+++ b/GlazeWM.Domain/Containers/SplitContainer.cs
@@ -1,10 +1,12 @@
-using GlazeWM.Domain.Common.Enums;
+using System;
+using GlazeWM.Domain.Common.Enums;
using GlazeWM.Infrastructure;
namespace GlazeWM.Domain.Containers
{
public class SplitContainer : Container, IResizable
{
+ public override string Id { get; init; } = $"SPLIT/{new Guid().ToString()}";
public Layout Layout { get; set; } = Layout.HORIZONTAL;
public double SizePercentage { get; set; } = 1;
diff --git a/GlazeWM.Domain/Monitors/Monitor.cs b/GlazeWM.Domain/Monitors/Monitor.cs
index 65e8bcf2..e6a2d5fc 100644
--- a/GlazeWM.Domain/Monitors/Monitor.cs
+++ b/GlazeWM.Domain/Monitors/Monitor.cs
@@ -5,6 +5,7 @@ namespace GlazeWM.Domain.Monitors
{
public sealed class Monitor : Container
{
+ public override string Id { get; init; }
public string DeviceName { get; set; }
public override int Width { get; set; }
public override int Height { get; set; }
@@ -22,6 +23,7 @@ namespace GlazeWM.Domain.Monitors
int x,
int y)
{
+ Id = $"MONITOR/{deviceName}";
DeviceName = deviceName;
Width = width;
Height = height;
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
index 54d1c5ab..bb29c7c9 100644
--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
@@ -53,22 +53,27 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
return;
var subjectContainer = _containerService.FocusedContainer;
+ var subjectContainerId = subjectContainer.Id;
// Invoke commands in sequence on keybinding press.
foreach (var commandString in formattedCommandStrings)
{
- var parsedCommand = _commandParsingService.ParseCommand(
- commandString,
- subjectContainer
- );
-
// 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);
}
});
});
diff --git a/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs
index e057b08c..8e7bbdf2 100644
--- a/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs
+++ b/GlazeWM.Domain/Windows/CommandHandlers/RunWindowRulesHandler.cs
@@ -9,31 +9,47 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
private readonly Bus _bus;
private readonly CommandParsingService _commandParsingService;
+ private readonly WindowService _windowService;
public RunWindowRulesHandler(
Bus bus,
- CommandParsingService commandParsingService)
+ 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 parsedCommands = windowRules
+ var commandStrings = windowRules
.SelectMany(rule => rule.CommandList)
- .Select(commandString => CommandParsingService.FormatCommand(commandString))
- .Select(commandString => _commandParsingService.ParseCommand(commandString, window))
- .ToList();
+ .Select(commandString => CommandParsingService.FormatCommand(commandString));
- // 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)
+ // Window to use as subject container when invoking commands.
+ var subjectWindow = window;
+
+ foreach (var commandString in commandStrings)
+ {
+ if (subjectWindow.IsDetached())
+ 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/Window.cs b/GlazeWM.Domain/Windows/Window.cs
index 295ef95b..ace0fd41 100644
--- a/GlazeWM.Domain/Windows/Window.cs
+++ b/GlazeWM.Domain/Windows/Window.cs
@@ -8,6 +8,7 @@ namespace GlazeWM.Domain.Windows
{
public abstract class Window : Container
{
+ public override string Id { get; init; }
public IntPtr Handle { get; }
/// <summary>
@@ -30,6 +31,7 @@ namespace GlazeWM.Domain.Windows
protected Window(IntPtr handle, Rect floatingPlacement, RectDelta borderDelta)
{
+ Id = $"WINDOW/{handle.ToString("x")}";
Handle = handle;
FloatingPlacement = floatingPlacement;
BorderDelta = borderDelta;
diff --git a/GlazeWM.Domain/Windows/WindowService.cs b/GlazeWM.Domain/Windows/WindowService.cs
index 9c347097..36834f9d 100644
--- a/GlazeWM.Domain/Windows/WindowService.cs
+++ b/GlazeWM.Domain/Windows/WindowService.cs
@@ -42,6 +42,11 @@ namespace GlazeWM.Domain.Windows
return _containerService.ContainerTree.Descendants.OfType<Window>();
}
+ public Window GetWindowByHandle(IntPtr handle)
+ {
+ return GetWindows().FirstOrDefault(window => window.Handle == handle);
+ }
+
/// <summary>
/// Get the id of the process that created the window.
/// </summary>
diff --git a/GlazeWM.Domain/Workspaces/Workspace.cs b/GlazeWM.Domain/Workspaces/Workspace.cs
index 835a0a8d..d8b276bc 100644
--- a/GlazeWM.Domain/Workspaces/Workspace.cs
+++ b/GlazeWM.Domain/Workspaces/Workspace.cs
@@ -8,6 +8,7 @@ namespace GlazeWM.Domain.Workspaces
{
public sealed class Workspace : SplitContainer
{
+ public override string Id { get; init; }
public string Name { get; set; }
private readonly UserConfigService _userConfigService =
@@ -62,6 +63,7 @@ namespace GlazeWM.Domain.Workspaces
public Workspace(string name)
{
+ Id = $"WORKSPACE/{name}";
Name = name;
}
}