2020-04-30 18:16:08 +08:00
|
|
|
#include "pch.h"
|
|
|
|
#include "WindowMoveHandler.h"
|
|
|
|
|
|
|
|
#include <common/notifications.h>
|
|
|
|
#include <common/notifications/fancyzones_notifications.h>
|
|
|
|
#include <common/window_helpers.h>
|
2020-07-01 21:36:05 +08:00
|
|
|
#include <common/dpi_aware.h>
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
#include "lib/Settings.h"
|
|
|
|
#include "lib/ZoneWindow.h"
|
|
|
|
#include "lib/util.h"
|
2020-05-06 23:16:16 +08:00
|
|
|
#include "VirtualDesktopUtils.h"
|
2020-06-05 22:53:08 +08:00
|
|
|
#include "lib/SecondaryMouseButtonsHook.h"
|
2020-06-17 21:06:16 +08:00
|
|
|
#include <lib/ShiftKeyHook.h>
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class WindowMoveHandlerPrivate
|
|
|
|
{
|
|
|
|
public:
|
2020-06-17 21:06:16 +08:00
|
|
|
WindowMoveHandlerPrivate(const winrt::com_ptr<IFancyZonesSettings>& settings, SecondaryMouseButtonsHook* mouseHook, ShiftKeyHook* shiftHook) :
|
2020-06-05 22:53:08 +08:00
|
|
|
m_settings(settings),
|
2020-06-17 21:06:16 +08:00
|
|
|
m_mouseHook(mouseHook),
|
|
|
|
m_shiftHook(shiftHook){};
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
bool IsDragEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return m_dragEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool InMoveSize() const noexcept
|
|
|
|
{
|
|
|
|
return m_inMoveSize;
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
void OnMouseDown() noexcept;
|
2020-06-17 21:06:16 +08:00
|
|
|
void OnShiftChangeState(bool state) noexcept;
|
2020-06-05 22:53:08 +08:00
|
|
|
|
|
|
|
void MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept;
|
2020-05-31 18:36:45 +08:00
|
|
|
void MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept;
|
|
|
|
void MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept;
|
2020-04-30 18:16:08 +08:00
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<int>& indexSet, winrt::com_ptr<IZoneWindow> zoneWindow) noexcept;
|
|
|
|
bool MoveWindowIntoZoneByDirection(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr<IZoneWindow> zoneWindow);
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void UpdateDragState(HWND window) noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
winrt::com_ptr<IFancyZonesSettings> m_settings{};
|
2020-06-05 22:53:08 +08:00
|
|
|
SecondaryMouseButtonsHook* m_mouseHook{};
|
2020-06-17 21:06:16 +08:00
|
|
|
ShiftKeyHook* m_shiftHook{};
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
HWND m_windowMoveSize{}; // The window that is being moved/sized
|
|
|
|
bool m_inMoveSize{}; // Whether or not a move/size operation is currently active
|
|
|
|
winrt::com_ptr<IZoneWindow> 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
|
2020-06-17 21:06:16 +08:00
|
|
|
bool m_secondaryMouseButtonState{}; // True when secondary mouse button was clicked after window was moved
|
|
|
|
bool m_shiftKeyState{}; // True when shift key was pressed after window was moved
|
2020-04-30 18:16:08 +08:00
|
|
|
};
|
|
|
|
|
2020-06-17 21:06:16 +08:00
|
|
|
WindowMoveHandler::WindowMoveHandler(const winrt::com_ptr<IFancyZonesSettings>& settings, SecondaryMouseButtonsHook* mouseHook, ShiftKeyHook* shiftHook) :
|
|
|
|
pimpl(new WindowMoveHandlerPrivate(settings, mouseHook, shiftHook)) {}
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
WindowMoveHandler::~WindowMoveHandler()
|
|
|
|
{
|
|
|
|
delete pimpl;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowMoveHandler::InMoveSize() const noexcept
|
|
|
|
{
|
|
|
|
return pimpl->InMoveSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WindowMoveHandler::IsDragEnabled() const noexcept
|
|
|
|
{
|
|
|
|
return pimpl->IsDragEnabled();
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
void WindowMoveHandler::OnMouseDown() noexcept
|
|
|
|
{
|
|
|
|
pimpl->OnMouseDown();
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:06:16 +08:00
|
|
|
void WindowMoveHandler::OnShiftChangeState(bool state) noexcept
|
|
|
|
{
|
|
|
|
pimpl->OnShiftChangeState(state);
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandler::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
pimpl->MoveSizeStart(window, monitor, ptScreen, zoneWindowMap);
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandler::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
pimpl->MoveSizeUpdate(monitor, ptScreen, zoneWindowMap);
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandler::MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
pimpl->MoveSizeEnd(window, ptScreen, zoneWindowMap);
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandler::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<int>& indexSet, winrt::com_ptr<IZoneWindow> zoneWindow) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
2020-05-31 18:36:45 +08:00
|
|
|
pimpl->MoveWindowIntoZoneByIndexSet(window, indexSet, zoneWindow);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
bool WindowMoveHandler::MoveWindowIntoZoneByDirection(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr<IZoneWindow> zoneWindow)
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
2020-05-31 18:36:45 +08:00
|
|
|
return pimpl->MoveWindowIntoZoneByDirection(window, vkCode, cycle, zoneWindow);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
void WindowMoveHandlerPrivate::OnMouseDown() noexcept
|
|
|
|
{
|
|
|
|
m_secondaryMouseButtonState = !m_secondaryMouseButtonState;
|
|
|
|
}
|
|
|
|
|
2020-06-17 21:06:16 +08:00
|
|
|
void WindowMoveHandlerPrivate::OnShiftChangeState(bool state) noexcept
|
|
|
|
{
|
|
|
|
m_shiftKeyState = state;
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandlerPrivate::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
if (!IsInterestingWindow(window, m_settings->GetSettings()->excludedAppsArray) || WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_inMoveSize = true;
|
|
|
|
|
|
|
|
auto iter = zoneWindowMap.find(monitor);
|
|
|
|
if (iter == end(zoneWindowMap))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_windowMoveSize = window;
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
if (m_settings->GetSettings()->mouseSwitch)
|
|
|
|
{
|
|
|
|
m_mouseHook->enable();
|
|
|
|
}
|
|
|
|
|
2020-07-01 23:23:57 +08:00
|
|
|
m_shiftHook->enable();
|
2020-06-17 21:06:16 +08:00
|
|
|
|
2020-04-30 18:16:08 +08:00
|
|
|
// This updates m_dragEnabled depending on if the shift key is being held down.
|
|
|
|
UpdateDragState(window);
|
|
|
|
|
|
|
|
if (m_dragEnabled)
|
|
|
|
{
|
|
|
|
m_zoneWindowMoveSize = iter->second;
|
2020-06-05 22:53:08 +08:00
|
|
|
m_zoneWindowMoveSize->MoveSizeEnter(window);
|
2020-04-30 18:16:08 +08:00
|
|
|
if (m_settings->GetSettings()->showZonesOnAllMonitors)
|
|
|
|
{
|
|
|
|
for (auto [keyMonitor, zoneWindow] : zoneWindowMap)
|
|
|
|
{
|
|
|
|
// Skip calling ShowZoneWindow for iter->second (m_zoneWindowMoveSize) since it
|
|
|
|
// was already called in MoveSizeEnter
|
|
|
|
const bool moveSizeEnterCalled = zoneWindow == m_zoneWindowMoveSize;
|
|
|
|
if (zoneWindow && !moveSizeEnterCalled)
|
|
|
|
{
|
|
|
|
zoneWindow->ShowZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_zoneWindowMoveSize)
|
|
|
|
{
|
2020-05-27 22:55:46 +08:00
|
|
|
m_zoneWindowMoveSize->RestoreOriginalTransparency();
|
2020-04-30 18:16:08 +08:00
|
|
|
m_zoneWindowMoveSize = nullptr;
|
|
|
|
for (auto [keyMonitor, zoneWindow] : zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandlerPrivate::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
if (!m_inMoveSize)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This updates m_dragEnabled depending on if the shift key is being held down.
|
|
|
|
UpdateDragState(m_windowMoveSize);
|
|
|
|
|
|
|
|
if (m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
// Update the ZoneWindow already handling move/size
|
|
|
|
if (!m_dragEnabled)
|
|
|
|
{
|
|
|
|
// Drag got disabled, tell it to cancel and hide all windows
|
|
|
|
m_zoneWindowMoveSize = nullptr;
|
|
|
|
|
|
|
|
for (auto [keyMonitor, zoneWindow] : zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
2020-05-27 22:55:46 +08:00
|
|
|
zoneWindow->RestoreOriginalTransparency();
|
2020-04-30 18:16:08 +08:00
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto iter = zoneWindowMap.find(monitor);
|
|
|
|
if (iter != zoneWindowMap.end())
|
|
|
|
{
|
|
|
|
if (iter->second != m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
// The drag has moved to a different monitor.
|
2020-05-27 22:55:46 +08:00
|
|
|
m_zoneWindowMoveSize->RestoreOriginalTransparency();
|
2020-04-30 18:16:08 +08:00
|
|
|
|
|
|
|
if (!m_settings->GetSettings()->showZonesOnAllMonitors)
|
|
|
|
{
|
|
|
|
m_zoneWindowMoveSize->HideZoneWindow();
|
|
|
|
}
|
|
|
|
m_zoneWindowMoveSize = iter->second;
|
2020-06-05 22:53:08 +08:00
|
|
|
m_zoneWindowMoveSize->MoveSizeEnter(m_windowMoveSize);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (auto [keyMonitor, zoneWindow] : zoneWindowMap)
|
|
|
|
{
|
|
|
|
zoneWindow->MoveSizeUpdate(ptScreen, m_dragEnabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (m_dragEnabled)
|
|
|
|
{
|
|
|
|
// We'll get here if the user presses/releases shift while dragging.
|
|
|
|
// Restart the drag on the ZoneWindow that m_windowMoveSize is on
|
|
|
|
MoveSizeStart(m_windowMoveSize, monitor, ptScreen, zoneWindowMap);
|
|
|
|
MoveSizeUpdate(monitor, ptScreen, zoneWindowMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandlerPrivate::MoveSizeEnd(HWND window, POINT const& ptScreen, const std::unordered_map<HMONITOR, winrt::com_ptr<IZoneWindow>>& zoneWindowMap) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
if (window != m_windowMoveSize && !IsInterestingWindow(window, m_settings->GetSettings()->excludedAppsArray))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
m_mouseHook->disable();
|
2020-06-17 21:06:16 +08:00
|
|
|
m_shiftHook->disable();
|
2020-06-05 22:53:08 +08:00
|
|
|
|
2020-04-30 18:16:08 +08:00
|
|
|
m_inMoveSize = false;
|
|
|
|
m_dragEnabled = false;
|
2020-06-05 22:53:08 +08:00
|
|
|
m_secondaryMouseButtonState = false;
|
2020-04-30 18:16:08 +08:00
|
|
|
m_windowMoveSize = nullptr;
|
|
|
|
if (m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
auto zoneWindow = std::move(m_zoneWindowMoveSize);
|
|
|
|
zoneWindow->MoveSizeEnd(window, ptScreen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-01 21:36:05 +08:00
|
|
|
if (m_settings->GetSettings()->restoreSize)
|
|
|
|
{
|
|
|
|
if (WindowMoveHandlerUtils::IsCursorTypeIndicatingSizeEvent())
|
|
|
|
{
|
|
|
|
::RemoveProp(window, RESTORE_SIZE_STAMP);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RestoreWindowSize(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:16:08 +08:00
|
|
|
auto monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
if (monitor)
|
|
|
|
{
|
|
|
|
auto zoneWindow = zoneWindowMap.find(monitor);
|
|
|
|
if (zoneWindow != zoneWindowMap.end())
|
|
|
|
{
|
|
|
|
const auto zoneWindowPtr = zoneWindow->second;
|
|
|
|
const auto activeZoneSet = zoneWindowPtr->ActiveZoneSet();
|
|
|
|
if (activeZoneSet)
|
|
|
|
{
|
|
|
|
wil::unique_cotaskmem_string guidString;
|
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(activeZoneSet->Id(), &guidString)))
|
|
|
|
{
|
|
|
|
JSONHelpers::FancyZonesDataInstance().RemoveAppLastZone(window, zoneWindowPtr->UniqueId(), guidString.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-08 22:59:36 +08:00
|
|
|
::RemoveProp(window, MULTI_ZONE_STAMP);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Also, hide all windows (regardless of settings)
|
|
|
|
for (auto [keyMonitor, zoneWindow] : zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void WindowMoveHandlerPrivate::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<int>& indexSet, winrt::com_ptr<IZoneWindow> zoneWindow) noexcept
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
|
|
|
if (window != m_windowMoveSize)
|
|
|
|
{
|
2020-05-31 18:36:45 +08:00
|
|
|
zoneWindow->MoveWindowIntoZoneByIndexSet(window, indexSet);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
bool WindowMoveHandlerPrivate::MoveWindowIntoZoneByDirection(HWND window, DWORD vkCode, bool cycle, winrt::com_ptr<IZoneWindow> zoneWindow)
|
2020-04-30 18:16:08 +08:00
|
|
|
{
|
2020-05-31 18:36:45 +08:00
|
|
|
return zoneWindow && zoneWindow->MoveWindowIntoZoneByDirection(window, vkCode, cycle);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void WindowMoveHandlerPrivate::UpdateDragState(HWND window) noexcept
|
|
|
|
{
|
|
|
|
if (m_settings->GetSettings()->shiftDrag)
|
|
|
|
{
|
2020-06-17 21:06:16 +08:00
|
|
|
m_dragEnabled = (m_shiftKeyState ^ m_secondaryMouseButtonState);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-17 21:06:16 +08:00
|
|
|
m_dragEnabled = !(m_shiftKeyState ^ m_secondaryMouseButtonState);
|
2020-04-30 18:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool warning_shown = false;
|
|
|
|
if (!is_process_elevated() && IsProcessOfWindowElevated(window))
|
|
|
|
{
|
|
|
|
m_dragEnabled = false;
|
|
|
|
if (!warning_shown && !is_cant_drag_elevated_warning_disabled())
|
|
|
|
{
|
|
|
|
std::vector<notifications::action_t> actions = {
|
|
|
|
notifications::link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_LEARN_MORE), L"https://aka.ms/powertoysDetectedElevatedHelp" },
|
|
|
|
notifications::link_button{ GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED_DIALOG_DONT_SHOW_AGAIN), L"powertoys://cant_drag_elevated_disable/" }
|
|
|
|
};
|
|
|
|
notifications::show_toast_with_activations(GET_RESOURCE_STRING(IDS_CANT_DRAG_ELEVATED), {}, std::move(actions));
|
|
|
|
warning_shown = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|