mirror of
https://github.com/microsoft/PowerToys.git
synced 2024-12-04 20:21:18 +08:00
[FancyZones] Fix blank layout error (#26828)
This commit is contained in:
parent
1b9094ae2b
commit
397178deec
@ -1236,7 +1236,7 @@ bool FancyZones::ShouldProcessSnapHotkey(DWORD vkCode) noexcept
|
||||
return false;
|
||||
}
|
||||
|
||||
if (layout->Type() != FancyZonesDataTypes::ZoneSetLayoutType::Blank)
|
||||
if (layout->Zones().size() > 0)
|
||||
{
|
||||
if (vkCode == VK_UP || vkCode == VK_DOWN)
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ namespace FancyZonesDataTypes
|
||||
{
|
||||
enum class ZoneSetLayoutType : int
|
||||
{
|
||||
Blank = -1,
|
||||
Blank,
|
||||
Focus,
|
||||
Columns,
|
||||
Rows,
|
||||
|
@ -108,15 +108,20 @@ Layout::Layout(const LayoutData& data) :
|
||||
|
||||
bool Layout::Init(const FancyZonesUtils::Rect& workArea, HMONITOR monitor) noexcept
|
||||
{
|
||||
//invalid work area
|
||||
// invalid work area
|
||||
if (workArea.width() == 0 || workArea.height() == 0)
|
||||
{
|
||||
Logger::error(L"Layout initialization: invalid work area");
|
||||
return false;
|
||||
}
|
||||
|
||||
//invalid zoneCount, may cause division by zero
|
||||
if (m_data.zoneCount <= 0 && m_data.type != FancyZonesDataTypes::ZoneSetLayoutType::Custom)
|
||||
// invalid zoneCount, may cause division by zero
|
||||
bool isGridType = m_data.type == FancyZonesDataTypes::ZoneSetLayoutType::Columns
|
||||
|| m_data.type == FancyZonesDataTypes::ZoneSetLayoutType::Rows
|
||||
|| m_data.type == FancyZonesDataTypes::ZoneSetLayoutType::Grid
|
||||
|| m_data.type == FancyZonesDataTypes::ZoneSetLayoutType::PriorityGrid;
|
||||
|
||||
if (m_data.zoneCount < 0 || (m_data.zoneCount == 0 && isGridType))
|
||||
{
|
||||
Logger::error(L"Layout initialization: invalid zone count");
|
||||
return false;
|
||||
@ -126,6 +131,9 @@ bool Layout::Init(const FancyZonesUtils::Rect& workArea, HMONITOR monitor) noexc
|
||||
|
||||
switch (m_data.type)
|
||||
{
|
||||
case FancyZonesDataTypes::ZoneSetLayoutType::Blank:
|
||||
m_zones = {};
|
||||
break;
|
||||
case FancyZonesDataTypes::ZoneSetLayoutType::Focus:
|
||||
m_zones = LayoutConfigurator::Focus(workArea, m_data.zoneCount);
|
||||
break;
|
||||
|
@ -234,6 +234,11 @@ ZonesMap LayoutConfigurator::Focus(FancyZonesUtils::Rect workArea, int zoneCount
|
||||
|
||||
ZonesMap LayoutConfigurator::Rows(FancyZonesUtils::Rect workArea, int zoneCount, int spacing) noexcept
|
||||
{
|
||||
if (zoneCount == 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
ZonesMap zones;
|
||||
|
||||
long totalWidth = workArea.width() - (spacing * 2);
|
||||
@ -275,6 +280,11 @@ ZonesMap LayoutConfigurator::Rows(FancyZonesUtils::Rect workArea, int zoneCount,
|
||||
|
||||
ZonesMap LayoutConfigurator::Columns(FancyZonesUtils::Rect workArea, int zoneCount, int spacing) noexcept
|
||||
{
|
||||
if (zoneCount == 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
ZonesMap zones;
|
||||
|
||||
long totalWidth = workArea.width() - (spacing * (zoneCount + 1));
|
||||
@ -316,6 +326,11 @@ ZonesMap LayoutConfigurator::Columns(FancyZonesUtils::Rect workArea, int zoneCou
|
||||
|
||||
ZonesMap LayoutConfigurator::Grid(FancyZonesUtils::Rect workArea, int zoneCount, int spacing) noexcept
|
||||
{
|
||||
if (zoneCount == 0)
|
||||
{
|
||||
return {};
|
||||
}
|
||||
|
||||
int rows = 1, columns = 1;
|
||||
while (zoneCount / rows >= rows)
|
||||
{
|
||||
|
@ -112,15 +112,15 @@ namespace FancyZonesUnitTests
|
||||
TEST_METHOD (ZoneSetLayoutTypeToString)
|
||||
{
|
||||
std::map<int, std::wstring> expectedMap = {
|
||||
std::make_pair(-2, L"TypeToString_ERROR"),
|
||||
std::make_pair(-1, L"blank"),
|
||||
std::make_pair(0, L"focus"),
|
||||
std::make_pair(1, L"columns"),
|
||||
std::make_pair(2, L"rows"),
|
||||
std::make_pair(3, L"grid"),
|
||||
std::make_pair(4, L"priority-grid"),
|
||||
std::make_pair(5, L"custom"),
|
||||
std::make_pair(6, L"TypeToString_ERROR"),
|
||||
std::make_pair(-1, L"TypeToString_ERROR"),
|
||||
std::make_pair(0, L"blank"),
|
||||
std::make_pair(1, L"focus"),
|
||||
std::make_pair(2, L"columns"),
|
||||
std::make_pair(3, L"rows"),
|
||||
std::make_pair(4, L"grid"),
|
||||
std::make_pair(5, L"priority-grid"),
|
||||
std::make_pair(6, L"custom"),
|
||||
std::make_pair(7, L"TypeToString_ERROR"),
|
||||
};
|
||||
|
||||
for (const auto& expected : expectedMap)
|
||||
|
@ -382,7 +382,7 @@ namespace FancyZonesUnitTests
|
||||
|
||||
TEST_METHOD (ZeroZoneCount)
|
||||
{
|
||||
for (int type = static_cast<int>(ZoneSetLayoutType::Focus); type < static_cast<int>(ZoneSetLayoutType::Custom); type++)
|
||||
for (int type = static_cast<int>(ZoneSetLayoutType::Columns); type < static_cast<int>(ZoneSetLayoutType::Custom); type++)
|
||||
{
|
||||
LayoutData data = m_data;
|
||||
data.type = static_cast<ZoneSetLayoutType>(type);
|
||||
@ -395,6 +395,32 @@ namespace FancyZonesUnitTests
|
||||
Assert::IsFalse(result);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
LayoutData data = m_data;
|
||||
data.type = static_cast<ZoneSetLayoutType>(ZoneSetLayoutType::Blank);
|
||||
data.zoneCount = 0;
|
||||
auto layout = std::make_unique<Layout>(data);
|
||||
|
||||
for (const auto& rect : m_workAreaRects)
|
||||
{
|
||||
auto result = layout->Init(rect, Mocks::Monitor());
|
||||
Assert::IsTrue(result);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
LayoutData data = m_data;
|
||||
data.type = static_cast<ZoneSetLayoutType>(ZoneSetLayoutType::Focus);
|
||||
data.zoneCount = 0;
|
||||
auto layout = std::make_unique<Layout>(data);
|
||||
|
||||
for (const auto& rect : m_workAreaRects)
|
||||
{
|
||||
auto result = layout->Init(rect, Mocks::Monitor());
|
||||
Assert::IsTrue(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_METHOD (BigZoneCount)
|
||||
|
Loading…
Reference in New Issue
Block a user