2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
struct ZoneSet : winrt::implements<ZoneSet, IZoneSet>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ZoneSet(ZoneSetConfig const& config) : m_config(config)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ZoneSet(ZoneSetConfig const& config, std::vector<winrt::com_ptr<IZone>> zones) :
|
|
|
|
m_config(config),
|
|
|
|
m_zones(zones)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(GUID) Id() noexcept { return m_config.Id; }
|
|
|
|
IFACEMETHODIMP_(WORD) LayoutId() noexcept { return m_config.LayoutId; }
|
2019-11-19 07:29:42 +08:00
|
|
|
IFACEMETHODIMP AddZone(winrt::com_ptr<IZone> zone) noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
IFACEMETHODIMP_(winrt::com_ptr<IZone>) ZoneFromPoint(POINT pt) noexcept;
|
|
|
|
IFACEMETHODIMP_(int) GetZoneIndexFromWindow(HWND window) noexcept;
|
|
|
|
IFACEMETHODIMP_(std::vector<winrt::com_ptr<IZone>>) 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:
|
2019-11-19 07:29:42 +08:00
|
|
|
winrt::com_ptr<IZone> ZoneFromWindow(HWND window) noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
std::vector<winrt::com_ptr<IZone>> m_zones;
|
|
|
|
ZoneSetConfig m_config;
|
|
|
|
};
|
|
|
|
|
2019-11-19 07:29:42 +08:00
|
|
|
IFACEMETHODIMP ZoneSet::AddZone(winrt::com_ptr<IZone> zone) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-11-19 07:29:42 +08:00
|
|
|
m_zones.emplace_back(zone);
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
// 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<IZone>) ZoneSet::ZoneFromPoint(POINT pt) noexcept
|
|
|
|
{
|
2019-09-24 05:48:51 +08:00
|
|
|
winrt::com_ptr<IZone> smallestKnownZone = nullptr;
|
2019-09-27 01:47:44 +08:00
|
|
|
// To reduce redundant calculations, we will store the last known zones area.
|
|
|
|
int smallestKnownZoneArea = INT32_MAX;
|
2019-09-05 00:26:26 +08:00
|
|
|
for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++)
|
|
|
|
{
|
|
|
|
if (winrt::com_ptr<IZone> zone = iter->try_as<IZone>())
|
|
|
|
{
|
2019-09-24 05:48:51 +08:00
|
|
|
RECT* newZoneRect = &zone->GetZoneRect();
|
|
|
|
if (PtInRect(newZoneRect, pt))
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-09-27 01:47:44 +08:00
|
|
|
if (smallestKnownZone == nullptr)
|
2019-09-24 05:48:51 +08:00
|
|
|
{
|
|
|
|
smallestKnownZone = zone;
|
2019-09-27 01:47:44 +08:00
|
|
|
|
|
|
|
RECT* r = &smallestKnownZone->GetZoneRect();
|
|
|
|
smallestKnownZoneArea = (r->right-r->left)*(r->bottom-r->top);
|
2019-09-24 05:48:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int newZoneArea = (newZoneRect->right-newZoneRect->left)*(newZoneRect->bottom-newZoneRect->top);
|
|
|
|
|
2019-09-27 01:47:44 +08:00
|
|
|
if (newZoneArea<smallestKnownZoneArea)
|
|
|
|
{
|
2019-09-24 05:48:51 +08:00
|
|
|
smallestKnownZone = zone;
|
2019-09-27 01:47:44 +08:00
|
|
|
newZoneArea = smallestKnownZoneArea;
|
|
|
|
}
|
2019-09-24 05:48:51 +08:00
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-24 05:48:51 +08:00
|
|
|
|
|
|
|
return smallestKnownZone;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void) ZoneSet::Save() noexcept
|
|
|
|
{
|
|
|
|
size_t const zoneCount = m_zones.size();
|
|
|
|
if (zoneCount == 0)
|
|
|
|
{
|
|
|
|
RegistryHelpers::DeleteZoneSet(m_config.ResolutionKey, m_config.Id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ZoneSetPersistedData data{};
|
|
|
|
data.LayoutId = m_config.LayoutId;
|
|
|
|
data.ZoneCount = static_cast<DWORD>(zoneCount);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++)
|
|
|
|
{
|
|
|
|
winrt::com_ptr<IZone> zone = iter->as<IZone>();
|
|
|
|
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<BYTE*>(&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<IZone> zone = iter->try_as<IZone>())
|
|
|
|
{
|
|
|
|
if (zone->ContainsWindow(window))
|
|
|
|
{
|
|
|
|
return zoneIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void) ZoneSet::MoveWindowIntoZoneByIndex(HWND window, HWND windowZone, int index) noexcept
|
|
|
|
{
|
|
|
|
if (index >= static_cast<int>(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<IZone> oldZone;
|
|
|
|
winrt::com_ptr<IZone> 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<IZone>())
|
|
|
|
{
|
|
|
|
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<IZone>())
|
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:29:42 +08:00
|
|
|
winrt::com_ptr<IZone> ZoneSet::ZoneFromWindow(HWND window) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-11-19 07:29:42 +08:00
|
|
|
for (auto iter = m_zones.begin(); iter != m_zones.end(); iter++)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-11-19 07:29:42 +08:00
|
|
|
if (winrt::com_ptr<IZone> zone = iter->try_as<IZone>())
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-11-19 07:29:42 +08:00
|
|
|
if (zone->ContainsWindow(window))
|
|
|
|
{
|
|
|
|
return zone;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2019-11-19 07:29:42 +08:00
|
|
|
return nullptr;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
winrt::com_ptr<IZoneSet> MakeZoneSet(ZoneSetConfig const& config) noexcept
|
|
|
|
{
|
|
|
|
return winrt::make_self<ZoneSet>(config);
|
2019-09-24 05:48:51 +08:00
|
|
|
}
|