Skip to content

Commit

Permalink
[FancyZones] Refactor Zone class (#7022)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
stefansjfw committed Oct 14, 2020
1 parent 094136d commit d1372af
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 145 deletions.
97 changes: 48 additions & 49 deletions src/modules/fancyzones/lib/Zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "Settings.h"
#include "util.h"

#include "common/monitors.h"

namespace
{
bool ValidateZoneRect(const RECT& rect)
Expand All @@ -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;
}
}

struct Zone : winrt::implements<Zone, IZone>
{
public:
Zone(RECT zoneRect) :
m_zoneRect(zoneRect)
BOOL CALLBACK saveDisplayToVector(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
reinterpret_cast<std::vector<HMONITOR>*>(data)->emplace_back(monitor);
return true;
}

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;
bool allMonitorsHaveSameDpiScaling()
{
std::vector<HMONITOR> monitors;
EnumDisplayMonitors(NULL, NULL, saveDisplayToVector, reinterpret_cast<LPARAM>(&monitors));

private:
RECT m_zoneRect{};
size_t m_id{};
std::map<HWND, RECT> m_windows{};
};
if (monitors.size() < 2)
{
return true;
}

static BOOL CALLBACK saveDisplayToVector(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
reinterpret_cast<std::vector<HMONITOR>*>(data)->emplace_back(monitor);
return true;
}
UINT firstMonitorDpiX;
UINT firstMonitorDpiY;

bool allMonitorsHaveSameDpiScaling()
{
std::vector<HMONITOR> monitors;
EnumDisplayMonitors(NULL, NULL, saveDisplayToVector, reinterpret_cast<LPARAM>(&monitors));
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;
}
}

if (monitors.size() < 2)
{
return true;
}

UINT firstMonitorDpiX;
UINT firstMonitorDpiY;
}

if (S_OK != GetDpiForMonitor(monitors[0], MDT_EFFECTIVE_DPI, &firstMonitorDpiX, &firstMonitorDpiY))
struct Zone : winrt::implements<Zone, IZone>
{
public:
Zone(RECT zoneRect, const size_t zoneId) :
m_zoneRect(zoneRect),
m_id(zoneId)
{
return false;
}

for (int i = 1; i < monitors.size(); i++)
{
UINT iteratedMonitorDpiX;
UINT iteratedMonitorDpiY;
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;

if (S_OK != GetDpiForMonitor(monitors[i], MDT_EFFECTIVE_DPI, &iteratedMonitorDpiX, &iteratedMonitorDpiY) ||
iteratedMonitorDpiX != firstMonitorDpiX)
{
return false;
}
}

return true;
}
private:
RECT m_zoneRect{};
const size_t m_id{};
std::map<HWND, RECT> m_windows{};
};

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;
Expand Down Expand Up @@ -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
{
Expand Down
12 changes: 4 additions & 8 deletions src/modules/fancyzones/lib/Zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
11 changes: 4 additions & 7 deletions src/modules/fancyzones/lib/ZoneSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 5 additions & 6 deletions src/modules/fancyzones/tests/UnitTests/Zone.Spec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,25 @@ namespace FancyZonesUnitTests
public:
TEST_METHOD(TestCreateZone)
{
winrt::com_ptr<IZone> zone = MakeZone(m_zoneRect);
winrt::com_ptr<IZone> zone = MakeZone(m_zoneRect, 1);
Assert::IsNotNull(&zone);
CustomAssert::AreEqual(m_zoneRect, zone->GetZoneRect());
}

TEST_METHOD(TestCreateZoneZeroRect)
{
RECT zoneRect{ 0, 0, 0, 0 };
winrt::com_ptr<IZone> zone = MakeZone(zoneRect);
winrt::com_ptr<IZone> zone = MakeZone(zoneRect, 1);
Assert::IsNotNull(&zone);
CustomAssert::AreEqual(zoneRect, zone->GetZoneRect());
}

TEST_METHOD(GetSetId)
{
winrt::com_ptr<IZone> zone = MakeZone(m_zoneRect);
constexpr size_t zoneId = 123;
winrt::com_ptr<IZone> zone = MakeZone(m_zoneRect, zoneId);

constexpr size_t id = 10;
zone->SetId(id);
Assert::AreEqual(zone->Id(), id);
Assert::AreEqual(zone->Id(), zoneId);
}
};
}

0 comments on commit d1372af

Please sign in to comment.