forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoneSet.cpp
More file actions
704 lines (615 loc) · 22.8 KB
/
ZoneSet.cpp
File metadata and controls
704 lines (615 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#include "pch.h"
#include "util.h"
#include "lib/ZoneSet.h"
#include "Settings.h"
#include <common/dpi_aware.h>
namespace
{
constexpr int C_MULTIPLIER = 10000;
/*
struct GridLayoutInfo {
int rows;
int columns;
int rowsPercents[MAX_ZONE_COUNT];
int columnsPercents[MAX_ZONE_COUNT];
int cellChildMap[MAX_ZONE_COUNT][MAX_ZONE_COUNT];
};
*/
auto l = JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Minimal{ .rows = 1, .columns = 1 });
// PriorityGrid layout is unique for zoneCount <= 11. For zoneCount > 11 PriorityGrid is same as Grid
JSONHelpers::GridLayoutInfo predefinedPriorityGridLayouts[11] = {
/* 1 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 1,
.columns = 1,
.rowsPercents = { 10000 },
.columnsPercents = { 10000 },
.cellChildMap = { { 0 } } }),
/* 2 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 1,
.columns = 2,
.rowsPercents = { 10000 },
.columnsPercents = { 6667, 3333 },
.cellChildMap = { { 0, 1 } } }),
/* 3 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 1,
.columns = 3,
.rowsPercents = { 10000 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 } } }),
/* 4 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 2,
.columns = 3,
.rowsPercents = { 5000, 5000 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 }, { 0, 1, 3 } } }),
/* 5 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 2,
.columns = 3,
.rowsPercents = { 5000, 5000 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 }, { 3, 1, 4 } } }),
/* 6 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 3,
.columns = 3,
.rowsPercents = { 3333, 3334, 3333 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 }, { 0, 1, 3 }, { 4, 1, 5 } } }),
/* 7 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.rows = 3,
.columns = 3,
.rowsPercents = { 3333, 3334, 3333 },
.columnsPercents = { 2500, 5000, 2500 },
.cellChildMap = { { 0, 1, 2 }, { 3, 1, 4 }, { 5, 1, 6 } } }),
/* 8 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.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 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.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 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.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 */
JSONHelpers::GridLayoutInfo(JSONHelpers::GridLayoutInfo::Full{
.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 } } }),
};
}
struct ZoneSet : winrt::implements<ZoneSet, IZoneSet>
{
public:
ZoneSet(ZoneSetConfig const& config) :
m_config(config)
{
}
ZoneSet(ZoneSetConfig const& config, std::vector<winrt::com_ptr<IZone>> zones) :
m_config(config),
m_zones(zones)
{
}
IFACEMETHODIMP_(GUID)
Id() noexcept { return m_config.Id; }
IFACEMETHODIMP_(JSONHelpers::ZoneSetLayoutType)
LayoutType() noexcept { return m_config.LayoutType; }
IFACEMETHODIMP AddZone(winrt::com_ptr<IZone> zone) noexcept;
IFACEMETHODIMP_(std::vector<int>)
ZonesFromPoint(POINT pt) noexcept;
IFACEMETHODIMP_(std::vector<int>)
GetZoneIndexSetFromWindow(HWND window) noexcept;
IFACEMETHODIMP_(std::vector<winrt::com_ptr<IZone>>)
GetZones() noexcept { return m_zones; }
IFACEMETHODIMP_(void)
MoveWindowIntoZoneByIndex(HWND window, HWND zoneWindow, int index) noexcept;
IFACEMETHODIMP_(void)
MoveWindowIntoZoneByIndexSet(HWND window, HWND windowZone, const std::vector<int>& indexSet) noexcept;
IFACEMETHODIMP_(bool)
MoveWindowIntoZoneByDirection(HWND window, HWND zoneWindow, DWORD vkCode, bool cycle) noexcept;
IFACEMETHODIMP_(void)
MoveWindowIntoZoneByPoint(HWND window, HWND zoneWindow, POINT ptClient) noexcept;
IFACEMETHODIMP_(bool)
CalculateZones(MONITORINFO monitorInfo, int zoneCount, int spacing) noexcept;
IFACEMETHODIMP_(bool)
IsZoneEmpty(int zoneIndex) noexcept;
private:
bool CalculateFocusLayout(Rect workArea, int zoneCount) noexcept;
bool CalculateColumnsAndRowsLayout(Rect workArea, JSONHelpers::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept;
bool CalculateGridLayout(Rect workArea, JSONHelpers::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept;
bool CalculateUniquePriorityGridLayout(Rect workArea, int zoneCount, int spacing) noexcept;
bool CalculateCustomLayout(Rect workArea, int spacing) noexcept;
bool CalculateGridZones(Rect workArea, JSONHelpers::GridLayoutInfo gridLayoutInfo, int spacing);
void StampWindow(HWND window, size_t bitmask) noexcept;
std::vector<winrt::com_ptr<IZone>> m_zones;
std::map<HWND, std::vector<int>> m_windowIndexSet;
ZoneSetConfig m_config;
};
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;
}
IFACEMETHODIMP_(std::vector<int>)
ZoneSet::ZonesFromPoint(POINT pt) noexcept
{
const int SENSITIVITY_RADIUS = 20;
std::vector<int> capturedZones;
std::vector<int> strictlyCapturedZones;
for (size_t i = 0; i < m_zones.size(); i++)
{
auto zone = m_zones[i];
RECT newZoneRect = zone->GetZoneRect();
if (newZoneRect.left < newZoneRect.right && newZoneRect.top < newZoneRect.bottom) // proper zone
{
if (newZoneRect.left - SENSITIVITY_RADIUS <= pt.x && pt.x <= newZoneRect.right + SENSITIVITY_RADIUS &&
newZoneRect.top - SENSITIVITY_RADIUS <= pt.y && pt.y <= newZoneRect.bottom + SENSITIVITY_RADIUS)
{
capturedZones.emplace_back(static_cast<int>(i));
}
if (newZoneRect.left <= pt.x && pt.x < newZoneRect.right &&
newZoneRect.top <= pt.y && pt.y < newZoneRect.bottom)
{
strictlyCapturedZones.emplace_back(static_cast<int>(i));
}
}
}
// 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 {};
}
// If captured zones do not overlap, return all of them
// Otherwise, return the smallest one
bool overlap = false;
for (size_t i = 0; i < capturedZones.size(); ++i)
{
for (size_t j = i + 1; j < capturedZones.size(); ++j)
{
auto rectI = m_zones[capturedZones[i]]->GetZoneRect();
auto rectJ = m_zones[capturedZones[j]]->GetZoneRect();
if (max(rectI.top, rectJ.top) < min(rectI.bottom, rectJ.bottom) &&
max(rectI.left, rectJ.left) < min(rectI.right, rectJ.right))
{
overlap = true;
i = capturedZones.size() - 1;
break;
}
}
}
if (overlap)
{
size_t smallestIdx = 0;
for (size_t i = 1; i < capturedZones.size(); ++i)
{
auto rectS = m_zones[capturedZones[smallestIdx]]->GetZoneRect();
auto rectI = m_zones[capturedZones[i]]->GetZoneRect();
int smallestSize = (rectS.bottom - rectS.top) * (rectS.right - rectS.left);
int iSize = (rectI.bottom - rectI.top) * (rectI.right - rectI.left);
if (iSize <= smallestSize)
{
smallestIdx = i;
}
}
capturedZones = { capturedZones[smallestIdx] };
}
return capturedZones;
}
std::vector<int> ZoneSet::GetZoneIndexSetFromWindow(HWND window) noexcept
{
auto it = m_windowIndexSet.find(window);
if (it == m_windowIndexSet.end())
{
return {};
}
else
{
return it->second;
}
}
IFACEMETHODIMP_(void)
ZoneSet::MoveWindowIntoZoneByIndex(HWND window, HWND windowZone, int index) noexcept
{
MoveWindowIntoZoneByIndexSet(window, windowZone, { index });
}
IFACEMETHODIMP_(void)
ZoneSet::MoveWindowIntoZoneByIndexSet(HWND window, HWND windowZone, const std::vector<int>& indexSet) noexcept
{
if (m_zones.empty())
{
return;
}
RECT size;
bool sizeEmpty = true;
size_t bitmask = 0;
auto& storedIndexSet = m_windowIndexSet[window];
storedIndexSet = {};
for (int index : indexSet)
{
if (index < static_cast<int>(m_zones.size()))
{
RECT newSize = m_zones.at(index)->ComputeActualZoneRect(window, windowZone);
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;
}
storedIndexSet.push_back(index);
}
if (index < std::numeric_limits<size_t>::digits)
{
bitmask |= 1ull << index;
}
}
if (!sizeEmpty)
{
SaveWindowSizeAndOrigin(window);
SizeWindowToRect(window, size);
StampWindow(window, bitmask);
}
}
IFACEMETHODIMP_(bool)
ZoneSet::MoveWindowIntoZoneByDirection(HWND window, HWND windowZone, DWORD vkCode, bool cycle) noexcept
{
if (m_zones.empty())
{
return false;
}
auto indexSet = GetZoneIndexSetFromWindow(window);
int numZones = static_cast<int>(m_zones.size());
// The window was not assigned to any zone here
if (indexSet.size() == 0)
{
MoveWindowIntoZoneByIndexSet(window, windowZone, { vkCode == VK_LEFT ? numZones - 1 : 0 });
return true;
}
int oldIndex = indexSet[0];
// We reached the edge
if ((vkCode == VK_LEFT && oldIndex == 0) || (vkCode == VK_RIGHT && oldIndex == numZones - 1))
{
if (!cycle)
{
MoveWindowIntoZoneByIndexSet(window, windowZone, {});
return false;
}
else
{
MoveWindowIntoZoneByIndexSet(window, windowZone, { vkCode == VK_LEFT ? numZones - 1 : 0 });
return true;
}
}
// We didn't reach the edge
if (vkCode == VK_LEFT)
{
MoveWindowIntoZoneByIndexSet(window, windowZone, { oldIndex - 1 });
}
else
{
MoveWindowIntoZoneByIndexSet(window, windowZone, { oldIndex + 1 });
}
return true;
}
IFACEMETHODIMP_(void)
ZoneSet::MoveWindowIntoZoneByPoint(HWND window, HWND zoneWindow, POINT ptClient) noexcept
{
auto zones = ZonesFromPoint(ptClient);
MoveWindowIntoZoneByIndexSet(window, zoneWindow, zones);
}
IFACEMETHODIMP_(bool)
ZoneSet::CalculateZones(MONITORINFO monitorInfo, int zoneCount, int spacing) noexcept
{
Rect const workArea(monitorInfo.rcWork);
//invalid work area
if (workArea.width() == 0 || workArea.height() == 0)
{
return false;
}
//invalid zoneCount, may cause division by zero
if (zoneCount <= 0 && m_config.LayoutType != JSONHelpers::ZoneSetLayoutType::Custom)
{
return false;
}
bool success = true;
switch (m_config.LayoutType)
{
case JSONHelpers::ZoneSetLayoutType::Focus:
success = CalculateFocusLayout(workArea, zoneCount);
break;
case JSONHelpers::ZoneSetLayoutType::Columns:
case JSONHelpers::ZoneSetLayoutType::Rows:
success = CalculateColumnsAndRowsLayout(workArea, m_config.LayoutType, zoneCount, spacing);
break;
case JSONHelpers::ZoneSetLayoutType::Grid:
case JSONHelpers::ZoneSetLayoutType::PriorityGrid:
success = CalculateGridLayout(workArea, m_config.LayoutType, zoneCount, spacing);
break;
case JSONHelpers::ZoneSetLayoutType::Custom:
success = CalculateCustomLayout(workArea, spacing);
break;
}
return success;
}
bool ZoneSet::IsZoneEmpty(int zoneIndex) noexcept
{
for (auto& [window, zones] : m_windowIndexSet)
{
if (find(begin(zones), end(zones), zoneIndex) != end(zones))
{
return false;
}
}
return true;
}
bool ZoneSet::CalculateFocusLayout(Rect workArea, int zoneCount) noexcept
{
bool success = true;
long left{ long(workArea.width() * 0.1) };
long top{ long(workArea.height() * 0.1) };
long right{ left + long(workArea.width() * 0.6) };
long bottom{ top + long(workArea.height() * 0.6) };
RECT focusZoneRect{ left, top, right, bottom };
long focusRectXIncrement = (zoneCount <= 1) ? 0 : (int)(workArea.width() * 0.2) / (zoneCount - 1);
long focusRectYIncrement = (zoneCount <= 1) ? 0 : (int)(workArea.height() * 0.2) / (zoneCount - 1);
if (left >= right || top >= bottom || left < 0 || right < 0 || top < 0 || bottom < 0)
{
success = false;
}
for (int i = 0; i < zoneCount; i++)
{
AddZone(MakeZone(focusZoneRect));
focusZoneRect.left += focusRectXIncrement;
focusZoneRect.right += focusRectXIncrement;
focusZoneRect.bottom += focusRectYIncrement;
focusZoneRect.top += focusRectYIncrement;
}
return success;
}
bool ZoneSet::CalculateColumnsAndRowsLayout(Rect workArea, JSONHelpers::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept
{
bool success = true;
long totalWidth;
long totalHeight;
if (type == JSONHelpers::ZoneSetLayoutType::Columns)
{
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;
long bottom;
long right;
// 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}.
for (int zone = 0; zone < zoneCount; zone++)
{
if (type == JSONHelpers::ZoneSetLayoutType::Columns)
{
right = left + (zone + 1) * totalWidth / zoneCount - zone * totalWidth / zoneCount;
bottom = totalHeight + spacing;
}
else
{ //Rows
right = totalWidth + spacing;
bottom = top + (zone + 1) * totalHeight / zoneCount - zone * totalHeight / zoneCount;
}
if (left >= right || top >= bottom || left < 0 || right < 0 || top < 0 || bottom < 0)
{
success = false;
}
RECT focusZoneRect{ left, top, right, bottom };
AddZone(MakeZone(focusZoneRect));
if (type == JSONHelpers::ZoneSetLayoutType::Columns)
{
left = right + spacing;
}
else
{ //Rows
top = bottom + spacing;
}
}
return success;
}
bool ZoneSet::CalculateGridLayout(Rect workArea, JSONHelpers::ZoneSetLayoutType type, int zoneCount, int spacing) noexcept
{
const auto count = sizeof(predefinedPriorityGridLayouts) / sizeof(JSONHelpers::GridLayoutInfo);
if (type == JSONHelpers::ZoneSetLayoutType::PriorityGrid && zoneCount < count)
{
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++;
}
JSONHelpers::GridLayoutInfo gridLayoutInfo(JSONHelpers::GridLayoutInfo::Minimal{ .rows = rows, .columns = columns });
// 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
for (int row = 0; row < rows; row++)
{
gridLayoutInfo.rowsPercents()[row] = C_MULTIPLIER * (row + 1) / rows - C_MULTIPLIER * row / rows;
}
for (int col = 0; col < columns; col++)
{
gridLayoutInfo.columnsPercents()[col] = C_MULTIPLIER * (col + 1) / columns - C_MULTIPLIER * col / columns;
}
for (int i = 0; i < rows; ++i)
{
gridLayoutInfo.cellChildMap()[i] = std::vector<int>(columns);
}
int index = 0;
for (int col = columns - 1; col >= 0; col--)
{
for (int row = rows - 1; row >= 0; row--)
{
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
{
wil::unique_cotaskmem_string guidStr;
if (SUCCEEDED(StringFromCLSID(m_config.Id, &guidStr)))
{
const std::wstring guid = guidStr.get();
const auto zoneSetSearchResult = JSONHelpers::FancyZonesDataInstance().FindCustomZoneSet(guid);
if (!zoneSetSearchResult.has_value())
{
return false;
}
const auto& zoneSet = *zoneSetSearchResult;
if (zoneSet.type == JSONHelpers::CustomLayoutType::Canvas && std::holds_alternative<JSONHelpers::CanvasLayoutInfo>(zoneSet.info))
{
const auto& zoneSetInfo = std::get<JSONHelpers::CanvasLayoutInfo>(zoneSet.info);
for (const auto& zone : zoneSetInfo.zones)
{
int x = zone.x;
int y = zone.y;
int width = zone.width;
int height = zone.height;
if (x < 0 || y < 0 || width < 0 || height < 0)
{
return false;
}
DPIAware::Convert(m_config.Monitor, x, y);
DPIAware::Convert(m_config.Monitor, width, height);
AddZone(MakeZone(RECT{ x, y, x + width, y + height }));
}
return true;
}
else if (zoneSet.type == JSONHelpers::CustomLayoutType::Grid && std::holds_alternative<JSONHelpers::GridLayoutInfo>(zoneSet.info))
{
const auto& info = std::get<JSONHelpers::GridLayoutInfo>(zoneSet.info);
return CalculateGridZones(workArea, info, spacing);
}
}
return false;
}
bool ZoneSet::CalculateGridZones(Rect workArea, JSONHelpers::GridLayoutInfo gridLayoutInfo, int spacing)
{
bool success = true;
long totalWidth = workArea.width() - (spacing * (gridLayoutInfo.columns() + 1));
long totalHeight = workArea.height() - (spacing * (gridLayoutInfo.rows() + 1));
struct Info
{
long Extent;
long Start;
long End;
};
std::vector<Info> rowInfo(gridLayoutInfo.rows());
std::vector<Info> columnInfo(gridLayoutInfo.columns());
// Note: The expressions below are carefully written to
// make the sum of all zones' sizes exactly total{Width|Height}
int totalPercents = 0;
for (int row = 0; row < gridLayoutInfo.rows(); row++)
{
rowInfo[row].Start = totalPercents * totalHeight / C_MULTIPLIER + (row + 1) * spacing;
totalPercents += gridLayoutInfo.rowsPercents()[row];
rowInfo[row].End = totalPercents * totalHeight / C_MULTIPLIER + (row + 1) * spacing;
rowInfo[row].Extent = rowInfo[row].End - rowInfo[row].Start;
}
totalPercents = 0;
for (int col = 0; col < gridLayoutInfo.columns(); col++)
{
columnInfo[col].Start = totalPercents * totalWidth / C_MULTIPLIER + (col + 1) * spacing;
totalPercents += gridLayoutInfo.columnsPercents()[col];
columnInfo[col].End = totalPercents * totalWidth / C_MULTIPLIER + (col + 1) * spacing;
columnInfo[col].Extent = columnInfo[col].End - columnInfo[col].Start;
}
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)))
{
long left = columnInfo[col].Start;
long top = rowInfo[row].Start;
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;
if (left >= right || top >= bottom || left < 0 || right < 0 || top < 0 || bottom < 0)
{
success = false;
}
AddZone(MakeZone(RECT{ left, top, right, bottom }));
}
}
}
return success;
}
void ZoneSet::StampWindow(HWND window, size_t bitmask) noexcept
{
SetProp(window, MULTI_ZONE_STAMP, reinterpret_cast<HANDLE>(bitmask));
}
winrt::com_ptr<IZoneSet> MakeZoneSet(ZoneSetConfig const& config) noexcept
{
return winrt::make_self<ZoneSet>(config);
}