Skip to content

Commit fcd05f2

Browse files
[FancyZones] Improve code quality (part 5: work area initialization) (#23671)
1 parent c1c14b4 commit fcd05f2

File tree

12 files changed

+469
-627
lines changed

12 files changed

+469
-627
lines changed

src/modules/fancyzones/FancyZonesLib/FancyZones.cpp

Lines changed: 92 additions & 116 deletions
Large diffs are not rendered by default.

src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<ClInclude Include="LayoutAssignedWindows.h" />
6060
<ClInclude Include="ModuleConstants.h" />
6161
<ClInclude Include="MonitorUtils.h" />
62-
<ClInclude Include="MonitorWorkAreaHandler.h" />
62+
<ClInclude Include="MonitorWorkAreaMap.h" />
6363
<ClInclude Include="NotificationUtil.h" />
6464
<ClInclude Include="pch.h" />
6565
<ClInclude Include="Generated Files/resource.h" />
@@ -113,7 +113,7 @@
113113
<ClCompile Include="LayoutConfigurator.cpp" />
114114
<ClCompile Include="LayoutAssignedWindows.cpp" />
115115
<ClCompile Include="MonitorUtils.cpp" />
116-
<ClCompile Include="MonitorWorkAreaHandler.cpp" />
116+
<ClCompile Include="MonitorWorkAreaMap.cpp" />
117117
<ClCompile Include="OnThreadExecutor.cpp" />
118118
<ClCompile Include="pch.cpp">
119119
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>

src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<ClInclude Include="SecondaryMouseButtonsHook.h">
5555
<Filter>Header Files</Filter>
5656
</ClInclude>
57-
<ClInclude Include="MonitorWorkAreaHandler.h">
57+
<ClInclude Include="MonitorWorkAreaMap.h">
5858
<Filter>Header Files</Filter>
5959
</ClInclude>
6060
<ClInclude Include="GenericKeyHook.h">
@@ -197,7 +197,7 @@
197197
<ClCompile Include="SecondaryMouseButtonsHook.cpp">
198198
<Filter>Source Files</Filter>
199199
</ClCompile>
200-
<ClCompile Include="MonitorWorkAreaHandler.cpp">
200+
<ClCompile Include="MonitorWorkAreaMap.cpp">
201201
<Filter>Source Files</Filter>
202202
</ClCompile>
203203
<ClCompile Include="FancyZonesData.cpp">

src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaHandler.cpp

Lines changed: 0 additions & 141 deletions
This file was deleted.

src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaHandler.h

Lines changed: 0 additions & 93 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include "pch.h"
2+
#include "MonitorWorkAreaMap.h"
3+
4+
#include <FancyZonesLib/WorkArea.h>
5+
6+
WorkArea* const MonitorWorkAreaMap::GetWorkArea(HMONITOR monitor) const
7+
{
8+
auto iter = m_workAreaMap.find(monitor);
9+
if (iter != m_workAreaMap.end())
10+
{
11+
return iter->second.get();
12+
}
13+
14+
return nullptr;
15+
}
16+
17+
WorkArea* const MonitorWorkAreaMap::GetWorkAreaFromCursor() const
18+
{
19+
const auto allMonitorsWorkArea = GetWorkArea(nullptr);
20+
if (allMonitorsWorkArea)
21+
{
22+
// First, check if there's a work area spanning all monitors (signalled by the NULL monitor handle)
23+
return allMonitorsWorkArea;
24+
}
25+
else
26+
{
27+
// Otherwise, look for the work area based on cursor position
28+
POINT cursorPoint;
29+
if (!GetCursorPos(&cursorPoint))
30+
{
31+
return nullptr;
32+
}
33+
34+
return GetWorkArea(MonitorFromPoint(cursorPoint, MONITOR_DEFAULTTONULL));
35+
}
36+
}
37+
38+
WorkArea* const MonitorWorkAreaMap::GetWorkAreaFromWindow(HWND window) const
39+
{
40+
const auto allMonitorsWorkArea = GetWorkArea(nullptr);
41+
if (allMonitorsWorkArea)
42+
{
43+
// First, check if there's a work area spanning all monitors (signalled by the NULL monitor handle)
44+
return allMonitorsWorkArea;
45+
}
46+
else
47+
{
48+
// Otherwise, look for the work area based on the window's position
49+
HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
50+
return GetWorkArea(monitor);
51+
}
52+
}
53+
54+
const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& MonitorWorkAreaMap::GetAllWorkAreas() const noexcept
55+
{
56+
return m_workAreaMap;
57+
}
58+
59+
void MonitorWorkAreaMap::AddWorkArea(HMONITOR monitor, std::unique_ptr<WorkArea> workArea)
60+
{
61+
m_workAreaMap.insert({ monitor, std::move(workArea) });
62+
}
63+
64+
void MonitorWorkAreaMap::Clear() noexcept
65+
{
66+
m_workAreaMap.clear();
67+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
3+
#include "GuidUtils.h"
4+
5+
class WorkArea;
6+
7+
class MonitorWorkAreaMap
8+
{
9+
public:
10+
/**
11+
* Get work area based on virtual desktop id and monitor handle.
12+
*
13+
* @param[in] monitor Monitor handle.
14+
*
15+
* @returns Object representing single work area, interface to all actions available on work area
16+
* (e.g. moving windows through zone layout specified for that work area).
17+
*/
18+
WorkArea* const GetWorkArea(HMONITOR monitor) const;
19+
20+
/**
21+
* Get work area based on virtual desktop id and the current cursor position.
22+
*
23+
* @returns Object representing single work area, interface to all actions available on work area
24+
* (e.g. moving windows through zone layout specified for that work area).
25+
*/
26+
WorkArea* const GetWorkAreaFromCursor() const;
27+
28+
/**
29+
* Get work area on which specified window is located.
30+
*
31+
* @param[in] window Window handle.
32+
*
33+
* @returns Object representing single work area, interface to all actions available on work area
34+
* (e.g. moving windows through zone layout specified for that work area).
35+
*/
36+
WorkArea* const GetWorkAreaFromWindow(HWND window) const;
37+
38+
/**
39+
* @returns All registered work areas.
40+
*/
41+
const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& GetAllWorkAreas() const noexcept;
42+
43+
/**
44+
* Register new work area.
45+
*
46+
* @param[in] monitor Monitor handle.
47+
* @param[in] workAra Object representing single work area.
48+
*/
49+
void AddWorkArea(HMONITOR monitor, std::unique_ptr<WorkArea> workArea);
50+
51+
/**
52+
* Clear all persisted work area related data.
53+
*/
54+
void Clear() noexcept;
55+
56+
private:
57+
std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>> m_workAreaMap;
58+
};

0 commit comments

Comments
 (0)