Skip to content

Commit

Permalink
[FancyZones] Improve code quality (part 2: WorkArea init) (#23030)
Browse files Browse the repository at this point in the history
* init WorkArea with a rectangle
* keep the highlighted zones state in a separate class
  • Loading branch information
SeraphimaZykova committed Jan 23, 2023
1 parent cc5633d commit 6f0b16d
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 162 deletions.
12 changes: 11 additions & 1 deletion src/modules/fancyzones/FancyZonesLib/FancyZones.cpp
Expand Up @@ -733,7 +733,17 @@ void FancyZones::AddWorkArea(HMONITOR monitor, const FancyZonesDataTypes::WorkAr
parentId = parentArea->UniqueId();
}

auto workArea = MakeWorkArea(m_hinstance, monitor, id, parentId);
FancyZonesUtils::Rect rect{};
if (monitor)
{
rect = MonitorUtils::GetWorkAreaRect(monitor);
}
else
{
rect = FancyZonesUtils::GetAllMonitorsCombinedRect<&MONITORINFO::rcWork>();
}

auto workArea = MakeWorkArea(m_hinstance, id, parentId, rect);
if (workArea)
{
m_workAreaHandler.AddWorkArea(VirtualDesktop::instance().GetCurrentVirtualDesktopId(), monitor, workArea);
Expand Down
2 changes: 2 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj
Expand Up @@ -75,6 +75,7 @@
<ClInclude Include="WindowUtils.h" />
<ClInclude Include="Zone.h" />
<ClInclude Include="Colors.h" />
<ClInclude Include="HighlightedZones.h" />
<ClInclude Include="ZoneIndexSetBitmask.h" />
<ClInclude Include="WorkArea.h" />
<ClInclude Include="ZonesOverlay.h" />
Expand Down Expand Up @@ -124,6 +125,7 @@
<ClCompile Include="WindowUtils.cpp" />
<ClCompile Include="Zone.cpp" />
<ClCompile Include="WorkArea.cpp" />
<ClCompile Include="HighlightedZones.cpp" />
<ClCompile Include="ZonesOverlay.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
Expand Up @@ -153,6 +153,9 @@
<ClInclude Include="FancyZonesData\LayoutData.h">
<Filter>Header Files\FancyZonesData</Filter>
</ClInclude>
<ClInclude Include="HighlightedZones.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="NotificationUtil.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -251,6 +254,9 @@
<ClCompile Include="LayoutAssignedWindows.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="HighlightedZones.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
55 changes: 55 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/HighlightedZones.cpp
@@ -0,0 +1,55 @@
#include "pch.h"
#include "HighlightedZones.h"

#include <FancyZonesLib/Layout.h>

HighlightedZones::HighlightedZones()
{
}

const ZoneIndexSet& HighlightedZones::Zones() const noexcept
{
return m_highlightZone;
}

bool HighlightedZones::Empty() const noexcept
{
return m_highlightZone.empty();
}

bool HighlightedZones::Update(const Layout* layout, POINT const& point, bool selectManyZones) noexcept
{
if (!layout)
{
return false;
}

auto highlightZone = layout->ZonesFromPoint(point);

if (selectManyZones)
{
if (m_initialHighlightZone.empty())
{
// first time
m_initialHighlightZone = highlightZone;
}
else
{
highlightZone = layout->GetCombinedZoneRange(m_initialHighlightZone, highlightZone);
}
}
else
{
m_initialHighlightZone = {};
}

const bool updated = (highlightZone != m_highlightZone);
m_highlightZone = std::move(highlightZone);
return updated;
}

void HighlightedZones::Reset() noexcept
{
m_highlightZone = {};
m_initialHighlightZone = {};
}
22 changes: 22 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/HighlightedZones.h
@@ -0,0 +1,22 @@
#pragma once

#include <FancyZonesLib/Zone.h>

class Layout;

class HighlightedZones
{
public:
HighlightedZones();
~HighlightedZones() = default;

const ZoneIndexSet& Zones() const noexcept;
bool Empty() const noexcept;

bool Update(const Layout* layout, POINT const& point, bool selectManyZones) noexcept;
void Reset() noexcept;

private:
ZoneIndexSet m_initialHighlightZone;
ZoneIndexSet m_highlightZone;
};
15 changes: 15 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/MonitorUtils.cpp
Expand Up @@ -383,4 +383,19 @@ namespace MonitorUtils

return displays;
}

FancyZonesUtils::Rect GetWorkAreaRect(HMONITOR monitor)
{
if (monitor)
{
MONITORINFO mi{};
mi.cbSize = sizeof(mi);
if (GetMonitorInfoW(monitor, &mi))
{
return FancyZonesUtils::Rect(mi.rcWork);
}
}

return FancyZonesUtils::Rect{};
}
}
3 changes: 3 additions & 0 deletions src/modules/fancyzones/FancyZonesLib/MonitorUtils.h
@@ -1,6 +1,7 @@
#pragma once

#include <FancyZonesLib/FancyZonesDataTypes.h>
#include <FancyZonesLib/util.h>

namespace MonitorUtils
{
Expand All @@ -19,4 +20,6 @@ namespace MonitorUtils

std::vector<FancyZonesDataTypes::MonitorId> IdentifyMonitors() noexcept;
void OpenWindowOnActiveMonitor(HWND window, HMONITOR monitor) noexcept;

FancyZonesUtils::Rect GetWorkAreaRect(HMONITOR monitor);
};
72 changes: 18 additions & 54 deletions src/modules/fancyzones/FancyZonesLib/WorkArea.cpp
Expand Up @@ -109,8 +109,9 @@ namespace
WindowPool windowPool;
}

