Skip to content

Commit

Permalink
[FancyZones] Zone indexes type change (#12724)
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova committed Aug 11, 2021
1 parent 7278d62 commit 7ea1f26
Show file tree
Hide file tree
Showing 24 changed files with 239 additions and 215 deletions.
48 changes: 18 additions & 30 deletions src/modules/fancyzones/FancyZonesLib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <common/SettingsAPI/FileWatcher.h>

#include <FancyZonesLib/FancyZonesData.h>
#include <FancyZonesLib/FancyZonesWindowProperties.h>
#include <FancyZonesLib/FancyZonesWinHookEventIDs.h>
#include <FancyZonesLib/MonitorUtils.h>
#include <FancyZonesLib/Settings.h>
Expand Down Expand Up @@ -156,8 +157,8 @@ struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZone

void OnSettingsChanged() noexcept;

std::pair<winrt::com_ptr<IWorkArea>, std::vector<size_t>> GetAppZoneHistoryInfo(HWND window, HMONITOR monitor, bool isPrimaryMonitor) noexcept;
void MoveWindowIntoZone(HWND window, winrt::com_ptr<IWorkArea> zoneWindow, const std::vector<size_t>& zoneIndexSet) noexcept;
std::pair<winrt::com_ptr<IWorkArea>, ZoneIndexSet> GetAppZoneHistoryInfo(HWND window, HMONITOR monitor, bool isPrimaryMonitor) noexcept;
void MoveWindowIntoZone(HWND window, winrt::com_ptr<IWorkArea> zoneWindow, const ZoneIndexSet& zoneIndexSet) noexcept;

void OnEditorExitEvent() noexcept;
void UpdateZoneSets() noexcept;
Expand Down Expand Up @@ -265,9 +266,9 @@ FancyZones::VirtualDesktopChanged() noexcept
PostMessage(m_window, WM_PRIV_VD_SWITCH, 0, 0);
}

std::pair<winrt::com_ptr<IWorkArea>, std::vector<size_t>> FancyZones::GetAppZoneHistoryInfo(HWND window, HMONITOR monitor, bool isPrimaryMonitor) noexcept
std::pair<winrt::com_ptr<IWorkArea>, ZoneIndexSet> FancyZones::GetAppZoneHistoryInfo(HWND window, HMONITOR monitor, bool isPrimaryMonitor) noexcept
{
std::pair<winrt::com_ptr<IWorkArea>, std::vector<size_t>> appZoneHistoryInfo{ nullptr, {} };
std::pair<winrt::com_ptr<IWorkArea>, ZoneIndexSet> appZoneHistoryInfo{ nullptr, {} };
auto workAreaMap = m_workAreaHandler.GetWorkAreasByDesktopId(m_currentDesktopId);
if (workAreaMap.empty())
{
Expand Down Expand Up @@ -301,7 +302,7 @@ std::pair<winrt::com_ptr<IWorkArea>, std::vector<size_t>> FancyZones::GetAppZone
return appZoneHistoryInfo;
}

void FancyZones::MoveWindowIntoZone(HWND window, winrt::com_ptr<IWorkArea> zoneWindow, const std::vector<size_t>& zoneIndexSet) noexcept
void FancyZones::MoveWindowIntoZone(HWND window, winrt::com_ptr<IWorkArea> zoneWindow, const ZoneIndexSet& zoneIndexSet) noexcept
{
_TRACER_;
auto& fancyZonesData = FancyZonesDataInstance();
Expand All @@ -328,7 +329,7 @@ void FancyZones::WindowCreated(HWND window) noexcept
// Avoid processing splash screens, already stamped (zoned) windows, or those windows
// that belong to excluded applications list.
const bool isSplashScreen = FancyZonesUtils::IsSplashScreen(window);
const bool isZoned = reinterpret_cast<size_t>(::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID)) != 0;
const bool isZoned = reinterpret_cast<ZoneIndex>(::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID)) != 0;
const bool isCandidateForLastKnownZone = FancyZonesUtils::IsCandidateForLastKnownZone(window, m_settings->GetSettings()->excludedAppsArray);
const bool shouldProcessNewWindow = !isSplashScreen && !isZoned && isCandidateForLastKnownZone;

