2020-02-10 21:59:51 +08:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
|
|
#include <common/common.h>
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
#include "FancyZonesData.h"
|
|
|
|
#include "FancyZonesDataTypes.h"
|
2020-02-10 21:59:51 +08:00
|
|
|
#include "ZoneWindow.h"
|
2020-08-26 00:55:29 +08:00
|
|
|
#include "ZoneWindowDrawing.h"
|
2020-02-10 21:59:51 +08:00
|
|
|
#include "trace.h"
|
|
|
|
#include "util.h"
|
2020-08-07 16:06:25 +08:00
|
|
|
#include "Settings.h"
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
#include <ShellScalingApi.h>
|
|
|
|
#include <mutex>
|
2020-07-06 23:40:25 +08:00
|
|
|
#include <fileapi.h>
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-25 22:38:44 +08:00
|
|
|
#include <gdiplus.h>
|
|
|
|
|
2020-08-11 19:51:06 +08:00
|
|
|
// Non-Localizable strings
|
|
|
|
namespace NonLocalizable
|
|
|
|
{
|
|
|
|
const wchar_t ToolWindowClassName[] = L"SuperFancyZones_ZoneWindow";
|
|
|
|
}
|
|
|
|
|
2020-08-25 01:38:15 +08:00
|
|
|
using namespace FancyZonesUtils;
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
namespace ZoneWindowUtils
|
|
|
|
{
|
|
|
|
std::wstring GenerateUniqueId(HMONITOR monitor, PCWSTR deviceId, PCWSTR virtualDesktopId)
|
|
|
|
{
|
|
|
|
wchar_t uniqueId[256]{}; // Parsed deviceId + resolution + virtualDesktopId
|
|
|
|
|
|
|
|
MONITORINFOEXW mi;
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
if (virtualDesktopId && GetMonitorInfo(monitor, &mi))
|
|
|
|
{
|
|
|
|
wchar_t parsedId[256]{};
|
|
|
|
ParseDeviceId(deviceId, parsedId, 256);
|
|
|
|
|
|
|
|
Rect const monitorRect(mi.rcMonitor);
|
|
|
|
StringCchPrintf(uniqueId, ARRAYSIZE(uniqueId), L"%s_%d_%d_%s", parsedId, monitorRect.width(), monitorRect.height(), virtualDesktopId);
|
|
|
|
}
|
|
|
|
return std::wstring{ uniqueId };
|
|
|
|
}
|
2020-08-07 16:06:25 +08:00
|
|
|
|
|
|
|
std::wstring GenerateUniqueIdAllMonitorsArea(PCWSTR virtualDesktopId)
|
|
|
|
{
|
2020-08-11 19:51:06 +08:00
|
|
|
std::wstring result{ ZonedWindowProperties::MultiMonitorDeviceID };
|
2020-08-07 16:06:25 +08:00
|
|
|
|
|
|
|
RECT combinedResolution = GetAllMonitorsCombinedRect<&MONITORINFO::rcMonitor>();
|
|
|
|
|
|
|
|
result += L'_';
|
|
|
|
result += std::to_wstring(combinedResolution.right - combinedResolution.left);
|
|
|
|
result += L'_';
|
|
|
|
result += std::to_wstring(combinedResolution.bottom - combinedResolution.top);
|
|
|
|
result += L'_';
|
|
|
|
result += virtualDesktopId;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ZoneWindow : public winrt::implements<ZoneWindow, IZoneWindow>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ZoneWindow(HINSTANCE hinstance);
|
2020-03-25 22:38:44 +08:00
|
|
|
~ZoneWindow();
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
bool Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId, bool flashZones);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
IFACEMETHODIMP MoveSizeEnter(HWND window) noexcept;
|
2020-07-10 17:06:01 +08:00
|
|
|
IFACEMETHODIMP MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool selectManyZones) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
|
2020-03-25 22:38:44 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 20:39:34 +08:00
|
|
|
MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept;
|
2020-04-10 22:09:08 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 20:39:34 +08:00
|
|
|
MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept;
|
2020-03-25 01:50:26 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-21 18:53:03 +08:00
|
|
|
MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle) noexcept;
|
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle) noexcept;
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
CycleActiveZoneSet(DWORD vkCode) noexcept;
|
|
|
|
IFACEMETHODIMP_(std::wstring)
|
2020-04-28 16:57:09 +08:00
|
|
|
UniqueId() noexcept { return { m_uniqueId }; }
|
2020-02-17 23:28:49 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
SaveWindowProcessToZoneIndex(HWND window) noexcept;
|
|
|
|
IFACEMETHODIMP_(IZoneSet*)
|
2020-04-28 16:57:09 +08:00
|
|
|
ActiveZoneSet() noexcept { return m_activeZoneSet.get(); }
|
2020-03-13 22:56:23 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ShowZoneWindow() noexcept;
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
HideZoneWindow() noexcept;
|
2020-05-06 23:16:16 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
UpdateActiveZoneSet() noexcept;
|
2020-07-10 17:06:01 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ClearSelectedZones() noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
static LRESULT CALLBACK s_WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept;
|
|
|
|
|
|
|
|
private:
|
2020-05-31 18:36:45 +08:00
|
|
|
void InitializeZoneSets(const std::wstring& parentUniqueId) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
void CalculateZoneSet() noexcept;
|
|
|
|
void UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept;
|
|
|
|
LRESULT WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept;
|
|
|
|
void OnPaint(wil::unique_hdc& hdc) noexcept;
|
|
|
|
void OnKeyUp(WPARAM wparam) noexcept;
|
2020-08-24 20:39:34 +08:00
|
|
|
std::vector<size_t> ZonesFromPoint(POINT pt) noexcept;
|
2020-02-10 21:59:51 +08:00
|
|
|
void CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept;
|
|
|
|
void FlashZones() noexcept;
|
|
|
|
|
|
|
|
winrt::com_ptr<IZoneWindowHost> m_host;
|
|
|
|
HMONITOR m_monitor{};
|
|
|
|
std::wstring m_uniqueId; // Parsed deviceId + resolution + virtualDesktopId
|
2020-03-10 02:22:53 +08:00
|
|
|
wil::unique_hwnd m_window{}; // Hidden tool window used to represent current monitor desktop work area.
|
2020-02-10 21:59:51 +08:00
|
|
|
HWND m_windowMoveSize{};
|
|
|
|
bool m_drawHints{};
|
|
|
|
bool m_flashMode{};
|
|
|
|
winrt::com_ptr<IZoneSet> m_activeZoneSet;
|
|
|
|
std::vector<winrt::com_ptr<IZoneSet>> m_zoneSets;
|
2020-08-24 20:39:34 +08:00
|
|
|
std::vector<size_t> m_initialHighlightZone;
|
|
|
|
std::vector<size_t> m_highlightZone;
|
2020-02-10 21:59:51 +08:00
|
|
|
WPARAM m_keyLast{};
|
|
|
|
size_t m_keyCycle{};
|
|
|
|
static const UINT m_showAnimationDuration = 200; // ms
|
|
|
|
static const UINT m_flashDuration = 700; // ms
|
2020-07-23 00:00:03 +08:00
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
ULONG_PTR gdiplusToken;
|
2020-02-10 21:59:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
ZoneWindow::ZoneWindow(HINSTANCE hinstance)
|
|
|
|
{
|
|
|
|
WNDCLASSEXW wcex{};
|
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wcex.lpfnWndProc = s_WndProc;
|
|
|
|
wcex.hInstance = hinstance;
|
2020-08-11 19:51:06 +08:00
|
|
|
wcex.lpszClassName = NonLocalizable::ToolWindowClassName;
|
2020-02-10 21:59:51 +08:00
|
|
|
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
|
|
|
RegisterClassExW(&wcex);
|
2020-03-25 22:38:44 +08:00
|
|
|
|
|
|
|
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
|
2020-04-28 16:57:09 +08:00
|
|
|
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
|
2020-03-25 22:38:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ZoneWindow::~ZoneWindow()
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
Gdiplus::GdiplusShutdown(gdiplusToken);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
bool ZoneWindow::Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId, bool flashZones)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
m_host.copy_from(host);
|
|
|
|
|
2020-08-07 16:06:25 +08:00
|
|
|
Rect workAreaRect;
|
|
|
|
m_monitor = monitor;
|
|
|
|
if (monitor)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-08-07 16:06:25 +08:00
|
|
|
MONITORINFO mi{};
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
if (!GetMonitorInfoW(monitor, &mi))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const UINT dpi = GetDpiForMonitor(m_monitor);
|
|
|
|
workAreaRect = Rect(mi.rcWork, dpi);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
workAreaRect = GetAllMonitorsCombinedRect<&MONITORINFO::rcWork>();
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
m_uniqueId = uniqueId;
|
2020-05-31 18:36:45 +08:00
|
|
|
InitializeZoneSets(parentUniqueId);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
m_window = wil::unique_hwnd{
|
2020-08-11 19:51:06 +08:00
|
|
|
CreateWindowExW(WS_EX_TOOLWINDOW, NonLocalizable::ToolWindowClassName, L"", WS_POPUP, workAreaRect.left(), workAreaRect.top(), workAreaRect.width(), workAreaRect.height(), nullptr, nullptr, hinstance, this)
|
2020-02-10 21:59:51 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!m_window)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MakeWindowTransparent(m_window.get());
|
2020-08-07 16:06:25 +08:00
|
|
|
|
|
|
|
// Ignore flashZones
|
|
|
|
/*
|
2020-02-10 21:59:51 +08:00
|
|
|
if (flashZones)
|
|
|
|
{
|
|
|
|
// Don't flash if the foreground window is in full screen mode
|
|
|
|
RECT windowRect;
|
|
|
|
if (!(GetWindowRect(GetForegroundWindow(), &windowRect) &&
|
|
|
|
windowRect.left == mi.rcMonitor.left &&
|
|
|
|
windowRect.top == mi.rcMonitor.top &&
|
|
|
|
windowRect.right == mi.rcMonitor.right &&
|
|
|
|
windowRect.bottom == mi.rcMonitor.bottom))
|
|
|
|
{
|
|
|
|
FlashZones();
|
|
|
|
}
|
|
|
|
}
|
2020-08-07 16:06:25 +08:00
|
|
|
*/
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-06-05 22:53:08 +08:00
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeEnter(HWND window) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
m_windowMoveSize = window;
|
|
|
|
m_drawHints = true;
|
|
|
|
m_highlightZone = {};
|
2020-07-10 17:06:01 +08:00
|
|
|
m_initialHighlightZone = {};
|
2020-02-10 21:59:51 +08:00
|
|
|
ShowZoneWindow();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:06:01 +08:00
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool selectManyZones) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
bool redraw = false;
|
|
|
|
POINT ptClient = ptScreen;
|
2020-04-28 16:57:09 +08:00
|
|
|
MapWindowPoints(nullptr, m_window.get(), &ptClient, 1);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
if (dragEnabled)
|
|
|
|
{
|
2020-04-10 22:09:08 +08:00
|
|
|
auto highlightZone = ZonesFromPoint(ptClient);
|
2020-07-10 17:06:01 +08:00
|
|
|
|
|
|
|
if (selectManyZones)
|
|
|
|
{
|
|
|
|
if (m_initialHighlightZone.empty())
|
|
|
|
{
|
|
|
|
// first time
|
|
|
|
m_initialHighlightZone = highlightZone;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-24 20:39:34 +08:00
|
|
|
std::vector<size_t> newHighlightZone;
|
2020-07-10 17:06:01 +08:00
|
|
|
std::set_union(begin(highlightZone), end(highlightZone), begin(m_initialHighlightZone), end(m_initialHighlightZone), std::back_inserter(newHighlightZone));
|
|
|
|
|
|
|
|
RECT boundingRect;
|
|
|
|
bool boundingRectEmpty = true;
|
|
|
|
auto zones = m_activeZoneSet->GetZones();
|
|
|
|
|
2020-08-24 20:39:34 +08:00
|
|
|
for (size_t zoneId : newHighlightZone)
|
2020-07-10 17:06:01 +08:00
|
|
|
{
|
|
|
|
RECT rect = zones[zoneId]->GetZoneRect();
|
|
|
|
if (boundingRectEmpty)
|
|
|
|
{
|
|
|
|
boundingRect = rect;
|
|
|
|
boundingRectEmpty = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
boundingRect.left = min(boundingRect.left, rect.left);
|
|
|
|
boundingRect.top = min(boundingRect.top, rect.top);
|
|
|
|
boundingRect.right = max(boundingRect.right, rect.right);
|
|
|
|
boundingRect.bottom = max(boundingRect.bottom, rect.bottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
highlightZone.clear();
|
|
|
|
|
|
|
|
if (!boundingRectEmpty)
|
|
|
|
{
|
|
|
|
for (size_t zoneId = 0; zoneId < zones.size(); zoneId++)
|
|
|
|
{
|
|
|
|
RECT rect = zones[zoneId]->GetZoneRect();
|
|
|
|
if (boundingRect.left <= rect.left && rect.right <= boundingRect.right &&
|
|
|
|
boundingRect.top <= rect.top && rect.bottom <= boundingRect.bottom)
|
|
|
|
{
|
2020-08-24 20:39:34 +08:00
|
|
|
highlightZone.push_back(zoneId);
|
2020-07-10 17:06:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_initialHighlightZone = {};
|
|
|
|
}
|
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
redraw = (highlightZone != m_highlightZone);
|
|
|
|
m_highlightZone = std::move(highlightZone);
|
|
|
|
}
|
2020-04-10 22:09:08 +08:00
|
|
|
else if (m_highlightZone.size())
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-10 22:09:08 +08:00
|
|
|
m_highlightZone = {};
|
2020-02-10 21:59:51 +08:00
|
|
|
redraw = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redraw)
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
InvalidateRect(m_window.get(), nullptr, true);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_windowMoveSize != window)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
POINT ptClient = ptScreen;
|
2020-04-28 16:57:09 +08:00
|
|
|
MapWindowPoints(nullptr, m_window.get(), &ptClient, 1);
|
2020-07-10 17:06:01 +08:00
|
|
|
m_activeZoneSet->MoveWindowIntoZoneByIndexSet(window, m_window.get(), m_highlightZone);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-08-28 21:00:21 +08:00
|
|
|
auto windowInfo = FancyZonesUtils::GetFancyZonesWindowInfo(window);
|
|
|
|
if (windowInfo.noVisibleOwner)
|
|
|
|
{
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-04-28 16:57:09 +08:00
|
|
|
Trace::ZoneWindow::MoveSizeEnd(m_activeZoneSet);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
HideZoneWindow();
|
|
|
|
m_windowMoveSize = nullptr;
|
|
|
|
return S_OK;
|
|
|
|
}
|
2020-03-25 22:38:44 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 20:39:34 +08:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept
|
2020-04-10 22:09:08 +08:00
|
|
|
{
|
|
|
|
MoveWindowIntoZoneByIndexSet(window, { index });
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 20:39:34 +08:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-06-01 22:14:29 +08:00
|
|
|
m_activeZoneSet->MoveWindowIntoZoneByIndexSet(window, m_window.get(), indexSet);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 01:50:26 +08:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-21 18:53:03 +08:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle) noexcept
|
|
|
|
{
|
|
|
|
if (m_activeZoneSet)
|
|
|
|
{
|
|
|
|
if (m_activeZoneSet->MoveWindowIntoZoneByDirectionAndIndex(window, m_window.get(), vkCode, cycle))
|
|
|
|
{
|
2020-08-28 21:00:21 +08:00
|
|
|
auto windowInfo = FancyZonesUtils::GetFancyZonesWindowInfo(window);
|
|
|
|
if (windowInfo.noVisibleOwner)
|
|
|
|
{
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
}
|
2020-08-21 18:53:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
ZoneWindow::MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-08-21 18:53:03 +08:00
|
|
|
if (m_activeZoneSet->MoveWindowIntoZoneByDirectionAndPosition(window, m_window.get(), vkCode, cycle))
|
2020-03-25 01:50:26 +08:00
|
|
|
{
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-03-25 01:50:26 +08:00
|
|
|
return false;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::CycleActiveZoneSet(DWORD wparam) noexcept
|
|
|
|
{
|
|
|
|
CycleActiveZoneSetInternal(wparam, Trace::ZoneWindow::InputMode::Keyboard);
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_windowMoveSize)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
InvalidateRect(m_window.get(), nullptr, true);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FlashZones();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::SaveWindowProcessToZoneIndex(HWND window) noexcept
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2019-11-19 07:29:42 +08:00
|
|
|
{
|
2020-05-26 22:01:12 +08:00
|
|
|
auto zoneIndexSet = m_activeZoneSet->GetZoneIndexSetFromWindow(window);
|
|
|
|
if (zoneIndexSet.size())
|
2019-11-19 07:29:42 +08:00
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
OLECHAR* guidString;
|
2020-04-28 16:57:09 +08:00
|
|
|
if (StringFromCLSID(m_activeZoneSet->Id(), &guidString) == S_OK)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataInstance().SetAppLastZones(window, m_uniqueId, guidString, zoneIndexSet);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CoTaskMemFree(guidString);
|
2019-11-19 07:29:42 +08:00
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2019-12-06 22:09:27 +08:00
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-03-13 22:56:23 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::ShowZoneWindow() noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-05-01 22:14:35 +08:00
|
|
|
auto window = m_window.get();
|
|
|
|
if (!window)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-05-01 22:14:35 +08:00
|
|
|
return;
|
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-05-01 22:14:35 +08:00
|
|
|
m_flashMode = false;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-05-01 22:14:35 +08:00
|
|
|
UINT flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-05-01 22:14:35 +08:00
|
|
|
HWND windowInsertAfter = m_windowMoveSize;
|
|
|
|
if (windowInsertAfter == nullptr)
|
|
|
|
{
|
|
|
|
windowInsertAfter = HWND_TOPMOST;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-05-01 22:14:35 +08:00
|
|
|
|
|
|
|
SetWindowPos(window, windowInsertAfter, 0, 0, 0, 0, flags);
|
|
|
|
|
2020-08-21 00:55:41 +08:00
|
|
|
std::thread{ [this, strong_this{ get_strong() }]() {
|
|
|
|
auto window = m_window.get();
|
2020-05-01 22:14:35 +08:00
|
|
|
AnimateWindow(window, m_showAnimationDuration, AW_BLEND);
|
|
|
|
InvalidateRect(window, nullptr, true);
|
2020-05-22 22:42:29 +08:00
|
|
|
if (!m_host->InMoveSize())
|
2020-05-21 00:52:48 +08:00
|
|
|
{
|
|
|
|
HideZoneWindow();
|
|
|
|
}
|
2020-05-01 22:14:35 +08:00
|
|
|
} }.detach();
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-03-13 22:56:23 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::HideZoneWindow() noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_window)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
ShowWindow(m_window.get(), SW_HIDE);
|
2020-02-10 21:59:51 +08:00
|
|
|
m_keyLast = 0;
|
|
|
|
m_windowMoveSize = nullptr;
|
|
|
|
m_drawHints = false;
|
2020-04-10 22:09:08 +08:00
|
|
|
m_highlightZone = {};
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-06 23:16:16 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::UpdateActiveZoneSet() noexcept
|
|
|
|
{
|
|
|
|
CalculateZoneSet();
|
|
|
|
}
|
|
|
|
|
2020-07-10 17:06:01 +08:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
ZoneWindow::ClearSelectedZones() noexcept
|
|
|
|
{
|
|
|
|
if (m_highlightZone.size())
|
|
|
|
{
|
|
|
|
m_highlightZone.clear();
|
|
|
|
InvalidateRect(m_window.get(), nullptr, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 22:56:23 +08:00
|
|
|
#pragma region private
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
void ZoneWindow::InitializeZoneSets(const std::wstring& parentUniqueId) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-05-31 18:36:45 +08:00
|
|
|
// If there is not defined zone layout for this work area, created default entry.
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataInstance().AddDevice(m_uniqueId);
|
2020-05-31 18:36:45 +08:00
|
|
|
if (!parentUniqueId.empty())
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataInstance().CloneDeviceInfo(parentUniqueId, m_uniqueId);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
CalculateZoneSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::CalculateZoneSet() noexcept
|
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
const auto& fancyZonesData = FancyZonesDataInstance();
|
2020-04-28 16:57:09 +08:00
|
|
|
const auto deviceInfoData = fancyZonesData.FindDeviceInfo(m_uniqueId);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-02-17 23:28:49 +08:00
|
|
|
if (!deviceInfoData.has_value())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& activeZoneSet = deviceInfoData->activeZoneSet;
|
|
|
|
|
2020-07-22 16:39:13 +08:00
|
|
|
if (activeZoneSet.uuid.empty() || activeZoneSet.type == FancyZonesDataTypes::ZoneSetLayoutType::Blank)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
GUID zoneSetId;
|
|
|
|
if (SUCCEEDED_LOG(CLSIDFromString(activeZoneSet.uuid.c_str(), &zoneSetId)))
|
|
|
|
{
|
|
|
|
auto zoneSet = MakeZoneSet(ZoneSetConfig(
|
|
|
|
zoneSetId,
|
|
|
|
activeZoneSet.type,
|
2020-07-24 17:17:39 +08:00
|
|
|
m_monitor));
|
2020-08-07 16:06:25 +08:00
|
|
|
|
|
|
|
RECT workArea;
|
|
|
|
if (m_monitor)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-08-07 16:06:25 +08:00
|
|
|
MONITORINFO monitorInfo{};
|
|
|
|
monitorInfo.cbSize = sizeof(monitorInfo);
|
|
|
|
if (GetMonitorInfoW(m_monitor, &monitorInfo))
|
|
|
|
{
|
|
|
|
workArea = monitorInfo.rcWork;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
workArea = GetAllMonitorsCombinedRect<&MONITORINFO::rcWork>();
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-08-07 16:06:25 +08:00
|
|
|
|
|
|
|
bool showSpacing = deviceInfoData->showSpacing;
|
|
|
|
int spacing = showSpacing ? deviceInfoData->spacing : 0;
|
|
|
|
int zoneCount = deviceInfoData->zoneCount;
|
|
|
|
zoneSet->CalculateZones(workArea, zoneCount, spacing);
|
|
|
|
UpdateActiveZoneSet(zoneSet.get());
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
m_activeZoneSet.copy_from(zoneSet);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
wil::unique_cotaskmem_string zoneSetId;
|
2020-04-28 16:57:09 +08:00
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(m_activeZoneSet->Id(), &zoneSetId)))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataTypes::ZoneSetData data{
|
2020-02-10 21:59:51 +08:00
|
|
|
.uuid = zoneSetId.get(),
|
2020-04-28 16:57:09 +08:00
|
|
|
.type = m_activeZoneSet->LayoutType()
|
2020-02-10 21:59:51 +08:00
|
|
|
};
|
2020-07-22 16:39:13 +08:00
|
|
|
FancyZonesDataInstance().SetActiveZoneSet(m_uniqueId, data);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
|
|
|
|
{
|
|
|
|
switch (message)
|
|
|
|
{
|
2020-03-25 22:38:44 +08:00
|
|
|
case WM_NCDESTROY:
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
::DefWindowProc(m_window.get(), message, wparam, lparam);
|
|
|
|
SetWindowLongPtr(m_window.get(), GWLP_USERDATA, 0);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case WM_ERASEBKGND:
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
case WM_PRINTCLIENT:
|
2020-03-25 22:38:44 +08:00
|
|
|
case WM_PAINT:
|
|
|
|
{
|
2020-02-10 21:59:51 +08:00
|
|
|
PAINTSTRUCT ps;
|
|
|
|
wil::unique_hdc hdc{ reinterpret_cast<HDC>(wparam) };
|
|
|
|
if (!hdc)
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
hdc.reset(BeginPaint(m_window.get(), &ps));
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
OnPaint(hdc);
|
|
|
|
|
|
|
|
if (wparam == 0)
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
EndPaint(m_window.get(), &ps);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
hdc.release();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
return DefWindowProc(m_window.get(), message, wparam, lparam);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::OnPaint(wil::unique_hdc& hdc) noexcept
|
|
|
|
{
|
|
|
|
RECT clientRect;
|
2020-04-28 16:57:09 +08:00
|
|
|
GetClientRect(m_window.get(), &clientRect);
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
wil::unique_hdc hdcMem;
|
|
|
|
HPAINTBUFFER bufferedPaint = BeginBufferedPaint(hdc.get(), &clientRect, BPBF_TOPDOWNDIB, nullptr, &hdcMem);
|
|
|
|
if (bufferedPaint)
|
|
|
|
{
|
2020-08-26 00:55:29 +08:00
|
|
|
ZoneWindowDrawing::DrawBackdrop(hdcMem, clientRect);
|
2020-03-25 22:38:44 +08:00
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet && m_host)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-08-26 00:55:29 +08:00
|
|
|
ZoneWindowDrawing::DrawActiveZoneSet(hdcMem,
|
2020-04-28 16:57:09 +08:00
|
|
|
m_host->GetZoneColor(),
|
|
|
|
m_host->GetZoneBorderColor(),
|
|
|
|
m_host->GetZoneHighlightColor(),
|
|
|
|
m_host->GetZoneHighlightOpacity(),
|
|
|
|
m_activeZoneSet->GetZones(),
|
|
|
|
m_highlightZone,
|
|
|
|
m_flashMode,
|
|
|
|
m_drawHints);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
EndBufferedPaint(bufferedPaint, TRUE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::OnKeyUp(WPARAM wparam) noexcept
|
|
|
|
{
|
|
|
|
bool fRedraw = false;
|
|
|
|
Trace::ZoneWindow::KeyUp(wparam);
|
|
|
|
|
|
|
|
if ((wparam >= '0') && (wparam <= '9'))
|
|
|
|
{
|
|
|
|
CycleActiveZoneSetInternal(static_cast<DWORD>(wparam), Trace::ZoneWindow::InputMode::Keyboard);
|
2020-04-28 16:57:09 +08:00
|
|
|
InvalidateRect(m_window.get(), nullptr, true);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 20:39:34 +08:00
|
|
|
std::vector<size_t> ZoneWindow::ZonesFromPoint(POINT pt) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
return m_activeZoneSet->ZonesFromPoint(pt);
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-04-10 22:09:08 +08:00
|
|
|
return {};
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
Trace::ZoneWindow::CycleActiveZoneSet(m_activeZoneSet, mode);
|
|
|
|
if (m_keyLast != wparam)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
m_keyCycle = 0;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
m_keyLast = wparam;
|
2020-02-10 21:59:51 +08:00
|
|
|
|
|
|
|
bool loopAround = true;
|
|
|
|
size_t const val = static_cast<size_t>(wparam - L'0');
|
|
|
|
size_t i = 0;
|
2020-04-28 16:57:09 +08:00
|
|
|
for (auto zoneSet : m_zoneSets)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
if (zoneSet->GetZones().size() == val)
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
if (i < m_keyCycle)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-06 22:09:27 +08:00
|
|
|
UpdateActiveZoneSet(zoneSet.get());
|
2020-02-10 21:59:51 +08:00
|
|
|
loopAround = false;
|
|
|
|
break;
|
2019-12-06 22:09:27 +08:00
|
|
|
}
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if ((m_keyCycle > 0) && loopAround)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
// Cycling through a non-empty group and hit the end
|
2020-04-28 16:57:09 +08:00
|
|
|
m_keyCycle = 0;
|
2020-02-10 21:59:51 +08:00
|
|
|
OnKeyUp(wparam);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
m_keyCycle++;
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
if (m_host)
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
2020-04-28 16:57:09 +08:00
|
|
|
m_host->MoveWindowsOnActiveZoneSetChange();
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
2020-04-10 22:09:08 +08:00
|
|
|
m_highlightZone = {};
|
2020-02-10 21:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ZoneWindow::FlashZones() noexcept
|
|
|
|
{
|
2020-03-27 18:24:27 +08:00
|
|
|
// "Turning FLASHING_ZONE option off"
|
2020-04-04 02:17:12 +08:00
|
|
|
if (true)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-03-27 18:24:27 +08:00
|
|
|
|
2020-02-10 21:59:51 +08:00
|
|
|
m_flashMode = true;
|
|
|
|
|
2020-04-28 16:57:09 +08:00
|
|
|
ShowWindow(m_window.get(), SW_SHOWNA);
|
|
|
|
std::thread([window = m_window.get()]() {
|
2020-02-10 21:59:51 +08:00
|
|
|
AnimateWindow(window, m_flashDuration, AW_HIDE | AW_BLEND);
|
|
|
|
}).detach();
|
|
|
|
}
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
LRESULT CALLBACK ZoneWindow::s_WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept
|
|
|
|
{
|
|
|
|
auto thisRef = reinterpret_cast<ZoneWindow*>(GetWindowLongPtr(window, GWLP_USERDATA));
|
|
|
|
if ((thisRef == nullptr) && (message == WM_CREATE))
|
|
|
|
{
|
|
|
|
auto createStruct = reinterpret_cast<LPCREATESTRUCT>(lparam);
|
|
|
|
thisRef = reinterpret_cast<ZoneWindow*>(createStruct->lpCreateParams);
|
|
|
|
SetWindowLongPtr(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(thisRef));
|
|
|
|
}
|
|
|
|
|
|
|
|
return (thisRef != nullptr) ? thisRef->WndProc(message, wparam, lparam) :
|
|
|
|
DefWindowProc(window, message, wparam, lparam);
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:36:45 +08:00
|
|
|
winrt::com_ptr<IZoneWindow> MakeZoneWindow(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId, bool flashZones) noexcept
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
auto self = winrt::make_self<ZoneWindow>(hinstance);
|
2020-05-31 18:36:45 +08:00
|
|
|
if (self->Init(host, hinstance, monitor, uniqueId, parentUniqueId, flashZones))
|
2020-02-10 21:59:51 +08:00
|
|
|
{
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|