Skip to content

Commit

Permalink
[FancyZones] Reset layouts after screen locking fix (#13703)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova authored and jaimecbernardo committed Oct 11, 2021
1 parent 45d2752 commit afdefa2
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 8 deletions.
16 changes: 9 additions & 7 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ const std::unordered_map<std::wstring, std::vector<FancyZonesDataTypes::AppZoneH
return appZoneHistoryMap;
}

std::optional<FancyZonesDataTypes::DeviceInfoData> FancyZonesData::FindDeviceInfo(const FancyZonesDataTypes::DeviceIdData& zoneWindowId) const
std::optional<FancyZonesDataTypes::DeviceInfoData> FancyZonesData::FindDeviceInfo(const FancyZonesDataTypes::DeviceIdData& id) const
{
std::scoped_lock lock{ dataLock };
for (const auto& [deviceId, deviceInfo] : deviceInfoMap)
{
if (zoneWindowId.isEqualWithNullVirtualDesktopId(deviceId))
if (id.isEqualWithNullVirtualDesktopId(deviceId))
{
return deviceInfo;
}
Expand All @@ -206,8 +206,11 @@ bool FancyZonesData::AddDevice(const FancyZonesDataTypes::DeviceIdData& deviceId
_TRACER_;
using namespace FancyZonesDataTypes;

auto deviceInfo = FindDeviceInfo(deviceId);

std::scoped_lock lock{ dataLock };
if (!deviceInfoMap.contains(deviceId))

if (!deviceInfo.has_value())
{
wil::unique_cotaskmem_string virtualDesktopId;
if (SUCCEEDED(StringFromCLSID(deviceId.virtualDesktopId, &virtualDesktopId)))
Expand All @@ -224,13 +227,12 @@ bool FancyZonesData::AddDevice(const FancyZonesDataTypes::DeviceIdData& deviceId
const ZoneSetData zoneSetData{ guidString.get(), ZoneSetLayoutType::PriorityGrid };
DeviceInfoData defaultDeviceInfoData{ zoneSetData, DefaultValues::ShowSpacing, DefaultValues::Spacing, DefaultValues::ZoneCount, DefaultValues::SensitivityRadius };
deviceInfoMap[deviceId] = std::move(defaultDeviceInfoData);
return true;
}
else
{
deviceInfoMap[deviceId] = DeviceInfoData{ ZoneSetData{ NonLocalizable::NullStr, ZoneSetLayoutType::Blank } };
Logger::error("Failed to create an ID for the new layout");
}

return true;
}

return false;
Expand All @@ -245,7 +247,7 @@ void FancyZonesData::CloneDeviceInfo(const FancyZonesDataTypes::DeviceIdData& so
std::scoped_lock lock{ dataLock };

// The source virtual desktop is deleted, simply ignore it.
if (!deviceInfoMap.contains(source))
if (!FindDeviceInfo(source).has_value())
{
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/FancyZonesData.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class FancyZonesData

void SetVirtualDesktopCheckCallback(std::function<bool(GUID)> callback);

std::optional<FancyZonesDataTypes::DeviceInfoData> FindDeviceInfo(const FancyZonesDataTypes::DeviceIdData& zoneWindowId) const;
std::optional<FancyZonesDataTypes::DeviceInfoData> FindDeviceInfo(const FancyZonesDataTypes::DeviceIdData& id) const;
std::optional<FancyZonesDataTypes::CustomZoneSetData> FindCustomZoneSet(const std::wstring& guid) const;

const JSONHelpers::TDeviceInfoMap& GetDeviceInfoMap() const;
Expand Down
10 changes: 10 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesDataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ namespace FancyZonesDataTypes
int sensitivityRadius;
};

inline bool operator==(const ZoneSetData& lhs, const ZoneSetData& rhs)
{
return lhs.type == rhs.type && lhs.uuid == rhs.uuid;
}

inline bool operator==(const DeviceIdData& lhs, const DeviceIdData& rhs)
{
return lhs.deviceName.compare(rhs.deviceName) == 0 && lhs.width == rhs.width && lhs.height == rhs.height && lhs.virtualDesktopId == rhs.virtualDesktopId && lhs.monitorId.compare(rhs.monitorId) == 0;
Expand All @@ -160,6 +165,11 @@ namespace FancyZonesDataTypes
{
return lhs.deviceName.compare(rhs.deviceName) < 0 || lhs.width < rhs.width || lhs.height < rhs.height || lhs.monitorId.compare(rhs.monitorId) < 0;
}

inline bool operator==(const DeviceInfoData& lhs, const DeviceInfoData& rhs)
{
return lhs.activeZoneSet == rhs.activeZoneSet && lhs.showSpacing == rhs.showSpacing && lhs.spacing == rhs.spacing && lhs.zoneCount == rhs.zoneCount && lhs.sensitivityRadius == rhs.sensitivityRadius;
}
}

namespace std
Expand Down
252 changes: 252 additions & 0 deletions src/modules/fancyzones/FancyZonesTests/UnitTests/JsonHelpers.Tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,258 @@ namespace FancyZonesUnitTests

Assert::IsFalse(data.RemoveAppLastZone(nullptr, deviceId, zoneSetId));
}

TEST_METHOD (AddDevice)
{
FancyZonesDataTypes::DeviceIdData expected{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};

auto result = m_fzData.AddDevice(expected);
Assert::IsTrue(result);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(expected) == actualMap.end());
}

TEST_METHOD (AddDeviceWithNullVirtualDesktopId)
{
FancyZonesDataTypes::DeviceIdData expected{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = GUID_NULL
};

auto result = m_fzData.AddDevice(expected);
Assert::IsTrue(result);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(expected) == actualMap.end());
}

TEST_METHOD (AddDeviceDuplicate)
{
FancyZonesDataTypes::DeviceIdData expected{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};

auto result = m_fzData.AddDevice(expected);
Assert::IsTrue(result);

auto result2 = m_fzData.AddDevice(expected);
Assert::IsFalse(result2);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(expected) == actualMap.end());
}