WorkArea::WorkArea(HINSTANCE hinstance, const FancyZonesDataTypes::WorkAreaId& uniqueId) :
m_uniqueId(uniqueId)
WorkArea::WorkArea(HINSTANCE hinstance, const FancyZonesDataTypes::WorkAreaId& uniqueId, const FancyZonesUtils::Rect& workAreaRect) :
m_uniqueId(uniqueId),
m_workAreaRect(workAreaRect)
{
WNDCLASSEXW wcex{};
wcex.cbSize = sizeof(WNDCLASSEX);
Expand All @@ -129,8 +130,7 @@ WorkArea::~WorkArea()
HRESULT WorkArea::MoveSizeEnter(HWND window) noexcept
{
m_windowMoveSize = window;
m_highlightZone = {};
m_initialHighlightZone = {};
m_highlightedZones.Reset();
ShowZonesOverlay();
Trace::WorkArea::MoveOrResizeStarted(m_layout.get(), m_layoutWindows.get());
return S_OK;
Expand All @@ -144,42 +144,23 @@ HRESULT WorkArea::MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool s
}

bool redraw = false;
POINT ptClient = ptScreen;
MapWindowPoints(nullptr, m_window, &ptClient, 1);

if (dragEnabled)
{
auto highlightZone = ZonesFromPoint(ptClient);
POINT ptClient = ptScreen;
MapWindowPoints(nullptr, m_window, &ptClient, 1);

if (selectManyZones)
{
if (m_initialHighlightZone.empty())
{
// first time
m_initialHighlightZone = highlightZone;
}
else
{
highlightZone = m_layout->GetCombinedZoneRange(m_initialHighlightZone, highlightZone);
}
}
else
{
m_initialHighlightZone = {};
}

redraw = (highlightZone != m_highlightZone);
m_highlightZone = std::move(highlightZone);
redraw = m_highlightedZones.Update(m_layout.get(), ptClient, selectManyZones);
}
else if (m_highlightZone.size())
else if (!m_highlightedZones.Empty())
{
m_highlightZone = {};
m_highlightedZones.Reset();
redraw = true;
}

if (redraw && m_zonesOverlay)
{
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightZone, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightedZones.Zones(), Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
}

return S_OK;
Expand All @@ -192,7 +173,8 @@ HRESULT WorkArea::MoveSizeEnd(HWND window) noexcept
return E_INVALIDARG;
}

MoveWindowIntoZoneByIndexSet(window, m_highlightZone);
MoveWindowIntoZoneByIndexSet(window, m_highlightedZones.Zones());
m_highlightedZones.Reset();

Trace::WorkArea::MoveOrResizeEnd(m_layout.get(), m_layoutWindows.get());

Expand Down Expand Up @@ -505,7 +487,7 @@ void WorkArea::ShowZonesOverlay() noexcept
if (m_window && m_layout)
{
SetAsTopmostWindow();
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightZone, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightedZones.Zones(), Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
m_zonesOverlay->Show();
}
}
Expand All @@ -515,9 +497,7 @@ void WorkArea::HideZonesOverlay() noexcept
if (m_window)
{
m_zonesOverlay->Hide();
m_keyLast = 0;
m_windowMoveSize = nullptr;
m_highlightZone = {};
}
}

Expand All @@ -532,8 +512,7 @@ void WorkArea::UpdateActiveZoneSet() noexcept
CalculateZoneSet();
if (m_window && m_layout)
{
m_highlightZone.clear();
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightZone, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), {}, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
}
}

Expand All @@ -547,10 +526,10 @@ void WorkArea::CycleWindows(HWND window, bool reverse) noexcept

void WorkArea::ClearSelectedZones() noexcept
{
if (m_highlightZone.size() && m_layout)
if (!m_highlightedZones.Empty() && m_layout)
{
m_highlightZone.clear();
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), m_highlightZone, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
m_highlightedZones.Reset();
m_zonesOverlay->DrawActiveZoneSet(m_layout->Zones(), {}, Colors::GetZoneColors(), FancyZonesSettings::settings().showZoneNumber);
}
}

Expand Down Expand Up @@ -609,7 +588,7 @@ void WorkArea::CalculateZoneSet() noexcept
}

m_layout = std::make_unique<Layout>(appliedLayout.value());
m_layout->Init(m_workAreaRect, m_monitor);
m_layout->Init(m_workAreaRect, m_uniqueId.monitorId.monitor);

if (!m_layoutWindows)
{
Expand Down Expand Up @@ -639,16 +618,6 @@ LRESULT WorkArea::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
return 0;
}

ZoneIndexSet WorkArea::ZonesFromPoint(POINT pt) noexcept
{
if (m_layout)
{
return m_layout->ZonesFromPoint(pt);
}

return {};
}

void WorkArea::SetAsTopmostWindow() noexcept
{
if (!m_window)
Expand All @@ -667,11 +636,6 @@ void WorkArea::SetAsTopmostWindow() noexcept
SetWindowPos(m_window, windowInsertAfter, 0, 0, 0, 0, flags);
}

void WorkArea::LogInitializationError()
{
Logger::error(L"Unable to get monitor info, {}", get_last_error_or_default(GetLastError()));
}

#pragma endregion

LRESULT CALLBACK WorkArea::s_WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept
Expand Down

0 comments on commit 6f0b16d

Please sign in to comment.