From 87743e8e5d90e6aa4101a0bc25a92927f22dea5e Mon Sep 17 00:00:00 2001 From: Seraphima Zykova Date: Tue, 17 Jan 2023 13:50:40 +0300 Subject: [PATCH] [FancyZones] Improve code quality (part 1: moved utils) (#23028) --- .../FancyZonesLib/FancyZonesLib.vcxproj | 1 + .../FancyZonesLib.vcxproj.filters | 3 + .../FancyZonesLib/NotificationUtil.h | 35 +++++++ .../FancyZonesLib/WindowMoveHandler.cpp | 96 +++---------------- .../FancyZonesLib/WindowMoveHandler.h | 2 - .../fancyzones/FancyZonesLib/WindowUtils.cpp | 27 ++++++ .../fancyzones/FancyZonesLib/WindowUtils.h | 2 + src/modules/fancyzones/FancyZonesLib/util.cpp | 9 ++ src/modules/fancyzones/FancyZonesLib/util.h | 2 + 9 files changed, 94 insertions(+), 83 deletions(-) create mode 100644 src/modules/fancyzones/FancyZonesLib/NotificationUtil.h diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj index e572d4ed7f..c26b30d52e 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj @@ -59,6 +59,7 @@ + diff --git a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters index e37de33469..29ed814973 100644 --- a/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters +++ b/src/modules/fancyzones/FancyZonesLib/FancyZonesLib.vcxproj.filters @@ -153,6 +153,9 @@ Header Files\FancyZonesData + + Header Files + diff --git a/src/modules/fancyzones/FancyZonesLib/NotificationUtil.h b/src/modules/fancyzones/FancyZonesLib/NotificationUtil.h new file mode 100644 index 0000000000..7686f409be --- /dev/null +++ b/src/modules/fancyzones/FancyZonesLib/NotificationUtil.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include + +namespace FancyZonesNotifications +{ + // Non-Localizable strings + namespace NonLocalizable + { + const wchar_t FancyZonesRunAsAdminInfoPage[] = L"https://aka.ms/powertoysDetectedElevatedHelp"; + const wchar_t ToastNotificationButtonUrl[] = L"powertoys://cant_drag_elevated_disable/"; + } + + inline void WarnIfElevationIsRequired() + { + using namespace notifications; + using namespace NonLocalizable; + + static bool warning_shown = false; + if (!warning_shown && !is_toast_disabled(CantDragElevatedDontShowAgainRegistryPath, CantDragElevatedDisableIntervalInDays)) + { + std::vector actions = { + link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_LEARN_MORE), FancyZonesRunAsAdminInfoPage }, + link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_DIALOG_DONT_SHOW_AGAIN), ToastNotificationButtonUrl } + }; + show_toast_with_activations(GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED), + GET_RESOURCE_STRING(IDS_FANCYZONES), + {}, + std::move(actions)); + warning_shown = true; + } + } +} diff --git a/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.cpp b/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.cpp index a53d4d3981..1e2867513e 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.cpp +++ b/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.cpp @@ -2,54 +2,17 @@ #include "WindowMoveHandler.h" #include -#include -#include +#include #include -#include +#include #include "FancyZonesData/AppZoneHistory.h" #include "Settings.h" #include "WorkArea.h" #include +#include #include -// Non-Localizable strings -namespace NonLocalizable -{ - const wchar_t FancyZonesRunAsAdminInfoPage[] = L"https://aka.ms/powertoysDetectedElevatedHelp"; - const wchar_t ToastNotificationButtonUrl[] = L"powertoys://cant_drag_elevated_disable/"; -} - -namespace WindowMoveHandlerUtils -{ - bool IsCursorTypeIndicatingSizeEvent() - { - CURSORINFO cursorInfo = { 0 }; - cursorInfo.cbSize = sizeof(cursorInfo); - - if (::GetCursorInfo(&cursorInfo)) - { - if (::LoadCursor(NULL, IDC_SIZENS) == cursorInfo.hCursor) - { - return true; - } - if (::LoadCursor(NULL, IDC_SIZEWE) == cursorInfo.hCursor) - { - return true; - } - if (::LoadCursor(NULL, IDC_SIZENESW) == cursorInfo.hCursor) - { - return true; - } - if (::LoadCursor(NULL, IDC_SIZENWSE) == cursorInfo.hCursor) - { - return true; - } - } - return false; - } -} - WindowMoveHandler::WindowMoveHandler(const std::function& keyUpdateCallback) : m_mouseState(false), m_mouseHook(std::bind(&WindowMoveHandler::OnMouseDown, this)), @@ -67,7 +30,7 @@ void WindowMoveHandler::MoveSizeStart(HWND window, HMONITOR monitor, POINT const return; } - if (!FancyZonesWindowUtils::IsCandidateForZoning(window) || WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent()) + if (!FancyZonesWindowUtils::IsCandidateForZoning(window) || FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent()) { return; } @@ -96,9 +59,13 @@ void WindowMoveHandler::MoveSizeStart(HWND window, HMONITOR monitor, POINT const // This updates m_dragEnabled depending on if the shift key is being held down UpdateDragState(); - // Notifies user if unable to drag elevated window - WarnIfElevationIsRequired(window); - + if (!is_process_elevated() && FancyZonesWindowUtils::IsProcessOfWindowElevated(window)) + { + // Notifies user if unable to drag elevated window + FancyZonesNotifications::WarnIfElevationIsRequired(); + m_dragEnabled = false; + } + if (m_dragEnabled) { m_draggedWindowWorkArea = iter->second; @@ -241,12 +208,12 @@ void WindowMoveHandler::MoveSizeEnd(HWND window, const std::unordered_map actions = { - link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_LEARN_MORE), FancyZonesRunAsAdminInfoPage }, - link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_DIALOG_DONT_SHOW_AGAIN), ToastNotificationButtonUrl } - }; - show_toast_with_activations(GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED), - GET_RESOURCE_STRING(IDS_FANCYZONES), - {}, - std::move(actions)); - warning_shown = true; - } - } -} - void WindowMoveHandler::UpdateDragState() noexcept { if (FancyZonesSettings::settings().shiftDrag) @@ -426,13 +369,4 @@ void WindowMoveHandler::ResetWindowTransparency() noexcept m_windowTransparencyProperties.draggedWindow = nullptr; } -} - -void WindowMoveHandler::SwallowKey(const WORD key) noexcept -{ - INPUT inputKey[1] = {}; - inputKey[0].type = INPUT_KEYBOARD; - inputKey[0].ki.wVk = key; - inputKey[0].ki.dwFlags = KEYEVENTF_KEYUP; - SendInput(1, inputKey, sizeof(INPUT)); -} +} \ No newline at end of file diff --git a/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.h b/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.h index b4f8281e99..67d96036f0 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.h +++ b/src/modules/fancyzones/FancyZonesLib/WindowMoveHandler.h @@ -60,12 +60,10 @@ private: bool hasNoVisibleOwner = false; }; - void WarnIfElevationIsRequired(HWND window) noexcept; void UpdateDragState() noexcept; void SetWindowTransparency(HWND window) noexcept; void ResetWindowTransparency() noexcept; - void SwallowKey(const WORD key) noexcept; bool m_inDragging{}; // Whether or not a move/size operation is currently active HWND m_draggedWindow{}; // The window that is being moved/sized diff --git a/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp b/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp index ee37b65866..41f079f6b6 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp +++ b/src/modules/fancyzones/FancyZonesLib/WindowUtils.cpp @@ -546,3 +546,30 @@ void FancyZonesWindowUtils::MakeWindowTransparent(HWND window) DwmEnableBlurBehindWindow(window, &bh); } } + +bool FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent() +{ + CURSORINFO cursorInfo = { 0 }; + cursorInfo.cbSize = sizeof(cursorInfo); + + if (::GetCursorInfo(&cursorInfo)) + { + if (::LoadCursor(NULL, IDC_SIZENS) == cursorInfo.hCursor) + { + return true; + } + if (::LoadCursor(NULL, IDC_SIZEWE) == cursorInfo.hCursor) + { + return true; + } + if (::LoadCursor(NULL, IDC_SIZENESW) == cursorInfo.hCursor) + { + return true; + } + if (::LoadCursor(NULL, IDC_SIZENWSE) == cursorInfo.hCursor) + { + return true; + } + } + return false; +} diff --git a/src/modules/fancyzones/FancyZonesLib/WindowUtils.h b/src/modules/fancyzones/FancyZonesLib/WindowUtils.h index a46ce4aa20..d935587e42 100644 --- a/src/modules/fancyzones/FancyZonesLib/WindowUtils.h +++ b/src/modules/fancyzones/FancyZonesLib/WindowUtils.h @@ -36,4 +36,6 @@ namespace FancyZonesWindowUtils void DisableRoundCorners(HWND window) noexcept; void ResetRoundCornersPreference(HWND window) noexcept; + + bool IsCursorTypeIndicatingSizeEvent(); } \ No newline at end of file diff --git a/src/modules/fancyzones/FancyZonesLib/util.cpp b/src/modules/fancyzones/FancyZonesLib/util.cpp index 4aca170180..151e5d9220 100644 --- a/src/modules/fancyzones/FancyZonesLib/util.cpp +++ b/src/modules/fancyzones/FancyZonesLib/util.cpp @@ -273,4 +273,13 @@ namespace FancyZonesUtils return windowRect; } + + void SwallowKey(const WORD key) noexcept + { + INPUT inputKey[1] = {}; + inputKey[0].type = INPUT_KEYBOARD; + inputKey[0].ki.wVk = key; + inputKey[0].ki.dwFlags = KEYEVENTF_KEYUP; + SendInput(1, inputKey, sizeof(INPUT)); + } } diff --git a/src/modules/fancyzones/FancyZonesLib/util.h b/src/modules/fancyzones/FancyZonesLib/util.h index 8787368aa1..8a6e28a313 100644 --- a/src/modules/fancyzones/FancyZonesLib/util.h +++ b/src/modules/fancyzones/FancyZonesLib/util.h @@ -177,4 +177,6 @@ namespace FancyZonesUtils RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept; size_t ChooseNextZoneByPosition(DWORD vkCode, RECT windowRect, const std::vector& zoneRects) noexcept; + + void SwallowKey(const WORD key) noexcept; }