Skip to content

Commit

Permalink
[FancyZones] Improve code quality (part 5: work area initialization) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SeraphimaZykova committed Jan 31, 2023
1 parent c1c14b4 commit fcd05f2
Show file tree
Hide file tree
Showing 12 changed files with 469 additions and 627 deletions.
208 changes: 92 additions & 116 deletions src/modules/fancyzones/FancyZonesLib/FancyZones.cpp

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<ClInclude Include="LayoutAssignedWindows.h" />
<ClInclude Include="ModuleConstants.h" />
<ClInclude Include="MonitorUtils.h" />
<ClInclude Include="MonitorWorkAreaHandler.h" />
<ClInclude Include="MonitorWorkAreaMap.h" />
<ClInclude Include="NotificationUtil.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="Generated Files/resource.h" />
Expand Down Expand Up @@ -113,7 +113,7 @@
<ClCompile Include="LayoutConfigurator.cpp" />
<ClCompile Include="LayoutAssignedWindows.cpp" />
<ClCompile Include="MonitorUtils.cpp" />
<ClCompile Include="MonitorWorkAreaHandler.cpp" />
<ClCompile Include="MonitorWorkAreaMap.cpp" />
<ClCompile Include="OnThreadExecutor.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(CIBuild)'!='true'">Create</PrecompiledHeader>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<ClInclude Include="SecondaryMouseButtonsHook.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="MonitorWorkAreaHandler.h">
<ClInclude Include="MonitorWorkAreaMap.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="GenericKeyHook.h">
Expand Down Expand Up @@ -197,7 +197,7 @@
<ClCompile Include="SecondaryMouseButtonsHook.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="MonitorWorkAreaHandler.cpp">
<ClCompile Include="MonitorWorkAreaMap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="FancyZonesData.cpp">
Expand Down
141 changes: 0 additions & 141 deletions src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaHandler.cpp

This file was deleted.

93 changes: 0 additions & 93 deletions src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaHandler.h

This file was deleted.

67 changes: 67 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaMap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "pch.h"
#include "MonitorWorkAreaMap.h"

#include <FancyZonesLib/WorkArea.h>

WorkArea* const MonitorWorkAreaMap::GetWorkArea(HMONITOR monitor) const
{
auto iter = m_workAreaMap.find(monitor);
if (iter != m_workAreaMap.end())
{
return iter->second.get();
}

return nullptr;
}

WorkArea* const MonitorWorkAreaMap::GetWorkAreaFromCursor() const
{
const auto allMonitorsWorkArea = GetWorkArea(nullptr);
if (allMonitorsWorkArea)
{
// First, check if there's a work area spanning all monitors (signalled by the NULL monitor handle)
return allMonitorsWorkArea;
}
else
{
// Otherwise, look for the work area based on cursor position
POINT cursorPoint;
if (!GetCursorPos(&cursorPoint))
{
return nullptr;
}

return GetWorkArea(MonitorFromPoint(cursorPoint, MONITOR_DEFAULTTONULL));
}
}

WorkArea* const MonitorWorkAreaMap::GetWorkAreaFromWindow(HWND window) const
{
const auto allMonitorsWorkArea = GetWorkArea(nullptr);
if (allMonitorsWorkArea)
{
// First, check if there's a work area spanning all monitors (signalled by the NULL monitor handle)
return allMonitorsWorkArea;
}
else
{
// Otherwise, look for the work area based on the window's position
HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
return GetWorkArea(monitor);
}
}

const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& MonitorWorkAreaMap::GetAllWorkAreas() const noexcept
{
return m_workAreaMap;
}

void MonitorWorkAreaMap::AddWorkArea(HMONITOR monitor, std::unique_ptr<WorkArea> workArea)
{
m_workAreaMap.insert({ monitor, std::move(workArea) });
}

void MonitorWorkAreaMap::Clear() noexcept
{
m_workAreaMap.clear();
}
58 changes: 58 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/MonitorWorkAreaMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

#include "GuidUtils.h"

class WorkArea;

class MonitorWorkAreaMap
{
public:
/**
* Get work area based on virtual desktop id and monitor handle.
*
* @param[in] monitor Monitor handle.
*
* @returns Object representing single work area, interface to all actions available on work area
* (e.g. moving windows through zone layout specified for that work area).
*/
WorkArea* const GetWorkArea(HMONITOR monitor) const;

/**
* Get work area based on virtual desktop id and the current cursor position.
*
* @returns Object representing single work area, interface to all actions available on work area
* (e.g. moving windows through zone layout specified for that work area).
*/
WorkArea* const GetWorkAreaFromCursor() const;

/**
* Get work area on which specified window is located.
*
* @param[in] window Window handle.
*
* @returns Object representing single work area, interface to all actions available on work area
* (e.g. moving windows through zone layout specified for that work area).
*/
WorkArea* const GetWorkAreaFromWindow(HWND window) const;

/**
* @returns All registered work areas.
*/
const std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>>& GetAllWorkAreas() const noexcept;

/**
* Register new work area.
*
* @param[in] monitor Monitor handle.
* @param[in] workAra Object representing single work area.
*/
void AddWorkArea(HMONITOR monitor, std::unique_ptr<WorkArea> workArea);

/**
* Clear all persisted work area related data.
*/
void Clear() noexcept;

private:
std::unordered_map<HMONITOR, std::unique_ptr<WorkArea>> m_workAreaMap;
};
Loading

0 comments on commit fcd05f2

Please sign in to comment.