TEST_METHOD (AddDeviceWithNullVirtualDesktopIdDuplicated)
{
FancyZonesDataTypes::DeviceIdData expected{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = GUID_NULL
};

auto result = m_fzData.AddDevice(expected);
Assert::IsTrue(result);

auto result2 = m_fzData.AddDevice(expected);
Assert::IsFalse(result2);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(expected) == actualMap.end());
}

TEST_METHOD (AddDeviceDuplicatedComparedWithNillVirtualDesktopId)
{
FancyZonesDataTypes::DeviceIdData device1{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};

FancyZonesDataTypes::DeviceIdData device2{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = GUID_NULL
};

auto result = m_fzData.AddDevice(device1);
Assert::IsTrue(result);

auto result2 = m_fzData.AddDevice(device2);
Assert::IsFalse(result2);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(device1) == actualMap.end());
Assert::IsTrue(actualMap.find(device2) == actualMap.end());
}

TEST_METHOD (AddDeviceDuplicatedComparedWithNillVirtualDesktopId2)
{
FancyZonesDataTypes::DeviceIdData device1{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};

FancyZonesDataTypes::DeviceIdData device2{
.deviceName = L"Device",
.width = 200,
.height = 100,
.virtualDesktopId = GUID_NULL
};

auto result2 = m_fzData.AddDevice(device2);
Assert::IsTrue(result2);

auto result1 = m_fzData.AddDevice(device1);
Assert::IsFalse(result1);

auto actualMap = m_fzData.GetDeviceInfoMap();

Assert::IsFalse(actualMap.find(device2) == actualMap.end());
Assert::IsTrue(actualMap.find(device1) == actualMap.end());
}

