2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
#include "ZoneSet.h"
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
#include "FancyZonesData.h"
|
|
|
|
#include "FancyZonesDataTypes.h"
|
2021-08-11 22:05:03 +08:00
|
|
|
#include "FancyZonesWindowProperties.h"
|
2020-10-16 21:25:30 +08:00
|
|
|
#include "Settings.h"
|
|
|
|
#include "Zone.h"
|
|
|
|
#include "util.h"
|
2019-12-17 16:21:46 +08:00
|
|
|
|
2021-02-08 17:48:11 +08:00
|
|
|
#include <common/logger/logger.h>
|
2020-12-15 20:16:09 +08:00
|
|
|
#include <common/display/dpi_aware.h>
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
2020-07-22 16:39:13 +08:00
|
|
|
#include <utility>
|
|
|
|
|
2020-08-25 01:38:15 +08:00
|
|
|
using namespace FancyZonesUtils;
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
constexpr int C_MULTIPLIER = 10000;
|
2021-06-16 22:49:29 +08:00
|
|
|
constexpr int OVERLAPPING_CENTERS_SENSITIVITY = 75;
|
2020-10-16 21:25:30 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
// PriorityGrid layout is unique for zoneCount <= 11. For zoneCount > 11 PriorityGrid is same as Grid
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo predefinedPriorityGridLayouts[11] = {
|
2020-02-10 21:59:51 +08:00
|
|
|
/* 1 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 1,
|
|
|
|
.columns = 1,
|
|
|
|
.rowsPercents = { 10000 },
|
|
|
|
.columnsPercents = { 10000 },
|
|
|
|
.cellChildMap = { { 0 } } }),
|
|
|
|
/* 2 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 1,
|
|
|
|
.columns = 2,
|
|
|
|
.rowsPercents = { 10000 },
|
|
|
|
.columnsPercents = { 6667, 3333 },
|
|
|
|
.cellChildMap = { { 0, 1 } } }),
|
|
|
|
/* 3 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 1,
|
|
|
|
.columns = 3,
|
|
|
|
.rowsPercents = { 10000 },
|
|
|
|
.columnsPercents = { 2500, 5000, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2 } } }),
|
|
|
|
/* 4 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 2,
|
|
|
|
.columns = 3,
|
|
|
|
.rowsPercents = { 5000, 5000 },
|
|
|
|
.columnsPercents = { 2500, 5000, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2 }, { 0, 1, 3 } } }),
|
|
|
|
/* 5 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 2,
|
|
|
|
.columns = 3,
|
|
|
|
.rowsPercents = { 5000, 5000 },
|
|
|
|
.columnsPercents = { 2500, 5000, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2 }, { 3, 1, 4 } } }),
|
|
|
|
/* 6 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 3,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 5000, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2 }, { 0, 1, 3 }, { 4, 1, 5 } } }),
|
|
|
|
/* 7 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 3,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 5000, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2 }, { 3, 1, 4 }, { 5, 1, 6 } } }),
|
|
|
|
/* 8 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 4,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 2500, 2500, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2, 3 }, { 4, 1, 2, 5 }, { 6, 1, 2, 7 } } }),
|
|
|
|
/* 9 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 4,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 2500, 2500, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2, 3 }, { 4, 1, 2, 5 }, { 6, 1, 7, 8 } } }),
|
|
|
|
/* 10 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 4,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 2500, 2500, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2, 3 }, { 4, 1, 5, 6 }, { 7, 1, 8, 9 } } }),
|
|
|
|
/* 11 */
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Full{
|
2020-02-10 21:59:51 +08:00
|
|
|
.rows = 3,
|
|
|
|
.columns = 4,
|
|
|
|
.rowsPercents = { 3333, 3334, 3333 },
|
|
|
|
.columnsPercents = { 2500, 2500, 2500, 2500 },
|
|
|
|
.cellChildMap = { { 0, 1, 2, 3 }, { 4, 1, 5, 6 }, { 7, 8, 9, 10 } } }),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
struct ZoneSet : winrt::implements<ZoneSet, IZoneSet>
|
|
|
|
{
|
|
|
|
public:
|
2020-02-10 21:59:51 +08:00
|
|
|
ZoneSet(ZoneSetConfig const& config) :
|
|
|
|
m_config(config)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
ZoneSet(ZoneSetConfig const& config, ZonesMap zones) :
|
2019-09-05 00:26:26 +08:00
|
|
|
m_config(config),
|
|
|
|
m_zones(zones)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(GUID)
|
2020-10-16 21:25:30 +08:00
|
|
|
Id() const noexcept { return m_config.Id; }
|
2020-07-22 16:39:13 +08:00
|
|
|
IFACEMETHODIMP_(FancyZonesDataTypes::ZoneSetLayoutType)
|
2020-10-16 21:25:30 +08:00
|
|
|
LayoutType() const noexcept { return m_config.LayoutType; }
|
2019-11-19 07:29:42 +08:00
|
|
|
IFACEMETHODIMP AddZone(winrt::com_ptr<IZone> zone) noexcept;
|
2021-08-11 22:05:03 +08:00
|
|
|
IFACEMETHODIMP_(ZoneIndexSet) ZonesFromPoint(POINT pt) const noexcept;
|
|
|
|
IFACEMETHODIMP_(ZoneIndexSet) GetZoneIndexSetFromWindow(HWND window) const noexcept;
|
|
|
|
IFACEMETHODIMP_(ZonesMap) GetZones()const noexcept override { return m_zones; }
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2021-08-11 22:05:03 +08:00
|
|
|
MoveWindowIntoZoneByIndex(HWND window, HWND workAreaWindow, ZoneIndex index) noexcept;
|
2020-04-10 22:09:08 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2021-08-11 22:05:03 +08:00
|
|
|
MoveWindowIntoZoneByIndexSet(HWND window, HWND workAreaWindow, const ZoneIndexSet& indexSet) noexcept;
|
2020-03-25 01:50:26 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByDirectionAndIndex(HWND window, HWND workAreaWindow, DWORD vkCode, bool cycle) noexcept;
|
2020-08-21 18:53:03 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByDirectionAndPosition(HWND window, HWND workAreaWindow, DWORD vkCode, bool cycle) noexcept;
|
2020-09-11 17:32:45 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
ExtendWindowByDirectionAndPosition(HWND window, HWND workAreaWindow, DWORD vkCode) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByPoint(HWND window, HWND workAreaWindow, POINT ptClient) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-07 16:06:25 +08:00
|
|
|
CalculateZones(RECT workArea, int zoneCount, int spacing) noexcept;
|
2021-08-11 22:05:03 +08:00
|
|
|
IFACEMETHODIMP_(bool) IsZoneEmpty(ZoneIndex zoneIndex) const noexcept;
|
|
|
|
IFACEMETHODIMP_(ZoneIndexSet) GetCombinedZoneRange(const ZoneIndexSet& initialZones, const ZoneIndexSet& finalZones) const noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
private:
|
2020-02-10 21:59:51 +08:00
|
|
|
bool CalculateFocusLayout(Rect workArea, int zoneCount) noexcept;
|
2020-07-22 16:39:13 +08:00
|
|
|
bool CalculateColumnsAndRowsLayout(Rect workArea, FancyZonesDataTypes::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept;
|
|
|
|
bool CalculateGridLayout(Rect workArea, FancyZonesDataTypes::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
bool CalculateUniquePriorityGridLayout(Rect workArea, int zoneCount, int spacing) noexcept;
|
|
|
|
bool CalculateCustomLayout(Rect workArea, int spacing) noexcept;
|
2020-07-22 16:39:13 +08:00
|
|
|
bool CalculateGridZones(Rect workArea, FancyZonesDataTypes::GridLayoutInfo gridLayoutInfo, int spacing);
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSelectSubregion(const ZoneIndexSet& capturedZones, POINT pt) const;
|
|
|
|
ZoneIndexSet ZoneSelectClosestCenter(const ZoneIndexSet& capturedZones, POINT pt) const;
|
2021-02-08 17:48:11 +08:00
|
|
|
|
|
|
|
// `compare` should return true if the first argument is a better choice than the second argument.
|
|
|
|
template<class CompareF>
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSelectPriority(const ZoneIndexSet& capturedZones, CompareF compare) const;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
ZonesMap m_zones;
|
2021-08-11 22:05:03 +08:00
|
|
|
std::map<HWND, ZoneIndexSet> m_windowIndexSet;
|
2020-09-11 17:32:45 +08:00
|
|
|
|
|
|
|
// Needed for ExtendWindowByDirectionAndPosition
|
2021-08-11 22:05:03 +08:00
|
|
|
std::map<HWND, ZoneIndexSet> m_windowInitialIndexSet;
|
|
|
|
std::map<HWND, ZoneIndex> m_windowFinalIndex;
|
2020-09-11 17:32:45 +08:00
|
|
|
bool m_inExtendWindow = false;
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
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
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
auto zoneId = zone->Id();
|
|
|
|
if (m_zones.contains(zoneId))
|
|
|
|
{
|
|
|
|
return S_FALSE;
|
|
|
|
}
|
|
|
|
m_zones[zoneId] = zone;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
IFACEMETHODIMP_(ZoneIndexSet)
|
2020-10-16 21:25:30 +08:00
|
|
|
ZoneSet::ZonesFromPoint(POINT pt) const noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet capturedZones;
|
|
|
|
ZoneIndexSet strictlyCapturedZones;
|
2020-10-26 16:07:11 +08:00
|
|
|
for (const auto& [zoneId, zone] : m_zones)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
const RECT& zoneRect = zone->GetZoneRect();
|
2020-10-16 21:25:30 +08:00
|
|
|
if (zoneRect.left - m_config.SensitivityRadius <= pt.x && pt.x <= zoneRect.right + m_config.SensitivityRadius &&
|
|
|
|
zoneRect.top - m_config.SensitivityRadius <= pt.y && pt.y <= zoneRect.bottom + m_config.SensitivityRadius)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
capturedZones.emplace_back(zoneId);
|
2020-10-16 21:25:30 +08:00
|
|
|
}
|
2020-04-10 22:09:08 +08:00
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
if (zoneRect.left <= pt.x && pt.x < zoneRect.right &&
|
|
|
|
zoneRect.top <= pt.y && pt.y < zoneRect.bottom)
|
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
strictlyCapturedZones.emplace_back(zoneId);
|
2020-04-10 22:09:08 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-27 01:47:44 +08:00
|
|
|
|
2020-04-10 22:09:08 +08:00
|
|
|
// If only one zone is captured, but it's not strictly captured
|
|
|
|
// don't consider it as captured
|
|
|
|
if (capturedZones.size() == 1 && strictlyCapturedZones.size() == 0)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
2019-09-24 05:48:51 +08:00
|
|
|
|
2020-04-10 22:09:08 +08:00
|
|
|
// If captured zones do not overlap, return all of them
|
2021-02-08 17:48:11 +08:00
|
|
|
// Otherwise, return one of them based on the chosen selection algorithm.
|
2020-04-10 22:09:08 +08:00
|
|
|
bool overlap = false;
|
|
|
|
for (size_t i = 0; i < capturedZones.size(); ++i)
|
|
|
|
{
|
|
|
|
for (size_t j = i + 1; j < capturedZones.size(); ++j)
|
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
RECT rectI;
|
|
|
|
RECT rectJ;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
rectI = m_zones.at(capturedZones[i])->GetZoneRect();
|
|
|
|
rectJ = m_zones.at(capturedZones[j])->GetZoneRect();
|
|
|
|
}
|
|
|
|
catch (std::out_of_range)
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
if (max(rectI.top, rectJ.top) + m_config.SensitivityRadius < min(rectI.bottom, rectJ.bottom) &&
|
|
|
|
max(rectI.left, rectJ.left) + m_config.SensitivityRadius < min(rectI.right, rectJ.right))
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
|
|
|
overlap = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-16 21:25:30 +08:00
|
|
|
if (overlap)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2020-04-10 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (overlap)
|
|
|
|
{
|
2021-02-08 17:48:11 +08:00
|
|
|
try
|
|
|
|
{
|
2021-07-07 18:18:52 +08:00
|
|
|
using Algorithm = OverlappingZonesAlgorithm;
|
2021-02-25 23:23:05 +08:00
|
|
|
|
2021-02-08 17:48:11 +08:00
|
|
|
switch (m_config.SelectionAlgorithm)
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
2021-02-25 23:23:05 +08:00
|
|
|
case Algorithm::Smallest:
|
2021-06-22 16:34:27 +08:00
|
|
|
return ZoneSelectPriority(capturedZones, [&](auto zone1, auto zone2) { return zone1->GetZoneArea() < zone2->GetZoneArea(); });
|
2021-02-25 23:23:05 +08:00
|
|
|
case Algorithm::Largest:
|
2021-06-22 16:34:27 +08:00
|
|
|
return ZoneSelectPriority(capturedZones, [&](auto zone1, auto zone2) { return zone1->GetZoneArea() > zone2->GetZoneArea(); });
|
2021-02-25 23:23:05 +08:00
|
|
|
case Algorithm::Positional:
|
|
|
|
return ZoneSelectSubregion(capturedZones, pt);
|
2021-06-16 22:49:29 +08:00
|
|
|
case Algorithm::ClosestCenter:
|
2021-06-22 16:34:27 +08:00
|
|
|
return ZoneSelectClosestCenter(capturedZones, pt);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 17:48:11 +08:00
|
|
|
catch (std::out_of_range)
|
|
|
|
{
|
|
|
|
Logger::error("Exception out_of_range was thrown in ZoneSet::ZonesFromPoint");
|
|
|
|
return { capturedZones[0] };
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-09-24 05:48:51 +08:00
|
|
|
|
2020-04-10 22:09:08 +08:00
|
|
|
return capturedZones;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSet::GetZoneIndexSetFromWindow(HWND window) const noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-05-26 22:01:12 +08:00
|
|
|
auto it = m_windowIndexSet.find(window);
|
|
|
|
if (it == m_windowIndexSet.end())
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-05-26 22:01:12 +08:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return it->second;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneSet::MoveWindowIntoZoneByIndex(HWND window, HWND workAreaWindow, ZoneIndex index) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByIndexSet(window, workAreaWindow, { index });
|
2020-04-10 22:09:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneSet::MoveWindowIntoZoneByIndexSet(HWND window, HWND workAreaWindow, const ZoneIndexSet& zoneIds) noexcept
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
|
|
|
if (m_zones.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:32:45 +08:00
|
|
|
// Always clear the info related to SelectManyZones if it's not being used
|
|
|
|
if (!m_inExtendWindow)
|
|
|
|
{
|
|
|
|
m_windowFinalIndex.erase(window);
|
|
|
|
m_windowInitialIndexSet.erase(window);
|
|
|
|
}
|
|
|
|
|
2020-04-10 22:09:08 +08:00
|
|
|
RECT size;
|
|
|
|
bool sizeEmpty = true;
|
2021-08-11 22:05:03 +08:00
|
|
|
Bitmask bitmask = 0;
|
2020-05-26 22:01:12 +08:00
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
m_windowIndexSet[window] = {};
|
2020-04-10 22:09:08 +08:00
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
for (ZoneIndex id : zoneIds)
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
if (m_zones.contains(id))
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
const auto& zone = m_zones.at(id);
|
|
|
|
const RECT newSize = zone->ComputeActualZoneRect(window, workAreaWindow);
|
2020-04-10 22:09:08 +08:00
|
|
|
if (!sizeEmpty)
|
|
|
|
{
|
|
|
|
size.left = min(size.left, newSize.left);
|
|
|
|
size.top = min(size.top, newSize.top);
|
|
|
|
size.right = max(size.right, newSize.right);
|
|
|
|
size.bottom = max(size.bottom, newSize.bottom);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size = newSize;
|
|
|
|
sizeEmpty = false;
|
|
|
|
}
|
2020-05-26 22:01:12 +08:00
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
m_windowIndexSet[window].push_back(id);
|
2020-05-26 22:01:12 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
if (id < std::numeric_limits<ZoneIndex>::digits)
|
2020-05-26 22:01:12 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
bitmask |= 1ull << id;
|
2020-04-10 22:09:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!sizeEmpty)
|
|
|
|
{
|
2020-07-01 21:36:05 +08:00
|
|
|
SaveWindowSizeAndOrigin(window);
|
2020-04-10 22:09:08 +08:00
|
|
|
SizeWindowToRect(window, size);
|
2020-06-01 22:14:29 +08:00
|
|
|
StampWindow(window, bitmask);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 01:50:26 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
ZoneSet::MoveWindowIntoZoneByDirectionAndIndex(HWND window, HWND workAreaWindow, DWORD vkCode, bool cycle) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
if (m_zones.empty())
|
|
|
|
{
|
2020-03-25 01:50:26 +08:00
|
|
|
return false;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-26 22:01:12 +08:00
|
|
|
auto indexSet = GetZoneIndexSetFromWindow(window);
|
2021-08-11 22:05:03 +08:00
|
|
|
auto numZones = m_zones.size();
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-05-26 22:01:12 +08:00
|
|
|
// The window was not assigned to any zone here
|
|
|
|
if (indexSet.size() == 0)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, vkCode == VK_LEFT ? numZones - 1 : 0);
|
2020-05-26 22:01:12 +08:00
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-05-26 22:01:12 +08:00
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndex oldId = indexSet[0];
|
2020-05-26 22:01:12 +08:00
|
|
|
|
|
|
|
// We reached the edge
|
2020-10-26 16:07:11 +08:00
|
|
|
if ((vkCode == VK_LEFT && oldId == 0) || (vkCode == VK_RIGHT && oldId == numZones - 1))
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-05-26 22:01:12 +08:00
|
|
|
if (!cycle)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByIndexSet(window, workAreaWindow, {});
|
2020-05-26 22:01:12 +08:00
|
|
|
return false;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-05-26 22:01:12 +08:00
|
|
|
else
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, vkCode == VK_LEFT ? numZones - 1 : 0);
|
2020-05-26 22:01:12 +08:00
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-26 22:01:12 +08:00
|
|
|
// We didn't reach the edge
|
|
|
|
if (vkCode == VK_LEFT)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, oldId - 1);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-05-26 22:01:12 +08:00
|
|
|
else
|
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, oldId + 1);
|
2020-05-26 22:01:12 +08:00
|
|
|
}
|
|
|
|
return true;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-08-21 18:53:03 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
ZoneSet::MoveWindowIntoZoneByDirectionAndPosition(HWND window, HWND workAreaWindow, DWORD vkCode, bool cycle) noexcept
|
2020-08-21 18:53:03 +08:00
|
|
|
{
|
|
|
|
if (m_zones.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
std::vector<bool> usedZoneIndices(m_zones.size(), false);
|
2021-08-11 22:05:03 +08:00
|
|
|
for (ZoneIndex id : GetZoneIndexSetFromWindow(window))
|
2020-08-21 18:53:03 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
usedZoneIndices[id] = true;
|
2020-08-21 18:53:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<RECT> zoneRects;
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet freeZoneIndices;
|
2020-08-21 18:53:03 +08:00
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
for (const auto& [zoneId, zone] : m_zones)
|
2020-08-21 18:53:03 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
if (!usedZoneIndices[zoneId])
|
2020-08-21 18:53:03 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
zoneRects.emplace_back(m_zones[zoneId]->GetZoneRect());
|
|
|
|
freeZoneIndices.emplace_back(zoneId);
|
2020-08-21 18:53:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RECT windowRect, windowZoneRect;
|
2020-10-16 21:25:30 +08:00
|
|
|
if (GetWindowRect(window, &windowRect) && GetWindowRect(workAreaWindow, &windowZoneRect))
|
2020-08-21 18:53:03 +08:00
|
|
|
{
|
|
|
|
// Move to coordinates relative to windowZone
|
|
|
|
windowRect.top -= windowZoneRect.top;
|
|
|
|
windowRect.bottom -= windowZoneRect.top;
|
|
|
|
windowRect.left -= windowZoneRect.left;
|
|
|
|
windowRect.right -= windowZoneRect.left;
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
auto result = FancyZonesUtils::ChooseNextZoneByPosition(vkCode, windowRect, zoneRects);
|
2020-08-21 18:53:03 +08:00
|
|
|
if (result < zoneRects.size())
|
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, freeZoneIndices[result]);
|
2020-08-21 18:53:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (cycle)
|
|
|
|
{
|
|
|
|
// Try again from the position off the screen in the opposite direction to vkCode
|
2020-09-11 17:32:45 +08:00
|
|
|
// Consider all zones as available
|
2020-10-16 21:25:30 +08:00
|
|
|
zoneRects.resize(m_zones.size());
|
2020-10-26 16:07:11 +08:00
|
|
|
std::transform(m_zones.begin(), m_zones.end(), zoneRects.begin(), [](auto zone) { return zone.second->GetZoneRect(); });
|
2020-08-25 01:38:15 +08:00
|
|
|
windowRect = FancyZonesUtils::PrepareRectForCycling(windowRect, windowZoneRect, vkCode);
|
|
|
|
result = FancyZonesUtils::ChooseNextZoneByPosition(vkCode, windowRect, zoneRects);
|
2020-08-21 18:53:03 +08:00
|
|
|
|
|
|
|
if (result < zoneRects.size())
|
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, workAreaWindow, result);
|
2020-08-21 18:53:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 17:32:45 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-10-16 21:25:30 +08:00
|
|
|
ZoneSet::ExtendWindowByDirectionAndPosition(HWND window, HWND workAreaWindow, DWORD vkCode) noexcept
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
|
|
|
if (m_zones.empty())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RECT windowRect, windowZoneRect;
|
2020-10-16 21:25:30 +08:00
|
|
|
if (GetWindowRect(window, &windowRect) && GetWindowRect(workAreaWindow, &windowZoneRect))
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
|
|
|
auto oldZones = GetZoneIndexSetFromWindow(window);
|
2020-10-16 21:25:30 +08:00
|
|
|
std::vector<bool> usedZoneIndices(m_zones.size(), false);
|
2020-09-11 17:32:45 +08:00
|
|
|
std::vector<RECT> zoneRects;
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet freeZoneIndices;
|
2020-09-11 17:32:45 +08:00
|
|
|
|
|
|
|
// If selectManyZones = true for the second time, use the last zone into which we moved
|
|
|
|
// instead of the window rect and enable moving to all zones except the old one
|
|
|
|
auto finalIndexIt = m_windowFinalIndex.find(window);
|
|
|
|
if (finalIndexIt != m_windowFinalIndex.end())
|
|
|
|
{
|
|
|
|
usedZoneIndices[finalIndexIt->second] = true;
|
2020-10-16 21:25:30 +08:00
|
|
|
windowRect = m_zones[finalIndexIt->second]->GetZoneRect();
|
2020-09-11 17:32:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-08-11 22:05:03 +08:00
|
|
|
for (ZoneIndex idx : oldZones)
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
|
|
|
usedZoneIndices[idx] = true;
|
|
|
|
}
|
|
|
|
// Move to coordinates relative to windowZone
|
|
|
|
windowRect.top -= windowZoneRect.top;
|
|
|
|
windowRect.bottom -= windowZoneRect.top;
|
|
|
|
windowRect.left -= windowZoneRect.left;
|
|
|
|
windowRect.right -= windowZoneRect.left;
|
|
|
|
}
|
|
|
|
|
2020-10-16 21:25:30 +08:00
|
|
|
for (size_t i = 0; i < m_zones.size(); i++)
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
|
|
|
if (!usedZoneIndices[i])
|
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
zoneRects.emplace_back(m_zones[i]->GetZoneRect());
|
2020-09-11 17:32:45 +08:00
|
|
|
freeZoneIndices.emplace_back(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
auto result = FancyZonesUtils::ChooseNextZoneByPosition(vkCode, windowRect, zoneRects);
|
2020-09-11 17:32:45 +08:00
|
|
|
if (result < zoneRects.size())
|
|
|
|
{
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndex targetZone = freeZoneIndices[result];
|
|
|
|
ZoneIndexSet resultIndexSet;
|
2020-09-11 17:32:45 +08:00
|
|
|
|
|
|
|
// First time with selectManyZones = true for this window?
|
|
|
|
if (finalIndexIt == m_windowFinalIndex.end())
|
|
|
|
{
|
|
|
|
// Already zoned?
|
|
|
|
if (oldZones.size())
|
|
|
|
{
|
|
|
|
m_windowInitialIndexSet[window] = oldZones;
|
|
|
|
m_windowFinalIndex[window] = targetZone;
|
|
|
|
resultIndexSet = GetCombinedZoneRange(oldZones, { targetZone });
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_windowInitialIndexSet[window] = { targetZone };
|
|
|
|
m_windowFinalIndex[window] = targetZone;
|
|
|
|
resultIndexSet = { targetZone };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto deletethis = m_windowInitialIndexSet[window];
|
|
|
|
m_windowFinalIndex[window] = targetZone;
|
|
|
|
resultIndexSet = GetCombinedZoneRange(m_windowInitialIndexSet[window], { targetZone });
|
|
|
|
}
|
|
|
|
|
|
|
|
m_inExtendWindow = true;
|
2020-10-16 21:25:30 +08:00
|
|
|
MoveWindowIntoZoneByIndexSet(window, workAreaWindow, resultIndexSet);
|
2020-09-11 17:32:45 +08:00
|
|
|
m_inExtendWindow = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-10-16 21:25:30 +08:00
|
|
|
ZoneSet::MoveWindowIntoZoneByPoint(HWND window, HWND workAreaWindow, POINT ptClient) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-10-16 21:25:30 +08:00
|
|
|
const auto& zones = ZonesFromPoint(ptClient);
|
|
|
|
MoveWindowIntoZoneByIndexSet(window, workAreaWindow, zones);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-07 16:06:25 +08:00
|
|
|
ZoneSet::CalculateZones(RECT workAreaRect, int zoneCount, int spacing) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-08-07 16:06:25 +08:00
|
|
|
Rect workArea(workAreaRect);
|
2020-02-10 21:59:51 +08:00
|
|
|
//invalid work area
|
|
|
|
if (workArea.width() == 0 || workArea.height() == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//invalid zoneCount, may cause division by zero
|
2020-07-22 16:39:13 +08:00
|
|
|
if (zoneCount <= 0 && m_config.LayoutType != FancyZonesDataTypes::ZoneSetLayoutType::Custom)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool success = true;
|
|
|
|
switch (m_config.LayoutType)
|
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::Focus:
|
2020-02-10 21:59:51 +08:00
|
|
|
success = CalculateFocusLayout(workArea, zoneCount);
|
|
|
|
break;
|
2020-07-22 16:39:13 +08:00
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::Columns:
|
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::Rows:
|
2020-02-10 21:59:51 +08:00
|
|
|
success = CalculateColumnsAndRowsLayout(workArea, m_config.LayoutType, zoneCount, spacing);
|
|
|
|
break;
|
2020-07-22 16:39:13 +08:00
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::Grid:
|
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::PriorityGrid:
|
2020-02-10 21:59:51 +08:00
|
|
|
success = CalculateGridLayout(workArea, m_config.LayoutType, zoneCount, spacing);
|
|
|
|
break;
|
2020-07-22 16:39:13 +08:00
|
|
|
case FancyZonesDataTypes::ZoneSetLayoutType::Custom:
|
2020-02-10 21:59:51 +08:00
|
|
|
success = CalculateCustomLayout(workArea, spacing);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
bool ZoneSet::IsZoneEmpty(ZoneIndex zoneIndex) const noexcept
|
2020-05-26 22:01:12 +08:00
|
|
|
{
|
|
|
|
for (auto& [window, zones] : m_windowIndexSet)
|
|
|
|
{
|
|
|
|
if (find(begin(zones), end(zones), zoneIndex) != end(zones))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
bool ZoneSet::CalculateFocusLayout(Rect workArea, int zoneCount) noexcept
|
|
|
|
{
|
2020-08-06 22:41:15 +08:00
|
|
|
long left{ 100 };
|
|
|
|
long top{ 100 };
|
|
|
|
long right{ left + long(workArea.width() * 0.4) };
|
|
|
|
long bottom{ top + long(workArea.height() * 0.4) };
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
RECT focusZoneRect{ left, top, right, bottom };
|
|
|
|
|
2020-08-06 22:41:15 +08:00
|
|
|
long focusRectXIncrement = (zoneCount <= 1) ? 0 : 50;
|
|
|
|
long focusRectYIncrement = (zoneCount <= 1) ? 0 : 50;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < zoneCount; i++)
|
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
auto zone = MakeZone(focusZoneRect, m_zones.size());
|
2020-09-08 18:06:54 +08:00
|
|
|
if (zone)
|
|
|
|
{
|
|
|
|
AddZone(zone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All zones within zone set should be valid in order to use its functionality.
|
|
|
|
m_zones.clear();
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
focusZoneRect.left += focusRectXIncrement;
|
|
|
|
focusZoneRect.right += focusRectXIncrement;
|
|
|
|
focusZoneRect.bottom += focusRectYIncrement;
|
|
|
|
focusZoneRect.top += focusRectYIncrement;
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:06:54 +08:00
|
|
|
return true;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
bool ZoneSet::CalculateColumnsAndRowsLayout(Rect workArea, FancyZonesDataTypes::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
long totalWidth;
|
|
|
|
long totalHeight;
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
if (type == FancyZonesDataTypes::ZoneSetLayoutType::Columns)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
totalWidth = workArea.width() - (spacing * (zoneCount + 1));
|
|
|
|
totalHeight = workArea.height() - (spacing * 2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //Rows
|
|
|
|
totalWidth = workArea.width() - (spacing * 2);
|
|
|
|
totalHeight = workArea.height() - (spacing * (zoneCount + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
long top = spacing;
|
|
|
|
long left = spacing;
|
2020-03-05 22:54:06 +08:00
|
|
|
long bottom;
|
|
|
|
long right;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-05 22:54:06 +08:00
|
|
|
// Note: The expressions below are NOT equal to total{Width|Height} / zoneCount and are done
|
|
|
|
// like this to make the sum of all zones' sizes exactly total{Width|Height}.
|
2020-09-08 18:06:54 +08:00
|
|
|
for (int zoneIndex = 0; zoneIndex < zoneCount; ++zoneIndex)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
if (type == FancyZonesDataTypes::ZoneSetLayoutType::Columns)
|
2020-03-05 22:54:06 +08:00
|
|
|
{
|
2020-09-08 18:06:54 +08:00
|
|
|
right = left + (zoneIndex + 1) * totalWidth / zoneCount - zoneIndex * totalWidth / zoneCount;
|
2020-03-06 04:51:58 +08:00
|
|
|
bottom = totalHeight + spacing;
|
2020-03-05 22:54:06 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //Rows
|
2020-03-06 04:51:58 +08:00
|
|
|
right = totalWidth + spacing;
|
2020-09-08 18:06:54 +08:00
|
|
|
bottom = top + (zoneIndex + 1) * totalHeight / zoneCount - zoneIndex * totalHeight / zoneCount;
|
2020-03-05 22:54:06 +08:00
|
|
|
}
|
2020-09-08 18:06:54 +08:00
|
|
|
|
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
auto zone = MakeZone(RECT{ left, top, right, bottom }, m_zones.size());
|
2020-09-08 18:06:54 +08:00
|
|
|
if (zone)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-09-08 18:06:54 +08:00
|
|
|
AddZone(zone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All zones within zone set should be valid in order to use its functionality.
|
|
|
|
m_zones.clear();
|
|
|
|
return false;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
if (type == FancyZonesDataTypes::ZoneSetLayoutType::Columns)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-03-05 22:54:06 +08:00
|
|
|
left = right + spacing;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //Rows
|
2020-03-05 22:54:06 +08:00
|
|
|
top = bottom + spacing;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:06:54 +08:00
|
|
|
return true;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
bool ZoneSet::CalculateGridLayout(Rect workArea, FancyZonesDataTypes::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
const auto count = sizeof(predefinedPriorityGridLayouts) / sizeof(FancyZonesDataTypes::GridLayoutInfo);
|
|
|
|
if (type == FancyZonesDataTypes::ZoneSetLayoutType::PriorityGrid && zoneCount < count)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return CalculateUniquePriorityGridLayout(workArea, zoneCount, spacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rows = 1, columns = 1;
|
|
|
|
while (zoneCount / rows >= rows)
|
|
|
|
{
|
|
|
|
rows++;
|
|
|
|
}
|
|
|
|
rows--;
|
|
|
|
columns = zoneCount / rows;
|
|
|
|
if (zoneCount % rows == 0)
|
|
|
|
{
|
|
|
|
// even grid
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
columns++;
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::GridLayoutInfo gridLayoutInfo(FancyZonesDataTypes::GridLayoutInfo::Minimal{ .rows = rows, .columns = columns });
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-05 22:54:06 +08:00
|
|
|
// Note: The expressions below are NOT equal to C_MULTIPLIER / {rows|columns} and are done
|
|
|
|
// like this to make the sum of all percents exactly C_MULTIPLIER
|
2020-02-10 21:59:51 +08:00
|
|
|
for (int row = 0; row < rows; row++)
|
|
|
|
{
|
2020-03-05 22:54:06 +08:00
|
|
|
gridLayoutInfo.rowsPercents()[row] = C_MULTIPLIER * (row + 1) / rows - C_MULTIPLIER * row / rows;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
for (int col = 0; col < columns; col++)
|
|
|
|
{
|
2020-03-05 22:54:06 +08:00
|
|
|
gridLayoutInfo.columnsPercents()[col] = C_MULTIPLIER * (col + 1) / columns - C_MULTIPLIER * col / columns;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < rows; ++i)
|
|
|
|
{
|
|
|
|
gridLayoutInfo.cellChildMap()[i] = std::vector<int>(columns);
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = 0;
|
2020-10-26 16:07:11 +08:00
|
|
|
for (int row = 0; row < rows; row++)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
for (int col = 0; col < columns; col++)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
gridLayoutInfo.cellChildMap()[row][col] = index++;
|
|
|
|
if (index == zoneCount)
|
|
|
|
{
|
|
|
|
index--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CalculateGridZones(workArea, gridLayoutInfo, spacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZoneSet::CalculateUniquePriorityGridLayout(Rect workArea, int zoneCount, int spacing) noexcept
|
|
|
|
{
|
|
|
|
if (zoneCount <= 0 || zoneCount >= sizeof(predefinedPriorityGridLayouts))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CalculateGridZones(workArea, predefinedPriorityGridLayouts[zoneCount - 1], spacing);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ZoneSet::CalculateCustomLayout(Rect workArea, int spacing) noexcept
|
|
|
|
{
|
2020-05-27 22:55:46 +08:00
|
|
|
wil::unique_cotaskmem_string guidStr;
|
2020-05-29 15:38:29 +08:00
|
|
|
if (SUCCEEDED(StringFromCLSID(m_config.Id, &guidStr)))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-05-27 22:55:46 +08:00
|
|
|
const std::wstring guid = guidStr.get();
|
2020-02-17 23:28:49 +08:00
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
const auto zoneSetSearchResult = FancyZonesDataInstance().FindCustomZoneSet(guid);
|
2020-02-17 23:28:49 +08:00
|
|
|
|
|
|
|
if (!zoneSetSearchResult.has_value())
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-17 23:28:49 +08:00
|
|
|
const auto& zoneSet = *zoneSetSearchResult;
|
2020-07-22 16:39:13 +08:00
|
|
|
if (zoneSet.type == FancyZonesDataTypes::CustomLayoutType::Canvas && std::holds_alternative<FancyZonesDataTypes::CanvasLayoutInfo>(zoneSet.info))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
const auto& zoneSetInfo = std::get<FancyZonesDataTypes::CanvasLayoutInfo>(zoneSet.info);
|
2020-02-10 21:59:51 +08:00
|
|
|
for (const auto& zone : zoneSetInfo.zones)
|
|
|
|
{
|
|
|
|
int x = zone.x;
|
|
|
|
int y = zone.y;
|
|
|
|
int width = zone.width;
|
|
|
|
int height = zone.height;
|
|
|
|
|
|
|
|
DPIAware::Convert(m_config.Monitor, x, y);
|
|
|
|
DPIAware::Convert(m_config.Monitor, width, height);
|
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
auto zone = MakeZone(RECT{ x, y, x + width, y + height }, m_zones.size());
|
2020-09-08 18:06:54 +08:00
|
|
|
if (zone)
|
|
|
|
{
|
|
|
|
AddZone(zone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All zones within zone set should be valid in order to use its functionality.
|
|
|
|
m_zones.clear();
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-07-22 16:39:13 +08:00
|
|
|
else if (zoneSet.type == FancyZonesDataTypes::CustomLayoutType::Grid && std::holds_alternative<FancyZonesDataTypes::GridLayoutInfo>(zoneSet.info))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
const auto& info = std::get<FancyZonesDataTypes::GridLayoutInfo>(zoneSet.info);
|
2020-02-10 21:59:51 +08:00
|
|
|
return CalculateGridZones(workArea, info, spacing);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
bool ZoneSet::CalculateGridZones(Rect workArea, FancyZonesDataTypes::GridLayoutInfo gridLayoutInfo, int spacing)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2021-03-10 20:22:19 +08:00
|
|
|
long totalWidth = workArea.width();
|
|
|
|
long totalHeight = workArea.height();
|
2020-02-10 21:59:51 +08:00
|
|
|
struct Info
|
|
|
|
{
|
|
|
|
long Extent;
|
|
|
|
long Start;
|
|
|
|
long End;
|
|
|
|
};
|
2020-03-05 21:59:47 +08:00
|
|
|
std::vector<Info> rowInfo(gridLayoutInfo.rows());
|
|
|
|
std::vector<Info> columnInfo(gridLayoutInfo.columns());
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-05 22:54:06 +08:00
|
|
|
// Note: The expressions below are carefully written to
|
|
|
|
// make the sum of all zones' sizes exactly total{Width|Height}
|
2020-03-17 00:54:30 +08:00
|
|
|
int totalPercents = 0;
|
2020-02-10 21:59:51 +08:00
|
|
|
for (int row = 0; row < gridLayoutInfo.rows(); row++)
|
|
|
|
{
|
2021-03-10 20:22:19 +08:00
|
|
|
rowInfo[row].Start = totalPercents * totalHeight / C_MULTIPLIER;
|
2020-03-05 22:54:06 +08:00
|
|
|
totalPercents += gridLayoutInfo.rowsPercents()[row];
|
2021-03-10 20:22:19 +08:00
|
|
|
rowInfo[row].End = totalPercents * totalHeight / C_MULTIPLIER;
|
2020-03-05 22:54:06 +08:00
|
|
|
rowInfo[row].Extent = rowInfo[row].End - rowInfo[row].Start;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:54:06 +08:00
|
|
|
totalPercents = 0;
|
2020-02-10 21:59:51 +08:00
|
|
|
for (int col = 0; col < gridLayoutInfo.columns(); col++)
|
|
|
|
{
|
2021-03-10 20:22:19 +08:00
|
|
|
columnInfo[col].Start = totalPercents * totalWidth / C_MULTIPLIER;
|
2020-03-05 22:54:06 +08:00
|
|
|
totalPercents += gridLayoutInfo.columnsPercents()[col];
|
2021-03-10 20:22:19 +08:00
|
|
|
columnInfo[col].End = totalPercents * totalWidth / C_MULTIPLIER;
|
2020-03-05 22:54:06 +08:00
|
|
|
columnInfo[col].Extent = columnInfo[col].End - columnInfo[col].Start;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int row = 0; row < gridLayoutInfo.rows(); row++)
|
|
|
|
{
|
|
|
|
for (int col = 0; col < gridLayoutInfo.columns(); col++)
|
|
|
|
{
|
|
|
|
int i = gridLayoutInfo.cellChildMap()[row][col];
|
|
|
|
if (((row == 0) || (gridLayoutInfo.cellChildMap()[row - 1][col] != i)) &&
|
|
|
|
((col == 0) || (gridLayoutInfo.cellChildMap()[row][col - 1] != i)))
|
|
|
|
{
|
2020-03-05 22:54:06 +08:00
|
|
|
long left = columnInfo[col].Start;
|
|
|
|
long top = rowInfo[row].Start;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
int maxRow = row;
|
|
|
|
while (((maxRow + 1) < gridLayoutInfo.rows()) && (gridLayoutInfo.cellChildMap()[maxRow + 1][col] == i))
|
|
|
|
{
|
|
|
|
maxRow++;
|
|
|
|
}
|
|
|
|
int maxCol = col;
|
|
|
|
while (((maxCol + 1) < gridLayoutInfo.columns()) && (gridLayoutInfo.cellChildMap()[row][maxCol + 1] == i))
|
|
|
|
{
|
|
|
|
maxCol++;
|
|
|
|
}
|
|
|
|
|
|
|
|
long right = columnInfo[maxCol].End;
|
|
|
|
long bottom = rowInfo[maxRow].End;
|
|
|
|
|
2021-03-10 20:22:19 +08:00
|
|
|
top += row == 0 ? spacing : spacing / 2;
|
2021-04-07 15:30:35 +08:00
|
|
|
bottom -= maxRow == gridLayoutInfo.rows() - 1 ? spacing : spacing / 2;
|
2021-03-10 20:22:19 +08:00
|
|
|
left += col == 0 ? spacing : spacing / 2;
|
2021-04-07 15:30:35 +08:00
|
|
|
right -= maxCol == gridLayoutInfo.columns() - 1 ? spacing : spacing / 2;
|
2021-03-10 20:22:19 +08:00
|
|
|
|
2020-10-26 16:07:11 +08:00
|
|
|
auto zone = MakeZone(RECT{ left, top, right, bottom }, i);
|
2020-09-08 18:06:54 +08:00
|
|
|
if (zone)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-09-08 18:06:54 +08:00
|
|
|
AddZone(zone);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// All zones within zone set should be valid in order to use its functionality.
|
|
|
|
m_zones.clear();
|
|
|
|
return false;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-08 18:06:54 +08:00
|
|
|
return true;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSet::GetCombinedZoneRange(const ZoneIndexSet& initialZones, const ZoneIndexSet& finalZones) const noexcept
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet combinedZones, result;
|
2020-09-11 17:32:45 +08:00
|
|
|
std::set_union(begin(initialZones), end(initialZones), begin(finalZones), end(finalZones), std::back_inserter(combinedZones));
|
|
|
|
|
|
|
|
RECT boundingRect;
|
|
|
|
bool boundingRectEmpty = true;
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
for (ZoneIndex zoneId : combinedZones)
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
if (m_zones.contains(zoneId))
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
const RECT rect = m_zones.at(zoneId)->GetZoneRect();
|
|
|
|
if (boundingRectEmpty)
|
|
|
|
{
|
|
|
|
boundingRect = rect;
|
|
|
|
boundingRectEmpty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boundingRect.left = min(boundingRect.left, rect.left);
|
|
|
|
boundingRect.top = min(boundingRect.top, rect.top);
|
|
|
|
boundingRect.right = max(boundingRect.right, rect.right);
|
|
|
|
boundingRect.bottom = max(boundingRect.bottom, rect.bottom);
|
|
|
|
}
|
2020-09-11 17:32:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!boundingRectEmpty)
|
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
for (const auto& [zoneId, zone] : m_zones)
|
2020-09-11 17:32:45 +08:00
|
|
|
{
|
2020-10-26 16:07:11 +08:00
|
|
|
const RECT rect = zone->GetZoneRect();
|
2020-09-11 17:32:45 +08:00
|
|
|
if (boundingRect.left <= rect.left && rect.right <= boundingRect.right &&
|
|
|
|
boundingRect.top <= rect.top && rect.bottom <= boundingRect.bottom)
|
|
|
|
{
|
|
|
|
result.push_back(zoneId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSet::ZoneSelectSubregion(const ZoneIndexSet& capturedZones, POINT pt) const
|
2021-02-08 17:48:11 +08:00
|
|
|
{
|
|
|
|
auto expand = [&](RECT& rect) {
|
|
|
|
rect.top -= m_config.SensitivityRadius / 2;
|
|
|
|
rect.bottom += m_config.SensitivityRadius / 2;
|
|
|
|
rect.left -= m_config.SensitivityRadius / 2;
|
|
|
|
rect.right += m_config.SensitivityRadius / 2;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compute the overlapped rectangle.
|
|
|
|
RECT overlap = m_zones.at(capturedZones[0])->GetZoneRect();
|
|
|
|
expand(overlap);
|
|
|
|
|
|
|
|
for (size_t i = 1; i < capturedZones.size(); ++i)
|
|
|
|
{
|
|
|
|
RECT current = m_zones.at(capturedZones[i])->GetZoneRect();
|
|
|
|
expand(current);
|
|
|
|
|
|
|
|
overlap.top = max(overlap.top, current.top);
|
|
|
|
overlap.left = max(overlap.left, current.left);
|
|
|
|
overlap.bottom = min(overlap.bottom, current.bottom);
|
|
|
|
overlap.right = min(overlap.right, current.right);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Avoid division by zero
|
|
|
|
int width = max(overlap.right - overlap.left, 1);
|
|
|
|
int height = max(overlap.bottom - overlap.top, 1);
|
|
|
|
|
|
|
|
bool verticalSplit = height > width;
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndex zoneIndex;
|
2021-02-08 17:48:11 +08:00
|
|
|
|
|
|
|
if (verticalSplit)
|
|
|
|
{
|
|
|
|
zoneIndex = (pt.y - overlap.top) * capturedZones.size() / height;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
zoneIndex = (pt.x - overlap.left) * capturedZones.size() / width;
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
zoneIndex = std::clamp(zoneIndex, ZoneIndex(0), static_cast<ZoneIndex>(capturedZones.size()) - 1);
|
2021-02-08 17:48:11 +08:00
|
|
|
|
|
|
|
return { capturedZones[zoneIndex] };
|
|
|
|
}
|
|
|
|
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSet::ZoneSelectClosestCenter(const ZoneIndexSet& capturedZones, POINT pt) const
|
2021-06-22 16:34:27 +08:00
|
|
|
{
|
|
|
|
auto getCenter = [](auto zone) {
|
|
|
|
RECT rect = zone->GetZoneRect();
|
|
|
|
return POINT{ (rect.right + rect.left) / 2, (rect.top + rect.bottom) / 2 };
|
|
|
|
};
|
|
|
|
auto pointDifference = [](POINT pt1, POINT pt2) {
|
|
|
|
return (pt1.x - pt2.x) * (pt1.x - pt2.x) + (pt1.y - pt2.y) * (pt1.y - pt2.y);
|
|
|
|
};
|
|
|
|
auto distanceFromCenter = [&](auto zone) {
|
|
|
|
POINT center = getCenter(zone);
|
|
|
|
return pointDifference(center, pt);
|
|
|
|
};
|
|
|
|
auto closerToCenter = [&](auto zone1, auto zone2) {
|
|
|
|
if (pointDifference(getCenter(zone1), getCenter(zone2)) > OVERLAPPING_CENTERS_SENSITIVITY)
|
|
|
|
{
|
|
|
|
return distanceFromCenter(zone1) < distanceFromCenter(zone2);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return zone1->GetZoneArea() < zone2->GetZoneArea();
|
|
|
|
};
|
|
|
|
};
|
|
|
|
return ZoneSelectPriority(capturedZones, closerToCenter);
|
|
|
|
}
|
|
|
|
|
2021-02-08 17:48:11 +08:00
|
|
|
template<class CompareF>
|
2021-08-11 22:05:03 +08:00
|
|
|
ZoneIndexSet ZoneSet::ZoneSelectPriority(const ZoneIndexSet& capturedZones, CompareF compare) const
|
2021-02-08 17:48:11 +08:00
|
|
|
{
|
|
|
|
size_t chosen = 0;
|
|
|
|
|
|
|
|
for (size_t i = 1; i < capturedZones.size(); ++i)
|
|
|
|
{
|
|
|
|
if (compare(m_zones.at(capturedZones[i]), m_zones.at(capturedZones[chosen])))
|
|
|
|
{
|
|
|
|
chosen = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { capturedZones[chosen] };
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-07-01 21:36:05 +08:00
|
|
|
|