From 4b22e2cc5a50d25f7a7edf92c8c998df601110d7 Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Thu, 9 Apr 2020 15:57:23 +0200 Subject: [PATCH 1/7] First version --- .../tests/UnitTests/ZoneWindow.Spec.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index bece6fd65a7..3476076001f 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -650,5 +650,30 @@ namespace FancyZonesUnitTests const auto actual = m_fancyZonesData.GetAppZoneHistoryMap().at(processPath).zoneIndex; Assert::AreEqual(expected, actual); } + + TEST_METHOD (TestNew) + { + m_zoneWindow = InitZoneWindowWithActiveZoneSet(); + Assert::IsNotNull(m_zoneWindow->ActiveZoneSet()); + + auto window = Mocks::WindowCreate(m_hInst); + + int orginalWidth = 450; + int orginalHeight = 550; + + SetWindowPos(window, nullptr, 150, 150, orginalWidth, orginalHeight, SWP_SHOWWINDOW); + /* SetProcessDPIAware(window, DPI_AWARENESS_SYSTEM_AWARE);*/ + ShowWindow(window, SW_SHOW); + + auto zone = MakeZone(RECT{ 0, 0, 300, 300 }); + m_zoneWindow->ActiveZoneSet()->AddZone(zone); + + m_zoneWindow->MoveWindowIntoZoneByDirection(window, VK_LEFT, true); + + RECT inZoneRect; + GetWindowRect(window, &inZoneRect); + Assert::AreEqual(orginalWidth, (int)inZoneRect.right - (int)inZoneRect.left); + Assert::AreEqual(orginalHeight, (int)inZoneRect.bottom - (int)inZoneRect.top); + } }; } From 7ae5c87d4ce667094c466d724ca2423da1117c3d Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Thu, 9 Apr 2020 15:57:36 +0200 Subject: [PATCH 2/7] First version --- src/modules/fancyzones/lib/Zone.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/modules/fancyzones/lib/Zone.cpp b/src/modules/fancyzones/lib/Zone.cpp index 6b3347f390e..486963ce11f 100644 --- a/src/modules/fancyzones/lib/Zone.cpp +++ b/src/modules/fancyzones/lib/Zone.cpp @@ -77,14 +77,19 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept const auto level = DPIAware::GetAwarenessLevel(GetWindowDpiAwarenessContext(window)); const bool accountForUnawareness = level < DPIAware::PER_MONITOR_AWARE; + + LONG leftMargin = 0; + LONG rightMargin = 0; + LONG bottomMargin = 0; + if (SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRect, sizeof(frameRect)))) { - const auto left_margin = frameRect.left - windowRect.left; - const auto right_margin = frameRect.right - windowRect.right; - const auto bottom_margin = frameRect.bottom - windowRect.bottom; - newWindowRect.left -= left_margin; - newWindowRect.right -= right_margin; - newWindowRect.bottom -= bottom_margin; + leftMargin = frameRect.left - windowRect.left; + rightMargin = frameRect.right - windowRect.right; + bottomMargin = frameRect.bottom - windowRect.bottom; + newWindowRect.left -= leftMargin; + newWindowRect.right -= rightMargin; + newWindowRect.bottom -= bottomMargin; } // Map to screen coords @@ -98,8 +103,8 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept OffsetRect(&newWindowRect, -taskbar_left_size, -taskbar_top_size); if (accountForUnawareness) { - newWindowRect.left = max(mi.rcMonitor.left, newWindowRect.left); - newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size, newWindowRect.right); + newWindowRect.left = max(mi.rcMonitor.left - leftMargin, newWindowRect.left); + newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size - rightMargin, newWindowRect.right); newWindowRect.top = max(mi.rcMonitor.top, newWindowRect.top); newWindowRect.bottom = min(mi.rcMonitor.bottom - taskbar_top_size, newWindowRect.bottom); } From 25030a78177b27b179f39fadd3c7c76216d6126e Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Tue, 14 Apr 2020 09:28:28 +0200 Subject: [PATCH 3/7] test update --- .../fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index 3476076001f..0ad4f0ce865 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -651,18 +651,15 @@ namespace FancyZonesUnitTests Assert::AreEqual(expected, actual); } - TEST_METHOD (TestNew) + TEST_METHOD (TestNesdsdw) { m_zoneWindow = InitZoneWindowWithActiveZoneSet(); Assert::IsNotNull(m_zoneWindow->ActiveZoneSet()); auto window = Mocks::WindowCreate(m_hInst); - int orginalWidth = 450; - int orginalHeight = 550; - - SetWindowPos(window, nullptr, 150, 150, orginalWidth, orginalHeight, SWP_SHOWWINDOW); - /* SetProcessDPIAware(window, DPI_AWARENESS_SYSTEM_AWARE);*/ + SetWindowPos(window, nullptr, 150, 150, 450, 550, SWP_SHOWWINDOW); + Assert::IsTrue(AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_UNAWARE, GetWindowDpiAwarenessContext(window))); ShowWindow(window, SW_SHOW); auto zone = MakeZone(RECT{ 0, 0, 300, 300 }); @@ -672,8 +669,8 @@ namespace FancyZonesUnitTests RECT inZoneRect; GetWindowRect(window, &inZoneRect); - Assert::AreEqual(orginalWidth, (int)inZoneRect.right - (int)inZoneRect.left); - Assert::AreEqual(orginalHeight, (int)inZoneRect.bottom - (int)inZoneRect.top); + Assert::AreEqual(0, (int)inZoneRect.left); + Assert::AreEqual(0, (int)inZoneRect.bottom - (int)inZoneRect.top); } }; } From dfa432999ddda73439c5ce00882e58f309a2d227 Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Tue, 14 Apr 2020 11:09:28 +0200 Subject: [PATCH 4/7] Updated test --- .../tests/UnitTests/ZoneWindow.Spec.cpp | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index 0ad4f0ce865..0d71afe9bee 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -651,7 +651,7 @@ namespace FancyZonesUnitTests Assert::AreEqual(expected, actual); } - TEST_METHOD (TestNesdsdw) + TEST_METHOD (WhenPlacingDpiUnawareWindowIntoTheZoneItRectShouldBeExtendedToFitTargetZone) { m_zoneWindow = InitZoneWindowWithActiveZoneSet(); Assert::IsNotNull(m_zoneWindow->ActiveZoneSet()); @@ -660,17 +660,30 @@ namespace FancyZonesUnitTests SetWindowPos(window, nullptr, 150, 150, 450, 550, SWP_SHOWWINDOW); Assert::IsTrue(AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_UNAWARE, GetWindowDpiAwarenessContext(window))); + + RECT nonExtendedWindowRect{}; + ::GetWindowRect(window, &nonExtendedWindowRect); + + RECT extendedFrameRect{}; + Assert::IsTrue(SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &extendedFrameRect, sizeof(extendedFrameRect)))); + + int leftMargin = extendedFrameRect.left - nonExtendedWindowRect.left; + int rightMargin = extendedFrameRect.right - nonExtendedWindowRect.right; + int bottomMargin = extendedFrameRect.bottom - nonExtendedWindowRect.bottom; ShowWindow(window, SW_SHOW); - auto zone = MakeZone(RECT{ 0, 0, 300, 300 }); + RECT zoneRect{ 0, 0, 250, 350 }; + auto zone = MakeZone(zoneRect); m_zoneWindow->ActiveZoneSet()->AddZone(zone); m_zoneWindow->MoveWindowIntoZoneByDirection(window, VK_LEFT, true); RECT inZoneRect; GetWindowRect(window, &inZoneRect); - Assert::AreEqual(0, (int)inZoneRect.left); - Assert::AreEqual(0, (int)inZoneRect.bottom - (int)inZoneRect.top); + Assert::AreEqual(-leftMargin, (int)inZoneRect.left); + Assert::AreEqual((int)zoneRect.right - rightMargin, (int)inZoneRect.right); + Assert::AreEqual((int)zoneRect.bottom - bottomMargin, (int)inZoneRect.bottom); + Assert::AreEqual(0, (int)inZoneRect.top); } }; } From 1aacb9e3346f13598b2d0d15d5f5b16286d34d27 Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:20:57 +0200 Subject: [PATCH 5/7] Single monitor fix --- src/modules/fancyzones/lib/Zone.cpp | 10 ++++-- .../tests/UnitTests/ZoneWindow.Spec.cpp | 35 ------------------- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/src/modules/fancyzones/lib/Zone.cpp b/src/modules/fancyzones/lib/Zone.cpp index 486963ce11f..6fc06e7846e 100644 --- a/src/modules/fancyzones/lib/Zone.cpp +++ b/src/modules/fancyzones/lib/Zone.cpp @@ -5,6 +5,8 @@ #include "Zone.h" #include "Settings.h" +#include "common/monitors.h" + struct Zone : winrt::implements { public: @@ -101,10 +103,12 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept const auto taskbar_left_size = std::abs(mi.rcMonitor.left - mi.rcWork.left); const auto taskbar_top_size = std::abs(mi.rcMonitor.top - mi.rcWork.top); OffsetRect(&newWindowRect, -taskbar_left_size, -taskbar_top_size); - if (accountForUnawareness) + + int monitorCount = MonitorInfo::GetMonitors(true).size(); + if (accountForUnawareness && monitorCount > 1) { - newWindowRect.left = max(mi.rcMonitor.left - leftMargin, newWindowRect.left); - newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size - rightMargin, newWindowRect.right); + newWindowRect.left = max(mi.rcMonitor.left, newWindowRect.left); + newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size, newWindowRect.right); newWindowRect.top = max(mi.rcMonitor.top, newWindowRect.top); newWindowRect.bottom = min(mi.rcMonitor.bottom - taskbar_top_size, newWindowRect.bottom); } diff --git a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp index 0d71afe9bee..bece6fd65a7 100644 --- a/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp +++ b/src/modules/fancyzones/tests/UnitTests/ZoneWindow.Spec.cpp @@ -650,40 +650,5 @@ namespace FancyZonesUnitTests const auto actual = m_fancyZonesData.GetAppZoneHistoryMap().at(processPath).zoneIndex; Assert::AreEqual(expected, actual); } - - TEST_METHOD (WhenPlacingDpiUnawareWindowIntoTheZoneItRectShouldBeExtendedToFitTargetZone) - { - m_zoneWindow = InitZoneWindowWithActiveZoneSet(); - Assert::IsNotNull(m_zoneWindow->ActiveZoneSet()); - - auto window = Mocks::WindowCreate(m_hInst); - - SetWindowPos(window, nullptr, 150, 150, 450, 550, SWP_SHOWWINDOW); - Assert::IsTrue(AreDpiAwarenessContextsEqual(DPI_AWARENESS_CONTEXT_UNAWARE, GetWindowDpiAwarenessContext(window))); - - RECT nonExtendedWindowRect{}; - ::GetWindowRect(window, &nonExtendedWindowRect); - - RECT extendedFrameRect{}; - Assert::IsTrue(SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &extendedFrameRect, sizeof(extendedFrameRect)))); - - int leftMargin = extendedFrameRect.left - nonExtendedWindowRect.left; - int rightMargin = extendedFrameRect.right - nonExtendedWindowRect.right; - int bottomMargin = extendedFrameRect.bottom - nonExtendedWindowRect.bottom; - ShowWindow(window, SW_SHOW); - - RECT zoneRect{ 0, 0, 250, 350 }; - auto zone = MakeZone(zoneRect); - m_zoneWindow->ActiveZoneSet()->AddZone(zone); - - m_zoneWindow->MoveWindowIntoZoneByDirection(window, VK_LEFT, true); - - RECT inZoneRect; - GetWindowRect(window, &inZoneRect); - Assert::AreEqual(-leftMargin, (int)inZoneRect.left); - Assert::AreEqual((int)zoneRect.right - rightMargin, (int)inZoneRect.right); - Assert::AreEqual((int)zoneRect.bottom - bottomMargin, (int)inZoneRect.bottom); - Assert::AreEqual(0, (int)inZoneRect.top); - } }; } From 8e5f78bf235851c27d1e679a7bc3a7c2b1fa7b59 Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:22:27 +0200 Subject: [PATCH 6/7] minor fix --- src/modules/fancyzones/lib/Zone.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/modules/fancyzones/lib/Zone.cpp b/src/modules/fancyzones/lib/Zone.cpp index 6fc06e7846e..7ff59414c74 100644 --- a/src/modules/fancyzones/lib/Zone.cpp +++ b/src/modules/fancyzones/lib/Zone.cpp @@ -80,15 +80,11 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept const auto level = DPIAware::GetAwarenessLevel(GetWindowDpiAwarenessContext(window)); const bool accountForUnawareness = level < DPIAware::PER_MONITOR_AWARE; - LONG leftMargin = 0; - LONG rightMargin = 0; - LONG bottomMargin = 0; - if (SUCCEEDED(DwmGetWindowAttribute(window, DWMWA_EXTENDED_FRAME_BOUNDS, &frameRect, sizeof(frameRect)))) { - leftMargin = frameRect.left - windowRect.left; - rightMargin = frameRect.right - windowRect.right; - bottomMargin = frameRect.bottom - windowRect.bottom; + LONG leftMargin = frameRect.left - windowRect.left; + LONG rightMargin = frameRect.right - windowRect.right; + LONG bottomMargin = frameRect.bottom - windowRect.bottom; newWindowRect.left -= leftMargin; newWindowRect.right -= rightMargin; newWindowRect.bottom -= bottomMargin; From 5ca0e74b793fc5a03163ba97a717b5c9492405e5 Mon Sep 17 00:00:00 2001 From: PrzemyslawTusinski <61138537+PrzemyslawTusinski@users.noreply.github.com> Date: Wed, 15 Apr 2020 11:49:14 +0200 Subject: [PATCH 7/7] Small refactor --- src/common/monitors.cpp | 5 +++++ src/common/monitors.h | 1 + src/modules/fancyzones/lib/Zone.cpp | 3 +-- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/common/monitors.cpp b/src/common/monitors.cpp index e3b4f766d81..c96a78c86e4 100644 --- a/src/common/monitors.cpp +++ b/src/common/monitors.cpp @@ -38,6 +38,11 @@ std::vector MonitorInfo::GetMonitors(bool include_toolbar) return monitors; } +int MonitorInfo::GetMonitorsCount() +{ + return GetMonitors(true).size(); +} + static BOOL CALLBACK get_primary_display_enum_cb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data) { MONITORINFOEX monitor_info; diff --git a/src/common/monitors.h b/src/common/monitors.h index 616656e737d..ebe77fabd2c 100644 --- a/src/common/monitors.h +++ b/src/common/monitors.h @@ -32,6 +32,7 @@ struct MonitorInfo : ScreenSize // Returns monitor rects ordered from left to right static std::vector GetMonitors(bool include_toolbar); + static int GetMonitorsCount(); // Return primary display static MonitorInfo GetPrimaryMonitor(); // Return monitor on which hwnd window is displayed diff --git a/src/modules/fancyzones/lib/Zone.cpp b/src/modules/fancyzones/lib/Zone.cpp index 7ff59414c74..4e9305f0346 100644 --- a/src/modules/fancyzones/lib/Zone.cpp +++ b/src/modules/fancyzones/lib/Zone.cpp @@ -100,8 +100,7 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept const auto taskbar_top_size = std::abs(mi.rcMonitor.top - mi.rcWork.top); OffsetRect(&newWindowRect, -taskbar_left_size, -taskbar_top_size); - int monitorCount = MonitorInfo::GetMonitors(true).size(); - if (accountForUnawareness && monitorCount > 1) + if (accountForUnawareness && MonitorInfo::GetMonitorsCount() > 1) { newWindowRect.left = max(mi.rcMonitor.left, newWindowRect.left); newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size, newWindowRect.right);