Skip to content

Commit

Permalink
fix: maximized windows stay maximized when new windows are created an…
Browse files Browse the repository at this point in the history
…d after switching worskpaces
  • Loading branch information
phisko committed Oct 5, 2023
1 parent f292744 commit 2b97c94
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Windows.Commands;
using GlazeWM.Infrastructure.Bussing;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;
Expand All @@ -6,12 +7,18 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
internal sealed class SetMaximizedHandler : ICommandHandler<SetMaximizedCommand>
{
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;
}
}
Expand Down
13 changes: 10 additions & 3 deletions GlazeWM.Domain/Windows/CommandHandlers/ToggleMaximizedHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@ namespace GlazeWM.Domain.Windows.CommandHandlers
{
internal sealed class ToggleMaximizedHandler : ICommandHandler<ToggleMaximizedCommand>
{
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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));
}
}
}
}

0 comments on commit 2b97c94

Please sign in to comment.