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 873eaf7
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 5 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,30 +1,128 @@
using System;
using GlazeWM.Domain.Containers;
using GlazeWM.Domain.Containers.Commands;
using GlazeWM.Domain.Monitors.Commands;
using GlazeWM.Domain.Workspaces;
using GlazeWM.Infrastructure.Bussing;
using GlazeWM.Infrastructure.Common.Events;
using static GlazeWM.Infrastructure.WindowsApi.WindowsApiService;

namespace GlazeWM.Domain.Windows.EventHandlers
{
internal sealed class WindowLocationChangedHandler : IEventHandler<WindowLocationChangedEvent>
{
private readonly Bus _bus;
private readonly ContainerService _containerService;
private readonly WindowService _windowService;

public WindowLocationChangedHandler(Bus bus, WindowService windowService)
public WindowLocationChangedHandler(Bus bus,
WindowService windowService,
ContainerService containerService)
{
_bus = bus;
_containerService = containerService;
_windowService = windowService;
}

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 previousState = WindowService.GetWindowType(window);
var maximizedWindow = new MaximizedWindow(
window.Handle,
window.FloatingPlacement,
window.BorderDelta,
previousState
)
{
Id = window.Id
};

_bus.Invoke(new ReplaceContainerCommand(maximizedWindow, window.Parent, window.Index));
}

if (!isMaximized && window is MaximizedWindow)
{
var restoredWindow = CreateWindowFromPreviousState(window as MaximizedWindow);

_bus.Invoke(new ReplaceContainerCommand(restoredWindow, window.Parent, window.Index));

if (restoredWindow is not TilingWindow)
{
// Needed to reposition the floating window
_bus.Invoke(new RedrawContainersCommand());
return;
}

var workspace = WorkspaceService.GetWorkspaceFromChildContainer(window);
var insertionTarget = workspace.LastFocusedDescendantOfType<IResizable>();

// Insert the created tiling window after the last focused descendant of the workspace.
if (insertionTarget == null)
_bus.Invoke(new MoveContainerWithinTreeCommand(restoredWindow, workspace, 0, true));
else
_bus.Invoke(
new MoveContainerWithinTreeCommand(
restoredWindow,
insertionTarget.Parent,
insertionTarget.Index + 1,
true
)
);

_containerService.ContainersToRedraw.Add(workspace);
_bus.Invoke(new RedrawContainersCommand());
}
}

private static Window CreateWindowFromPreviousState(MaximizedWindow window)
{
Window restoredWindow = window.PreviousState switch
{
WindowType.Floating => new FloatingWindow(
window.Handle,
window.FloatingPlacement,
window.BorderDelta
),
WindowType.Fullscreen => new FullscreenWindow(
window.Handle,
window.FloatingPlacement,
window.BorderDelta
),
// Set `SizePercentage` to 0 to correctly resize the container when moved within tree.
WindowType.Tiling => new TilingWindow(
window.Handle,
window.FloatingPlacement,
window.BorderDelta,
0
),
WindowType.Maximized => throw new ArgumentException(null, nameof(window)),
_ => throw new ArgumentException(null, nameof(window)),
};

restoredWindow.Id = window.Id;
return restoredWindow;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ private static Window CreateWindowFromPreviousState(MinimizedWindow window)
WindowType.Maximized => new MaximizedWindow(
window.Handle,
window.FloatingPlacement,
window.BorderDelta
window.BorderDelta,
WindowType.Tiling
),
WindowType.Fullscreen => new FullscreenWindow(
window.Handle,
Expand Down
6 changes: 5 additions & 1 deletion GlazeWM.Domain/Windows/MaximizedWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ namespace GlazeWM.Domain.Windows
{
public sealed class MaximizedWindow : Window
{
public WindowType PreviousState;

public MaximizedWindow(
IntPtr handle,
Rect floatingPlacement,
RectDelta borderDelta
RectDelta borderDelta,
WindowType previousState
) : base(handle, floatingPlacement, borderDelta)
{
PreviousState = previousState;
}
}
}

0 comments on commit 873eaf7

Please sign in to comment.