Expand All @@ -347,7 +348,7 @@ void FancyZones::WindowCreated(HWND window) noexcept
if (moveToAppLastZone)
{
const bool primaryActive = (primary == active);
std::pair<winrt::com_ptr<IWorkArea>, std::vector<size_t>> appZoneHistoryInfo = GetAppZoneHistoryInfo(window, active, primaryActive);
std::pair<winrt::com_ptr<IWorkArea>, ZoneIndexSet> appZoneHistoryInfo = GetAppZoneHistoryInfo(window, active, primaryActive);

if (!appZoneHistoryInfo.second.empty())
{
Expand Down Expand Up @@ -828,31 +829,18 @@ void FancyZones::UpdateZoneWindows() noexcept
void FancyZones::UpdateWindowsPositions() noexcept
{
auto callback = [](HWND window, LPARAM data) -> BOOL {
size_t bitmask = reinterpret_cast<size_t>(::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID));

if (bitmask != 0)
auto zoneIndexSet = GetZoneIndexSet(window);
auto strongThis = reinterpret_cast<FancyZones*>(data);
auto desktopId = strongThis->m_virtualDesktop.GetWindowDesktopId(window);
if (desktopId.has_value())
{
std::vector<size_t> indexSet;
for (int i = 0; i < std::numeric_limits<size_t>::digits; i++)
{
if ((1ull << i) & bitmask)
{
indexSet.push_back(i);
}
}

auto strongThis = reinterpret_cast<FancyZones*>(data);
auto desktopId = strongThis->m_virtualDesktop.GetWindowDesktopId(window);
if (desktopId.has_value())
auto zoneWindow = strongThis->m_workAreaHandler.GetWorkArea(window, *desktopId);
if (zoneWindow)
{
auto zoneWindow = strongThis->m_workAreaHandler.GetWorkArea(window, *desktopId);
if (zoneWindow)
{
strongThis->m_windowMoveHandler.MoveWindowIntoZoneByIndexSet(window, indexSet, zoneWindow);
}
strongThis->m_windowMoveHandler.MoveWindowIntoZoneByIndexSet(window, zoneIndexSet, zoneWindow);
}

}

return TRUE;
};
EnumWindows(callback, reinterpret_cast<LPARAM>(this));
Expand Down Expand Up @@ -933,7 +921,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept

// If that didn't work, extract zones from all other monitors and target one of them
std::vector<RECT> zoneRects;
std::vector<std::pair<size_t, winrt::com_ptr<IWorkArea>>> zoneRectsInfo;
std::vector<std::pair<ZoneIndex, winrt::com_ptr<IWorkArea>>> zoneRectsInfo;
RECT currentMonitorRect{ .top = 0, .bottom = -1 };

for (const auto& [monitor, monitorRect] : allMonitors)
Expand Down Expand Up @@ -975,7 +963,7 @@ bool FancyZones::OnSnapHotkeyBasedOnPosition(HWND window, DWORD vkCode) noexcept
return false;
}

size_t chosenIdx = FancyZonesUtils::ChooseNextZoneByPosition(vkCode, windowRect, zoneRects);
auto chosenIdx = FancyZonesUtils::ChooseNextZoneByPosition(vkCode, windowRect, zoneRects);

if (chosenIdx < zoneRects.size())
{
Expand Down
9 changes: 5 additions & 4 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <common/Display/dpi_aware.h>
#include <common/utils/json.h>
#include <FancyZonesLib/util.h>
#include <FancyZonesLib/FancyZonesWindowProperties.h>

#include <shlwapi.h>
#include <filesystem>
Expand Down Expand Up @@ -375,7 +376,7 @@ void FancyZonesData::UpdateProcessIdToHandleMap(HWND window, const std::wstring_
}
}

std::vector<size_t> FancyZonesData::GetAppLastZoneIndexSet(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const
ZoneIndexSet FancyZonesData::GetAppLastZoneIndexSet(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const
{
std::scoped_lock lock{ dataLock };
auto processPath = get_process_path(window);
Expand Down Expand Up @@ -422,10 +423,10 @@ bool FancyZonesData::RemoveAppLastZone(HWND window, const std::wstring_view& dev
}

// if there is another instance of same application placed in the same zone don't erase history
size_t windowZoneStamp = reinterpret_cast<size_t>(::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID));
ZoneIndex windowZoneStamp = reinterpret_cast<ZoneIndex>(::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID));
for (auto placedWindow : data->processIdToHandleMap)
{
size_t placedWindowZoneStamp = reinterpret_cast<size_t>(::GetProp(placedWindow.second, ZonedWindowProperties::PropertyMultipleZoneID));
ZoneIndex placedWindowZoneStamp = reinterpret_cast<ZoneIndex>(::GetProp(placedWindow.second, ZonedWindowProperties::PropertyMultipleZoneID));
if (IsWindow(placedWindow.second) && (windowZoneStamp == placedWindowZoneStamp))
{
return false;
Expand All @@ -451,7 +452,7 @@ bool FancyZonesData::RemoveAppLastZone(HWND window, const std::wstring_view& dev
return false;
}

bool FancyZonesData::SetAppLastZones(HWND window, const std::wstring& deviceId, const std::wstring& zoneSetId, const std::vector<size_t>& zoneIndexSet)
bool FancyZonesData::SetAppLastZones(HWND window, const std::wstring& deviceId, const std::wstring& zoneSetId, const ZoneIndexSet& zoneIndexSet)
{
_TRACER_;
std::scoped_lock lock{ dataLock };
Expand Down
4 changes: 2 additions & 2 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesData.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ class FancyZonesData

bool IsAnotherWindowOfApplicationInstanceZoned(HWND window, const std::wstring_view& deviceId) const;
void UpdateProcessIdToHandleMap(HWND window, const std::wstring_view& deviceId);
std::vector<size_t> GetAppLastZoneIndexSet(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const;
ZoneIndexSet GetAppLastZoneIndexSet(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId) const;
bool RemoveAppLastZone(HWND window, const std::wstring_view& deviceId, const std::wstring_view& zoneSetId);
bool SetAppLastZones(HWND window, const std::wstring& deviceId, const std::wstring& zoneSetId, const std::vector<size_t>& zoneIndexSet);
bool SetAppLastZones(HWND window, const std::wstring& deviceId, const std::wstring& zoneSetId, const ZoneIndexSet& zoneIndexSet);

void SetActiveZoneSet(const std::wstring& deviceId, const FancyZonesDataTypes::ZoneSetData& zoneSet);

Expand Down
4 changes: 3 additions & 1 deletion src/modules/fancyzones/FancyZonesLib/FancyZonesDataTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#include <windef.h>

#include <FancyZonesLib/Zone.h>

namespace FancyZonesDataTypes
{
enum class ZoneSetLayoutType : int
Expand Down Expand Up @@ -117,7 +119,7 @@ namespace FancyZonesDataTypes

std::wstring zoneSetUuid;
std::wstring deviceId;
std::vector<size_t> zoneIndexSet;
ZoneIndexSet zoneIndexSet;
};

struct DeviceIdData
Expand Down
1 change: 1 addition & 0 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<ClInclude Include="util.h" />
<ClInclude Include="VirtualDesktop.h" />
<ClInclude Include="WindowMoveHandler.h" />
<ClInclude Include="FancyZonesWindowProperties.h" />
<ClInclude Include="Zone.h" />
<ClInclude Include="ZoneColors.h" />
<ClInclude Include="ZoneSet.h" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@
<ClInclude Include="ZoneColors.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="FancyZonesWindowProperties.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="pch.cpp">
Expand Down
46 changes: 46 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesWindowProperties.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#pragma once

#include <vector>

#include <FancyZonesLib/Zone.h>

// Zoned window properties are not localized.
namespace ZonedWindowProperties
{
const wchar_t PropertyMultipleZoneID[] = L"FancyZones_zones";
const wchar_t PropertyRestoreSizeID[] = L"FancyZones_RestoreSize";
const wchar_t PropertyRestoreOriginID[] = L"FancyZones_RestoreOrigin";

const wchar_t MultiMonitorDeviceID[] = L"FancyZones#MultiMonitorDevice";
}

inline ZoneIndexSet GetZoneIndexSet(HWND window)
{
HANDLE handle = ::GetProp(window, ZonedWindowProperties::PropertyMultipleZoneID);
ZoneIndexSet zoneIndexSet;

std::array<int, 2> data;
memcpy(data.data(), &handle, sizeof data);
uint64_t bitmask = ((uint64_t)data[0] << 32) + data[1];

if (bitmask != 0)
{
for (int i = 0; i < std::numeric_limits<ZoneIndex>::digits; i++)
{
if ((1ull << i) & bitmask)
{
zoneIndexSet.push_back(i);
}
}
}

return zoneIndexSet;
}

inline void StampWindow(HWND window, Bitmask bitmask) noexcept
{
std::array<int, 2> data = { (bitmask >> 32), static_cast<int>(bitmask) };
HANDLE rawData;
memcpy(&rawData, data.data(), sizeof data);
SetProp(window, ZonedWindowProperties::PropertyMultipleZoneID, rawData);
}
6 changes: 3 additions & 3 deletions src/modules/fancyzones/FancyZonesLib/JsonHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ namespace
data.zoneIndexSet = {};
for (const auto& value : json.GetNamedArray(NonLocalizable::ZoneIndexSetStr))
{
data.zoneIndexSet.push_back(static_cast<size_t>(value.GetNumber()));
data.zoneIndexSet.push_back(static_cast<ZoneIndex>(value.GetNumber()));
}
}
else if (json.HasKey(NonLocalizable::ZoneIndexStr))
{
data.zoneIndexSet = { static_cast<size_t>(json.GetNamedNumber(NonLocalizable::ZoneIndexStr)) };
data.zoneIndexSet = { static_cast<ZoneIndex>(json.GetNamedNumber(NonLocalizable::ZoneIndexStr)) };
}

data.deviceId = json.GetNamedString(NonLocalizable::DeviceIdStr);
Expand Down Expand Up @@ -371,7 +371,7 @@ namespace JSONHelpers
{
json::JsonObject desktopData;
json::JsonArray jsonIndexSet;
for (size_t index : data.zoneIndexSet)
for (ZoneIndex index : data.zoneIndexSet)
{
jsonIndexSet.Append(json::value(static_cast<int>(index)));
}
Expand Down
10 changes: 0 additions & 10 deletions src/modules/fancyzones/FancyZonesLib/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,6 @@

#include <common/SettingsAPI/settings_objects.h>

// Zoned window properties are not localized.
namespace ZonedWindowProperties
{
const wchar_t PropertyMultipleZoneID[] = L"FancyZones_zones";
const wchar_t PropertyRestoreSizeID[] = L"FancyZones_RestoreSize";
const wchar_t PropertyRestoreOriginID[] = L"FancyZones_RestoreOrigin";

const wchar_t MultiMonitorDeviceID[] = L"FancyZones#MultiMonitorDevice";
}

enum struct OverlappingZonesAlgorithm : int
{
Smallest = 0,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ void WindowMoveHandler::MoveSizeEnd(HWND window, POINT const& ptScreen, const st
}
}

void WindowMoveHandler::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet, winrt::com_ptr<IWorkArea> zoneWindow) noexcept
void WindowMoveHandler::MoveWindowIntoZoneByIndexSet(HWND window, const ZoneIndexSet& indexSet, winrt::com_ptr<IWorkArea> zoneWindow) noexcept
{
if (window != m_windowMoveSize)
{
Expand Down
3 changes: 2 additions & 1 deletion src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "FancyZonesWindowProperties.h"
#include "KeyState.h"
#include "SecondaryMouseButtonsHook.h"

Expand All @@ -17,7 +18,7 @@ class WindowMoveHandler
void MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IWorkArea>>& zoneWindowMap) noexcept;
void MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IWorkArea>>& zoneWindowMap) noexcept;

void MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet, winrt::com_ptr<IWorkArea> zoneWindow) noexcept;
void MoveWindowIntoZoneByIndexSet(HWND window, const ZoneIndexSet& indexSet, winrt::com_ptr<IWorkArea> zoneWindow) noexcept;
bool MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr<IWorkArea> zoneWindow) noexcept;
bool MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr<IWorkArea> zoneWindow) noexcept;
bool ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr<IWorkArea> zoneWindow) noexcept;
Expand Down
20 changes: 10 additions & 10 deletions src/modules/fancyzones/FancyZonesLib/WorkArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ struct WorkArea : public winrt::implements<WorkArea, IWorkArea>
IFACEMETHODIMP MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool selectManyZones) noexcept;
IFACEMETHODIMP MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
IFACEMETHODIMP_(void)
MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept;
MoveWindowIntoZoneByIndex(HWND window, ZoneIndex index) noexcept;
IFACEMETHODIMP_(void)
MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept;
MoveWindowIntoZoneByIndexSet(HWND window, const ZoneIndexSet& indexSet) noexcept;
IFACEMETHODIMP_(bool)
MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle) noexcept;
IFACEMETHODIMP_(bool)
Expand All @@ -130,7 +130,7 @@ struct WorkArea : public winrt::implements<WorkArea, IWorkArea>
SaveWindowProcessToZoneIndex(HWND window) noexcept;
IFACEMETHODIMP_(IZoneSet*)
ActiveZoneSet() const noexcept { return m_activeZoneSet.get(); }
IFACEMETHODIMP_(std::vector<size_t>)
IFACEMETHODIMP_(ZoneIndexSet)
GetWindowZoneIndexes(HWND window) const noexcept;
IFACEMETHODIMP_(void)
ShowZoneWindow() noexcept;
Expand All @@ -155,7 +155,7 @@ struct WorkArea : public winrt::implements<WorkArea, IWorkArea>
void CalculateZoneSet(OverlappingZonesAlgorithm overlappingAlgorithm) noexcept;
void UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept;
LRESULT WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept;
std::vector<size_t> ZonesFromPoint(POINT pt) noexcept;
ZoneIndexSet ZonesFromPoint(POINT pt) noexcept;
void SetAsTopmostWindow() noexcept;

