[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:
stefansjfw
2020-10-14 09:00:50 +02:00
committed by GitHub
parent 094136daec
commit d1372af581
6 changed files with 135 additions and 148 deletions

View File

@@ -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
{

View File

@@ -8,15 +8,11 @@ interface __declspec(uuid("{8228E934-B6EF-402A-9892-15A1441BF8B0}")) IZone : pub
/**
* @returns Zone coordinates (top-left and bottom-right corner) represented as RECT structure.
*/
IFACEMETHOD_(RECT, GetZoneRect)() = 0;
/**
* @param id Zone identifier.
*/
IFACEMETHOD_(void, SetId)(size_t id) = 0;
IFACEMETHOD_(RECT, GetZoneRect)() const = 0;
/**
* @returns Zone identifier.
*/
IFACEMETHOD_(size_t, Id)() = 0;
IFACEMETHOD_(size_t, Id)() const = 0;
/**
* Compute the coordinates of the rectangle to which a window should be resized.
*
@@ -25,8 +21,8 @@ interface __declspec(uuid("{8228E934-B6EF-402A-9892-15A1441BF8B0}")) IZone : pub
* current monitor desktop work area.
* @returns a RECT structure, describing global coordinates to which a window should be resized
*/
IFACEMETHOD_(RECT, ComputeActualZoneRect)(HWND window, HWND zoneWindow) = 0;
IFACEMETHOD_(RECT, ComputeActualZoneRect)(HWND window, HWND zoneWindow) const = 0;
};
winrt::com_ptr<IZone> MakeZone(const RECT& zoneRect) noexcept;
winrt::com_ptr<IZone> MakeZone(const RECT& zoneRect, const size_t zoneId) noexcept;

View File

@@ -178,9 +178,6 @@ IFACEMETHODIMP ZoneSet::AddZone(winrt::com_ptr<IZone> 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;
}
@@ -603,7 +600,7 @@ bool ZoneSet::CalculateFocusLayout(Rect workArea, int zoneCount) noexcept
for (int i = 0; i < zoneCount; i++)
{
auto zone = MakeZone(focusZoneRect);
auto zone = MakeZone(focusZoneRect, m_zones.size() + 1);
if (zone)
{
AddZone(zone);
@@ -660,7 +657,7 @@ bool ZoneSet::CalculateColumnsAndRowsLayout(Rect workArea, FancyZonesDataTypes::
}
auto zone = MakeZone(RECT{ left, top, right, bottom });
auto zone = MakeZone(RECT{ left, top, right, bottom }, m_zones.size() + 1);
if (zone)
{
AddZone(zone);
@@ -780,7 +777,7 @@ bool ZoneSet::CalculateCustomLayout(Rect workArea, int spacing) noexcept
DPIAware::Convert(m_config.Monitor, x, y);
DPIAware::Convert(m_config.Monitor, width, height);
auto zone = MakeZone(RECT{ x, y, x + width, y + height });
auto zone = MakeZone(RECT{ x, y, x + width, y + height }, m_zones.size() + 1);
if (zone)
{
AddZone(zone);
@@ -863,7 +860,7 @@ bool ZoneSet::CalculateGridZones(Rect workArea, FancyZonesDataTypes::GridLayoutI
long right = columnInfo[maxCol].End;
long bottom = rowInfo[maxRow].End;
auto zone = MakeZone(RECT{ left, top, right, bottom });
auto zone = MakeZone(RECT{ left, top, right, bottom }, m_zones.size() + 1);
if (zone)
{
AddZone(zone);