2019-09-05 00:26:26 +08:00
|
|
|
#include "pch.h"
|
2019-09-09 14:53:30 +08:00
|
|
|
#include "common/dpi_aware.h"
|
2019-11-08 02:56:32 +08:00
|
|
|
#include "common/on_thread_executor.h"
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-12-17 16:21:46 +08:00
|
|
|
#include "FancyZones.h"
|
|
|
|
#include "lib/Settings.h"
|
|
|
|
#include "lib/ZoneWindow.h"
|
|
|
|
#include "lib/RegistryHelpers.h"
|
2020-02-10 21:59:51 +08:00
|
|
|
#include "lib/JsonHelpers.h"
|
|
|
|
#include "lib/ZoneSet.h"
|
2019-12-17 16:21:46 +08:00
|
|
|
#include "trace.h"
|
|
|
|
|
2019-12-12 17:10:55 +08:00
|
|
|
#include <functional>
|
2019-12-17 16:21:46 +08:00
|
|
|
#include <common/common.h>
|
2020-03-24 22:17:25 +08:00
|
|
|
#include <common/window_helpers.h>
|
|
|
|
#include <common/notifications.h>
|
|
|
|
#include <lib/util.h>
|
2020-02-18 18:55:08 +08:00
|
|
|
#include <unordered_set>
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-24 22:17:25 +08:00
|
|
|
#include <common/notifications/fancyzones_notifications.h>
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
enum class DisplayChangeType
|
|
|
|
{
|
|
|
|
WorkArea,
|
|
|
|
DisplayChange,
|
|
|
|
VirtualDesktop,
|
|
|
|
Editor,
|
|
|
|
Initialization
|
|
|
|
};
|
2019-12-12 17:10:55 +08:00
|
|
|
|
2020-03-24 22:17:25 +08:00
|
|
|
extern "C" IMAGE_DOS_HEADER __ImageBase;
|
|
|
|
|
2019-12-12 17:10:55 +08:00
|
|
|
namespace std
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
template<>
|
|
|
|
struct hash<GUID>
|
2019-12-12 17:10:55 +08:00
|
|
|
{
|
|
|
|
size_t operator()(const GUID& Value) const
|
|
|
|
{
|
|
|
|
RPC_STATUS status = RPC_S_OK;
|
|
|
|
return ::UuidHash(&const_cast<GUID&>(Value), &status);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
struct FancyZones : public winrt::implements<FancyZones, IFancyZones, IFancyZonesCallback, IZoneWindowHost>
|
|
|
|
{
|
|
|
|
public:
|
2020-02-10 21:59:51 +08:00
|
|
|
FancyZones(HINSTANCE hinstance, const winrt::com_ptr<IFancyZonesSettings>& settings) noexcept :
|
|
|
|
m_hinstance(hinstance),
|
|
|
|
m_settings(settings)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
m_settings->SetCallback(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZones
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
Run() noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
Destroy() noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
InMoveSize() noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
return m_inMoveSize;
|
|
|
|
}
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen) noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen) noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
VirtualDesktopChanged() noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
VirtualDesktopInitialize() noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
WindowCreated(HWND window) noexcept;
|
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
OnKeyDown(PKBDLLHOOKSTRUCT info) noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ToggleEditor() noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
SettingsChanged() noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
// IZoneWindowHost
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
MoveWindowsOnActiveZoneSetChange() noexcept;
|
|
|
|
IFACEMETHODIMP_(COLORREF)
|
2020-03-25 22:38:44 +08:00
|
|
|
GetZoneColor() noexcept
|
|
|
|
{
|
|
|
|
// Skip the leading # and convert to long
|
|
|
|
const auto color = m_settings->GetSettings()->zoneColor;
|
|
|
|
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
|
|
|
const auto nR = (tmp & 0xFF0000) >> 16;
|
|
|
|
const auto nG = (tmp & 0xFF00) >> 8;
|
|
|
|
const auto nB = (tmp & 0xFF);
|
|
|
|
return RGB(nR, nG, nB);
|
|
|
|
}
|
|
|
|
IFACEMETHODIMP_(COLORREF)
|
|
|
|
GetZoneBorderColor() noexcept
|
|
|
|
{
|
|
|
|
// Skip the leading # and convert to long
|
|
|
|
const auto color = m_settings->GetSettings()->zoneBorderColor;
|
|
|
|
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
|
|
|
const auto nR = (tmp & 0xFF0000) >> 16;
|
|
|
|
const auto nG = (tmp & 0xFF00) >> 8;
|
|
|
|
const auto nB = (tmp & 0xFF);
|
|
|
|
return RGB(nR, nG, nB);
|
|
|
|
}
|
|
|
|
IFACEMETHODIMP_(COLORREF)
|
2020-02-17 23:28:49 +08:00
|
|
|
GetZoneHighlightColor() noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
// Skip the leading # and convert to long
|
2020-03-13 17:55:15 +08:00
|
|
|
const auto color = m_settings->GetSettings()->zoneHightlightColor;
|
2019-09-05 00:26:26 +08:00
|
|
|
const auto tmp = std::stol(color.substr(1), nullptr, 16);
|
|
|
|
const auto nR = (tmp & 0xFF0000) >> 16;
|
|
|
|
const auto nG = (tmp & 0xFF00) >> 8;
|
|
|
|
const auto nB = (tmp & 0xFF);
|
|
|
|
return RGB(nR, nG, nB);
|
|
|
|
}
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(IZoneWindow*)
|
|
|
|
GetParentZoneWindow(HMONITOR monitor) noexcept
|
2019-12-06 22:09:27 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
//NOTE: as public method it's unsafe without lock, but it's called from AddZoneWindow through making ZoneWindow that causes deadlock
|
|
|
|
//TODO: needs refactoring
|
|
|
|
auto it = m_zoneWindowMap.find(monitor);
|
|
|
|
if (it != m_zoneWindowMap.end())
|
|
|
|
{
|
|
|
|
return it->second.get();
|
2019-12-06 22:09:27 +08:00
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
return nullptr;
|
2019-12-06 22:09:27 +08:00
|
|
|
}
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(int)
|
|
|
|
GetZoneHighlightOpacity() noexcept
|
2020-01-07 01:59:18 +08:00
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
return m_settings->GetSettings()->zoneHighlightOpacity;
|
2020-01-07 01:59:18 +08:00
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-03-25 22:38:44 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
isMakeDraggedWindowTransparentActive() noexcept
|
|
|
|
{
|
|
|
|
return m_settings->GetSettings()->makeDraggedWindowTransparent;
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
LRESULT WndProc(HWND, UINT, WPARAM, LPARAM) noexcept;
|
|
|
|
void OnDisplayChange(DisplayChangeType changeType) noexcept;
|
|
|
|
void AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept;
|
2020-03-10 16:59:34 +08:00
|
|
|
|
|
|
|
void MoveWindowIntoZoneByIndex(HWND window, HMONITOR monitor, int index) noexcept;
|
2020-04-10 22:09:08 +08:00
|
|
|
void MoveWindowIntoZoneByIndexSet(HWND window, HMONITOR monitor, const std::vector<int>& indexSet) noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static LRESULT CALLBACK s_WndProc(HWND, UINT, WPARAM, LPARAM) noexcept;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct require_read_lock
|
|
|
|
{
|
|
|
|
template<typename T>
|
2020-02-10 21:59:51 +08:00
|
|
|
require_read_lock(const std::shared_lock<T>& lock)
|
|
|
|
{
|
|
|
|
lock;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
template<typename T>
|
2020-02-10 21:59:51 +08:00
|
|
|
require_read_lock(const std::unique_lock<T>& lock)
|
|
|
|
{
|
|
|
|
lock;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct require_write_lock
|
|
|
|
{
|
|
|
|
template<typename T>
|
2020-02-10 21:59:51 +08:00
|
|
|
require_write_lock(const std::unique_lock<T>& lock)
|
|
|
|
{
|
|
|
|
lock;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
};
|
|
|
|
|
2020-02-03 16:32:38 +08:00
|
|
|
bool IsInterestingWindow(HWND window) noexcept;
|
2020-04-08 21:12:04 +08:00
|
|
|
bool IsCursorTypeIndicatingSizeEvent();
|
2019-09-05 00:26:26 +08:00
|
|
|
void UpdateZoneWindows() noexcept;
|
|
|
|
void MoveWindowsOnDisplayChange() noexcept;
|
2020-03-24 22:17:25 +08:00
|
|
|
void UpdateDragState(HWND window, require_write_lock) noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
void CycleActiveZoneSet(DWORD vkCode) noexcept;
|
2020-02-06 20:12:59 +08:00
|
|
|
bool OnSnapHotkey(DWORD vkCode) noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
void MoveSizeStartInternal(HWND window, HMONITOR monitor, POINT const& ptScreen, require_write_lock) noexcept;
|
|
|
|
void MoveSizeEndInternal(HWND window, POINT const& ptScreen, require_write_lock) noexcept;
|
|
|
|
void MoveSizeUpdateInternal(HMONITOR monitor, POINT const& ptScreen, require_write_lock) noexcept;
|
2020-02-18 18:55:08 +08:00
|
|
|
|
2019-12-12 17:10:55 +08:00
|
|
|
void HandleVirtualDesktopUpdates(HANDLE fancyZonesDestroyedEvent) noexcept;
|
2020-02-18 18:55:08 +08:00
|
|
|
void RegisterVirtualDesktopUpdates(std::unordered_set<GUID>& currentVirtualDesktopIds) noexcept;
|
|
|
|
void RegisterNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept;
|
|
|
|
bool IsNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept;
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
void OnEditorExitEvent() noexcept;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-03-25 01:50:26 +08:00
|
|
|
std::vector<std::pair<HMONITOR, RECT>> GetRawMonitorData() noexcept;
|
|
|
|
std::vector<HMONITOR> GetMonitorsSorted() noexcept;
|
|
|
|
bool MoveWindowIntoZoneByDirection(HMONITOR monitor, HWND window, DWORD vkCode, bool cycle);
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
const HINSTANCE m_hinstance{};
|
|
|
|
|
2019-12-12 17:10:55 +08:00
|
|
|
HKEY m_virtualDesktopsRegKey{ nullptr };
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
mutable std::shared_mutex m_lock;
|
|
|
|
HWND m_window{};
|
|
|
|
HWND m_windowMoveSize{}; // The window that is being moved/sized
|
2020-02-10 21:59:51 +08:00
|
|
|
bool m_inMoveSize{}; // Whether or not a move/size operation is currently active
|
2019-09-05 00:26:26 +08:00
|
|
|
bool m_dragEnabled{}; // True if we should be showing zone hints while dragging
|
|
|
|
std::map<HMONITOR, winrt::com_ptr<IZoneWindow>> m_zoneWindowMap; // Map of monitor to ZoneWindow (one per monitor)
|
|
|
|
winrt::com_ptr<IZoneWindow> m_zoneWindowMoveSize; // "Active" ZoneWindow, where the move/size is happening. Will update as drag moves between monitors.
|
2020-02-10 21:59:51 +08:00
|
|
|
winrt::com_ptr<IFancyZonesSettings> m_settings{};
|
2019-11-19 07:29:42 +08:00
|
|
|
GUID m_currentVirtualDesktopId{}; // UUID of the current virtual desktop. Is GUID_NULL until first VD switch per session.
|
2020-02-18 18:55:08 +08:00
|
|
|
std::unordered_map<GUID, std::vector<HMONITOR>> m_processedWorkAreas; // Work area is defined by monitor and virtual desktop id.
|
2019-11-19 07:29:42 +08:00
|
|
|
wil::unique_handle m_terminateEditorEvent; // Handle of FancyZonesEditor.exe we launch and wait on
|
2019-12-12 17:10:55 +08:00
|
|
|
wil::unique_handle m_terminateVirtualDesktopTrackerEvent;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-11-08 02:56:32 +08:00
|
|
|
OnThreadExecutor m_dpiUnawareThread;
|
2019-12-12 17:10:55 +08:00
|
|
|
OnThreadExecutor m_virtualDesktopTrackerThread;
|
2019-11-08 02:56:32 +08:00
|
|
|
|
2019-11-19 07:29:42 +08:00
|
|
|
static UINT WM_PRIV_VDCHANGED; // Message to get back on to the UI thread when virtual desktop changes
|
2019-12-24 22:47:28 +08:00
|
|
|
static UINT WM_PRIV_VDINIT; // Message to get back to the UI thread when FancyZones are initialized
|
2019-11-19 07:29:42 +08:00
|
|
|
static UINT WM_PRIV_EDITOR; // Message to get back on to the UI thread when the editor exits
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-11-19 07:29:42 +08:00
|
|
|
// Did we terminate the editor or was it closed cleanly?
|
2019-09-05 00:26:26 +08:00
|
|
|
enum class EditorExitKind : byte
|
|
|
|
{
|
|
|
|
Exit,
|
|
|
|
Terminate
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
UINT FancyZones::WM_PRIV_VDCHANGED = RegisterWindowMessage(L"{128c2cb0-6bdf-493e-abbe-f8705e04aa95}");
|
2019-12-24 22:47:28 +08:00
|
|
|
UINT FancyZones::WM_PRIV_VDINIT = RegisterWindowMessage(L"{469818a8-00fa-4069-b867-a1da484fcd9a}");
|
2019-09-05 00:26:26 +08:00
|
|
|
UINT FancyZones::WM_PRIV_EDITOR = RegisterWindowMessage(L"{87543824-7080-4e91-9d9c-0404642fc7b6}");
|
|
|
|
|
|
|
|
// IFancyZones
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::Run() noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
|
|
|
|
WNDCLASSEXW wcex{};
|
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wcex.lpfnWndProc = s_WndProc;
|
|
|
|
wcex.hInstance = m_hinstance;
|
|
|
|
wcex.lpszClassName = L"SuperFancyZones";
|
|
|
|
RegisterClassExW(&wcex);
|
|
|
|
|
|
|
|
BufferedPaintInit();
|
|
|
|
|
|
|
|
m_window = CreateWindowExW(WS_EX_TOOLWINDOW, L"SuperFancyZones", L"", WS_POPUP, 0, 0, 0, 0, nullptr, nullptr, m_hinstance, this);
|
2020-02-10 21:59:51 +08:00
|
|
|
if (!m_window)
|
|
|
|
return;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2020-03-13 17:55:15 +08:00
|
|
|
RegisterHotKey(m_window, 1, m_settings->GetSettings()->editorHotkey.get_modifiers(), m_settings->GetSettings()->editorHotkey.get_code());
|
2019-11-19 07:29:42 +08:00
|
|
|
|
2019-12-24 22:47:28 +08:00
|
|
|
VirtualDesktopInitialize();
|
2019-11-08 02:56:32 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
m_dpiUnawareThread.submit(OnThreadExecutor::task_t{ [] {
|
|
|
|
SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE);
|
|
|
|
SetThreadDpiHostingBehavior(DPI_HOSTING_BEHAVIOR_MIXED);
|
|
|
|
} })
|
|
|
|
.wait();
|
2019-12-12 17:10:55 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\VirtualDesktops", 0, KEY_ALL_ACCESS, &m_virtualDesktopsRegKey) == ERROR_SUCCESS)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
m_terminateVirtualDesktopTrackerEvent.reset(CreateEvent(nullptr, FALSE, FALSE, nullptr));
|
|
|
|
m_virtualDesktopTrackerThread.submit(
|
|
|
|
OnThreadExecutor::task_t{ std::bind(&FancyZones::HandleVirtualDesktopUpdates, this, m_terminateVirtualDesktopTrackerEvent.get()) });
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZones
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::Destroy() noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
2019-11-08 03:05:12 +08:00
|
|
|
m_zoneWindowMap.clear();
|
2019-09-05 00:26:26 +08:00
|
|
|
BufferedPaintUnInit();
|
|
|
|
if (m_window)
|
|
|
|
{
|
|
|
|
DestroyWindow(m_window);
|
|
|
|
m_window = nullptr;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
if (m_terminateVirtualDesktopTrackerEvent)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
SetEvent(m_terminateVirtualDesktopTrackerEvent.get());
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
if (m_virtualDesktopsRegKey)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
RegCloseKey(m_virtualDesktopsRegKey);
|
|
|
|
m_virtualDesktopsRegKey = nullptr;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::MoveSizeStart(HWND window, HMONITOR monitor, POINT const& ptScreen) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-03 16:32:38 +08:00
|
|
|
if (IsInterestingWindow(window))
|
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
MoveSizeStartInternal(window, monitor, ptScreen, writeLock);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::MoveSizeUpdate(HMONITOR monitor, POINT const& ptScreen) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
MoveSizeUpdateInternal(monitor, ptScreen, writeLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-03 16:32:38 +08:00
|
|
|
if (window == m_windowMoveSize || IsInterestingWindow(window))
|
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
MoveSizeEndInternal(window, ptScreen, writeLock);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::VirtualDesktopChanged() noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
// VirtualDesktopChanged is called from another thread but results in new windows being created.
|
|
|
|
// Jump over to the UI thread to handle it.
|
2020-02-10 21:59:51 +08:00
|
|
|
std::shared_lock readLock(m_lock);
|
2019-09-05 00:26:26 +08:00
|
|
|
PostMessage(m_window, WM_PRIV_VDCHANGED, 0, 0);
|
|
|
|
}
|
|
|
|
|
2019-12-24 22:47:28 +08:00
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::VirtualDesktopInitialize() noexcept
|
2019-12-24 22:47:28 +08:00
|
|
|
{
|
|
|
|
PostMessage(m_window, WM_PRIV_VDINIT, 0, 0);
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::WindowCreated(HWND window) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->appLastZone_moveWindows && IsInterestingWindow(window))
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
for (const auto& [monitor, zoneWindow] : m_zoneWindowMap)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
const auto activeZoneSet = zoneWindow->ActiveZoneSet();
|
|
|
|
if (activeZoneSet)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance();
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-10 16:59:34 +08:00
|
|
|
wil::unique_cotaskmem_string guidString;
|
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(activeZoneSet->Id(), &guidString)))
|
|
|
|
{
|
|
|
|
int zoneIndex = fancyZonesData.GetAppLastZoneIndex(window, zoneWindow->UniqueId(), guidString.get());
|
|
|
|
if (zoneIndex != -1)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
MoveWindowIntoZoneByIndex(window, monitor, zoneIndex);
|
|
|
|
break;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
FancyZones::OnKeyDown(PKBDLLHOOKSTRUCT info) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
// Return true to swallow the keyboard event
|
|
|
|
bool const shift = GetAsyncKeyState(VK_SHIFT) & 0x8000;
|
|
|
|
bool const win = GetAsyncKeyState(VK_LWIN) & 0x8000;
|
|
|
|
if (win && !shift)
|
|
|
|
{
|
|
|
|
bool const ctrl = GetAsyncKeyState(VK_CONTROL) & 0x8000;
|
|
|
|
if (ctrl)
|
|
|
|
{
|
2020-03-26 01:32:33 +08:00
|
|
|
// Temporarily disable Win+Ctrl+Number functionality
|
|
|
|
//if ((info->vkCode >= '0') && (info->vkCode <= '9'))
|
|
|
|
//{
|
|
|
|
// // Win+Ctrl+Number will cycle through ZoneSets
|
|
|
|
// Trace::FancyZones::OnKeyDown(info->vkCode, win, ctrl, false /*inMoveSize*/);
|
|
|
|
// CycleActiveZoneSet(info->vkCode);
|
|
|
|
// return true;
|
|
|
|
//}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
else if ((info->vkCode == VK_RIGHT) || (info->vkCode == VK_LEFT))
|
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->overrideSnapHotkeys)
|
2019-11-19 07:29:42 +08:00
|
|
|
{
|
|
|
|
// Win+Left, Win+Right will cycle through Zones in the active ZoneSet
|
|
|
|
Trace::FancyZones::OnKeyDown(info->vkCode, win, ctrl, false /*inMoveSize*/);
|
2020-02-06 20:12:59 +08:00
|
|
|
return OnSnapHotkey(info->vkCode);
|
2019-11-19 07:29:42 +08:00
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 01:32:33 +08:00
|
|
|
// Temporarily disable Win+Ctrl+Number functionality
|
|
|
|
//else if (m_inMoveSize && (info->vkCode >= '0') && (info->vkCode <= '9'))
|
|
|
|
//{
|
|
|
|
// // This allows you to cycle through ZoneSets while dragging a window
|
|
|
|
// Trace::FancyZones::OnKeyDown(info->vkCode, win, false /*control*/, true /*inMoveSize*/);
|
|
|
|
// CycleActiveZoneSet(info->vkCode);
|
|
|
|
// return false;
|
|
|
|
//}
|
2020-03-05 15:01:58 +08:00
|
|
|
if (m_dragEnabled && shift)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IFancyZonesCallback
|
|
|
|
void FancyZones::ToggleEditor() noexcept
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
if (m_terminateEditorEvent)
|
|
|
|
{
|
|
|
|
SetEvent(m_terminateEditorEvent.get());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
m_terminateEditorEvent.reset(CreateEvent(nullptr, true, false, nullptr));
|
|
|
|
}
|
|
|
|
|
2019-09-28 22:29:29 +08:00
|
|
|
HMONITOR monitor{};
|
2019-11-08 02:56:32 +08:00
|
|
|
HWND foregroundWindow{};
|
2019-09-28 22:29:29 +08:00
|
|
|
|
2020-03-13 17:55:15 +08:00
|
|
|
const bool use_cursorpos_editor_startupscreen = m_settings->GetSettings()->use_cursorpos_editor_startupscreen;
|
2019-11-08 02:56:32 +08:00
|
|
|
POINT currentCursorPos{};
|
|
|
|
if (use_cursorpos_editor_startupscreen)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-09-28 22:29:29 +08:00
|
|
|
GetCursorPos(¤tCursorPos);
|
|
|
|
monitor = MonitorFromPoint(currentCursorPos, MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-08 02:56:32 +08:00
|
|
|
foregroundWindow = GetForegroundWindow();
|
2019-09-28 22:29:29 +08:00
|
|
|
monitor = MonitorFromWindow(foregroundWindow, MONITOR_DEFAULTTOPRIMARY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!monitor)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
auto iter = m_zoneWindowMap.find(monitor);
|
|
|
|
if (iter == m_zoneWindowMap.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MONITORINFOEX mi;
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
m_dpiUnawareThread.submit(OnThreadExecutor::task_t{ [&] {
|
|
|
|
GetMonitorInfo(monitor, &mi);
|
|
|
|
} })
|
|
|
|
.wait();
|
2019-11-08 02:56:32 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
auto zoneWindow = iter->second;
|
|
|
|
|
|
|
|
const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance();
|
|
|
|
fancyZonesData.CustomZoneSetsToJsonFile(ZoneWindowUtils::GetCustomZoneSetsTmpPath());
|
|
|
|
|
2019-11-08 02:56:32 +08:00
|
|
|
// Do not scale window params by the dpi, that will be done in the editor - see LayoutModel.Apply
|
2020-04-07 17:03:57 +08:00
|
|
|
const auto taskbar_x_offset = mi.rcWork.left - mi.rcMonitor.left;
|
|
|
|
const auto taskbar_y_offset = mi.rcWork.top - mi.rcMonitor.top;
|
2019-11-08 02:56:32 +08:00
|
|
|
const auto x = mi.rcMonitor.left + taskbar_x_offset;
|
|
|
|
const auto y = mi.rcMonitor.top + taskbar_y_offset;
|
|
|
|
const auto width = mi.rcWork.right - mi.rcWork.left;
|
|
|
|
const auto height = mi.rcWork.bottom - mi.rcWork.top;
|
2020-02-10 21:59:51 +08:00
|
|
|
const std::wstring editorLocation =
|
2019-09-28 22:29:29 +08:00
|
|
|
std::to_wstring(x) + L"_" +
|
|
|
|
std::to_wstring(y) + L"_" +
|
2019-11-08 02:56:32 +08:00
|
|
|
std::to_wstring(width) + L"_" +
|
|
|
|
std::to_wstring(height);
|
2019-09-28 22:29:29 +08:00
|
|
|
|
2020-02-17 23:28:49 +08:00
|
|
|
const auto deviceInfo = fancyZonesData.FindDeviceInfo(zoneWindow->UniqueId());
|
|
|
|
if (!deviceInfo.has_value())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-02-17 23:28:49 +08:00
|
|
|
JSONHelpers::DeviceInfoJSON deviceInfoJson{ zoneWindow->UniqueId(), *deviceInfo };
|
2020-02-10 21:59:51 +08:00
|
|
|
fancyZonesData.SerializeDeviceInfoToTmpFile(deviceInfoJson, ZoneWindowUtils::GetActiveZoneSetTmpPath());
|
2019-12-06 22:09:27 +08:00
|
|
|
|
2019-09-28 22:29:29 +08:00
|
|
|
const std::wstring params =
|
2020-02-10 21:59:51 +08:00
|
|
|
/*1*/ std::to_wstring(reinterpret_cast<UINT_PTR>(monitor)) + L" " +
|
|
|
|
/*2*/ editorLocation + L" " +
|
|
|
|
/*3*/ zoneWindow->WorkAreaKey() + L" " +
|
2020-03-18 01:12:32 +08:00
|
|
|
/*4*/ L"\"" + ZoneWindowUtils::GetActiveZoneSetTmpPath() + L"\" " +
|
|
|
|
/*5*/ L"\"" + ZoneWindowUtils::GetAppliedZoneSetTmpPath() + L"\" " +
|
|
|
|
/*6*/ L"\"" + ZoneWindowUtils::GetCustomZoneSetsTmpPath() + L"\"";
|
2019-09-28 22:29:29 +08:00
|
|
|
|
|
|
|
SHELLEXECUTEINFO sei{ sizeof(sei) };
|
|
|
|
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
|
|
|
sei.lpFile = L"modules\\FancyZonesEditor.exe";
|
|
|
|
sei.lpParameters = params.c_str();
|
|
|
|
sei.nShow = SW_SHOWNORMAL;
|
|
|
|
ShellExecuteEx(&sei);
|
2020-03-26 18:54:12 +08:00
|
|
|
Trace::FancyZones::EditorLaunched(1);
|
2019-09-28 22:29:29 +08:00
|
|
|
|
|
|
|
// Launch the editor on a background thread
|
|
|
|
// Wait for the editor's process to exit
|
|
|
|
// Post back to the main thread to update
|
2020-02-10 21:59:51 +08:00
|
|
|
std::thread waitForEditorThread([window = m_window, processHandle = sei.hProcess, terminateEditorEvent = m_terminateEditorEvent.get()]() {
|
2019-09-28 22:29:29 +08:00
|
|
|
HANDLE waitEvents[2] = { processHandle, terminateEditorEvent };
|
|
|
|
auto result = WaitForMultipleObjects(2, waitEvents, false, INFINITE);
|
|
|
|
if (result == WAIT_OBJECT_0 + 0)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-09-28 22:29:29 +08:00
|
|
|
// Editor exited
|
|
|
|
// Update any changes it may have made
|
|
|
|
PostMessage(window, WM_PRIV_EDITOR, 0, static_cast<LPARAM>(EditorExitKind::Exit));
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2019-09-28 22:29:29 +08:00
|
|
|
else if (result == WAIT_OBJECT_0 + 1)
|
|
|
|
{
|
|
|
|
// User hit Win+~ while editor is already running
|
|
|
|
// Shut it down
|
|
|
|
TerminateProcess(processHandle, 2);
|
|
|
|
PostMessage(window, WM_PRIV_EDITOR, 0, static_cast<LPARAM>(EditorExitKind::Terminate));
|
|
|
|
}
|
|
|
|
CloseHandle(processHandle);
|
|
|
|
});
|
|
|
|
|
|
|
|
waitForEditorThread.detach();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2019-10-02 23:18:55 +08:00
|
|
|
void FancyZones::SettingsChanged() noexcept
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
std::shared_lock readLock(m_lock);
|
2019-10-02 23:18:55 +08:00
|
|
|
// Update the hotkey
|
|
|
|
UnregisterHotKey(m_window, 1);
|
2020-03-13 17:55:15 +08:00
|
|
|
RegisterHotKey(m_window, 1, m_settings->GetSettings()->editorHotkey.get_modifiers(), m_settings->GetSettings()->editorHotkey.get_code());
|
2019-10-02 23:18:55 +08:00
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
// IZoneWindowHost
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
FancyZones::MoveWindowsOnActiveZoneSetChange() noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->zoneSetChange_moveWindows)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
MoveWindowsOnDisplayChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT FancyZones::WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept
|
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
|
|
|
case WM_HOTKEY:
|
|
|
|
{
|
|
|
|
if (wparam == 1)
|
|
|
|
{
|
2019-11-19 07:29:42 +08:00
|
|
|
ToggleEditor();
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_SETTINGCHANGE:
|
|
|
|
{
|
|
|
|
if (wparam == SPI_SETWORKAREA)
|
|
|
|
{
|
|
|
|
OnDisplayChange(DisplayChangeType::WorkArea);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_DISPLAYCHANGE:
|
|
|
|
{
|
|
|
|
OnDisplayChange(DisplayChangeType::DisplayChange);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
if (message == WM_PRIV_VDCHANGED)
|
|
|
|
{
|
|
|
|
OnDisplayChange(DisplayChangeType::VirtualDesktop);
|
|
|
|
}
|
2019-12-24 22:47:28 +08:00
|
|
|
else if (message == WM_PRIV_VDINIT)
|
|
|
|
{
|
|
|
|
OnDisplayChange(DisplayChangeType::Initialization);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
else if (message == WM_PRIV_EDITOR)
|
|
|
|
{
|
|
|
|
if (lparam == static_cast<LPARAM>(EditorExitKind::Exit))
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
OnEditorExitEvent();
|
2019-09-05 00:26:26 +08:00
|
|
|
OnDisplayChange(DisplayChangeType::Editor);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// Clean up the event either way
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
m_terminateEditorEvent.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return DefWindowProc(window, message, wparam, lparam);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::OnDisplayChange(DisplayChangeType changeType) noexcept
|
|
|
|
{
|
2019-12-24 22:47:28 +08:00
|
|
|
if (changeType == DisplayChangeType::VirtualDesktop ||
|
|
|
|
changeType == DisplayChangeType::Initialization)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
// Explorer persists this value to the registry on a per session basis but only after
|
|
|
|
// the first virtual desktop switch happens. If the user hasn't switched virtual desktops in this session
|
|
|
|
// then this value will be empty. This means loading the first virtual desktop's configuration can be
|
|
|
|
// funky the first time we load up at boot since the user will not have switched virtual desktops yet.
|
|
|
|
GUID currentVirtualDesktopId{};
|
|
|
|
if (SUCCEEDED(RegistryHelpers::GetCurrentVirtualDesktop(¤tVirtualDesktopId)))
|
|
|
|
{
|
2020-02-17 23:28:49 +08:00
|
|
|
std::unique_lock writeLock(m_lock);
|
2019-09-05 00:26:26 +08:00
|
|
|
m_currentVirtualDesktopId = currentVirtualDesktopId;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TODO: Use the previous "Desktop 1" fallback
|
|
|
|
// Need to maintain a map of desktop name to virtual desktop uuid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateZoneWindows();
|
|
|
|
|
|
|
|
if ((changeType == DisplayChangeType::WorkArea) || (changeType == DisplayChangeType::DisplayChange))
|
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->displayChange_moveWindows)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
MoveWindowsOnDisplayChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (changeType == DisplayChangeType::VirtualDesktop)
|
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->virtualDesktopChange_moveWindows)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
MoveWindowsOnDisplayChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (changeType == DisplayChangeType::Editor)
|
|
|
|
{
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->zoneSetChange_moveWindows)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
MoveWindowsOnDisplayChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::AddZoneWindow(HMONITOR monitor, PCWSTR deviceId) noexcept
|
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
wil::unique_cotaskmem_string virtualDesktopId;
|
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(m_currentVirtualDesktopId, &virtualDesktopId)))
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
std::wstring uniqueId = ZoneWindowUtils::GenerateUniqueId(monitor, deviceId, virtualDesktopId.get());
|
2020-02-18 18:55:08 +08:00
|
|
|
JSONHelpers::FancyZonesDataInstance().SetActiveDeviceId(uniqueId);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-02-18 18:55:08 +08:00
|
|
|
const bool newWorkArea = IsNewWorkArea(m_currentVirtualDesktopId, monitor);
|
2020-03-27 18:24:27 +08:00
|
|
|
// "Turning FLASHING_ZONE option off"
|
|
|
|
//const bool flash = m_settings->GetSettings()->zoneSetChange_flashZones && newWorkArea;
|
|
|
|
const bool flash = false;
|
2019-12-12 17:10:55 +08:00
|
|
|
|
2020-04-10 22:29:18 +08:00
|
|
|
auto zoneWindow = MakeZoneWindow(this, m_hinstance, monitor, uniqueId, flash, newWorkArea);
|
2020-02-10 21:59:51 +08:00
|
|
|
if (zoneWindow)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
m_zoneWindowMap[monitor] = std::move(zoneWindow);
|
|
|
|
}
|
2020-03-05 15:01:58 +08:00
|
|
|
|
2020-02-18 18:55:08 +08:00
|
|
|
if (newWorkArea)
|
|
|
|
{
|
|
|
|
RegisterNewWorkArea(m_currentVirtualDesktopId, monitor);
|
|
|
|
JSONHelpers::FancyZonesDataInstance().SaveFancyZonesData();
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-10 16:59:34 +08:00
|
|
|
void FancyZones::MoveWindowIntoZoneByIndex(HWND window, HMONITOR monitor, int index) noexcept
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
MoveWindowIntoZoneByIndexSet(window, monitor, { index });
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::MoveWindowIntoZoneByIndexSet(HWND window, HMONITOR monitor, const std::vector<int>& indexSet) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
if (window != m_windowMoveSize)
|
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
const HMONITOR hm = (monitor != nullptr) ? monitor : MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
if (hm)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
auto zoneWindow = m_zoneWindowMap.find(hm);
|
|
|
|
if (zoneWindow != m_zoneWindowMap.end())
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-10 16:59:34 +08:00
|
|
|
const auto& zoneWindowPtr = zoneWindow->second;
|
2020-04-10 22:09:08 +08:00
|
|
|
zoneWindowPtr->MoveWindowIntoZoneByIndexSet(window, indexSet);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT CALLBACK FancyZones::s_WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept
|
|
|
|
{
|
|
|
|
auto thisRef = reinterpret_cast<FancyZones*>(GetWindowLongPtr(window, GWLP_USERDATA));
|
|
|
|
if (!thisRef && (message == WM_CREATE))
|
|
|
|
{
|
|
|
|
const auto createStruct = reinterpret_cast<LPCREATESTRUCT>(lparam);
|
|
|
|
thisRef = reinterpret_cast<FancyZones*>(createStruct->lpCreateParams);
|
|
|
|
SetWindowLongPtr(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(thisRef));
|
|
|
|
}
|
|
|
|
|
|
|
|
return thisRef ? thisRef->WndProc(window, message, wparam, lparam) :
|
2020-02-10 21:59:51 +08:00
|
|
|
DefWindowProc(window, message, wparam, lparam);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
2020-02-03 16:32:38 +08:00
|
|
|
bool FancyZones::IsInterestingWindow(HWND window) noexcept
|
|
|
|
{
|
2020-02-07 22:53:57 +08:00
|
|
|
auto filtered = get_fancyzones_filtered_window(window);
|
|
|
|
if (!filtered.zonable)
|
2020-02-03 16:32:38 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Filter out user specified apps
|
2020-02-07 22:53:57 +08:00
|
|
|
CharUpperBuffW(filtered.process_path.data(), (DWORD)filtered.process_path.length());
|
2020-02-03 16:32:38 +08:00
|
|
|
if (m_settings)
|
|
|
|
{
|
2020-03-16 20:25:30 +08:00
|
|
|
const auto& excludedAppsArray = m_settings->GetSettings()->excludedAppsArray;
|
|
|
|
if (find_app_name_in_path(filtered.process_path, excludedAppsArray))
|
2020-02-03 16:32:38 +08:00
|
|
|
{
|
2020-03-16 20:25:30 +08:00
|
|
|
return false;
|
2020-02-03 16:32:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:12:04 +08:00
|
|
|
bool FancyZones::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;
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
void FancyZones::UpdateZoneWindows() noexcept
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
auto callback = [](HMONITOR monitor, HDC, RECT*, LPARAM data) -> BOOL {
|
2019-09-05 00:26:26 +08:00
|
|
|
MONITORINFOEX mi;
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
if (GetMonitorInfo(monitor, &mi))
|
|
|
|
{
|
|
|
|
DISPLAY_DEVICE displayDevice = { sizeof(displayDevice) };
|
|
|
|
PCWSTR deviceId = nullptr;
|
|
|
|
|
|
|
|
bool validMonitor = true;
|
|
|
|
if (EnumDisplayDevices(mi.szDevice, 0, &displayDevice, 1))
|
|
|
|
{
|
|
|
|
if (WI_IsFlagSet(displayDevice.StateFlags, DISPLAY_DEVICE_MIRRORING_DRIVER))
|
|
|
|
{
|
|
|
|
validMonitor = FALSE;
|
|
|
|
}
|
|
|
|
else if (displayDevice.DeviceID[0] != L'\0')
|
|
|
|
{
|
|
|
|
deviceId = displayDevice.DeviceID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (validMonitor)
|
|
|
|
{
|
|
|
|
if (!deviceId)
|
|
|
|
{
|
|
|
|
deviceId = GetSystemMetrics(SM_REMOTESESSION) ?
|
2020-02-10 21:59:51 +08:00
|
|
|
L"\\\\?\\DISPLAY#REMOTEDISPLAY#" :
|
|
|
|
L"\\\\?\\DISPLAY#LOCALDISPLAY#";
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
auto strongThis = reinterpret_cast<FancyZones*>(data);
|
|
|
|
strongThis->AddZoneWindow(monitor, deviceId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
};
|
|
|
|
|
|
|
|
EnumDisplayMonitors(nullptr, nullptr, callback, reinterpret_cast<LPARAM>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::MoveWindowsOnDisplayChange() noexcept
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
auto callback = [](HWND window, LPARAM data) -> BOOL {
|
2019-09-05 00:26:26 +08:00
|
|
|
int i = static_cast<int>(reinterpret_cast<UINT_PTR>(::GetProp(window, ZONE_STAMP)));
|
|
|
|
if (i != 0)
|
|
|
|
{
|
|
|
|
// i is off by 1 since 0 is special.
|
|
|
|
auto strongThis = reinterpret_cast<FancyZones*>(data);
|
2020-03-10 16:59:34 +08:00
|
|
|
strongThis->MoveWindowIntoZoneByIndex(window, nullptr, i - 1);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
};
|
|
|
|
EnumWindows(callback, reinterpret_cast<LPARAM>(this));
|
|
|
|
}
|
|
|
|
|
2020-03-24 22:17:25 +08:00
|
|
|
void FancyZones::UpdateDragState(HWND window, require_write_lock) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
const bool shift = GetAsyncKeyState(VK_SHIFT) & 0x8000;
|
2019-12-06 19:32:43 +08:00
|
|
|
const bool mouseL = GetAsyncKeyState(VK_LBUTTON) & 0x8000;
|
|
|
|
const bool mouseR = GetAsyncKeyState(VK_RBUTTON) & 0x8000;
|
|
|
|
const bool mouseM = GetAsyncKeyState(VK_MBUTTON) & 0x8000;
|
|
|
|
const bool mouseX1 = GetAsyncKeyState(VK_XBUTTON1) & 0x8000;
|
|
|
|
const bool mouseX2 = GetAsyncKeyState(VK_XBUTTON2) & 0x8000;
|
|
|
|
|
|
|
|
// Note, Middle, X1 and X2 can also be used in addition to R/L
|
2020-02-10 21:59:51 +08:00
|
|
|
bool mouse = mouseM | mouseX1 | mouseX2;
|
2019-12-06 19:32:43 +08:00
|
|
|
// If the user has swapped their Right and Left Buttons, use the "Right" equivalent
|
|
|
|
if (GetSystemMetrics(SM_SWAPBUTTON))
|
|
|
|
{
|
|
|
|
mouse |= mouseL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mouse |= mouseR;
|
|
|
|
}
|
|
|
|
|
2020-03-13 17:55:15 +08:00
|
|
|
if (m_settings->GetSettings()->shiftDrag)
|
2019-12-06 19:32:43 +08:00
|
|
|
{
|
|
|
|
m_dragEnabled = (shift | mouse);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_dragEnabled = !(shift | mouse);
|
|
|
|
}
|
2020-03-24 22:17:25 +08:00
|
|
|
|
|
|
|
static bool warning_shown = false;
|
2020-04-13 23:22:37 +08:00
|
|
|
if (!is_process_elevated() && IsProcessOfWindowElevated(window))
|
2020-03-24 22:17:25 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::CycleActiveZoneSet(DWORD vkCode) noexcept
|
|
|
|
{
|
2020-02-03 16:32:38 +08:00
|
|
|
auto window = GetForegroundWindow();
|
|
|
|
if (IsInterestingWindow(window))
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
const HMONITOR monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
if (monitor)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
auto iter = m_zoneWindowMap.find(monitor);
|
|
|
|
if (iter != m_zoneWindowMap.end())
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
const auto& zoneWindowPtr = iter->second;
|
|
|
|
zoneWindowPtr->CycleActiveZoneSet(vkCode);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-06 20:12:59 +08:00
|
|
|
bool FancyZones::OnSnapHotkey(DWORD vkCode) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-03 16:32:38 +08:00
|
|
|
auto window = GetForegroundWindow();
|
|
|
|
if (IsInterestingWindow(window))
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-25 01:50:26 +08:00
|
|
|
const HMONITOR current = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
if (current)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-25 01:50:26 +08:00
|
|
|
std::vector<HMONITOR> monitorInfo = GetMonitorsSorted();
|
|
|
|
if (monitorInfo.size() > 1)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-03-25 01:50:26 +08:00
|
|
|
// Multi monitor environment.
|
|
|
|
auto currMonitorInfo = std::find(std::begin(monitorInfo), std::end(monitorInfo), current);
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (MoveWindowIntoZoneByDirection(*currMonitorInfo, window, vkCode, false /* cycle through zones */))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// We iterated through all zones in current monitor zone layout, move on to next one (or previous depending on direction).
|
|
|
|
if (vkCode == VK_RIGHT)
|
|
|
|
{
|
|
|
|
currMonitorInfo = std::next(currMonitorInfo);
|
|
|
|
if (currMonitorInfo == std::end(monitorInfo))
|
|
|
|
{
|
|
|
|
currMonitorInfo = std::begin(monitorInfo);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (vkCode == VK_LEFT)
|
|
|
|
{
|
|
|
|
if (currMonitorInfo == std::begin(monitorInfo))
|
|
|
|
{
|
|
|
|
currMonitorInfo = std::end(monitorInfo);
|
|
|
|
}
|
|
|
|
currMonitorInfo = std::prev(currMonitorInfo);
|
|
|
|
}
|
|
|
|
} while (*currMonitorInfo != current);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Single monitor environment.
|
|
|
|
return MoveWindowIntoZoneByDirection(current, window, vkCode, true /* cycle through zones */);
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-06 20:12:59 +08:00
|
|
|
return false;
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::MoveSizeStartInternal(HWND window, HMONITOR monitor, POINT const& ptScreen, require_write_lock writeLock) noexcept
|
|
|
|
{
|
2020-04-08 21:12:04 +08:00
|
|
|
if (IsCursorTypeIndicatingSizeEvent())
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2019-11-08 02:56:32 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_inMoveSize = true;
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-11-08 02:56:32 +08:00
|
|
|
auto iter = m_zoneWindowMap.find(monitor);
|
|
|
|
if (iter == end(m_zoneWindowMap))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
|
2019-11-08 02:56:32 +08:00
|
|
|
m_windowMoveSize = window;
|
|
|
|
|
|
|
|
// This updates m_dragEnabled depending on if the shift key is being held down.
|
2020-03-24 22:17:25 +08:00
|
|
|
UpdateDragState(window, writeLock);
|
2019-11-08 02:56:32 +08:00
|
|
|
|
|
|
|
if (m_dragEnabled)
|
|
|
|
{
|
|
|
|
m_zoneWindowMoveSize = iter->second;
|
|
|
|
m_zoneWindowMoveSize->MoveSizeEnter(window, m_dragEnabled);
|
2020-03-14 02:40:28 +08:00
|
|
|
if (m_settings->GetSettings()->showZonesOnAllMonitors)
|
2020-03-13 22:56:23 +08:00
|
|
|
{
|
|
|
|
for (auto [keyMonitor, zoneWindow] : m_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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-08 02:56:32 +08:00
|
|
|
}
|
|
|
|
else if (m_zoneWindowMoveSize)
|
|
|
|
{
|
2020-03-25 22:38:44 +08:00
|
|
|
m_zoneWindowMoveSize->RestoreOrginalTransparency();
|
2019-11-08 02:56:32 +08:00
|
|
|
m_zoneWindowMoveSize = nullptr;
|
2020-03-13 22:56:23 +08:00
|
|
|
for (auto [keyMonitor, zoneWindow] : m_zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::MoveSizeEndInternal(HWND window, POINT const& ptScreen, require_write_lock) noexcept
|
|
|
|
{
|
|
|
|
m_inMoveSize = false;
|
|
|
|
m_dragEnabled = false;
|
|
|
|
m_windowMoveSize = nullptr;
|
|
|
|
if (m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
auto zoneWindow = std::move(m_zoneWindowMoveSize);
|
|
|
|
zoneWindow->MoveSizeEnd(window, ptScreen);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
::RemoveProp(window, ZONE_STAMP);
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
auto monitor = MonitorFromWindow(window, MONITOR_DEFAULTTONULL);
|
|
|
|
if (monitor)
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
auto zoneWindow = m_zoneWindowMap.find(monitor);
|
|
|
|
if (zoneWindow != m_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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
2020-03-13 22:56:23 +08:00
|
|
|
|
|
|
|
// Also, hide all windows (regardless of settings)
|
|
|
|
for (auto [keyMonitor, zoneWindow] : m_zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::MoveSizeUpdateInternal(HMONITOR monitor, POINT const& ptScreen, require_write_lock writeLock) noexcept
|
|
|
|
{
|
|
|
|
if (m_inMoveSize)
|
|
|
|
{
|
|
|
|
// This updates m_dragEnabled depending on if the shift key is being held down.
|
2020-03-24 22:17:25 +08:00
|
|
|
UpdateDragState(m_windowMoveSize, writeLock);
|
2019-09-05 00:26:26 +08:00
|
|
|
|
|
|
|
if (m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
// Update the ZoneWindow already handling move/size
|
|
|
|
if (!m_dragEnabled)
|
|
|
|
{
|
2020-03-13 22:56:23 +08:00
|
|
|
// Drag got disabled, tell it to cancel and hide all windows
|
|
|
|
m_zoneWindowMoveSize = nullptr;
|
2020-03-25 22:38:44 +08:00
|
|
|
|
2020-03-13 22:56:23 +08:00
|
|
|
for (auto [keyMonitor, zoneWindow] : m_zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (zoneWindow)
|
|
|
|
{
|
2020-03-25 22:38:44 +08:00
|
|
|
zoneWindow->RestoreOrginalTransparency();
|
2020-03-13 22:56:23 +08:00
|
|
|
zoneWindow->HideZoneWindow();
|
|
|
|
}
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto iter = m_zoneWindowMap.find(monitor);
|
|
|
|
if (iter != m_zoneWindowMap.end())
|
|
|
|
{
|
|
|
|
if (iter->second != m_zoneWindowMoveSize)
|
|
|
|
{
|
|
|
|
// The drag has moved to a different monitor.
|
2020-03-25 22:38:44 +08:00
|
|
|
m_zoneWindowMoveSize->RestoreOrginalTransparency();
|
|
|
|
|
2020-03-14 02:40:28 +08:00
|
|
|
if (!m_settings->GetSettings()->showZonesOnAllMonitors)
|
2020-03-13 22:56:23 +08:00
|
|
|
{
|
|
|
|
m_zoneWindowMoveSize->HideZoneWindow();
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
m_zoneWindowMoveSize = iter->second;
|
2020-03-27 23:09:37 +08:00
|
|
|
m_zoneWindowMoveSize->MoveSizeEnter(m_windowMoveSize, m_zoneWindowMoveSize->IsDragEnabled());
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
2020-03-27 00:48:01 +08:00
|
|
|
|
|
|
|
for (auto [keyMonitor, zoneWindow] : m_zoneWindowMap)
|
|
|
|
{
|
|
|
|
zoneWindow->MoveSizeUpdate(ptScreen, m_dragEnabled);
|
|
|
|
}
|
2019-09-05 00:26:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
MoveSizeStartInternal(m_windowMoveSize, monitor, ptScreen, writeLock);
|
|
|
|
MoveSizeUpdateInternal(monitor, ptScreen, writeLock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-12 17:10:55 +08:00
|
|
|
void FancyZones::HandleVirtualDesktopUpdates(HANDLE fancyZonesDestroyedEvent) noexcept
|
|
|
|
{
|
|
|
|
HANDLE regKeyEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
|
|
|
|
HANDLE events[2] = { regKeyEvent, fancyZonesDestroyedEvent };
|
2020-02-10 21:59:51 +08:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (RegNotifyChangeKeyValue(HKEY_CURRENT_USER, TRUE, REG_NOTIFY_CHANGE_LAST_SET, regKeyEvent, TRUE) != ERROR_SUCCESS)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
if (WaitForMultipleObjects(2, events, FALSE, INFINITE) != (WAIT_OBJECT_0 + 0))
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
// if fancyZonesDestroyedEvent is signalized or WaitForMultipleObjects failed, terminate thread execution
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
DWORD bufferCapacity;
|
|
|
|
const WCHAR* key = L"VirtualDesktopIDs";
|
|
|
|
// request regkey binary buffer capacity only
|
2020-02-10 21:59:51 +08:00
|
|
|
if (RegQueryValueExW(m_virtualDesktopsRegKey, key, 0, nullptr, nullptr, &bufferCapacity) != ERROR_SUCCESS)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::unique_ptr<BYTE[]> buffer = std::make_unique<BYTE[]>(bufferCapacity);
|
|
|
|
// request regkey binary content
|
2020-02-10 21:59:51 +08:00
|
|
|
if (RegQueryValueExW(m_virtualDesktopsRegKey, key, 0, nullptr, buffer.get(), &bufferCapacity) != ERROR_SUCCESS)
|
|
|
|
{
|
2019-12-12 17:10:55 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-02-18 18:55:08 +08:00
|
|
|
const size_t guidSize = sizeof(GUID);
|
|
|
|
std::unordered_set<GUID> temp;
|
2019-12-12 17:10:55 +08:00
|
|
|
temp.reserve(bufferCapacity / guidSize);
|
2020-02-10 21:59:51 +08:00
|
|
|
for (size_t i = 0; i < bufferCapacity; i += guidSize)
|
|
|
|
{
|
|
|
|
GUID* guid = reinterpret_cast<GUID*>(buffer.get() + i);
|
2020-02-18 18:55:08 +08:00
|
|
|
temp.insert(*guid);
|
2019-12-12 17:10:55 +08:00
|
|
|
}
|
2020-02-18 18:55:08 +08:00
|
|
|
RegisterVirtualDesktopUpdates(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::RegisterVirtualDesktopUpdates(std::unordered_set<GUID>& currentVirtualDesktopIds) noexcept
|
|
|
|
{
|
|
|
|
std::unique_lock writeLock(m_lock);
|
|
|
|
bool modified{ false };
|
|
|
|
for (auto it = begin(m_processedWorkAreas); it != end(m_processedWorkAreas);)
|
|
|
|
{
|
|
|
|
auto iter = currentVirtualDesktopIds.find(it->first);
|
|
|
|
if (iter == currentVirtualDesktopIds.end())
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-02-18 18:55:08 +08:00
|
|
|
// if we couldn't find the GUID in currentVirtualDesktopIds, we must remove it from both m_processedWorkAreas and deviceInfoMap
|
|
|
|
wil::unique_cotaskmem_string virtualDesktopId;
|
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(it->first, &virtualDesktopId)))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-02-18 18:55:08 +08:00
|
|
|
modified |= JSONHelpers::FancyZonesDataInstance().RemoveDevicesByVirtualDesktopId(virtualDesktopId.get());
|
2019-12-12 17:10:55 +08:00
|
|
|
}
|
2020-02-18 18:55:08 +08:00
|
|
|
it = m_processedWorkAreas.erase(it);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentVirtualDesktopIds.erase(it->first); // virtual desktop already in map, skip it
|
|
|
|
++it;
|
2019-12-12 17:10:55 +08:00
|
|
|
}
|
|
|
|
}
|
2020-02-18 18:55:08 +08:00
|
|
|
if (modified)
|
|
|
|
{
|
|
|
|
JSONHelpers::FancyZonesDataInstance().SaveFancyZonesData();
|
|
|
|
}
|
|
|
|
// register new virtual desktops, if any
|
|
|
|
for (const auto& id : currentVirtualDesktopIds)
|
|
|
|
{
|
|
|
|
m_processedWorkAreas[id] = std::vector<HMONITOR>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FancyZones::RegisterNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept
|
|
|
|
{
|
|
|
|
if (!m_processedWorkAreas.contains(virtualDesktopId))
|
|
|
|
{
|
|
|
|
m_processedWorkAreas[virtualDesktopId] = { monitor };
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_processedWorkAreas[virtualDesktopId].push_back(monitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FancyZones::IsNewWorkArea(GUID virtualDesktopId, HMONITOR monitor) noexcept
|
|
|
|
{
|
|
|
|
auto it = m_processedWorkAreas.find(virtualDesktopId);
|
|
|
|
if (it != m_processedWorkAreas.end())
|
|
|
|
{
|
|
|
|
// virtual desktop exists, check if it's processed on given monitor
|
|
|
|
return std::find(it->second.begin(), it->second.end(), monitor) == it->second.end();
|
|
|
|
}
|
|
|
|
return true;
|
2019-12-12 17:10:55 +08:00
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
void FancyZones::OnEditorExitEvent() noexcept
|
|
|
|
{
|
|
|
|
// Colect information about changes in zone layout after editor exited.
|
|
|
|
JSONHelpers::FancyZonesDataInstance().ParseDeviceInfoFromTmpFile(ZoneWindowUtils::GetActiveZoneSetTmpPath());
|
|
|
|
JSONHelpers::FancyZonesDataInstance().ParseDeletedCustomZoneSetsFromTmpFile(ZoneWindowUtils::GetCustomZoneSetsTmpPath());
|
|
|
|
JSONHelpers::FancyZonesDataInstance().ParseCustomZoneSetFromTmpFile(ZoneWindowUtils::GetAppliedZoneSetTmpPath());
|
|
|
|
JSONHelpers::FancyZonesDataInstance().SaveFancyZonesData();
|
|
|
|
}
|
|
|
|
|
2020-03-25 01:50:26 +08:00
|
|
|
std::vector<HMONITOR> FancyZones::GetMonitorsSorted() noexcept
|
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
|
|
|
|
auto monitorInfo = GetRawMonitorData();
|
|
|
|
OrderMonitors(monitorInfo);
|
|
|
|
std::vector<HMONITOR> output;
|
|
|
|
std::transform(std::begin(monitorInfo), std::end(monitorInfo), std::back_inserter(output), [](const auto& info) { return info.first; });
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::pair<HMONITOR, RECT>> FancyZones::GetRawMonitorData() noexcept
|
|
|
|
{
|
|
|
|
std::shared_lock readLock(m_lock);
|
|
|
|
|
|
|
|
std::vector<std::pair<HMONITOR, RECT>> monitorInfo;
|
|
|
|
for (const auto& [monitor, window] : m_zoneWindowMap)
|
|
|
|
{
|
|
|
|
if (window->ActiveZoneSet() != nullptr)
|
|
|
|
{
|
|
|
|
MONITORINFOEX mi;
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
GetMonitorInfo(monitor, &mi);
|
|
|
|
monitorInfo.push_back({ monitor, mi.rcMonitor });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return monitorInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FancyZones::MoveWindowIntoZoneByDirection(HMONITOR monitor, HWND window, DWORD vkCode, bool cycle)
|
|
|
|
{
|
|
|
|
auto iter = m_zoneWindowMap.find(monitor);
|
|
|
|
if (iter != std::end(m_zoneWindowMap))
|
|
|
|
{
|
|
|
|
const auto& zoneWindowPtr = iter->second;
|
|
|
|
return zoneWindowPtr->MoveWindowIntoZoneByDirection(window, vkCode, cycle);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
winrt::com_ptr<IFancyZones> MakeFancyZones(HINSTANCE hinstance, const winrt::com_ptr<IFancyZonesSettings>& settings) noexcept
|
2019-09-05 00:26:26 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
if (!settings)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-09-05 00:26:26 +08:00
|
|
|
return winrt::make_self<FancyZones>(hinstance, settings);
|
2020-03-24 22:17:25 +08:00
|
|
|
}
|