mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 04:07:40 +02:00
[FancyZones] Improve code quality (part 1: moved utils) (#23028)
This commit is contained in:
@@ -59,6 +59,7 @@
|
|||||||
<ClInclude Include="ModuleConstants.h" />
|
<ClInclude Include="ModuleConstants.h" />
|
||||||
<ClInclude Include="MonitorUtils.h" />
|
<ClInclude Include="MonitorUtils.h" />
|
||||||
<ClInclude Include="MonitorWorkAreaHandler.h" />
|
<ClInclude Include="MonitorWorkAreaHandler.h" />
|
||||||
|
<ClInclude Include="NotificationUtil.h" />
|
||||||
<ClInclude Include="pch.h" />
|
<ClInclude Include="pch.h" />
|
||||||
<ClInclude Include="Generated Files/resource.h" />
|
<ClInclude Include="Generated Files/resource.h" />
|
||||||
<None Include="resource.base.h" />
|
<None Include="resource.base.h" />
|
||||||
|
|||||||
@@ -153,6 +153,9 @@
|
|||||||
<ClInclude Include="FancyZonesData\LayoutData.h">
|
<ClInclude Include="FancyZonesData\LayoutData.h">
|
||||||
<Filter>Header Files\FancyZonesData</Filter>
|
<Filter>Header Files\FancyZonesData</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="NotificationUtil.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClCompile Include="pch.cpp">
|
<ClCompile Include="pch.cpp">
|
||||||
|
|||||||
35
src/modules/fancyzones/FancyZonesLib/NotificationUtil.h
Normal file
35
src/modules/fancyzones/FancyZonesLib/NotificationUtil.h
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <common/notifications/notifications.h>
|
||||||
|
#include <common/notifications/dont_show_again.h>
|
||||||
|
#include <common/utils/resources.h>
|
||||||
|
|
||||||
|
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<action_t> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,54 +2,17 @@
|
|||||||
#include "WindowMoveHandler.h"
|
#include "WindowMoveHandler.h"
|
||||||
|
|
||||||
#include <common/display/dpi_aware.h>
|
#include <common/display/dpi_aware.h>
|
||||||
#include <common/notifications/notifications.h>
|
#include <common/logger/logger.h>
|
||||||
#include <common/notifications/dont_show_again.h>
|
|
||||||
#include <common/utils/elevation.h>
|
#include <common/utils/elevation.h>
|
||||||
#include <common/utils/resources.h>
|
#include <common/utils/winapi_error.h>
|
||||||
|
|
||||||
#include "FancyZonesData/AppZoneHistory.h"
|
#include "FancyZonesData/AppZoneHistory.h"
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
#include "WorkArea.h"
|
#include "WorkArea.h"
|
||||||
#include <FancyZonesLib/FancyZonesWindowProcessing.h>
|
#include <FancyZonesLib/FancyZonesWindowProcessing.h>
|
||||||
|
#include <FancyZonesLib/NotificationUtil.h>
|
||||||
#include <FancyZonesLib/WindowUtils.h>
|
#include <FancyZonesLib/WindowUtils.h>
|
||||||
|
|
||||||
// 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<void()>& keyUpdateCallback) :
|
WindowMoveHandler::WindowMoveHandler(const std::function<void()>& keyUpdateCallback) :
|
||||||
m_mouseState(false),
|
m_mouseState(false),
|
||||||
m_mouseHook(std::bind(&WindowMoveHandler::OnMouseDown, this)),
|
m_mouseHook(std::bind(&WindowMoveHandler::OnMouseDown, this)),
|
||||||
@@ -67,7 +30,7 @@ void WindowMoveHandler::MoveSizeStart(HWND window, HMONITOR monitor, POINT const
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!FancyZonesWindowUtils::IsCandidateForZoning(window) || WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent())
|
if (!FancyZonesWindowUtils::IsCandidateForZoning(window) || FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent())
|
||||||
{
|
{
|
||||||
return;
|
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
|
// This updates m_dragEnabled depending on if the shift key is being held down
|
||||||
UpdateDragState();
|
UpdateDragState();
|
||||||
|
|
||||||
// Notifies user if unable to drag elevated window
|
if (!is_process_elevated() && FancyZonesWindowUtils::IsProcessOfWindowElevated(window))
|
||||||
WarnIfElevationIsRequired(window);
|
{
|
||||||
|
// Notifies user if unable to drag elevated window
|
||||||
|
FancyZonesNotifications::WarnIfElevationIsRequired();
|
||||||
|
m_dragEnabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
if (m_dragEnabled)
|
if (m_dragEnabled)
|
||||||
{
|
{
|
||||||
m_draggedWindowWorkArea = iter->second;
|
m_draggedWindowWorkArea = iter->second;
|
||||||
@@ -241,12 +208,12 @@ void WindowMoveHandler::MoveSizeEnd(HWND window, const std::unordered_map<HMONIT
|
|||||||
{
|
{
|
||||||
if (leftShiftPressed)
|
if (leftShiftPressed)
|
||||||
{
|
{
|
||||||
SwallowKey(VK_LSHIFT);
|
FancyZonesUtils::SwallowKey(VK_LSHIFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rightShiftPressed)
|
if (rightShiftPressed)
|
||||||
{
|
{
|
||||||
SwallowKey(VK_RSHIFT);
|
FancyZonesUtils::SwallowKey(VK_RSHIFT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -257,7 +224,7 @@ void WindowMoveHandler::MoveSizeEnd(HWND window, const std::unordered_map<HMONIT
|
|||||||
{
|
{
|
||||||
if (FancyZonesSettings::settings().restoreSize)
|
if (FancyZonesSettings::settings().restoreSize)
|
||||||
{
|
{
|
||||||
if (WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent())
|
if (FancyZonesWindowUtils::IsCursorTypeIndicatingSizeEvent())
|
||||||
{
|
{
|
||||||
::RemoveProp(window, ZonedWindowProperties::PropertyRestoreSizeID);
|
::RemoveProp(window, ZonedWindowProperties::PropertyRestoreSizeID);
|
||||||
}
|
}
|
||||||
@@ -349,30 +316,6 @@ void WindowMoveHandler::AssignWindowsToZones(const std::unordered_map<HMONITOR,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WindowMoveHandler::WarnIfElevationIsRequired(HWND window) noexcept
|
|
||||||
{
|
|
||||||
using namespace notifications;
|
|
||||||
using namespace NonLocalizable;
|
|
||||||
|
|
||||||
static bool warning_shown = false;
|
|
||||||
if (!is_process_elevated() && FancyZonesWindowUtils::IsProcessOfWindowElevated(window))
|
|
||||||
{
|
|
||||||
m_dragEnabled = false;
|
|
||||||
if (!warning_shown && !is_toast_disabled(CantDragElevatedDontShowAgainRegistryPath, CantDragElevatedDisableIntervalInDays))
|
|
||||||
{
|
|
||||||
std::vector<action_t> 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
|
void WindowMoveHandler::UpdateDragState() noexcept
|
||||||
{
|
{
|
||||||
if (FancyZonesSettings::settings().shiftDrag)
|
if (FancyZonesSettings::settings().shiftDrag)
|
||||||
@@ -426,13 +369,4 @@ void WindowMoveHandler::ResetWindowTransparency() noexcept
|
|||||||
|
|
||||||
m_windowTransparencyProperties.draggedWindow = nullptr;
|
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));
|
|
||||||
}
|
|
||||||
@@ -60,12 +60,10 @@ private:
|
|||||||
bool hasNoVisibleOwner = false;
|
bool hasNoVisibleOwner = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
void WarnIfElevationIsRequired(HWND window) noexcept;
|
|
||||||
void UpdateDragState() noexcept;
|
void UpdateDragState() noexcept;
|
||||||
|
|
||||||
void SetWindowTransparency(HWND window) noexcept;
|
void SetWindowTransparency(HWND window) noexcept;
|
||||||
void ResetWindowTransparency() noexcept;
|
void ResetWindowTransparency() noexcept;
|
||||||
void SwallowKey(const WORD key) noexcept;
|
|
||||||
|
|
||||||
bool m_inDragging{}; // Whether or not a move/size operation is currently active
|
bool m_inDragging{}; // Whether or not a move/size operation is currently active
|
||||||
HWND m_draggedWindow{}; // The window that is being moved/sized
|
HWND m_draggedWindow{}; // The window that is being moved/sized
|
||||||
|
|||||||
@@ -546,3 +546,30 @@ void FancyZonesWindowUtils::MakeWindowTransparent(HWND window)
|
|||||||
DwmEnableBlurBehindWindow(window, &bh);
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -36,4 +36,6 @@ namespace FancyZonesWindowUtils
|
|||||||
|
|
||||||
void DisableRoundCorners(HWND window) noexcept;
|
void DisableRoundCorners(HWND window) noexcept;
|
||||||
void ResetRoundCornersPreference(HWND window) noexcept;
|
void ResetRoundCornersPreference(HWND window) noexcept;
|
||||||
|
|
||||||
|
bool IsCursorTypeIndicatingSizeEvent();
|
||||||
}
|
}
|
||||||
@@ -273,4 +273,13 @@ namespace FancyZonesUtils
|
|||||||
|
|
||||||
return windowRect;
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,4 +177,6 @@ namespace FancyZonesUtils
|
|||||||
|
|
||||||
RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept;
|
RECT PrepareRectForCycling(RECT windowRect, RECT workAreaRect, DWORD vkCode) noexcept;
|
||||||
size_t ChooseNextZoneByPosition(DWORD vkCode, RECT windowRect, const std::vector<RECT>& zoneRects) noexcept;
|
size_t ChooseNextZoneByPosition(DWORD vkCode, RECT windowRect, const std::vector<RECT>& zoneRects) noexcept;
|
||||||
|
|
||||||
|
void SwallowKey(const WORD key) noexcept;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user