#include "pch.h" #include "lib/ZoneSet.h" #include "lib/RegistryHelpers.h" struct ZoneSet : winrt::implements { public: ZoneSet(ZoneSetConfig const& config) : m_config(config) { } ZoneSet(ZoneSetConfig const& config, std::vector> zones) : m_config(config), m_zones(zones) { } IFACEMETHODIMP_(GUID) Id() noexcept { return m_config.Id; } IFACEMETHODIMP_(WORD) LayoutId() noexcept { return m_config.LayoutId; } IFACEMETHODIMP AddZone(winrt::com_ptr zone) noexcept; IFACEMETHODIMP_(winrt::com_ptr) ZoneFromPoint(POINT pt) noexcept; IFACEMETHODIMP_(int) GetZoneIndexFromWindow(HWND window) noexcept; IFACEMETHODIMP_(std::vector>) GetZones() noexcept { return m_zones; } IFACEMETHODIMP_(void) Save() noexcept; IFACEMETHODIMP_(void) MoveWindowIntoZoneByIndex(HWND window, HWND zoneWindow, int index) noexcept; IFACEMETHODIMP_(void) MoveWindowIntoZoneByDirection(HWND window, HWND zoneWindow, DWORD vkCode) noexcept; IFACEMETHODIMP_(void) MoveSizeEnd(HWND window, HWND zoneWindow, POINT ptClient) noexcept; private: winrt::com_ptr ZoneFromWindow(HWND window) noexcept; std::vector> m_zones; ZoneSetConfig m_config; }; IFACEMETHODIMP ZoneSet::AddZone(winrt::com_ptr zone) noexcept { m_zones.emplace_back(zone); // Important not to set Id 0 since we store it in the HWND using SetProp. // SetProp(0) doesn't really work. zone->SetId(m_zones.size()); return S_OK; } IFACEMETHODIMP_(winrt::com_ptr) ZoneSet::ZoneFromPoint(POINT pt) noexcept { winrt::com_ptr smallestKnownZone = nullptr; // To reduce redundant calculations, we will store the last known zones area. int smallestKnownZoneArea = INT32_MAX; for (auto iter = m_zones.rbegin(); iter != m_zones.rend(); iter++) { if (winrt::com_ptr zone = iter->try_as()) { RECT* newZoneRect = &zone->GetZoneRect(); if (PtInRect(newZoneRect, pt)) { if (smallestKnownZone == nullptr) { smallestKnownZone = zone; RECT* r = &smallestKnownZone->GetZoneRect(); smallestKnownZoneArea = (r->right-r->left)*(r->bottom-r->top); } else { int newZoneArea = (newZoneRect->right-newZoneRect->left)*(newZoneRect->bottom-newZoneRect->top); if (newZoneArea(zoneCount); int i = 0; for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++) { winrt::com_ptr zone = iter->as(); CopyRect(&data.Zones[i++], &zone->GetZoneRect()); } wil::unique_cotaskmem_string guid; if (SUCCEEDED_LOG(StringFromCLSID(m_config.Id, &guid))) { if (wil::unique_hkey hkey{ RegistryHelpers::CreateKey(m_config.ResolutionKey) }) { RegSetValueExW(hkey.get(), guid.get(), 0, REG_BINARY, reinterpret_cast(&data), sizeof(data)); } } } } IFACEMETHODIMP_(int) ZoneSet::GetZoneIndexFromWindow(HWND window) noexcept { int zoneIndex = 0; for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++, zoneIndex++) { if (winrt::com_ptr zone = iter->try_as()) { if (zone->ContainsWindow(window)) { return zoneIndex; } } } return -1; } IFACEMETHODIMP_(void) ZoneSet::MoveWindowIntoZoneByIndex(HWND window, HWND windowZone, int index) noexcept { if (index >= static_cast(m_zones.size())) { index = 0; } if (index < m_zones.size()) { if (auto zone = m_zones.at(index)) { zone->AddWindowToZone(window, windowZone, false); } } } IFACEMETHODIMP_(void) ZoneSet::MoveWindowIntoZoneByDirection(HWND window, HWND windowZone, DWORD vkCode) noexcept { winrt::com_ptr oldZone; winrt::com_ptr newZone; auto iter = std::find(m_zones.begin(), m_zones.end(), ZoneFromWindow(window)); if (iter == m_zones.end()) { iter = (vkCode == VK_RIGHT) ? m_zones.begin() : m_zones.end() - 1; } else if (oldZone = iter->as()) { if (vkCode == VK_LEFT) { if (iter == m_zones.begin()) { iter = m_zones.end(); } iter--; } else if (vkCode == VK_RIGHT) { iter++; if (iter == m_zones.end()) { iter = m_zones.begin(); } } } if (newZone = iter->as()) { if (oldZone) { oldZone->RemoveWindowFromZone(window, false); } newZone->AddWindowToZone(window, windowZone, true); } } IFACEMETHODIMP_(void) ZoneSet::MoveSizeEnd(HWND window, HWND zoneWindow, POINT ptClient) noexcept { if (auto zoneDrop = ZoneFromWindow(window)) { zoneDrop->RemoveWindowFromZone(window, !IsZoomed(window)); } if (auto zone = ZoneFromPoint(ptClient)) { zone->AddWindowToZone(window, zoneWindow, true); } } winrt::com_ptr ZoneSet::ZoneFromWindow(HWND window) noexcept { for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++) { if (winrt::com_ptr zone = iter->try_as()) { if (zone->ContainsWindow(window)) { return zone; } } } return nullptr; } winrt::com_ptr MakeZoneSet(ZoneSetConfig const& config) noexcept { return winrt::make_self(config); }