From 39c61b9abd5d401d166f8d8b6a4dead81218502a Mon Sep 17 00:00:00 2001 From: stefansjfw <57057282+stefansjfw@users.noreply.github.com> Date: Wed, 30 Sep 2020 09:19:20 +0200 Subject: [PATCH] [FancyZones] Remove WindowMoveHander pImpl pattern (#6894) * Remove WindowMoveHandlare pImpl pattern Introduces runtime overhead and makes debugging unecessary more painful * End file new line --- .../fancyzones/lib/WindowMoveHandler.cpp | 168 +++--------------- .../fancyzones/lib/WindowMoveHandler.h | 65 ++++++- 2 files changed, 80 insertions(+), 153 deletions(-) diff --git a/src/modules/fancyzones/lib/WindowMoveHandler.cpp b/src/modules/fancyzones/lib/WindowMoveHandler.cpp index 7b69275dba..2b060460da 100644 --- a/src/modules/fancyzones/lib/WindowMoveHandler.cpp +++ b/src/modules/fancyzones/lib/WindowMoveHandler.cpp @@ -1,19 +1,15 @@ #include "pch.h" #include "WindowMoveHandler.h" +#include #include #include #include -#include -#include "lib/Settings.h" -#include "lib/ZoneWindow.h" -#include "lib/util.h" -#include "VirtualDesktopUtils.h" -#include "lib/SecondaryMouseButtonsHook.h" -#include "lib/GenericKeyHook.h" -#include "lib/FancyZonesData.h" -#include "lib/KeyState.h" +#include "FancyZonesData.h" +#include "Settings.h" +#include "ZoneWindow.h" +#include "util.h" extern "C" IMAGE_DOS_HEADER __ImageBase; @@ -52,141 +48,19 @@ namespace WindowMoveHandlerUtils } return false; } - - // MoveSize related window properties - struct MoveSizeWindowInfo - { - // True if from the styles the window looks like a standard window - bool standardWindow = false; - // True if the window is a top-level window that does not have a visible owner - bool noVisibleOwner = false; - }; } -class WindowMoveHandlerPrivate -{ -public: - WindowMoveHandlerPrivate(const winrt::com_ptr& settings, const std::function& keyUpdateCallback) : - m_settings(settings), - m_mouseState(false), - m_mouseHook(std::bind(&WindowMoveHandlerPrivate::OnMouseDown, this)), - m_shiftKeyState(keyUpdateCallback), - m_ctrlKeyState(keyUpdateCallback), - m_keyUpdateCallback(keyUpdateCallback) - { - } - - bool IsDragEnabled() const noexcept - { - return m_dragEnabled; - } - - bool InMoveSize() const noexcept - { - return m_inMoveSize; - } - - void MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept; - void MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept; - void MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept; - - void MoveWindowIntoZoneByIndexSet(HWND window, const std::vector& indexSet, winrt::com_ptr zoneWindow) noexcept; - bool MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept; - bool MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept; - bool ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr zoneWindow) noexcept; - -private: - void WarnIfElevationIsRequired(HWND window) noexcept; - void UpdateDragState() noexcept; - - void SetWindowTransparency(HWND window) noexcept; - void ResetWindowTransparency() noexcept; - - inline void OnMouseDown() noexcept - { - m_mouseState = !m_mouseState; - m_keyUpdateCallback(); - } - -private: - winrt::com_ptr m_settings{}; - - HWND m_windowMoveSize{}; // The window that is being moved/sized - bool m_inMoveSize{}; // Whether or not a move/size operation is currently active - WindowMoveHandlerUtils::MoveSizeWindowInfo m_moveSizeWindowInfo; // MoveSizeWindowInfo of the window at the moment when dragging started - winrt::com_ptr m_zoneWindowMoveSize; // "Active" ZoneWindow, where the move/size is happening. Will update as drag moves between monitors. - bool m_dragEnabled{}; // True if we should be showing zone hints while dragging - - std::atomic m_mouseState; - SecondaryMouseButtonsHook m_mouseHook; - KeyState m_shiftKeyState; - KeyState m_ctrlKeyState; - std::function m_keyUpdateCallback; - - struct WindowTransparencyProperties - { - HWND draggedWindow = nullptr; - long draggedWindowExstyle = 0; - COLORREF draggedWindowCrKey = RGB(0, 0, 0); - DWORD draggedWindowDwFlags = 0; - BYTE draggedWindowInitialAlpha = 0; - } m_windowTransparencyProperties; -}; - WindowMoveHandler::WindowMoveHandler(const winrt::com_ptr& settings, const std::function& keyUpdateCallback) : - pimpl(new WindowMoveHandlerPrivate(settings, keyUpdateCallback)) {} - -WindowMoveHandler::~WindowMoveHandler() + m_settings(settings), + m_mouseState(false), + m_mouseHook(std::bind(&WindowMoveHandler::OnMouseDown, this)), + m_shiftKeyState(keyUpdateCallback), + m_ctrlKeyState(keyUpdateCallback), + m_keyUpdateCallback(keyUpdateCallback) { - delete pimpl; -} - -bool WindowMoveHandler::InMoveSize() const noexcept -{ - return pimpl->InMoveSize(); -} - -bool WindowMoveHandler::IsDragEnabled() const noexcept -{ - return pimpl->IsDragEnabled(); } void WindowMoveHandler::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept -{ - pimpl->MoveSizeStart(window, monitor, ptScreen, zoneWindowMap); -} - -void WindowMoveHandler::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept -{ - pimpl->MoveSizeUpdate(monitor, ptScreen, zoneWindowMap); -} - -void WindowMoveHandler::MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept -{ - pimpl->MoveSizeEnd(window, ptScreen, zoneWindowMap); -} - -void WindowMoveHandler::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector& indexSet, winrt::com_ptr zoneWindow) noexcept -{ - pimpl->MoveWindowIntoZoneByIndexSet(window, indexSet, zoneWindow); -} - -bool WindowMoveHandler::MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept -{ - return pimpl->MoveWindowIntoZoneByDirectionAndIndex(window, vkCode, cycle, zoneWindow); -} - -bool WindowMoveHandler::MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept -{ - return pimpl->MoveWindowIntoZoneByDirectionAndPosition(window, vkCode, cycle, zoneWindow); -} - -bool WindowMoveHandler::ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr zoneWindow) noexcept -{ - return pimpl->ExtendWindowByDirectionAndPosition(window, vkCode, zoneWindow); -} - -void WindowMoveHandlerPrivate::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept { if (!FancyZonesUtils::IsCandidateForZoning(window, m_settings->GetSettings()->excludedAppsArray) || WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent()) { @@ -252,7 +126,7 @@ void WindowMoveHandlerPrivate::MoveSizeStart(HWND window, HMONITOR monitor, POIN } } -void WindowMoveHandlerPrivate::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept +void WindowMoveHandler::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept { if (!m_inMoveSize) { @@ -319,7 +193,7 @@ void WindowMoveHandlerPrivate::MoveSizeUpdate(HMONITOR monitor, POINT const& ptS } } -void WindowMoveHandlerPrivate::MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept +void WindowMoveHandler::MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept { if (window != m_windowMoveSize) { @@ -400,7 +274,7 @@ void WindowMoveHandlerPrivate::MoveSizeEnd(HWND window, POINT const& ptScreen, c } } -void WindowMoveHandlerPrivate::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector& indexSet, winrt::com_ptr zoneWindow) noexcept +void WindowMoveHandler::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector& indexSet, winrt::com_ptr zoneWindow) noexcept { if (window != m_windowMoveSize) { @@ -408,22 +282,22 @@ void WindowMoveHandlerPrivate::MoveWindowIntoZoneByIndexSet(HWND window, const s } } -bool WindowMoveHandlerPrivate::MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept +bool WindowMoveHandler::MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept { return zoneWindow && zoneWindow->MoveWindowIntoZoneByDirectionAndIndex(window, vkCode, cycle); } -bool WindowMoveHandlerPrivate::MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept +bool WindowMoveHandler::MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept { return zoneWindow && zoneWindow->MoveWindowIntoZoneByDirectionAndPosition(window, vkCode, cycle); } -bool WindowMoveHandlerPrivate::ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr zoneWindow) noexcept +bool WindowMoveHandler::ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr zoneWindow) noexcept { return zoneWindow && zoneWindow->ExtendWindowByDirectionAndPosition(window, vkCode); } -void WindowMoveHandlerPrivate::WarnIfElevationIsRequired(HWND window) noexcept +void WindowMoveHandler::WarnIfElevationIsRequired(HWND window) noexcept { static bool warning_shown = false; if (!is_process_elevated() && IsProcessOfWindowElevated(window)) @@ -444,7 +318,7 @@ void WindowMoveHandlerPrivate::WarnIfElevationIsRequired(HWND window) noexcept } } -void WindowMoveHandlerPrivate::UpdateDragState() noexcept +void WindowMoveHandler::UpdateDragState() noexcept { if (m_settings->GetSettings()->shiftDrag) { @@ -456,7 +330,7 @@ void WindowMoveHandlerPrivate::UpdateDragState() noexcept } } -void WindowMoveHandlerPrivate::SetWindowTransparency(HWND window) noexcept +void WindowMoveHandler::SetWindowTransparency(HWND window) noexcept { if (m_settings->GetSettings()->makeDraggedWindowTransparent) { @@ -473,7 +347,7 @@ void WindowMoveHandlerPrivate::SetWindowTransparency(HWND window) noexcept } } -void WindowMoveHandlerPrivate::ResetWindowTransparency() noexcept +void WindowMoveHandler::ResetWindowTransparency() noexcept { if (m_settings->GetSettings()->makeDraggedWindowTransparent && m_windowTransparencyProperties.draggedWindow != nullptr) { diff --git a/src/modules/fancyzones/lib/WindowMoveHandler.h b/src/modules/fancyzones/lib/WindowMoveHandler.h index c37d4150ff..fba9c8235c 100644 --- a/src/modules/fancyzones/lib/WindowMoveHandler.h +++ b/src/modules/fancyzones/lib/WindowMoveHandler.h @@ -1,7 +1,10 @@ #pragma once +#include "KeyState.h" #include "SecondaryMouseButtonsHook.h" +#include + interface IFancyZonesSettings; interface IZoneWindow; @@ -9,10 +12,6 @@ class WindowMoveHandler { public: WindowMoveHandler(const winrt::com_ptr& settings, const std::function& keyUpdateCallback); - ~WindowMoveHandler(); - - bool InMoveSize() const noexcept; - bool IsDragEnabled() const noexcept; void MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept; void MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map>& zoneWindowMap) noexcept; @@ -23,6 +22,60 @@ public: bool MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr zoneWindow) noexcept; bool ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode, winrt::com_ptr zoneWindow) noexcept; + inline void OnMouseDown() noexcept + { + m_mouseState = !m_mouseState; + m_keyUpdateCallback(); + } + + inline bool IsDragEnabled() const noexcept + { + return m_dragEnabled; + } + + inline bool InMoveSize() const noexcept + { + return m_inMoveSize; + } + private: - class WindowMoveHandlerPrivate* pimpl; -}; \ No newline at end of file + struct WindowTransparencyProperties + { + HWND draggedWindow = nullptr; + long draggedWindowExstyle = 0; + COLORREF draggedWindowCrKey = RGB(0, 0, 0); + DWORD draggedWindowDwFlags = 0; + BYTE draggedWindowInitialAlpha = 0; + }; + + // MoveSize related window properties + struct MoveSizeWindowInfo + { + // True if from the styles the window looks like a standard window + bool standardWindow = false; + // True if the window is a top-level window that does not have a visible owner + bool noVisibleOwner = false; + }; + + void WarnIfElevationIsRequired(HWND window) noexcept; + void UpdateDragState() noexcept; + + void SetWindowTransparency(HWND window) noexcept; + void ResetWindowTransparency() noexcept; + + winrt::com_ptr m_settings{}; + + HWND m_windowMoveSize{}; // The window that is being moved/sized + bool m_inMoveSize{}; // Whether or not a move/size operation is currently active + MoveSizeWindowInfo m_moveSizeWindowInfo; // MoveSizeWindowInfo of the window at the moment when dragging started + winrt::com_ptr m_zoneWindowMoveSize; // "Active" ZoneWindow, where the move/size is happening. Will update as drag moves between monitors. + bool m_dragEnabled{}; // True if we should be showing zone hints while dragging + + WindowTransparencyProperties m_windowTransparencyProperties; + + std::atomic m_mouseState; + SecondaryMouseButtonsHook m_mouseHook; + KeyState m_shiftKeyState; + KeyState m_ctrlKeyState; + std::function m_keyUpdateCallback; +};