mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-05 10:46:33 +02:00
[FancyZones] Refactor Zone class (#7022)
* Pass zoneId on zone creaton and make it const Refactor IZone and Zone - make methods const and remove SetId * Update tests * Fix Grid layout zone order
This commit is contained in:
@@ -9,8 +9,6 @@
|
||||
#include "Settings.h"
|
||||
#include "util.h"
|
||||
|
||||
#include "common/monitors.h"
|
||||
|
||||
namespace
|
||||
{
|
||||
bool ValidateZoneRect(const RECT& rect)
|
||||
@@ -19,67 +17,68 @@ namespace
|
||||
int height = rect.bottom - rect.top;
|
||||
return rect.left >= 0 && rect.right >= 0 && rect.top >= 0 && rect.bottom >= 0 && width >= 0 && height >= 0;
|
||||
}
|
||||
|
||||
BOOL CALLBACK saveDisplayToVector(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
|
||||
{
|
||||
reinterpret_cast<std::vector<HMONITOR>*>(data)->emplace_back(monitor);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool allMonitorsHaveSameDpiScaling()
|
||||
{
|
||||
std::vector<HMONITOR> monitors;
|
||||
EnumDisplayMonitors(NULL, NULL, saveDisplayToVector, reinterpret_cast<LPARAM>(&monitors));
|
||||
|
||||
if (monitors.size() < 2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
UINT firstMonitorDpiX;
|
||||
UINT firstMonitorDpiY;
|
||||
|
||||
if (S_OK != GetDpiForMonitor(monitors[0], MDT_EFFECTIVE_DPI, &firstMonitorDpiX, &firstMonitorDpiY))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 1; i < monitors.size(); i++)
|
||||
{
|
||||
UINT iteratedMonitorDpiX;
|
||||
UINT iteratedMonitorDpiY;
|
||||
|
||||
if (S_OK != GetDpiForMonitor(monitors[i], MDT_EFFECTIVE_DPI, &iteratedMonitorDpiX, &iteratedMonitorDpiY) ||
|
||||
iteratedMonitorDpiX != firstMonitorDpiX)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
struct Zone : winrt::implements<Zone, IZone>
|
||||
{
|
||||
public:
|
||||
Zone(RECT zoneRect) :
|
||||
m_zoneRect(zoneRect)
|
||||
Zone(RECT zoneRect, const size_t zoneId) :
|
||||
m_zoneRect(zoneRect),
|
||||
m_id(zoneId)
|
||||
{
|
||||
}
|
||||
|
||||
IFACEMETHODIMP_(RECT) GetZoneRect() noexcept { return m_zoneRect; }
|
||||
IFACEMETHODIMP_(void) SetId(size_t id) noexcept { m_id = id; }
|
||||
IFACEMETHODIMP_(size_t) Id() noexcept { return m_id; }
|
||||
IFACEMETHODIMP_(RECT) ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept;
|
||||
IFACEMETHODIMP_(RECT) GetZoneRect() const noexcept { return m_zoneRect; }
|
||||
IFACEMETHODIMP_(size_t) Id() const noexcept { return m_id; }
|
||||
IFACEMETHODIMP_(RECT) ComputeActualZoneRect(HWND window, HWND zoneWindow) const noexcept;
|
||||
|
||||
private:
|
||||
RECT m_zoneRect{};
|
||||
size_t m_id{};
|
||||
const size_t m_id{};
|
||||
std::map<HWND, RECT> m_windows{};
|
||||
};
|
||||
|
||||
static BOOL CALLBACK saveDisplayToVector(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
|
||||
{
|
||||
reinterpret_cast<std::vector<HMONITOR>*>(data)->emplace_back(monitor);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool allMonitorsHaveSameDpiScaling()
|
||||
{
|
||||
std::vector<HMONITOR> monitors;
|
||||
EnumDisplayMonitors(NULL, NULL, saveDisplayToVector, reinterpret_cast<LPARAM>(&monitors));
|
||||
|
||||
if (monitors.size() < 2)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
UINT firstMonitorDpiX;
|
||||
UINT firstMonitorDpiY;
|
||||
|
||||
if (S_OK != GetDpiForMonitor(monitors[0], MDT_EFFECTIVE_DPI, &firstMonitorDpiX, &firstMonitorDpiY))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
for (int i = 1; i < monitors.size(); i++)
|
||||
{
|
||||
UINT iteratedMonitorDpiX;
|
||||
UINT iteratedMonitorDpiY;
|
||||
|
||||
if (S_OK != GetDpiForMonitor(monitors[i], MDT_EFFECTIVE_DPI, &iteratedMonitorDpiX, &iteratedMonitorDpiY) ||
|
||||
iteratedMonitorDpiX != firstMonitorDpiX)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept
|
||||
RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) const noexcept
|
||||
{
|
||||
// Take care of 1px border
|
||||
RECT newWindowRect = m_zoneRect;
|
||||
@@ -130,11 +129,11 @@ RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept
|
||||
return newWindowRect;
|
||||
}
|
||||
|
||||
winrt::com_ptr<IZone> MakeZone(const RECT& zoneRect) noexcept
|
||||
winrt::com_ptr<IZone> MakeZone(const RECT& zoneRect, const size_t zoneId) noexcept
|
||||
{
|
||||
if (ValidateZoneRect(zoneRect))
|
||||
if (ValidateZoneRect(zoneRect) && zoneId > 0)
|
||||
{
|
||||
return winrt::make_self<Zone>(zoneRect);
|
||||
return winrt::make_self<Zone>(zoneRect, zoneId);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user