Repository: microsoft/PowerToys
File: src/modules/fancyzones/lib/ZoneSet.cpp (C++)

Code before:

    ZoneSetConfig m_config;
};

IFACEMETHODIMP ZoneSet::AddZone(winrt::com_ptr<IZone> zone) noexcept
{
    auto zoneId = zone->Id();
    if (m_zones.contains(zoneId))
    {
        return S_FALSE;
    }
    m_zones[zoneId] = zone;

    return S_OK;
}

IFACEMETHODIMP_(std::vector<size_t>)
ZoneSet::ZonesFromPoint(POINT pt) const noexcept
{
    std::vector<size_t> capturedZones;
    std::vector<size_t> strictlyCapturedZones;
    for (const auto& [zoneId, zone] : m_zones)
    {
        const RECT& zoneRect = zone->GetZoneRect();
        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 */)
        {
            capturedZones.emplace_back(zoneId);
        }
            
        if (zoneRect.left <= pt.x && pt.x < zoneRect.right &&
            zoneRect.top <= pt.y && pt.y < zoneRect.bottom)
        {
            strictlyCapturedZones.emplace_back(zoneId);
        }
    }

    // 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 one of them based on the chosen selection algorithm.
    bool overlap = false;
    for (size_t i = 0; i < capturedZones.size(); ++i)
    {
        for (size_t j = i + 1; j < capturedZones.size(); ++j)
        {

Review comment (suggestion):
If you need to make changes to this function, they need to be done only when the ClosestCenter algorithm is selected.

Code after:
    bool m_inExtendWindow = false;

    ZoneSetConfig m_config;
};

IFACEMETHODIMP ZoneSet::AddZone(winrt::com_ptr<IZone> zone) noexcept
{
    auto zoneId = zone->Id();
    if (m_zones.contains(zoneId))
    {
        return S_FALSE;
    }
    m_zones[zoneId] = zone;

    return S_OK;
}

IFACEMETHODIMP_(std::vector<size_t>)
ZoneSet::ZonesFromPoint(POINT pt) const noexcept
{
    std::vector<size_t> capturedZones;
    std::vector<size_t> strictlyCapturedZones;
    for (const auto& [zoneId, zone] : m_zones)
    {
        const RECT& zoneRect = zone->GetZoneRect();
        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)
        {
            capturedZones.emplace_back(zoneId);
        }
            
        if (zoneRect.left <= pt.x && pt.x < zoneRect.right &&
            zoneRect.top <= pt.y && pt.y < zoneRect.bottom)
        {
            strictlyCapturedZones.emplace_back(zoneId);
        }
    }

    // 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 one of them based on the chosen selection algorithm.
    bool overlap = false;
    for (size_t i = 0; i < capturedZones.size(); ++i)
    {
        for (size_t j = i + 1; j < capturedZones.size(); ++j)