HMONITOR m_monitor{};
Expand All @@ -164,8 +164,8 @@ struct WorkArea : public winrt::implements<WorkArea, IWorkArea>
HWND m_windowMoveSize{};
winrt::com_ptr<IZoneSet> m_activeZoneSet;
std::vector<winrt::com_ptr<IZoneSet>> m_zoneSets;
std::vector<size_t> m_initialHighlightZone;
std::vector<size_t> m_highlightZone;
ZoneIndexSet m_initialHighlightZone;
ZoneIndexSet m_highlightZone;
WPARAM m_keyLast{};
size_t m_keyCycle{};
std::unique_ptr<ZoneWindowDrawing> m_zoneWindowDrawing;
Expand Down Expand Up @@ -305,13 +305,13 @@ IFACEMETHODIMP WorkArea::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcep
}

IFACEMETHODIMP_(void)
WorkArea::MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept
WorkArea::MoveWindowIntoZoneByIndex(HWND window, ZoneIndex index) noexcept
{
MoveWindowIntoZoneByIndexSet(window, { index });
}

IFACEMETHODIMP_(void)
WorkArea::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept
WorkArea::MoveWindowIntoZoneByIndexSet(HWND window, const ZoneIndexSet& indexSet) noexcept
{
if (m_activeZoneSet)
{
Expand Down Expand Up @@ -383,7 +383,7 @@ WorkArea::SaveWindowProcessToZoneIndex(HWND window) noexcept
}
}

IFACEMETHODIMP_(std::vector<size_t>)
IFACEMETHODIMP_(ZoneIndexSet)
WorkArea::GetWindowZoneIndexes(HWND window) const noexcept
{
if (m_activeZoneSet)
Expand Down Expand Up @@ -575,7 +575,7 @@ LRESULT WorkArea::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
return 0;
}

std::vector<size_t> WorkArea::ZonesFromPoint(POINT pt) noexcept
ZoneIndexSet WorkArea::ZonesFromPoint(POINT pt) noexcept
{
if (m_activeZoneSet)
{
Expand Down

0 comments on commit 7ea1f26

Please sign in to comment.