Skip to content

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

fix: out of order commands & events (#138)

lars-berger66f3ba88a277
diff --git a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
index 96d5d5ed..6fdbf0ff 100644
--- a/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
+++ b/GlazeWM.Domain/UserConfigs/CommandHandlers/RegisterKeybindingsHandler.cs
@@ -1,4 +1,5 @@
using System.Linq;
+using System.Threading.Tasks;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Domain.Windows;
@@ -45,23 +46,26 @@ namespace GlazeWM.Domain.UserConfigs.CommandHandlers
foreach (var binding in keybindingConfig.BindingList)
_keybindingService.AddGlobalKeybinding(binding, () =>
{
- // Avoid invoking keybinding if an ignored window currently has focus.
- if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))
- return;
-
- // Invoke commands in sequence on keybinding press.
- foreach (var commandString in formattedCommandStrings)
+ Task.Run(() =>
{
- var subjectContainer = _containerService.FocusedContainer;
+ // Avoid invoking keybinding if an ignored window currently has focus.
+ if (_windowService.IgnoredHandles.Contains(GetForegroundWindow()))
+ return;
+
+ // Invoke commands in sequence on keybinding press.
+ foreach (var commandString in formattedCommandStrings)
+ {
+ var subjectContainer = _containerService.FocusedContainer;
- var parsedCommand = _commandParsingService.ParseCommand(
- commandString,
- subjectContainer
- );
+ var parsedCommand = _commandParsingService.ParseCommand(
+ commandString,
+ subjectContainer
+ );
- // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.
- _bus.Invoke((dynamic)parsedCommand);
- }
+ // Use `dynamic` to resolve the command type at runtime and allow multiple dispatch.
+ _bus.Invoke((dynamic)parsedCommand);
+ }
+ });
});
}
diff --git a/GlazeWM.Infrastructure/Bussing/Bus.cs b/GlazeWM.Infrastructure/Bussing/Bus.cs
index 36e0db26..9d3eee75 100644
--- a/GlazeWM.Infrastructure/Bussing/Bus.cs
+++ b/GlazeWM.Infrastructure/Bussing/Bus.cs
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Reactive.Subjects;
+using System.Threading.Tasks;
namespace GlazeWM.Infrastructure.Bussing
{
@@ -37,6 +38,11 @@ namespace GlazeWM.Infrastructure.Bussing
}
}
+ public Task<CommandResponse> InvokeAsync<T>(T command) where T : Command
+ {
+ return Task.Run(() => Invoke(command));
+ }
+
/// <summary>
/// Sends event to appropriate event handlers.
/// </summary>
@@ -59,5 +65,10 @@ namespace GlazeWM.Infrastructure.Bussing
// Emit event through subject.
Events.OnNext(@event);
}
+
+ public Task EmitAsync<T>(T @event) where T : Event
+ {
+ return Task.Run(() => RaiseEvent(@event));
+ }
}
}
diff --git a/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs b/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs
index a199202c..8e050b38 100644
--- a/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/SystemEventService.cs
@@ -25,7 +25,7 @@ namespace GlazeWM.Infrastructure.WindowsApi
handler => SystemEvents.DisplaySettingsChanged -= handler
);
- displaySettingChanges.Subscribe((_) => _bus.RaiseEvent(new DisplaySettingsChangedEvent()));
+ displaySettingChanges.Subscribe((_) => _bus.EmitAsync(new DisplaySettingsChangedEvent()));
}
}
}
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs b/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
index d1a35bb5..c9ae0274 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowEventService.cs
@@ -61,7 +61,7 @@ namespace GlazeWM.Infrastructure.WindowsApi
};
if (eventToRaise is not null)
- _bus.RaiseEvent((dynamic)eventToRaise);
+ _bus.EmitAsync((dynamic)eventToRaise);
}
}
}