diff --git a/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs b/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs index fe476c65b..176b02e9b 100644 --- a/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs +++ b/GlazeWM.Domain/Containers/CommandHandlers/RedrawContainersHandler.cs @@ -39,8 +39,8 @@ public CommandResponse Handle(RedrawContainersCommand command) var windowsToRestore = windowsToRedraw .Where( (window) => - window is not MinimizedWindow && - window.HasWindowStyle(WindowStyles.Maximize | WindowStyles.Minimize) + (window is not MinimizedWindow && window.HasWindowStyle(WindowStyles.Minimize)) || + (window is not MaximizedWindow && window.HasWindowStyle(WindowStyles.Maximize)) ) .ToList(); @@ -85,6 +85,9 @@ private void SetWindowPosition(Window window) else defaultFlags |= SetWindowPosFlags.HideWindow; + if (window is MaximizedWindow) + defaultFlags |= SetWindowPosFlags.NoSize; + // Transition display state depending on whether window will be shown/hidden. window.DisplayState = window.DisplayState switch { diff --git a/GlazeWM.Domain/Windows/CommandHandlers/SetMaximizedHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/SetMaximizedHandler.cs index 169886615..019625a96 100644 --- a/GlazeWM.Domain/Windows/CommandHandlers/SetMaximizedHandler.cs +++ b/GlazeWM.Domain/Windows/CommandHandlers/SetMaximizedHandler.cs @@ -1,3 +1,4 @@ +using GlazeWM.Domain.Containers.Commands; using GlazeWM.Domain.Windows.Commands; using GlazeWM.Infrastructure.Bussing; using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService; @@ -6,12 +7,18 @@ namespace GlazeWM.Domain.Windows.CommandHandlers { internal sealed class SetMaximizedHandler : ICommandHandler { + private readonly Bus _bus; + + public SetMaximizedHandler(Bus bus) + { + _bus = bus; + } + public CommandResponse Handle(SetMaximizedCommand command) { var window = command.Window; ShowWindow(window.Handle, ShowWindowFlags.Maximize); - return CommandResponse.Ok; } } diff --git a/GlazeWM.Domain/Windows/CommandHandlers/ToggleMaximizedHandler.cs b/GlazeWM.Domain/Windows/CommandHandlers/ToggleMaximizedHandler.cs index e7717033c..574e7a235 100644 --- a/GlazeWM.Domain/Windows/CommandHandlers/ToggleMaximizedHandler.cs +++ b/GlazeWM.Domain/Windows/CommandHandlers/ToggleMaximizedHandler.cs @@ -6,14 +6,21 @@ namespace GlazeWM.Domain.Windows.CommandHandlers { internal sealed class ToggleMaximizedHandler : ICommandHandler { + private readonly Bus _bus; + + public ToggleMaximizedHandler(Bus bus) + { + _bus = bus; + } + public CommandResponse Handle(ToggleMaximizedCommand command) { var window = command.Window; - if (window.HasWindowStyle(WindowStyles.Maximize)) - ShowWindow(window.Handle, ShowWindowFlags.Restore); + if (window is MaximizedWindow) + _bus.Invoke(new SetTilingCommand(window)); else - ShowWindow(window.Handle, ShowWindowFlags.Maximize); + _bus.Invoke(new SetMaximizedCommand(window)); return CommandResponse.Ok; } diff --git a/GlazeWM.Domain/Windows/EventHandlers/WindowLocationChangedHandler.cs b/GlazeWM.Domain/Windows/EventHandlers/WindowLocationChangedHandler.cs index f0d52ee83..f1b8ad042 100644 --- a/GlazeWM.Domain/Windows/EventHandlers/WindowLocationChangedHandler.cs +++ b/GlazeWM.Domain/Windows/EventHandlers/WindowLocationChangedHandler.cs @@ -1,7 +1,9 @@ +using System; using GlazeWM.Domain.Containers.Commands; using GlazeWM.Domain.Monitors.Commands; using GlazeWM.Infrastructure.Bussing; using GlazeWM.Infrastructure.Common.Events; +using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService; namespace GlazeWM.Domain.Windows.EventHandlers { @@ -20,11 +22,53 @@ public void Handle(WindowLocationChangedEvent @event) { var windowHandle = @event.WindowHandle; + HandleMaximizedWindow(windowHandle); + if (!_windowService.AppBarHandles.Contains(windowHandle)) return; _bus.Invoke(new RefreshMonitorStateCommand()); _bus.Invoke(new RedrawContainersCommand()); } + + private void HandleMaximizedWindow(IntPtr windowHandle) + { + var window = _windowService.GetWindowByHandle(windowHandle); + if (window is null) + return; + + var windowPlacement = WindowService.GetPlacementOfHandle(windowHandle); + var isMaximized = windowPlacement.ShowCommand == ShowWindowFlags.Maximize; + + if (isMaximized && window is not MaximizedWindow) + { + var maximizedWindow = new MaximizedWindow( + window.Handle, + window.FloatingPlacement, + window.BorderDelta + ) + { + Id = window.Id + }; + + _bus.Invoke(new ReplaceContainerCommand(maximizedWindow, window.Parent, window.Index)); + } + + if (!isMaximized && window is MaximizedWindow) + { + // C# isn't my mothertongue, I couldn't find a way to factorize the content of these two if blocks + // because of the type difference. Anyone's welcome to refactor this! + var tilingWindow = new TilingWindow( + window.Handle, + window.FloatingPlacement, + window.BorderDelta + ) + { + Id = window.Id + }; + + _bus.Invoke(new ReplaceContainerCommand(tilingWindow, window.Parent, window.Index)); + } + } } }