Skip to content

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

feat: window focus borders for W11 (#246)

blam728527227b51
diff --git a/GlazeWM.Bootstrapper/Startup.cs b/GlazeWM.Bootstrapper/Startup.cs
index a6cfe814..837e2093 100644
--- a/GlazeWM.Bootstrapper/Startup.cs
+++ b/GlazeWM.Bootstrapper/Startup.cs
@@ -5,6 +5,7 @@ using System.Windows.Forms;
using GlazeWM.Bar;
using GlazeWM.Domain.Common.Commands;
using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.Containers.Events;
using GlazeWM.Domain.UserConfigs;
using GlazeWM.Domain.UserConfigs.Commands;
using GlazeWM.Domain.Windows;
@@ -51,6 +52,8 @@ namespace GlazeWM.Bootstrapper
_bus.Events.OfType<ApplicationExitingEvent>()
.Subscribe(_ => OnApplicationExit());
+ _bus.Events.OfType<FocusChangedEvent>().Subscribe((@event) => _bus.InvokeAsync(new SetActiveWindowBorderCommand(@event.FocusedContainer as Window)));
+
// Launch bar WPF application. Spawns bar window when monitors are added, so the service needs
// to be initialized before populating initial state.
_barService.StartApp();
@@ -102,6 +105,7 @@ namespace GlazeWM.Bootstrapper
private void OnApplicationExit()
{
_bus.Invoke(new ShowAllWindowsCommand());
+ _bus.Invoke(new SetActiveWindowBorderCommand(null));
_barService.ExitApp();
_systemTrayIcon?.Remove();
Application.Exit();
diff --git a/GlazeWM.Domain/Containers/CommandHandlers/SetActiveWindowBorderHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/SetActiveWindowBorderHandler.cs
new file mode 100644
index 00000000..78808197
--- /dev/null
+++ b/GlazeWM.Domain/Containers/CommandHandlers/SetActiveWindowBorderHandler.cs
@@ -0,0 +1,49 @@
+using System;
+using System.Diagnostics;
+using GlazeWM.Domain.Containers.Commands;
+using GlazeWM.Domain.UserConfigs;
+using GlazeWM.Domain.Windows;
+using GlazeWM.Infrastructure.Bussing;
+using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
+
+namespace GlazeWM.Domain.Containers.CommandHandlers
+{
+ internal sealed class SetActiveWindowBorderHandler : ICommandHandler<SetActiveWindowBorderCommand>
+ {
+ private readonly UserConfigService _userConfigService;
+ private static Window _lastFocused;
+
+ private uint rgbToUint(string rgb)
+ {
+ var c = rgb.ToCharArray();
+ var bgr = string.Concat(c[5], c[6], c[3], c[4], c[1], c[2]);
+ return Convert.ToUInt32(bgr, 16);
+ }
+
+ public SetActiveWindowBorderHandler(UserConfigService userConfigService)
+ {
+ _userConfigService = userConfigService;
+ }
+
+ public CommandResponse Handle(SetActiveWindowBorderCommand command)
+ {
+ uint BorderColorAttribute = 34;
+ if (_lastFocused is not null)
+ {
+ uint defaultColor = 0xFFFFFFFF;
+ // Clear old window border
+ _ = DwmSetWindowAttribute(_lastFocused.Handle, BorderColorAttribute, ref defaultColor, 4);
+ }
+
+ var newWindowFocused = command.TargetWindow;
+ if (newWindowFocused is null)
+ return CommandResponse.Ok;
+
+ _lastFocused = command.TargetWindow;
+ // Set new window border
+ var configColor = rgbToUint(_userConfigService.GeneralConfig.FocusBorderColor);
+ _ = DwmSetWindowAttribute(_lastFocused.Handle, BorderColorAttribute, ref configColor, 4);
+ return CommandResponse.Ok;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/Containers/Commands/SetActiveWindowBorderCommand.cs b/GlazeWM.Domain/Containers/Commands/SetActiveWindowBorderCommand.cs
new file mode 100644
index 00000000..92ea678f
--- /dev/null
+++ b/GlazeWM.Domain/Containers/Commands/SetActiveWindowBorderCommand.cs
@@ -0,0 +1,19 @@
+using GlazeWM.Domain.Windows;
+using GlazeWM.Infrastructure.Bussing;
+using GlazeWM.Infrastructure.WindowsApi;
+
+namespace GlazeWM.Domain.Containers.Commands
+{
+ public class SetActiveWindowBorderCommand : Command
+ {
+ public Window TargetWindow { get; }
+
+ /// <summary>
+ /// Sets the newly focused window's border and removes border on older window.
+ /// </summary>
+ public SetActiveWindowBorderCommand(Window target)
+ {
+ TargetWindow = target;
+ }
+ }
+}
diff --git a/GlazeWM.Domain/DependencyInjection.cs b/GlazeWM.Domain/DependencyInjection.cs
index ce0c40a6..df0aec46 100644
--- a/GlazeWM.Domain/DependencyInjection.cs
+++ b/GlazeWM.Domain/DependencyInjection.cs
@@ -40,6 +40,7 @@ namespace GlazeWM.Domain
services.AddSingleton<ICommandHandler<AttachAndResizeContainerCommand>, AttachAndResizeContainerHandler>();
services.AddSingleton<ICommandHandler<AttachContainerCommand>, AttachContainerHandler>();
services.AddSingleton<ICommandHandler<CenterCursorOnContainerCommand>, CenterCursorOnContainerHandler>();
+ services.AddSingleton<ICommandHandler<SetActiveWindowBorderCommand>, SetActiveWindowBorderHandler>();
services.AddSingleton<ICommandHandler<ChangeContainerLayoutCommand>, ChangeContainerLayoutHandler>();
services.AddSingleton<ICommandHandler<ToggleContainerLayoutCommand>, ToggleContainerLayoutHandler>();
services.AddSingleton<ICommandHandler<DetachAndResizeContainerCommand>, DetachAndResizeContainerHandler>();
diff --git a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs b/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
index 5e752711..3643c3c3 100644
--- a/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
+++ b/GlazeWM.Domain/UserConfigs/GeneralConfig.cs
@@ -19,6 +19,9 @@ namespace GlazeWM.Domain.UserConfigs
/// </summary>
public string FloatingWindowMoveAmount { get; set; } = "5%";
/// <summary>
+ /// Color for border drawn around a focused window.
+ /// </summary>
+ public string FocusBorderColor { get; set; } = "#FFFFFFFF";
/// If activated, by switching to the current workspace the previous focused workspace is activated.
/// </summary>
public bool ToggleWorkspaceOnRefocus { get; set; }
diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs
index 70d1379c..b1d13dde 100644
--- a/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs
+++ b/GlazeWM.Domain/Windows/EventHandlers/WindowFocusedHandler.cs
@@ -7,6 +7,7 @@ using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Common.Events;
using Microsoft.Extensions.Logging;
+using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
namespace GlazeWM.Domain.Windows.EventHandlers
{
diff --git a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
index 092121b4..b5828716 100644
--- a/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
+++ b/GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
@@ -458,5 +458,8 @@ namespace GlazeWM.Infrastructure.WindowsApi
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
}
+
+ [DllImport("dwmapi.dll")]
+ public static extern int DwmSetWindowAttribute(IntPtr handle, uint attribute, ref uint value, uint size);
}
}