TEST_METHOD(CloneDeviceInfo)
{
FancyZonesDataTypes::DeviceIdData deviceSrc{
.deviceName = L"Device1",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};
FancyZonesDataTypes::DeviceIdData deviceDst{
.deviceName = L"Device2",
.width = 300,
.height = 400,
.virtualDesktopId = m_defaultVDId
};

Assert::IsTrue(m_fzData.AddDevice(deviceSrc));
Assert::IsTrue(m_fzData.AddDevice(deviceDst));

m_fzData.CloneDeviceInfo(deviceSrc, deviceDst);

auto actualMap = m_fzData.GetDeviceInfoMap();
Assert::IsFalse(actualMap.find(deviceSrc) == actualMap.end());
Assert::IsFalse(actualMap.find(deviceDst) == actualMap.end());

auto expected = m_fzData.FindDeviceInfo(deviceSrc);
auto actual = m_fzData.FindDeviceInfo(deviceDst);

Assert::IsTrue(expected.has_value());
Assert::IsTrue(actual.has_value());
Assert::IsTrue(expected.value() == actual.value());
}

TEST_METHOD (CloneDeviceInfoIntoUnknownDevice)
{
FancyZonesDataTypes::DeviceIdData deviceSrc{
.deviceName = L"Device1",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};
FancyZonesDataTypes::DeviceIdData deviceDst{
.deviceName = L"Device2",
.width = 300,
.height = 400,
.virtualDesktopId = m_defaultVDId
};

Assert::IsTrue(m_fzData.AddDevice(deviceSrc));

m_fzData.CloneDeviceInfo(deviceSrc, deviceDst);

auto actualMap = m_fzData.GetDeviceInfoMap();
Assert::IsFalse(actualMap.find(deviceSrc) == actualMap.end());
Assert::IsFalse(actualMap.find(deviceDst) == actualMap.end());

auto expected = m_fzData.FindDeviceInfo(deviceSrc);
auto actual = m_fzData.FindDeviceInfo(deviceDst);

Assert::IsTrue(expected.has_value());
Assert::IsTrue(actual.has_value());
Assert::IsTrue(expected.value() == actual.value());
}

TEST_METHOD (CloneDeviceInfoFromUnknownDevice)
{
FancyZonesDataTypes::DeviceIdData deviceSrc{
.deviceName = L"Device1",
.width = 200,
.height = 100,
.virtualDesktopId = m_defaultVDId
};
FancyZonesDataTypes::DeviceIdData deviceDst{
.deviceName = L"Device2",
.width = 300,
.height = 400,
.virtualDesktopId = m_defaultVDId
};

Assert::IsTrue(m_fzData.AddDevice(deviceDst));

m_fzData.CloneDeviceInfo(deviceSrc, deviceDst);

auto actualMap = m_fzData.GetDeviceInfoMap();
Assert::IsTrue(actualMap.find(deviceSrc) == actualMap.end());
Assert::IsFalse(actualMap.find(deviceDst) == actualMap.end());

Assert::IsFalse(m_fzData.FindDeviceInfo(deviceSrc).has_value());
Assert::IsTrue(m_fzData.FindDeviceInfo(deviceDst).has_value());
}

TEST_METHOD(CloneDeviceInfoNullVirtualDesktopId)
{
FancyZonesDataTypes::DeviceIdData deviceSrc{
.deviceName = L"Device1",
.width = 200,
.height = 100,
.virtualDesktopId = GUID_NULL
};
FancyZonesDataTypes::DeviceIdData deviceDst{
.deviceName = L"Device2",
.width = 300,
.height = 400,
.virtualDesktopId = m_defaultVDId
};

Assert::IsTrue(m_fzData.AddDevice(deviceSrc));
Assert::IsTrue(m_fzData.AddDevice(deviceDst));

m_fzData.CloneDeviceInfo(deviceSrc, deviceDst);

auto actualMap = m_fzData.GetDeviceInfoMap();
Assert::IsFalse(actualMap.find(deviceSrc) == actualMap.end());
Assert::IsFalse(actualMap.find(deviceDst) == actualMap.end());

auto expected = m_fzData.FindDeviceInfo(deviceSrc);
auto actual = m_fzData.FindDeviceInfo(deviceDst);

Assert::IsTrue(expected.has_value());
Assert::IsTrue(actual.has_value());
Assert::IsTrue(expected.value() == actual.value());
}
};

TEST_CLASS(EditorArgsUnitTests)
Expand Down

0 comments on commit afdefa2

Please sign in to comment.