2020-02-10 14:59:51 +01:00
|
|
|
#include "pch.h"
|
|
|
|
|
|
2020-12-15 15:16:09 +03:00
|
|
|
#include <common/logger/logger.h>
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-07-22 10:39:13 +02:00
|
|
|
#include "FancyZonesData.h"
|
|
|
|
|
#include "FancyZonesDataTypes.h"
|
2020-02-10 14:59:51 +01:00
|
|
|
#include "ZoneWindow.h"
|
2020-08-25 18:55:29 +02:00
|
|
|
#include "ZoneWindowDrawing.h"
|
2020-02-10 14:59:51 +01:00
|
|
|
#include "trace.h"
|
|
|
|
|
#include "util.h"
|
2020-12-15 15:16:09 +03:00
|
|
|
#include "on_thread_executor.h"
|
2020-08-07 10:06:25 +02:00
|
|
|
#include "Settings.h"
|
2021-03-15 13:58:25 +01:00
|
|
|
#include "CallTracer.h"
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
#include <ShellScalingApi.h>
|
|
|
|
|
#include <mutex>
|
2020-07-06 17:40:25 +02:00
|
|
|
#include <fileapi.h>
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-03-25 15:38:44 +01:00
|
|
|
#include <gdiplus.h>
|
|
|
|
|
|
2020-08-11 13:51:06 +02:00
|
|
|
// Non-Localizable strings
|
|
|
|
|
namespace NonLocalizable
|
|
|
|
|
{
|
|
|
|
|
const wchar_t ToolWindowClassName[] = L"SuperFancyZones_ZoneWindow";
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 19:38:15 +02:00
|
|
|
using namespace FancyZonesUtils;
|
|
|
|
|
|
2021-03-10 14:50:40 +01:00
|
|
|
struct ZoneWindow;
|
|
|
|
|
|
|
|
|
|
namespace
|
|
|
|
|
{
|
|
|
|
|
// The reason for using this class is the need to call ShowWindow(window, SW_SHOWNORMAL); on each
|
|
|
|
|
// newly created window for it to be displayed properly. The call sometimes has side effects when
|
|
|
|
|
// a fullscreen app is running, and happens when the resolution change event is triggered
|
|
|
|
|
// (e.g. when running some games).
|
|
|
|
|
// This class will serve as a pool of windows for which this call was already done.
|
|
|
|
|
class WindowPool
|
|
|
|
|
{
|
|
|
|
|
std::vector<HWND> m_pool;
|
|
|
|
|
std::mutex m_mutex;
|
|
|
|
|
|
|
|
|
|
HWND ExtractWindow()
|
|
|
|
|
{
|
2021-03-15 13:58:25 +01:00
|
|
|
_TRACER_;
|
2021-03-10 14:50:40 +01:00
|
|
|
std::unique_lock lock(m_mutex);
|
|
|
|
|
|
|
|
|
|
if (m_pool.empty())
|
|
|
|
|
{
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HWND window = m_pool.back();
|
|
|
|
|
m_pool.pop_back();
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
HWND NewZoneWindow(Rect position, HINSTANCE hinstance, ZoneWindow* owner)
|
|
|
|
|
{
|
|
|
|
|
HWND windowFromPool = ExtractWindow();
|
|
|
|
|
if (windowFromPool == NULL)
|
|
|
|
|
{
|
|
|
|
|
HWND window = CreateWindowExW(WS_EX_TOOLWINDOW, NonLocalizable::ToolWindowClassName, L"", WS_POPUP, position.left(), position.top(), position.width(), position.height(), nullptr, nullptr, hinstance, owner);
|
|
|
|
|
Logger::info("Creating new zone window, hWnd = {}", (void*)window);
|
|
|
|
|
MakeWindowTransparent(window);
|
|
|
|
|
|
|
|
|
|
// According to ShowWindow docs, we must call it with SW_SHOWNORMAL the first time
|
|
|
|
|
ShowWindow(window, SW_SHOWNORMAL);
|
|
|
|
|
ShowWindow(window, SW_HIDE);
|
|
|
|
|
return window;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Logger::info("Reusing zone window from pool, hWnd = {}", (void*)windowFromPool);
|
|
|
|
|
SetWindowLongPtrW(windowFromPool, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(owner));
|
|
|
|
|
MoveWindow(windowFromPool, position.left(), position.top(), position.width(), position.height(), TRUE);
|
|
|
|
|
return windowFromPool;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FreeZoneWindow(HWND window)
|
|
|
|
|
{
|
2021-03-15 13:58:25 +01:00
|
|
|
_TRACER_;
|
2021-03-10 14:50:40 +01:00
|
|
|
Logger::info("Freeing zone window, hWnd = {}", (void*)window);
|
|
|
|
|
SetWindowLongPtrW(window, GWLP_USERDATA, 0);
|
|
|
|
|
ShowWindow(window, SW_HIDE);
|
|
|
|
|
|
|
|
|
|
std::unique_lock lock(m_mutex);
|
|
|
|
|
m_pool.push_back(window);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~WindowPool()
|
|
|
|
|
{
|
|
|
|
|
for (HWND window : m_pool)
|
|
|
|
|
{
|
|
|
|
|
DestroyWindow(window);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
WindowPool windowPool;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
struct ZoneWindow : public winrt::implements<ZoneWindow, IZoneWindow>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ZoneWindow(HINSTANCE hinstance);
|
2020-03-25 15:38:44 +01:00
|
|
|
~ZoneWindow();
|
|
|
|
|
|
2020-10-22 23:27:55 +02:00
|
|
|
bool Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-06-05 17:53:08 +03:00
|
|
|
IFACEMETHODIMP MoveSizeEnter(HWND window) noexcept;
|
2020-07-10 11:06:01 +02:00
|
|
|
IFACEMETHODIMP MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool selectManyZones) noexcept;
|
2020-02-10 14:59:51 +01:00
|
|
|
IFACEMETHODIMP MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept;
|
2020-03-25 15:38:44 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 14:39:34 +02:00
|
|
|
MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept;
|
2020-04-10 16:09:08 +02:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 14:39:34 +02:00
|
|
|
MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept;
|
2020-03-24 18:50:26 +01:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-21 12:53:03 +02:00
|
|
|
MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle) noexcept;
|
|
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
|
MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle) noexcept;
|
2020-09-11 11:32:45 +02:00
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
|
ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode) noexcept;
|
2020-02-17 18:28:49 +03:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
CycleActiveZoneSet(DWORD vkCode) noexcept;
|
|
|
|
|
IFACEMETHODIMP_(std::wstring)
|
2020-04-28 11:57:09 +03:00
|
|
|
UniqueId() noexcept { return { m_uniqueId }; }
|
2020-02-17 18:28:49 +03:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
SaveWindowProcessToZoneIndex(HWND window) noexcept;
|
|
|
|
|
IFACEMETHODIMP_(IZoneSet*)
|
2020-04-28 11:57:09 +03:00
|
|
|
ActiveZoneSet() noexcept { return m_activeZoneSet.get(); }
|
2020-03-13 15:56:23 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ShowZoneWindow() noexcept;
|
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
HideZoneWindow() noexcept;
|
2020-05-06 17:16:16 +02:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
UpdateActiveZoneSet() noexcept;
|
2020-07-10 11:06:01 +02:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ClearSelectedZones() noexcept;
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
static LRESULT CALLBACK s_WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept;
|
|
|
|
|
|
|
|
|
|
private:
|
2020-05-31 12:36:45 +02:00
|
|
|
void InitializeZoneSets(const std::wstring& parentUniqueId) noexcept;
|
2020-02-10 14:59:51 +01:00
|
|
|
void CalculateZoneSet() noexcept;
|
|
|
|
|
void UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept;
|
|
|
|
|
LRESULT WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept;
|
|
|
|
|
void OnKeyUp(WPARAM wparam) noexcept;
|
2020-08-24 14:39:34 +02:00
|
|
|
std::vector<size_t> ZonesFromPoint(POINT pt) noexcept;
|
2020-02-10 14:59:51 +01:00
|
|
|
void CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept;
|
|
|
|
|
|
|
|
|
|
winrt::com_ptr<IZoneWindowHost> m_host;
|
|
|
|
|
HMONITOR m_monitor{};
|
|
|
|
|
std::wstring m_uniqueId; // Parsed deviceId + resolution + virtualDesktopId
|
2021-03-10 14:50:40 +01:00
|
|
|
HWND m_window{}; // Hidden tool window used to represent current monitor desktop work area.
|
2020-02-10 14:59:51 +01:00
|
|
|
HWND m_windowMoveSize{};
|
|
|
|
|
winrt::com_ptr<IZoneSet> m_activeZoneSet;
|
|
|
|
|
std::vector<winrt::com_ptr<IZoneSet>> m_zoneSets;
|
2020-08-24 14:39:34 +02:00
|
|
|
std::vector<size_t> m_initialHighlightZone;
|
|
|
|
|
std::vector<size_t> m_highlightZone;
|
2020-02-10 14:59:51 +01:00
|
|
|
WPARAM m_keyLast{};
|
|
|
|
|
size_t m_keyCycle{};
|
|
|
|
|
static const UINT m_showAnimationDuration = 200; // ms
|
|
|
|
|
static const UINT m_flashDuration = 700; // ms
|
2020-10-20 15:19:10 +02:00
|
|
|
std::unique_ptr<ZoneWindowDrawing> m_zoneWindowDrawing;
|
2020-02-10 14:59:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ZoneWindow::ZoneWindow(HINSTANCE hinstance)
|
|
|
|
|
{
|
|
|
|
|
WNDCLASSEXW wcex{};
|
|
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
|
wcex.lpfnWndProc = s_WndProc;
|
|
|
|
|
wcex.hInstance = hinstance;
|
2020-08-11 13:51:06 +02:00
|
|
|
wcex.lpszClassName = NonLocalizable::ToolWindowClassName;
|
2020-02-10 14:59:51 +01:00
|
|
|
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
|
|
|
|
|
RegisterClassExW(&wcex);
|
2020-03-25 15:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ZoneWindow::~ZoneWindow()
|
|
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
windowPool.FreeZoneWindow(m_window);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-22 23:27:55 +02:00
|
|
|
bool ZoneWindow::Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
m_host.copy_from(host);
|
|
|
|
|
|
2020-08-07 10:06:25 +02:00
|
|
|
Rect workAreaRect;
|
|
|
|
|
m_monitor = monitor;
|
|
|
|
|
if (monitor)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-08-07 10:06:25 +02:00
|
|
|
MONITORINFO mi{};
|
|
|
|
|
mi.cbSize = sizeof(mi);
|
|
|
|
|
if (!GetMonitorInfoW(monitor, &mi))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-12-08 13:31:30 +01:00
|
|
|
workAreaRect = Rect(mi.rcWork);
|
2020-08-07 10:06:25 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
workAreaRect = GetAllMonitorsCombinedRect<&MONITORINFO::rcWork>();
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_uniqueId = uniqueId;
|
2020-05-31 12:36:45 +02:00
|
|
|
InitializeZoneSets(parentUniqueId);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2021-03-10 14:50:40 +01:00
|
|
|
m_window = windowPool.NewZoneWindow(workAreaRect, hinstance, this);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
if (!m_window)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-10 14:50:40 +01:00
|
|
|
m_zoneWindowDrawing = std::make_unique<ZoneWindowDrawing>(m_window);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-05 17:53:08 +03:00
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeEnter(HWND window) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
m_windowMoveSize = window;
|
|
|
|
|
m_highlightZone = {};
|
2020-07-10 11:06:01 +02:00
|
|
|
m_initialHighlightZone = {};
|
2020-02-10 14:59:51 +01:00
|
|
|
ShowZoneWindow();
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 11:06:01 +02:00
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled, bool selectManyZones) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
bool redraw = false;
|
|
|
|
|
POINT ptClient = ptScreen;
|
2021-03-10 14:50:40 +01:00
|
|
|
MapWindowPoints(nullptr, m_window, &ptClient, 1);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
if (dragEnabled)
|
|
|
|
|
{
|
2020-04-10 16:09:08 +02:00
|
|
|
auto highlightZone = ZonesFromPoint(ptClient);
|
2020-07-10 11:06:01 +02:00
|
|
|
|
|
|
|
|
if (selectManyZones)
|
|
|
|
|
{
|
|
|
|
|
if (m_initialHighlightZone.empty())
|
|
|
|
|
{
|
|
|
|
|
// first time
|
|
|
|
|
m_initialHighlightZone = highlightZone;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-09-11 11:32:45 +02:00
|
|
|
highlightZone = m_activeZoneSet->GetCombinedZoneRange(m_initialHighlightZone, highlightZone);
|
2020-07-10 11:06:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
m_initialHighlightZone = {};
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
redraw = (highlightZone != m_highlightZone);
|
|
|
|
|
m_highlightZone = std::move(highlightZone);
|
|
|
|
|
}
|
2020-04-10 16:09:08 +02:00
|
|
|
else if (m_highlightZone.size())
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-10 16:09:08 +02:00
|
|
|
m_highlightZone = {};
|
2020-02-10 14:59:51 +01:00
|
|
|
redraw = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (redraw)
|
|
|
|
|
{
|
2020-10-23 15:22:45 +02:00
|
|
|
m_zoneWindowDrawing->DrawActiveZoneSet(m_activeZoneSet->GetZones(), m_highlightZone, m_host);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-12-15 15:16:09 +03:00
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
return S_OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IFACEMETHODIMP ZoneWindow::MoveSizeEnd(HWND window, POINT const& ptScreen) noexcept
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_windowMoveSize != window)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
return E_INVALIDARG;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
POINT ptClient = ptScreen;
|
2021-03-10 14:50:40 +01:00
|
|
|
MapWindowPoints(nullptr, m_window, &ptClient, 1);
|
|
|
|
|
m_activeZoneSet->MoveWindowIntoZoneByIndexSet(window, m_window, m_highlightZone);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-09-15 13:03:17 +02:00
|
|
|
if (FancyZonesUtils::HasNoVisibleOwner(window))
|
2020-08-28 15:00:21 +02:00
|
|
|
{
|
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-04-28 11:57:09 +03:00
|
|
|
Trace::ZoneWindow::MoveSizeEnd(m_activeZoneSet);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
HideZoneWindow();
|
|
|
|
|
m_windowMoveSize = nullptr;
|
|
|
|
|
return S_OK;
|
|
|
|
|
}
|
2020-03-25 15:38:44 +01:00
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 14:39:34 +02:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByIndex(HWND window, size_t index) noexcept
|
2020-04-10 16:09:08 +02:00
|
|
|
{
|
|
|
|
|
MoveWindowIntoZoneByIndexSet(window, { index });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
2020-08-24 14:39:34 +02:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<size_t>& indexSet) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
m_activeZoneSet->MoveWindowIntoZoneByIndexSet(window, m_window, indexSet);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 18:50:26 +01:00
|
|
|
IFACEMETHODIMP_(bool)
|
2020-08-21 12:53:03 +02:00
|
|
|
ZoneWindow::MoveWindowIntoZoneByDirectionAndIndex(HWND window, DWORD vkCode, bool cycle) noexcept
|
|
|
|
|
{
|
|
|
|
|
if (m_activeZoneSet)
|
|
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
if (m_activeZoneSet->MoveWindowIntoZoneByDirectionAndIndex(window, m_window, vkCode, cycle))
|
2020-08-21 12:53:03 +02:00
|
|
|
{
|
2020-09-15 13:03:17 +02:00
|
|
|
if (FancyZonesUtils::HasNoVisibleOwner(window))
|
2020-08-28 15:00:21 +02:00
|
|
|
{
|
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
|
}
|
2020-08-21 12:53:03 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
|
ZoneWindow::MoveWindowIntoZoneByDirectionAndPosition(HWND window, DWORD vkCode, bool cycle) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
if (m_activeZoneSet->MoveWindowIntoZoneByDirectionAndPosition(window, m_window, vkCode, cycle))
|
2020-03-24 18:50:26 +01:00
|
|
|
{
|
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-03-24 18:50:26 +01:00
|
|
|
return false;
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-09-11 11:32:45 +02:00
|
|
|
IFACEMETHODIMP_(bool)
|
|
|
|
|
ZoneWindow::ExtendWindowByDirectionAndPosition(HWND window, DWORD vkCode) noexcept
|
|
|
|
|
{
|
|
|
|
|
if (m_activeZoneSet)
|
|
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
if (m_activeZoneSet->ExtendWindowByDirectionAndPosition(window, m_window, vkCode))
|
2020-09-11 11:32:45 +02:00
|
|
|
{
|
|
|
|
|
SaveWindowProcessToZoneIndex(window);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::CycleActiveZoneSet(DWORD wparam) noexcept
|
|
|
|
|
{
|
|
|
|
|
CycleActiveZoneSetInternal(wparam, Trace::ZoneWindow::InputMode::Keyboard);
|
|
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_windowMoveSize)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
InvalidateRect(m_window, nullptr, true);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::SaveWindowProcessToZoneIndex(HWND window) noexcept
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2019-11-18 15:29:42 -08:00
|
|
|
{
|
2020-05-26 16:01:12 +02:00
|
|
|
auto zoneIndexSet = m_activeZoneSet->GetZoneIndexSetFromWindow(window);
|
|
|
|
|
if (zoneIndexSet.size())
|
2019-11-18 15:29:42 -08:00
|
|
|
{
|
2020-02-10 14:59:51 +01:00
|
|
|
OLECHAR* guidString;
|
2020-04-28 11:57:09 +03:00
|
|
|
if (StringFromCLSID(m_activeZoneSet->Id(), &guidString) == S_OK)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-07-22 10:39:13 +02:00
|
|
|
FancyZonesDataInstance().SetAppLastZones(window, m_uniqueId, guidString, zoneIndexSet);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CoTaskMemFree(guidString);
|
2019-11-18 15:29:42 -08:00
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2019-12-06 15:09:27 +01:00
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-03-13 15:56:23 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::ShowZoneWindow() noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
auto window = m_window;
|
2020-05-01 17:14:35 +03:00
|
|
|
if (!window)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-05-01 17:14:35 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-05-01 17:14:35 +03:00
|
|
|
UINT flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE;
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-05-01 17:14:35 +03:00
|
|
|
HWND windowInsertAfter = m_windowMoveSize;
|
|
|
|
|
if (windowInsertAfter == nullptr)
|
|
|
|
|
{
|
|
|
|
|
windowInsertAfter = HWND_TOPMOST;
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-05-01 17:14:35 +03:00
|
|
|
|
|
|
|
|
SetWindowPos(window, windowInsertAfter, 0, 0, 0, 0, flags);
|
2020-10-22 15:43:20 +02:00
|
|
|
m_zoneWindowDrawing->Show(m_showAnimationDuration);
|
2020-10-23 15:22:45 +02:00
|
|
|
m_zoneWindowDrawing->DrawActiveZoneSet(m_activeZoneSet->GetZones(), m_highlightZone, m_host);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-03-13 15:56:23 +01:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::HideZoneWindow() noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_window)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-10-22 15:43:20 +02:00
|
|
|
m_zoneWindowDrawing->Hide();
|
2020-02-10 14:59:51 +01:00
|
|
|
m_keyLast = 0;
|
|
|
|
|
m_windowMoveSize = nullptr;
|
2020-04-10 16:09:08 +02:00
|
|
|
m_highlightZone = {};
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-06 17:16:16 +02:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::UpdateActiveZoneSet() noexcept
|
|
|
|
|
{
|
|
|
|
|
CalculateZoneSet();
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-10 11:06:01 +02:00
|
|
|
IFACEMETHODIMP_(void)
|
|
|
|
|
ZoneWindow::ClearSelectedZones() noexcept
|
|
|
|
|
{
|
|
|
|
|
if (m_highlightZone.size())
|
|
|
|
|
{
|
|
|
|
|
m_highlightZone.clear();
|
2020-10-23 15:32:51 +02:00
|
|
|
m_zoneWindowDrawing->DrawActiveZoneSet(m_activeZoneSet->GetZones(), m_highlightZone, m_host);
|
2020-07-10 11:06:01 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 15:56:23 +01:00
|
|
|
#pragma region private
|
|
|
|
|
|
2020-05-31 12:36:45 +02:00
|
|
|
void ZoneWindow::InitializeZoneSets(const std::wstring& parentUniqueId) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-10-08 16:02:51 +02:00
|
|
|
bool deviceAdded = FancyZonesDataInstance().AddDevice(m_uniqueId);
|
|
|
|
|
// If the device has been added, check if it should inherit the parent's layout
|
|
|
|
|
if (deviceAdded && !parentUniqueId.empty())
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-07-22 10:39:13 +02:00
|
|
|
FancyZonesDataInstance().CloneDeviceInfo(parentUniqueId, m_uniqueId);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
CalculateZoneSet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ZoneWindow::CalculateZoneSet() noexcept
|
|
|
|
|
{
|
2020-07-22 10:39:13 +02:00
|
|
|
const auto& fancyZonesData = FancyZonesDataInstance();
|
2020-04-28 11:57:09 +03:00
|
|
|
const auto deviceInfoData = fancyZonesData.FindDeviceInfo(m_uniqueId);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-02-17 18:28:49 +03:00
|
|
|
if (!deviceInfoData.has_value())
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const auto& activeZoneSet = deviceInfoData->activeZoneSet;
|
|
|
|
|
|
2020-11-19 10:03:22 +03:00
|
|
|
if (activeZoneSet.uuid.empty())
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GUID zoneSetId;
|
|
|
|
|
if (SUCCEEDED_LOG(CLSIDFromString(activeZoneSet.uuid.c_str(), &zoneSetId)))
|
|
|
|
|
{
|
2020-09-18 09:16:06 +02:00
|
|
|
int sensitivityRadius = deviceInfoData->sensitivityRadius;
|
|
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
auto zoneSet = MakeZoneSet(ZoneSetConfig(
|
|
|
|
|
zoneSetId,
|
|
|
|
|
activeZoneSet.type,
|
2020-09-18 09:16:06 +02:00
|
|
|
m_monitor,
|
2021-02-25 16:23:05 +01:00
|
|
|
sensitivityRadius,
|
|
|
|
|
m_host->GetOverlappingZonesAlgorithm()));
|
2020-10-22 23:27:55 +02:00
|
|
|
|
2020-08-07 10:06:25 +02:00
|
|
|
RECT workArea;
|
|
|
|
|
if (m_monitor)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-08-07 10:06:25 +02: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 14:59:51 +01:00
|
|
|
}
|
2020-08-07 10:06:25 +02:00
|
|
|
|
|
|
|
|
bool showSpacing = deviceInfoData->showSpacing;
|
|
|
|
|
int spacing = showSpacing ? deviceInfoData->spacing : 0;
|
|
|
|
|
int zoneCount = deviceInfoData->zoneCount;
|
2020-10-22 23:27:55 +02:00
|
|
|
|
2020-08-07 10:06:25 +02:00
|
|
|
zoneSet->CalculateZones(workArea, zoneCount, spacing);
|
2020-10-22 23:27:55 +02:00
|
|
|
UpdateActiveZoneSet(zoneSet.get());
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ZoneWindow::UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
m_activeZoneSet.copy_from(zoneSet);
|
2020-02-10 14:59:51 +01:00
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
wil::unique_cotaskmem_string zoneSetId;
|
2020-04-28 11:57:09 +03:00
|
|
|
if (SUCCEEDED_LOG(StringFromCLSID(m_activeZoneSet->Id(), &zoneSetId)))
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-07-22 10:39:13 +02:00
|
|
|
FancyZonesDataTypes::ZoneSetData data{
|
2020-02-10 14:59:51 +01:00
|
|
|
.uuid = zoneSetId.get(),
|
2020-04-28 11:57:09 +03:00
|
|
|
.type = m_activeZoneSet->LayoutType()
|
2020-02-10 14:59:51 +01:00
|
|
|
};
|
2020-07-22 10:39:13 +02:00
|
|
|
FancyZonesDataInstance().SetActiveZoneSet(m_uniqueId, data);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
|
|
|
|
|
{
|
|
|
|
|
switch (message)
|
|
|
|
|
{
|
2020-03-25 15:38:44 +01:00
|
|
|
case WM_NCDESTROY:
|
|
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
::DefWindowProc(m_window, message, wparam, lparam);
|
|
|
|
|
SetWindowLongPtr(m_window, GWLP_USERDATA, 0);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case WM_ERASEBKGND:
|
|
|
|
|
return 1;
|
|
|
|
|
|
2020-10-23 15:22:45 +02:00
|
|
|
case WM_PAINT:
|
|
|
|
|
{
|
|
|
|
|
m_zoneWindowDrawing->ForceRender();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-10 14:59:51 +01:00
|
|
|
default:
|
|
|
|
|
{
|
2021-03-10 14:50:40 +01:00
|
|
|
return DefWindowProc(m_window, message, wparam, lparam);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-10-23 15:32:51 +02:00
|
|
|
m_zoneWindowDrawing->DrawActiveZoneSet(m_activeZoneSet->GetZones(), m_highlightZone, m_host);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-24 14:39:34 +02:00
|
|
|
std::vector<size_t> ZoneWindow::ZonesFromPoint(POINT pt) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_activeZoneSet)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
return m_activeZoneSet->ZonesFromPoint(pt);
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-04-10 16:09:08 +02:00
|
|
|
return {};
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ZoneWindow::CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
Trace::ZoneWindow::CycleActiveZoneSet(m_activeZoneSet, mode);
|
|
|
|
|
if (m_keyLast != wparam)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
m_keyCycle = 0;
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
m_keyLast = wparam;
|
2020-02-10 14:59:51 +01:00
|
|
|
|
|
|
|
|
bool loopAround = true;
|
|
|
|
|
size_t const val = static_cast<size_t>(wparam - L'0');
|
|
|
|
|
size_t i = 0;
|
2020-04-28 11:57:09 +03:00
|
|
|
for (auto zoneSet : m_zoneSets)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
if (zoneSet->GetZones().size() == val)
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
if (i < m_keyCycle)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-12-06 15:09:27 +01:00
|
|
|
UpdateActiveZoneSet(zoneSet.get());
|
2020-02-10 14:59:51 +01:00
|
|
|
loopAround = false;
|
|
|
|
|
break;
|
2019-12-06 15:09:27 +01:00
|
|
|
}
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
if ((m_keyCycle > 0) && loopAround)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
// Cycling through a non-empty group and hit the end
|
2020-04-28 11:57:09 +03:00
|
|
|
m_keyCycle = 0;
|
2020-02-10 14:59:51 +01:00
|
|
|
OnKeyUp(wparam);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
m_keyCycle++;
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
2020-04-28 11:57:09 +03:00
|
|
|
if (m_host)
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
2020-04-28 11:57:09 +03:00
|
|
|
m_host->MoveWindowsOnActiveZoneSetChange();
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
2020-04-10 16:09:08 +02:00
|
|
|
m_highlightZone = {};
|
2020-02-10 14:59:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#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-10-22 23:27:55 +02:00
|
|
|
winrt::com_ptr<IZoneWindow> MakeZoneWindow(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, const std::wstring& parentUniqueId) noexcept
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
auto self = winrt::make_self<ZoneWindow>(hinstance);
|
2020-10-22 23:27:55 +02:00
|
|
|
if (self->Init(host, hinstance, monitor, uniqueId, parentUniqueId))
|
2020-02-10 14:59:51 +01:00
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|