Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FanzyZones] Zone activation based on zone center #11361

Merged
merged 6 commits into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/modules/fancyzones/lib/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ struct Settings
Smallest = 0,
Largest = 1,
Positional = 2,
EnumElements = 3, // number of elements in the enum, not counting this
ClosestCenter = 3,
EnumElements = 4, // number of elements in the enum, not counting this
};

// The values specified here are the defaults.
Expand Down
25 changes: 25 additions & 0 deletions src/modules/fancyzones/lib/ZoneSet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ using namespace FancyZonesUtils;
namespace
{
constexpr int C_MULTIPLIER = 10000;
constexpr int OVERLAPPING_CENTERS_SENSITIVITY = 75;

// PriorityGrid layout is unique for zoneCount <= 11. For zoneCount > 11 PriorityGrid is same as Grid
FancyZonesDataTypes::GridLayoutInfo predefinedPriorityGridLayouts[11] = {
Expand Down Expand Up @@ -255,6 +256,28 @@ ZoneSet::ZonesFromPoint(POINT pt) const noexcept
return max(rect.bottom - rect.top, 0) * max(rect.right - rect.left, 0);
};

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 zoneArea(zone1) < zoneArea(zone2);
};
};

try
{
using Algorithm = Settings::OverlappingZonesAlgorithm;
Expand All @@ -267,6 +290,8 @@ ZoneSet::ZonesFromPoint(POINT pt) const noexcept
return ZoneSelectPriority(capturedZones, [&](auto zone1, auto zone2) { return zoneArea(zone1) > zoneArea(zone2); });
case Algorithm::Positional:
return ZoneSelectSubregion(capturedZones, pt);
case Algorithm::ClosestCenter:
return ZoneSelectPriority(capturedZones, closerToCenter);
}
}
catch (std::out_of_range)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,9 @@
<data name="ColorPicker_Editor.Text" xml:space="preserve">
<value>Editor</value>
</data>
<data name="FancyZones_OverlappingZonesClosestCenter.Content" xml:space="preserve">
<value>Activate the zone whose center is closest to the cursor</value>
</data>
<data name="FancyZones_OverlappingZonesLargest.Content" xml:space="preserve">
<value>Activate the largest zone by area</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
<ComboBoxItem x:Uid="FancyZones_OverlappingZonesSmallest" />
<ComboBoxItem x:Uid="FancyZones_OverlappingZonesLargest" />
<ComboBoxItem x:Uid="FancyZones_OverlappingZonesPositional" />
<ComboBoxItem x:Uid="FancyZones_OverlappingZonesClosestCenter" />
</ComboBox>

<TextBlock x:Uid="FancyZones_WindowBehavior_GroupSettings"
Expand Down