Skip to content

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

feat: improvements to event subscriptions (#364)

lars-bergerec3d5e993fa9
diff --git a/GlazeWM.App.IpcServer/Messages/GetMonitorsMessage.cs b/GlazeWM.App.IpcServer/ClientMessages/GetMonitorsMessage.cs
similarity index 71%
rename from GlazeWM.App.IpcServer/Messages/GetMonitorsMessage.cs
rename to GlazeWM.App.IpcServer/ClientMessages/GetMonitorsMessage.cs
index a7576899..3d087fd6 100644
--- a/GlazeWM.App.IpcServer/Messages/GetMonitorsMessage.cs
+++ b/GlazeWM.App.IpcServer/ClientMessages/GetMonitorsMessage.cs
@@ -1,6 +1,6 @@
using CommandLine;
-namespace GlazeWM.App.IpcServer.Messages
+namespace GlazeWM.App.IpcServer.ClientMessages
{
[Verb("monitors", HelpText = "Get all monitors.")]
public class GetMonitorsMessage
diff --git a/GlazeWM.App.IpcServer/Messages/GetWindowsMessage.cs b/GlazeWM.App.IpcServer/ClientMessages/GetWindowsMessage.cs
similarity index 71%
rename from GlazeWM.App.IpcServer/Messages/GetWindowsMessage.cs
rename to GlazeWM.App.IpcServer/ClientMessages/GetWindowsMessage.cs
index 66c860a6..5c17a4c0 100644
--- a/GlazeWM.App.IpcServer/Messages/GetWindowsMessage.cs
+++ b/GlazeWM.App.IpcServer/ClientMessages/GetWindowsMessage.cs
@@ -1,6 +1,6 @@
using CommandLine;
-namespace GlazeWM.App.IpcServer.Messages
+namespace GlazeWM.App.IpcServer.ClientMessages
{
[Verb("windows", HelpText = "Get all windows.")]
public class GetWindowsMessage
diff --git a/GlazeWM.App.IpcServer/Messages/GetWorkspacesMessage.cs b/GlazeWM.App.IpcServer/ClientMessages/GetWorkspacesMessage.cs
similarity index 72%
rename from GlazeWM.App.IpcServer/Messages/GetWorkspacesMessage.cs
rename to GlazeWM.App.IpcServer/ClientMessages/GetWorkspacesMessage.cs
index 720d61cd..eb3a0924 100644
--- a/GlazeWM.App.IpcServer/Messages/GetWorkspacesMessage.cs
+++ b/GlazeWM.App.IpcServer/ClientMessages/GetWorkspacesMessage.cs
@@ -1,6 +1,6 @@
using CommandLine;
-namespace GlazeWM.App.IpcServer.Messages
+namespace GlazeWM.App.IpcServer.ClientMessages
{
[Verb("workspaces", HelpText = "Get all workspaces.")]
public class GetWorkspacesMessage
diff --git a/GlazeWM.App.IpcServer/Messages/InvokeCommandMessage.cs b/GlazeWM.App.IpcServer/ClientMessages/InvokeCommandMessage.cs
similarity index 91%
rename from GlazeWM.App.IpcServer/Messages/InvokeCommandMessage.cs
rename to GlazeWM.App.IpcServer/ClientMessages/InvokeCommandMessage.cs
index 8c3cddfe..6568b56e 100644
--- a/GlazeWM.App.IpcServer/Messages/InvokeCommandMessage.cs
+++ b/GlazeWM.App.IpcServer/ClientMessages/InvokeCommandMessage.cs
@@ -1,6 +1,6 @@
using CommandLine;
-namespace GlazeWM.App.IpcServer.Messages
+namespace GlazeWM.App.IpcServer.ClientMessages
{
[Verb(
"command",
diff --git a/GlazeWM.App.IpcServer/Messages/SubscribeMessage.cs b/GlazeWM.App.IpcServer/ClientMessages/SubscribeMessage.cs
similarity index 87%
rename from GlazeWM.App.IpcServer/Messages/SubscribeMessage.cs
rename to GlazeWM.App.IpcServer/ClientMessages/SubscribeMessage.cs
index 01a12b7d..6c93ce17 100644
--- a/GlazeWM.App.IpcServer/Messages/SubscribeMessage.cs
+++ b/GlazeWM.App.IpcServer/ClientMessages/SubscribeMessage.cs
@@ -1,6 +1,6 @@
using CommandLine;
-namespace GlazeWM.App.IpcServer.Messages
+namespace GlazeWM.App.IpcServer.ClientMessages
{
[Verb(
"subscribe",
diff --git a/GlazeWM.App.IpcServer/EventSubscription.cs b/GlazeWM.App.IpcServer/EventSubscription.cs
new file mode 100644
index 00000000..bc445609
--- /dev/null
+++ b/GlazeWM.App.IpcServer/EventSubscription.cs
@@ -0,0 +1,7 @@
+using System;
+using System.Net.WebSockets;
+
+namespace GlazeWM.App.IpcServer
+{
+ public sealed record EventSubscription(Guid SubscriptionId, WebSocket WebSocket);
+}
diff --git a/GlazeWM.App.IpcServer/IpcMessageHandler.cs b/GlazeWM.App.IpcServer/IpcMessageHandler.cs
index d9924c68..b550ad26 100644
--- a/GlazeWM.App.IpcServer/IpcMessageHandler.cs
+++ b/GlazeWM.App.IpcServer/IpcMessageHandler.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Reactive.Linq;
@@ -8,14 +10,16 @@ using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
-using GlazeWM.App.IpcServer.Messages;
-using GlazeWM.App.IpcServer.Server;
+using GlazeWM.App.IpcServer.ClientMessages;
+using GlazeWM.App.IpcServer.ServerMessages;
+using GlazeWM.Domain.Common;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Monitors;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.Windows;
using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Common;
using GlazeWM.Infrastructure.Serialization;
using Microsoft.Extensions.Logging;
@@ -44,6 +48,40 @@ namespace GlazeWM.App.IpcServer
/// </summary>
private static readonly Regex _messagePartsRegex = new("(\".*?\"|\\S+)");
+ /// <summary>
+ /// Used to subscribe to all possible event types at once.
+ /// </summary>
+ private const string SubscribeAllKeyword = "all";
+
+ /// <summary>
+ /// Allowed events to subscribe to via `subscribe` message.
+ /// </summary>
+ private static readonly List<string> SubscribableEvents = new()
+ {
+ SubscribeAllKeyword,
+ DomainEvent.BindingModeChanged,
+ DomainEvent.FocusChanged,
+ DomainEvent.MonitorAdded,
+ DomainEvent.MonitorRemoved,
+ DomainEvent.TilingDirectionChanged,
+ DomainEvent.UserConfigReloaded,
+ DomainEvent.WindowManaged,
+ DomainEvent.WindowUnmanaged,
+ DomainEvent.WorkspaceActivated,
+ DomainEvent.WorkspaceDeactivated,
+ DomainEvent.WorkingAreaResized,
+ InfraEvent.ApplicationExiting,
+ };
+
+ /// <summary>
+ /// Dictionary of event names and connections subscribed to that event.
+ /// </summary>
+ internal ConcurrentDictionary<string, List<EventSubscription>> EventSubscriptions =
+ new(SubscribableEvents.ToDictionary(
+ (eventName) => eventName,
+ (_) => new List<EventSubscription>()
+ ));
+
public IpcMessageHandler(
Bus bus,
CommandParsingService commandParsingService,
@@ -62,6 +100,43 @@ namespace GlazeWM.App.IpcServer
_windowService = windowService;
}
+ /// <summary>
+ /// Subscribe to events emitted on the bus and emit them to subscribed connections.
+ /// </summary>
+ internal void Init()
+ {
+ _bus.Events.Subscribe((@event) =>
+ {
+ var eventSubscriptions = new List<EventSubscription>()
+ .Concat(EventSubscriptions.GetValueOrDefault(@event.Type, new()))
+ .Concat(EventSubscriptions.GetValueOrDefault(SubscribeAllKeyword, new()));
+
+ foreach (var subscription in eventSubscriptions)
+ {
+ try
+ {
+ _logger.LogDebug("Emitting event to IPC subscriber: {Event}", @event.Type);
+
+ var serverMessage = new EventSubscriptionMessage<object?>(
+ SubscriptionId: subscription.SubscriptionId,
+ Success: true,
+ MessageType: ServerMessageType.EventSubscription,
+ Data: @event,
+ Error: null
+ );
+
+ _ = subscription.WebSocket.SendAsync(
+ MessageToBytes(serverMessage),
+ WebSocketMessageType.Text,
+ true,
+ CancellationToken.None
+ );
+ }
+ catch { }
+ }
+ });
+ }
+
internal async Task Handle(WebSocket ws)
{
var buffer = new byte[1024];
@@ -91,19 +166,28 @@ namespace GlazeWM.App.IpcServer
var serverMessage = GetResponseMessage(clientMessage, ws);
await ws.SendAsync(
- serverMessage,
+ MessageToBytes(serverMessage),
WebSocketMessageType.Text,
true,
CancellationToken.None
);
}
- catch
- {
- }
+ catch { }
+ }
+
+ // Remove event subscription on websocket disconnect.
+ foreach (var (eventName, subscriptions) in EventSubscriptions)
+ {
+ EventSubscriptions.AddOrUpdate(
+ eventName,
+ new List<EventSubscription>(),
+ (_, subscriptions) =>
+ subscriptions.Where((subscription) => subscription.WebSocket == ws).ToList()
+ );
}
}
- private ArraySegment<byte> GetResponseMessage(string message, WebSocket ws)
+ private ClientResponseMessage<object?> GetResponseMessage(string message, WebSocket ws)
{
_logger.LogDebug("IPC message received: {Message}.", message);
@@ -121,7 +205,7 @@ namespace GlazeWM.App.IpcServer
GetWindowsMessage
>(messageParts);
- object? data = parsedArgs.Value switch
+ var data = parsedArgs.Value switch
{
InvokeCommandMessage commandMsg => HandleInvokeCommandMessage(commandMsg),
SubscribeMessage subscribeMsg => HandleSubscribeMessage(subscribeMsg, ws),
@@ -131,26 +215,27 @@ namespace GlazeWM.App.IpcServer
_ => throw new Exception($"Invalid message '{message}'")
};
- return ToServerMessage(
- success: true,
- messageType: ServerMessageType.ClientResponse,
- data: data,
- clientMessage: message
+ return new ClientResponseMessage<object?>(
+ ClientMessage: message,
+ Success: true,
+ MessageType: ServerMessageType.ClientResponse,
+ Data: data,
+ Error: null
);
}
catch (Exception exception)
{
- return ToServerMessage(
- success: false,
- messageType: ServerMessageType.ClientResponse,
- data: null,
- error: exception.Message,
- clientMessage: message
+ return new ClientResponseMessage<object?>(
+ ClientMessage: message,
+ Success: false,
+ MessageType: ServerMessageType.ClientResponse,
+ Data: null,
+ Error: exception.Message
);
}
}
- private bool? HandleInvokeCommandMessage(InvokeCommandMessage message)
+ private object HandleInvokeCommandMessage(InvokeCommandMessage message)
{
var contextContainer =
message.ContextContainerId is not null
@@ -165,51 +250,40 @@ namespace GlazeWM.App.IpcServer
);
_bus.Invoke((dynamic)command);
- return null;
+
+ return new { contextContainerId = contextContainer.Id };
}
- private bool? HandleSubscribeMessage(SubscribeMessage message, WebSocket ws)
+ private object HandleSubscribeMessage(SubscribeMessage message, WebSocket ws)
{
- var eventNames = message.Events.Split(',');
+ var eventNames = message.Events
+ .Split(',')
+ .Select(eventName => eventName.ToLowerInvariant());
- _bus.Events
- .TakeWhile((_) => ws.State == WebSocketState.Open)
- .Where(@event => eventNames.Contains(@event.FriendlyName))
- .Subscribe((@event) =>
- {
- var serverMessage = ToServerMessage(
- success: true,
- messageType: ServerMessageType.SubscribedEvent,
- data: @event
- );
+ foreach (var eventName in eventNames)
+ {
+ if (!SubscribableEvents.Contains(eventName))
+ throw new ArgumentException($"Invalid event '{eventName}'.");
+ }
- _ = ws.SendAsync(
- serverMessage,
- WebSocketMessageType.Text,
- true,
- CancellationToken.None
- );
- });
+ var subscriptionId = Guid.NewGuid();
- return null;
+ foreach (var eventName in eventNames)
+ {
+ var subscriptions = EventSubscriptions.GetValueOrDefault(
+ eventName,
+ new()
+ );
+
+ subscriptions.Add(new(subscriptionId, ws));
+ }
+
+ return new { subscriptionId };
}
- private ArraySegment<byte> ToServerMessage(
- bool success,
- ServerMessageType messageType,
- object? data,
- string? error = null,
- string? clientMessage = null)
+ private ArraySegment<byte> MessageToBytes<T>(T serverMessage)
+ where T : ServerMessage<object?>
{
- // Use `object` type so that the JSON serializer uses derived type.
- var serverMessage = new ServerMessage<object>(
- Success: success,
- MessageType: messageType,
- Data: data,
- Error: error,
- ClientMessage: clientMessage
- );
-
var messageString = JsonParser.ToString(serverMessage, _serializeOptions);
var messageBytes = Encoding.UTF8.GetBytes(messageString);
diff --git a/GlazeWM.App.IpcServer/IpcServerStartup.cs b/GlazeWM.App.IpcServer/IpcServerStartup.cs
index 22c24a7d..e485c87f 100644
--- a/GlazeWM.App.IpcServer/IpcServerStartup.cs
+++ b/GlazeWM.App.IpcServer/IpcServerStartup.cs
@@ -24,6 +24,8 @@ namespace GlazeWM.App.IpcServer
{
_logger.LogDebug("Starting IPC server on port {Port}.", port);
+ _ipcMessageHandler.Init();
+
var builder = WebApplication.CreateBuilder();
builder.Logging.ClearProviders();
builder.WebHost.UseUrls($"http://localhost:{port}");
diff --git a/GlazeWM.App.IpcServer/ServerMessageType.cs b/GlazeWM.App.IpcServer/ServerMessageType.cs
deleted file mode 100644
index 7e6ef73e..00000000
--- a/GlazeWM.App.IpcServer/ServerMessageType.cs
+++ /dev/null
@@ -1,8 +0,0 @@
-namespace GlazeWM.App.IpcServer.Server
-{
- internal enum ServerMessageType
- {
- ClientResponse,
- SubscribedEvent
- }
-}
diff --git a/GlazeWM.App.IpcServer/ServerMessages/ClientResponseMessage.cs b/GlazeWM.App.IpcServer/ServerMessages/ClientResponseMessage.cs
new file mode 100644
index 00000000..7fafaf43
--- /dev/null
+++ b/GlazeWM.App.IpcServer/ServerMessages/ClientResponseMessage.cs
@@ -0,0 +1,21 @@
+namespace GlazeWM.App.IpcServer.ServerMessages
+{
+ internal sealed record ClientResponseMessage<T>(
+ /// <summary>
+ /// The client message that this is in response to.
+ /// </summary>
+ string ClientMessage,
+
+ /// <inheritdoc/>
+ bool Success,
+
+ /// <inheritdoc/>
+ ServerMessageType MessageType,
+
+ /// <inheritdoc/>
+ T? Data,
+
+ /// <inheritdoc/>
+ string? Error
+ ) : ServerMessage<T>(Success, MessageType, Data, Error);
+}
diff --git a/GlazeWM.App.IpcServer/ServerMessages/EventSubscriptionMessage.cs b/GlazeWM.App.IpcServer/ServerMessages/EventSubscriptionMessage.cs
new file mode 100644
index 00000000..35cf4093
--- /dev/null
+++ b/GlazeWM.App.IpcServer/ServerMessages/EventSubscriptionMessage.cs
@@ -0,0 +1,23 @@
+using System;
+
+namespace GlazeWM.App.IpcServer.ServerMessages
+{
+ internal sealed record EventSubscriptionMessage<T>(
+ /// <summary>
+ /// ID of the subscription.
+ /// </summary>
+ Guid SubscriptionId,
+
+ /// <inheritdoc/>
+ bool Success,
+
+ /// <inheritdoc/>
+ ServerMessageType MessageType,
+
+ /// <inheritdoc/>
+ T? Data,
+
+ /// <inheritdoc/>
+ string? Error
+ ) : ServerMessage<T>(Success, MessageType, Data, Error);
+}
diff --git a/GlazeWM.App.IpcServer/ServerMessage.cs b/GlazeWM.App.IpcServer/ServerMessages/ServerMessage.cs
similarity index 59%
rename from GlazeWM.App.IpcServer/ServerMessage.cs
rename to GlazeWM.App.IpcServer/ServerMessages/ServerMessage.cs
index 04457c36..bb88758f 100644
--- a/GlazeWM.App.IpcServer/ServerMessage.cs
+++ b/GlazeWM.App.IpcServer/ServerMessages/ServerMessage.cs
@@ -1,6 +1,6 @@
-namespace GlazeWM.App.IpcServer.Server
+namespace GlazeWM.App.IpcServer.ServerMessages
{
- internal sealed record ServerMessage<T>(
+ internal abstract record ServerMessage<T>(
bool Success,
/// <summary>
@@ -18,11 +18,5 @@ namespace GlazeWM.App.IpcServer.Server
/// The error message. This property is only present for messages where 'Success'
/// is false.
/// </summary>
- string? Error,
-
- /// <summary>
- /// The client message that this is in response to. This property is only present for
- /// 'ClientResponse' message types.
- /// </summary>
- string? ClientMessage);
+ string? Error);
}
diff --git a/GlazeWM.App.IpcServer/ServerMessages/ServerMessageType.cs b/GlazeWM.App.IpcServer/ServerMessages/ServerMessageType.cs
new file mode 100644
index 00000000..47f6732a
--- /dev/null
+++ b/GlazeWM.App.IpcServer/ServerMessages/ServerMessageType.cs
@@ -0,0 +1,8 @@
+namespace GlazeWM.App.IpcServer.ServerMessages
+{
+ internal enum ServerMessageType
+ {
+ ClientResponse,
+ EventSubscription
+ }
+}
diff --git a/GlazeWM.App.Watcher/WatcherStartup.cs b/GlazeWM.App.Watcher/WatcherStartup.cs
index 0a80b904..f750cdef 100644
--- a/GlazeWM.App.Watcher/WatcherStartup.cs
+++ b/GlazeWM.App.Watcher/WatcherStartup.cs
@@ -69,13 +69,13 @@ namespace GlazeWM.App.Watcher
{
var response = await client.ReceiveAsync();
- return response.GetProperty("friendlyName").GetString() switch
+ return response.GetProperty("type").GetString() switch
{
- "window_managed" => (
+ DomainEvent.WindowManaged => (
true,
new IntPtr(response.GetProperty("window").GetProperty("handle").GetInt64())
),
- "window_unmanaged" => (
+ DomainEvent.WindowUnmanaged => (
false,
new IntPtr(response.GetProperty("removedHandle").GetInt64())
),
diff --git a/GlazeWM.App/Program.cs b/GlazeWM.App/Program.cs
index 9eac5912..5a157fe6 100644
--- a/GlazeWM.App/Program.cs
+++ b/GlazeWM.App/Program.cs
@@ -2,13 +2,12 @@ using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
-using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using CommandLine;
using GlazeWM.App.Cli;
using GlazeWM.App.IpcServer;
-using GlazeWM.App.IpcServer.Messages;
+using GlazeWM.App.IpcServer.ClientMessages;
using GlazeWM.App.Watcher;
using GlazeWM.App.WindowManager;
using GlazeWM.Bar;
diff --git a/GlazeWM.Domain/Common/DomainEvent.cs b/GlazeWM.Domain/Common/DomainEvent.cs
new file mode 100644
index 00000000..0bc986ce
--- /dev/null
+++ b/GlazeWM.Domain/Common/DomainEvent.cs
@@ -0,0 +1,17 @@
+namespace GlazeWM.Domain.Common
+{
+ public static class DomainEvent
+ {
+ public const string BindingModeChanged = "binding_mode_changed";
+ public const string FocusChanged = "focus_changed";
+ public const string MonitorAdded = "monitor_added";
+ public const string MonitorRemoved = "monitor_removed";
+ public const string TilingDirectionChanged = "tiling_direction_changed";
+ public const string UserConfigReloaded = "user_config_reloaded";
+ public const string WindowManaged = "window_managed";
+ public const string WindowUnmanaged = "window_unmanaged";
+ public const string WorkspaceActivated = "workspace_activated";
+ public const string WorkspaceDeactivated = "workspace_deactivated";
+ public const string WorkingAreaResized = "working_area_resized";
+ }
+}
diff --git a/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs b/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs
index 82b5a4ee..ab6ff4f6 100644
--- a/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs
+++ b/GlazeWM.Domain/Common/Events/BindingModeChangedEvent.cs
@@ -2,13 +2,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Common.Events
{
- public class BindingModeChangedEvent : Event
- {
- public string BindingMode { get; }
-
- public BindingModeChangedEvent(string bindingMode)
- {
- BindingMode = bindingMode;
- }
- }
+ public record BindingModeChangedEvent(string BindingMode)
+ : Event(DomainEvent.BindingModeChanged);
}
diff --git a/GlazeWM.Domain/Containers/Events/FocusChangedEvent.cs b/GlazeWM.Domain/Containers/Events/FocusChangedEvent.cs
index 0e9d59aa..2bf7556a 100644
--- a/GlazeWM.Domain/Containers/Events/FocusChangedEvent.cs
+++ b/GlazeWM.Domain/Containers/Events/FocusChangedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Containers.Events
{
- public class FocusChangedEvent : Event
- {
- public Container FocusedContainer { get; }
-
- public FocusChangedEvent(Container focusedContainer)
- {
- FocusedContainer = focusedContainer;
- }
- }
+ public record FocusChangedEvent(Container FocusedContainer)
+ : Event(DomainEvent.FocusChanged);
}
diff --git a/GlazeWM.Domain/Containers/Events/LayoutChangedEvent.cs b/GlazeWM.Domain/Containers/Events/LayoutChangedEvent.cs
deleted file mode 100644
index c5cac609..00000000
--- a/GlazeWM.Domain/Containers/Events/LayoutChangedEvent.cs
+++ /dev/null
@@ -1,15 +0,0 @@
-using GlazeWM.Domain.Common.Enums;
-using GlazeWM.Infrastructure.Bussing;
-
-namespace GlazeWM.Domain.Containers.Events
-{
- public class TilingDirectionChangedEvent : Event
- {
- public TilingDirection NewTilingDirection { get; }
-
- public TilingDirectionChangedEvent(TilingDirection newTilingDirection)
- {
- NewTilingDirection = newTilingDirection;
- }
- }
-}
diff --git a/GlazeWM.Domain/Containers/Events/TilingDirectionChangedEvent.cs b/GlazeWM.Domain/Containers/Events/TilingDirectionChangedEvent.cs
new file mode 100644
index 00000000..86767c4a
--- /dev/null
+++ b/GlazeWM.Domain/Containers/Events/TilingDirectionChangedEvent.cs
@@ -0,0 +1,9 @@
+using GlazeWM.Domain.Common;
+using GlazeWM.Domain.Common.Enums;
+using GlazeWM.Infrastructure.Bussing;
+
+namespace GlazeWM.Domain.Containers.Events
+{
+ public record TilingDirectionChangedEvent(TilingDirection NewTilingDirection)
+ : Event(DomainEvent.TilingDirectionChanged);
+}
diff --git a/GlazeWM.Domain/Monitors/Events/MonitorAddedEvent.cs b/GlazeWM.Domain/Monitors/Events/MonitorAddedEvent.cs
index 37251d0d..15a3d8bb 100644
--- a/GlazeWM.Domain/Monitors/Events/MonitorAddedEvent.cs
+++ b/GlazeWM.Domain/Monitors/Events/MonitorAddedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Monitors.Events
{
- public class MonitorAddedEvent : Event
- {
- public Monitor AddedMonitor { get; }
-
- public MonitorAddedEvent(Monitor addedMonitor)
- {
- AddedMonitor = addedMonitor;
- }
- }
+ public record MonitorAddedEvent(Monitor AddedMonitor)
+ : Event(DomainEvent.MonitorAdded);
}
diff --git a/GlazeWM.Domain/Monitors/Events/MonitorRemovedEvent.cs b/GlazeWM.Domain/Monitors/Events/MonitorRemovedEvent.cs
index 82e44153..d206c861 100644
--- a/GlazeWM.Domain/Monitors/Events/MonitorRemovedEvent.cs
+++ b/GlazeWM.Domain/Monitors/Events/MonitorRemovedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Monitors.Events
{
- public class MonitorRemovedEvent : Event
- {
- public string RemovedDeviceName { get; }
-
- public MonitorRemovedEvent(string removedDeviceName)
- {
- RemovedDeviceName = removedDeviceName;
- }
- }
+ public record MonitorRemovedEvent(string RemovedDeviceName)
+ : Event(DomainEvent.MonitorRemoved);
}
diff --git a/GlazeWM.Domain/Monitors/Events/WorkingAreaResizedEvent.cs b/GlazeWM.Domain/Monitors/Events/WorkingAreaResizedEvent.cs
index d7114466..73f7f18e 100644
--- a/GlazeWM.Domain/Monitors/Events/WorkingAreaResizedEvent.cs
+++ b/GlazeWM.Domain/Monitors/Events/WorkingAreaResizedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Monitors.Events
{
- public class WorkingAreaResizedEvent : Event
- {
- public Monitor AffectedMonitor { get; }
-
- public WorkingAreaResizedEvent(Monitor affectedMonitor)
- {
- AffectedMonitor = affectedMonitor;
- }
- }
+ public record WorkingAreaResizedEvent(Monitor AffectedMonitor)
+ : Event(DomainEvent.WorkingAreaResized);
}
diff --git a/GlazeWM.Domain/UserConfigs/Events/UserConfigReloadedEvent.cs b/GlazeWM.Domain/UserConfigs/Events/UserConfigReloadedEvent.cs
index d14a1f6a..6b65fcdc 100644
--- a/GlazeWM.Domain/UserConfigs/Events/UserConfigReloadedEvent.cs
+++ b/GlazeWM.Domain/UserConfigs/Events/UserConfigReloadedEvent.cs
@@ -1,8 +1,7 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.UserConfigs.Events
{
- public class UserConfigReloadedEvent : Event
- {
- }
+ public record UserConfigReloadedEvent() : Event(DomainEvent.UserConfigReloaded);
}
diff --git a/GlazeWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs b/GlazeWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs
deleted file mode 100644
index 24f6e1c9..00000000
--- a/GlazeWM.Domain/Windows/Events/FocusedWindowMovedEvent.cs
+++ /dev/null
@@ -1,17 +0,0 @@
-using GlazeWM.Domain.Containers;
-using GlazeWM.Infrastructure.Bussing;
-
-namespace GlazeWM.Domain.Windows.Events
-{
- public class FocusedWindowMovedEvent : Event
- {
- public Window FocusedWindow { get; }
- public SplitContainer OriginalParent { get; }
-
- public FocusedWindowMovedEvent(Window focusedWindow, SplitContainer originalParent)
- {
- FocusedWindow = focusedWindow;
- OriginalParent = originalParent;
- }
- }
-}
diff --git a/GlazeWM.Domain/Windows/Events/WindowManagedEvent.cs b/GlazeWM.Domain/Windows/Events/WindowManagedEvent.cs
index 413d3769..c9ad5322 100644
--- a/GlazeWM.Domain/Windows/Events/WindowManagedEvent.cs
+++ b/GlazeWM.Domain/Windows/Events/WindowManagedEvent.cs
@@ -1,14 +1,7 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Windows.Events
{
- public class WindowManagedEvent : Event
- {
- public Window Window { get; }
-
- public WindowManagedEvent(Window window)
- {
- Window = window;
- }
- }
+ public record WindowManagedEvent(Window Window) : Event(DomainEvent.WindowManaged);
}
diff --git a/GlazeWM.Domain/Windows/Events/WindowUnmanagedEvent.cs b/GlazeWM.Domain/Windows/Events/WindowUnmanagedEvent.cs
index fa02b159..365739ab 100644
--- a/GlazeWM.Domain/Windows/Events/WindowUnmanagedEvent.cs
+++ b/GlazeWM.Domain/Windows/Events/WindowUnmanagedEvent.cs
@@ -1,17 +1,9 @@
using System;
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Windows.Events
{
- public class WindowUnmanagedEvent : Event
- {
- public Guid RemovedId { get; }
- public IntPtr RemovedHandle { get; }
-
- public WindowUnmanagedEvent(Guid removedId, IntPtr removedHandle)
- {
- RemovedId = removedId;
- RemovedHandle = removedHandle;
- }
- }
+ public record WindowUnmanagedEvent(Guid RemovedId, IntPtr RemovedHandle)
+ : Event(DomainEvent.WindowUnmanaged);
}
diff --git a/GlazeWM.Domain/Workspaces/Events/WorkspaceActivatedEvent.cs b/GlazeWM.Domain/Workspaces/Events/WorkspaceActivatedEvent.cs
index a05e8b26..df070876 100644
--- a/GlazeWM.Domain/Workspaces/Events/WorkspaceActivatedEvent.cs
+++ b/GlazeWM.Domain/Workspaces/Events/WorkspaceActivatedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Workspaces.Events
{
- public class WorkspaceActivatedEvent : Event
- {
- public Workspace ActivatedWorkspace { get; }
-
- public WorkspaceActivatedEvent(Workspace activatedWorkspace)
- {
- ActivatedWorkspace = activatedWorkspace;
- }
- }
+ public record WorkspaceActivatedEvent(Workspace ActivatedWorkspace)
+ : Event(DomainEvent.WorkspaceActivated);
}
diff --git a/GlazeWM.Domain/Workspaces/Events/WorkspaceDeactivatedEvent.cs b/GlazeWM.Domain/Workspaces/Events/WorkspaceDeactivatedEvent.cs
index a39fe478..02cd0957 100644
--- a/GlazeWM.Domain/Workspaces/Events/WorkspaceDeactivatedEvent.cs
+++ b/GlazeWM.Domain/Workspaces/Events/WorkspaceDeactivatedEvent.cs
@@ -1,14 +1,8 @@
+using GlazeWM.Domain.Common;
using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Domain.Workspaces.Events
{
- public class WorkspaceDeactivatedEvent : Event
- {
- public Workspace DeactivatedWorkspace { get; }
-
- public WorkspaceDeactivatedEvent(Workspace deactivatedWorkspace)
- {
- DeactivatedWorkspace = deactivatedWorkspace;
- }
- }
+ public record WorkspaceDeactivatedEvent(Workspace DeactivatedWorkspace)
+ : Event(DomainEvent.WorkspaceDeactivated);
}
diff --git a/GlazeWM.Infrastructure/Bussing/Event.cs b/GlazeWM.Infrastructure/Bussing/Event.cs
index 225a2361..d1453a1f 100644
--- a/GlazeWM.Infrastructure/Bussing/Event.cs
+++ b/GlazeWM.Infrastructure/Bussing/Event.cs
@@ -2,20 +2,12 @@ using GlazeWM.Infrastructure.Utils;
namespace GlazeWM.Infrastructure.Bussing
{
- public class Event
- {
- public string Name => GetType().Name;
-
- /// <summary>
- /// Name used to subscribe to for IPC and CLI usage.
- /// </summary>
- public string FriendlyName
- {
- get
- {
- var shortName = Name.Replace("Event", "");
- return CasingUtil.PascalToSnake(shortName);
- }
- }
- }
+ /// <summary>
+ /// An event emitted on the bus.
+ /// </summary>
+ /// <param name="Type">
+ /// Identifier for the type of event. For CLI and IPC usage when subscribing to a
+ /// given event type.
+ /// </param>
+ public abstract record Event(string Type);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/ApplicationExitingEvent.cs b/GlazeWM.Infrastructure/Common/Events/ApplicationExitingEvent.cs
index a83a4655..a855920e 100644
--- a/GlazeWM.Infrastructure/Common/Events/ApplicationExitingEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/ApplicationExitingEvent.cs
@@ -2,7 +2,5 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class ApplicationExitingEvent : Event
- {
- }
+ public record ApplicationExitingEvent() : Event(InfraEvent.ApplicationExiting);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/DisplaySettingsChangedEvent.cs b/GlazeWM.Infrastructure/Common/Events/DisplaySettingsChangedEvent.cs
index 06f73a79..8a7c8a6e 100644
--- a/GlazeWM.Infrastructure/Common/Events/DisplaySettingsChangedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/DisplaySettingsChangedEvent.cs
@@ -2,7 +2,5 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class DisplaySettingsChangedEvent : Event
- {
- }
+ public record DisplaySettingsChangedEvent() : Event(InfraEvent.DisplaySettingsChanged);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowDestroyedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowDestroyedEvent.cs
index c0720b83..7a3234bb 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowDestroyedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowDestroyedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowDestroyedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowDestroyedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowDestroyedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowDestroyed);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowFocusedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowFocusedEvent.cs
index c10837b0..ee111306 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowFocusedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowFocusedEvent.cs
@@ -3,13 +3,5 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowFocusedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowFocusedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowFocusedEvent(IntPtr WindowHandle) : Event(InfraEvent.WindowFocused);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowHiddenEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowHiddenEvent.cs
index 5a9dd83e..90228df1 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowHiddenEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowHiddenEvent.cs
@@ -3,13 +3,5 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowHiddenEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowHiddenEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowHiddenEvent(IntPtr WindowHandle) : Event(InfraEvent.WindowHidden);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowLocationChangedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowLocationChangedEvent.cs
index bfe59b5b..1b415f7f 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowLocationChangedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowLocationChangedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowLocationChangedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowLocationChangedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowLocationChangedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowLocationChanged);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowMinimizeEndedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowMinimizeEndedEvent.cs
index 06375d53..d9b9bb5f 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowMinimizeEndedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowMinimizeEndedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowMinimizeEndedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowMinimizeEndedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowMinimizeEndedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowMinimizeEnded);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowMinimizedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowMinimizedEvent.cs
index 3a3b7cce..a51eb996 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowMinimizedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowMinimizedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowMinimizedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowMinimizedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowMinimizedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowMinimized);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowMovedOrResizedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowMovedOrResizedEvent.cs
index d58b8a52..63d077ef 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowMovedOrResizedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowMovedOrResizedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowMovedOrResizedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowMovedOrResizedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowMovedOrResizedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowMovedOrResized);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowShownEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowShownEvent.cs
index e0ed6eb1..d056238e 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowShownEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowShownEvent.cs
@@ -3,13 +3,5 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowShownEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowShownEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowShownEvent(IntPtr WindowHandle) : Event(InfraEvent.WindowShown);
}
diff --git a/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs b/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs
index c0dc68f3..d1a63182 100644
--- a/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs
+++ b/GlazeWM.Infrastructure/Common/Events/WindowTitleChangedEvent.cs
@@ -3,13 +3,6 @@ using GlazeWM.Infrastructure.Bussing;
namespace GlazeWM.Infrastructure.Common.Events
{
- public class WindowTitleChangedEvent : Event
- {
- public IntPtr WindowHandle { get; }
-
- public WindowTitleChangedEvent(IntPtr windowHandle)
- {
- WindowHandle = windowHandle;
- }
- }
+ public record WindowTitleChangedEvent(IntPtr WindowHandle)
+ : Event(InfraEvent.WindowTitleChanged);
}
diff --git a/GlazeWM.Infrastructure/Common/InfraEvent.cs b/GlazeWM.Infrastructure/Common/InfraEvent.cs
new file mode 100644
index 00000000..a6f01abe
--- /dev/null
+++ b/GlazeWM.Infrastructure/Common/InfraEvent.cs
@@ -0,0 +1,18 @@
+namespace GlazeWM.Infrastructure.Common
+{
+ public static class InfraEvent
+ {
+ public const string ApplicationExiting = "application_exiting";
+ public const string DisplaySettingsChanged = "display_settings_changed";
+ public const string MouseMove = "mouse_move";
+ public const string WindowDestroyed = "window_destroyed";
+ public const string WindowFocused = "window_focused";
+ public const string WindowHidden = "window_hidden";
+ public const string WindowLocationChanged = "window_location_changed";
+ public const string WindowMinimizeEnded = "window_minimize_ended";
+ public const string WindowMinimized = "window_minimized";
+ public const string WindowMovedOrResized = "window_moved_or_resized";
+ public const string WindowShown = "window_shown";
+ public const string WindowTitleChanged = "window_title_changed";
+ }
+}
diff --git a/GlazeWM.Infrastructure/Utils/DictionaryExtensions.cs b/GlazeWM.Infrastructure/Utils/DictionaryExtensions.cs
deleted file mode 100644
index 4610309f..00000000
--- a/GlazeWM.Infrastructure/Utils/DictionaryExtensions.cs
+++ /dev/null
@@ -1,33 +0,0 @@
-using System;
-using System.Collections.Generic;
-
-namespace GlazeWM.Infrastructure.Utils
-{
- public static class DictionaryExtensions
- {
- public static TValue GetValueOrDefault<TKey, TValue>(
- this Dictionary<TKey, TValue> dictionary,
- TKey key,
- TValue defaultValue)
- {
- TValue val;
-
- if (dictionary.TryGetValue(key, out val))
- return val ?? defaultValue;
-
- return defaultValue;
- }
-
- public static TValue GetValueOrThrow<TKey, TValue>(
- this Dictionary<TKey, TValue> dictionary,
- TKey key)
- {
- TValue val;
-
- if (!dictionary.TryGetValue(key, out val))
- throw new Exception($"Dictionary value does not exist at key '{key}'.");
-
- return val;
- }
- }
-}
diff --git a/GlazeWM.Infrastructure/WindowsApi/MouseMoveEvent.cs b/GlazeWM.Infrastructure/WindowsApi/MouseMoveEvent.cs
index 060f0ec0..81b331f8 100644
--- a/GlazeWM.Infrastructure/WindowsApi/MouseMoveEvent.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/MouseMoveEvent.cs
@@ -1,34 +1,26 @@
using System;
using System.Runtime.InteropServices;
using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.Common;
using GlazeWM.Infrastructure.WindowsApi;
-public class MouseMoveEvent : Event
-{
+public record MouseMoveEvent(
/// <summary>
/// (X,Y) location of mouse with 0,0 being the top-left corner of the main monitor
/// </summary>
- public Point Point { get; }
+ Point Point,
/// <summary>
/// Whether left-click is currently pressed.
/// </summary>
- public bool IsMouseDown { get; }
+ bool IsMouseDown,
/// <summary>
/// The time stamp for this message, equivalent to what `GetMessageTime` would
/// return.
/// </summary>
- public int TimeStamp { get; }
-
- public MouseMoveEvent(Point point, bool isMouseDown, int timeStamp)
- {
- Point = point;
- IsMouseDown = isMouseDown;
- TimeStamp = timeStamp;
- }
-}
-
+ int TimeStamp
+) : Event(InfraEvent.MouseMove);
public enum WMessages
{