Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: window focus borders for W11 #246

Merged
merged 10 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions GlazeWM.Bootstrapper/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand Down Expand Up @@ -51,6 +52,8 @@ public void Run()
_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();
Expand Down Expand Up @@ -102,6 +105,7 @@ public void Run()
private void OnApplicationExit()
{
_bus.Invoke(new ShowAllWindowsCommand());
_bus.Invoke(new SetActiveWindowBorderCommand(null));
_barService.ExitApp();
_systemTrayIcon?.Remove();
Application.Exit();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
19 changes: 19 additions & 0 deletions GlazeWM.Domain/Containers/Commands/SetActiveWindowBorderCommand.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
1 change: 1 addition & 0 deletions GlazeWM.Domain/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static IServiceCollection AddDomainServices(this IServiceCollection servi
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>();
Expand Down
3 changes: 3 additions & 0 deletions GlazeWM.Domain/UserConfigs/GeneralConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public class GeneralConfig
/// </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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Common.Events;
using Microsoft.Extensions.Logging;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;

namespace GlazeWM.Domain.Windows.EventHandlers
{
Expand Down
3 changes: 3 additions & 0 deletions GlazeWM.Infrastructure/WindowsApi/WindowsApiService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,8 @@ public struct SystemPowerStatus
public uint BatteryLifeTime;
public uint BatteryFullLifeTime;
}

[DllImport("dwmapi.dll")]
public static extern int DwmSetWindowAttribute(IntPtr handle, uint attribute, ref uint value, uint size);
}
}