Skip to content

Commit

Permalink
[FancyZones] Fix moved to the current active monitor when maximized
Browse files Browse the repository at this point in the history
  • Loading branch information
quyenvsp committed Dec 16, 2023
1 parent bf7462b commit f452e8e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ namespace MonitorUtils
if (GetMonitorInfo(monitor, &destMi))
{
RECT newPosition = FitOnScreen(placement.rcNormalPosition, originMi.rcWork, destMi.rcWork);
FancyZonesWindowUtils::SizeWindowToRect(window, newPosition);
FancyZonesWindowUtils::SizeWindowToRect(window, newPosition, false);
}
}
}
Expand Down
15 changes: 14 additions & 1 deletion src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ void FancyZonesWindowUtils::SwitchToWindow(HWND window) noexcept
}
}

void FancyZonesWindowUtils::SizeWindowToRect(HWND window, RECT rect) noexcept
void FancyZonesWindowUtils::SizeWindowToRect(HWND window, RECT rect, BOOL snapZone) noexcept
{
WINDOWPLACEMENT placement{};
::GetWindowPlacement(window, &placement);
Expand All @@ -265,8 +265,15 @@ void FancyZonesWindowUtils::SizeWindowToRect(HWND window, RECT rect) noexcept
::GetWindowPlacement(window, &placement);
}

BOOL maximizeLater = false;
if (IsWindowVisible(window))
{
// If is not snap zone then need keep maximize state (move to active monitor)
if (!snapZone && placement.showCmd == SW_SHOWMAXIMIZED)
{
maximizeLater = true;
}

// Do not restore minimized windows. We change their placement though so they restore to the correct zone.
if ((placement.showCmd != SW_SHOWMINIMIZED) &&
(placement.showCmd != SW_MINIMIZE))
Expand Down Expand Up @@ -294,6 +301,12 @@ void FancyZonesWindowUtils::SizeWindowToRect(HWND window, RECT rect) noexcept
Logger::error(L"SetWindowPlacement failed, {}", get_last_error_or_default(GetLastError()));
}

// make sure window is moved to the correct monitor before maximize.
if (maximizeLater)
{
placement.showCmd = SW_SHOWMAXIMIZED;
}

// Do it again, allowing Windows to resize the window and set correct scaling
// This fixes Issue #365
result = ::SetWindowPlacement(window, &placement);
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/WindowUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace FancyZonesWindowUtils
bool IsExcludedByDefault(const HWND& hwnd, const std::wstring& processPath) noexcept;

void SwitchToWindow(HWND window) noexcept;
void SizeWindowToRect(HWND window, RECT rect) noexcept; // Parameter rect must be in screen coordinates (e.g. obtained from GetWindowRect)
void SizeWindowToRect(HWND window, RECT rect, BOOL snapZone = true) noexcept; // Parameter rect must be in screen coordinates (e.g. obtained from GetWindowRect)
void SaveWindowSizeAndOrigin(HWND window) noexcept;
void RestoreWindowSize(HWND window) noexcept;
void RestoreWindowOrigin(HWND window) noexcept;
Expand Down

0 comments on commit f452e8e

Please sign in to comment.