Merge branch 'master' into dev/build-features

This commit is contained in:
Arjun 2020-04-20 09:15:32 -07:00
commit 62bae55fd1
10 changed files with 425 additions and 180 deletions

View File

@ -1,8 +1,9 @@
#include "pch.h" #include "pch.h"
#include "common.h"
#include "monitors.h" #include "monitors.h"
#include "common.h"
bool operator==(const ScreenSize& lhs, const ScreenSize& rhs) bool operator==(const ScreenSize& lhs, const ScreenSize& rhs)
{ {
auto lhs_tuple = std::make_tuple(lhs.rect.left, lhs.rect.right, lhs.rect.top, lhs.rect.bottom); auto lhs_tuple = std::make_tuple(lhs.rect.left, lhs.rect.right, lhs.rect.top, lhs.rect.bottom);
@ -10,48 +11,47 @@ bool operator==(const ScreenSize& lhs, const ScreenSize& rhs)
return lhs_tuple == rhs_tuple; return lhs_tuple == rhs_tuple;
} }
static BOOL CALLBACK get_displays_enum_cb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data) static BOOL CALLBACK GetDisplaysEnumCb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{ {
MONITORINFOEX monitor_info; MONITORINFOEX monitorInfo;
monitor_info.cbSize = sizeof(MONITORINFOEX); monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitor, &monitor_info); if (GetMonitorInfo(monitor, &monitorInfo))
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitor_info.rcWork); {
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitorInfo.rcWork);
}
return true; return true;
}; };
static BOOL CALLBACK get_displays_enum_cb_with_toolbar(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data) static BOOL CALLBACK GetDisplaysEnumCbWithNonWorkingArea(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{ {
MONITORINFOEX monitor_info; MONITORINFOEX monitorInfo;
monitor_info.cbSize = sizeof(MONITORINFOEX); monitorInfo.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitor, &monitor_info); if (GetMonitorInfo(monitor, &monitorInfo))
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitor_info.rcMonitor); {
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitorInfo.rcMonitor);
}
return true; return true;
}; };
std::vector<MonitorInfo> MonitorInfo::GetMonitors(bool include_toolbar) std::vector<MonitorInfo> MonitorInfo::GetMonitors(bool includeNonWorkingArea)
{ {
std::vector<MonitorInfo> monitors; std::vector<MonitorInfo> monitors;
EnumDisplayMonitors(NULL, NULL, include_toolbar ? get_displays_enum_cb_with_toolbar : get_displays_enum_cb, reinterpret_cast<LPARAM>(&monitors)); EnumDisplayMonitors(NULL, NULL, includeNonWorkingArea ? GetDisplaysEnumCbWithNonWorkingArea : GetDisplaysEnumCb, reinterpret_cast<LPARAM>(&monitors));
std::sort(begin(monitors), end(monitors), [](const MonitorInfo& lhs, const MonitorInfo& rhs) { std::sort(begin(monitors), end(monitors), [](const MonitorInfo& lhs, const MonitorInfo& rhs) {
return lhs.rect < rhs.rect; return lhs.rect < rhs.rect;
}); });
return monitors; return monitors;
} }
int MonitorInfo::GetMonitorsCount() static BOOL CALLBACK GetPrimaryDisplayEnumCb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{ {
return GetMonitors(true).size(); MONITORINFOEX monitorInfo;
} monitorInfo.cbSize = sizeof(MONITORINFOEX);
static BOOL CALLBACK get_primary_display_enum_cb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data) if (GetMonitorInfo(monitor, &monitorInfo) && (monitorInfo.dwFlags & MONITORINFOF_PRIMARY))
{
MONITORINFOEX monitor_info;
monitor_info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitor, &monitor_info);
if (monitor_info.dwFlags & MONITORINFOF_PRIMARY)
{ {
reinterpret_cast<MonitorInfo*>(data)->handle = monitor; reinterpret_cast<MonitorInfo*>(data)->handle = monitor;
reinterpret_cast<MonitorInfo*>(data)->rect = monitor_info.rcWork; reinterpret_cast<MonitorInfo*>(data)->rect = monitorInfo.rcWork;
} }
return true; return true;
}; };
@ -59,26 +59,6 @@ static BOOL CALLBACK get_primary_display_enum_cb(HMONITOR monitor, HDC hdc, LPRE
MonitorInfo MonitorInfo::GetPrimaryMonitor() MonitorInfo MonitorInfo::GetPrimaryMonitor()
{ {
MonitorInfo primary({}, {}); MonitorInfo primary({}, {});
EnumDisplayMonitors(NULL, NULL, get_primary_display_enum_cb, reinterpret_cast<LPARAM>(&primary)); EnumDisplayMonitors(NULL, NULL, GetPrimaryDisplayEnumCb, reinterpret_cast<LPARAM>(&primary));
return primary; return primary;
} }
MonitorInfo MonitorInfo::GetFromWindow(HWND hwnd)
{
auto monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
return GetFromHandle(monitor);
}
MonitorInfo MonitorInfo::GetFromPoint(POINT p)
{
auto monitor = MonitorFromPoint(p, MONITOR_DEFAULTTONEAREST);
return GetFromHandle(monitor);
}
MonitorInfo MonitorInfo::GetFromHandle(HMONITOR monitor)
{
MONITORINFOEX monitor_info;
monitor_info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(monitor, &monitor_info);
return MonitorInfo(monitor, monitor_info.rcWork);
}

View File

@ -31,16 +31,8 @@ struct MonitorInfo : ScreenSize
HMONITOR handle; HMONITOR handle;
// Returns monitor rects ordered from left to right // Returns monitor rects ordered from left to right
static std::vector<MonitorInfo> GetMonitors(bool include_toolbar); static std::vector<MonitorInfo> GetMonitors(bool includeNonWorkingArea);
static int GetMonitorsCount();
// Return primary display
static MonitorInfo GetPrimaryMonitor(); static MonitorInfo GetPrimaryMonitor();
// Return monitor on which hwnd window is displayed
static MonitorInfo GetFromWindow(HWND hwnd);
// Return monitor nearest to a point
static MonitorInfo GetFromPoint(POINT p);
// Return monitor info given a HMONITOR
static MonitorInfo GetFromHandle(HMONITOR monitor);
}; };
bool operator==(const ScreenSize& lhs, const ScreenSize& rhs); bool operator==(const ScreenSize& lhs, const ScreenSize& rhs);

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@ -222,6 +222,7 @@ namespace FancyZonesEditor
int newChildIndex = AddZone(); int newChildIndex = AddZone();
double offset = e.Offset; double offset = e.Offset;
double space = e.Space;
if (e.Orientation == Orientation.Vertical) if (e.Orientation == Orientation.Vertical)
{ {
@ -294,22 +295,28 @@ namespace FancyZonesEditor
model.CellChildMap = newCellChildMap; model.CellChildMap = newCellChildMap;
sourceCol = 0; sourceCol = 0;
double newTotalExtent = ActualWidth - (space * (cols + 1));
for (int col = 0; col < cols; col++) for (int col = 0; col < cols; col++)
{ {
if (col == foundCol) if (col == foundCol)
{ {
RowColInfo[] split = _colInfo[col].Split(offset); RowColInfo[] split = _colInfo[col].Split(offset, space);
newColInfo[col] = split[0];
newColPercents[col] = split[0].Percent; newColPercents[col] = split[0].Percent;
newColInfo[col++] = split[0]; col++;
newColPercents[col] = split[1].Percent;
newColInfo[col] = split[1]; newColInfo[col] = split[1];
sourceCol++; newColPercents[col] = split[1].Percent;
} }
else else
{ {
newColInfo[col] = _colInfo[sourceCol];
newColInfo[col].RecalculatePercent(newTotalExtent);
newColPercents[col] = model.ColumnPercents[sourceCol]; newColPercents[col] = model.ColumnPercents[sourceCol];
newColInfo[col] = _colInfo[sourceCol++];
} }
sourceCol++;
} }
_colInfo = newColInfo; _colInfo = newColInfo;
@ -389,22 +396,28 @@ namespace FancyZonesEditor
model.CellChildMap = newCellChildMap; model.CellChildMap = newCellChildMap;
sourceRow = 0; sourceRow = 0;
double newTotalExtent = ActualHeight - (space * (rows + 1));
for (int row = 0; row < rows; row++) for (int row = 0; row < rows; row++)
{ {
if (row == foundRow) if (row == foundRow)
{ {
RowColInfo[] split = _rowInfo[row].Split(offset); RowColInfo[] split = _rowInfo[row].Split(offset, space);
newRowInfo[row] = split[0];
newRowPercents[row] = split[0].Percent; newRowPercents[row] = split[0].Percent;
newRowInfo[row++] = split[0]; row++;
newRowPercents[row] = split[1].Percent;
newRowInfo[row] = split[1]; newRowInfo[row] = split[1];
sourceRow++; newRowPercents[row] = split[1].Percent;
} }
else else
{ {
newRowInfo[row] = _rowInfo[sourceRow];
newRowInfo[row].RecalculatePercent(newTotalExtent);
newRowPercents[row] = model.RowPercents[sourceRow]; newRowPercents[row] = model.RowPercents[sourceRow];
newRowInfo[row] = _rowInfo[sourceRow++];
} }
sourceRow++;
} }
_rowInfo = newRowInfo; _rowInfo = newRowInfo;

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@ -276,8 +276,15 @@ namespace FancyZonesEditor
} }
private void DoSplit(Orientation orientation, double offset) private void DoSplit(Orientation orientation, double offset)
{ {
Split?.Invoke(this, new SplitEventArgs(orientation, offset)); int spacing = 0;
Settings settings = ((App)Application.Current).ZoneSettings;
if (settings.ShowSpacing)
{
spacing = settings.Spacing;
}
Split?.Invoke(this, new SplitEventArgs(orientation, offset, spacing));
} }
private void FullSplit_Click(object sender, RoutedEventArgs e) private void FullSplit_Click(object sender, RoutedEventArgs e)

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@ -34,13 +34,24 @@ namespace FancyZonesEditor
return Extent; return Extent;
} }
public RowColInfo[] Split(double offset) public void RecalculatePercent(double newTotalExtent)
{
Percent = (int)(Extent * _multiplier / newTotalExtent);
}
public RowColInfo[] Split(double offset, double space)
{ {
RowColInfo[] info = new RowColInfo[2]; RowColInfo[] info = new RowColInfo[2];
int newPercent = (int)(Percent * offset / Extent); double totalExtent = Extent * _multiplier / Percent;
info[0] = new RowColInfo(newPercent); totalExtent -= space;
info[1] = new RowColInfo(Percent - newPercent);
int percent0 = (int)(offset * _multiplier / totalExtent);
int percent1 = (int)((Extent - space - offset) * _multiplier / totalExtent);
info[0] = new RowColInfo(percent0);
info[1] = new RowColInfo(percent1);
return info; return info;
} }
} }

View File

@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation // Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license. // The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information. // See the LICENSE file in the project root for more information.
@ -13,15 +13,18 @@ namespace FancyZonesEditor
{ {
} }
public SplitEventArgs(Orientation orientation, double offset) public SplitEventArgs(Orientation orientation, double offset, double thickness)
{ {
Orientation = orientation; Orientation = orientation;
Offset = offset; Offset = offset;
Space = thickness;
} }
public Orientation Orientation { get; } public Orientation Orientation { get; }
public double Offset { get; } public double Offset { get; }
public double Space { get; }
} }
public delegate void SplitEventHandler(object sender, SplitEventArgs args); public delegate void SplitEventHandler(object sender, SplitEventArgs args);

View File

@ -1,5 +1,8 @@
#include "pch.h" #include "pch.h"
#include <Shellscalingapi.h>
#include <common/dpi_aware.h> #include <common/dpi_aware.h>
#include <common/monitors.h> #include <common/monitors.h>
#include "Zone.h" #include "Zone.h"
@ -68,6 +71,45 @@ void Zone::SizeWindowToZone(HWND window, HWND zoneWindow) noexcept
SizeWindowToRect(window, ComputeActualZoneRect(window, zoneWindow)); SizeWindowToRect(window, ComputeActualZoneRect(window, zoneWindow));
} }
static BOOL CALLBACK saveDisplayToVector(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
reinterpret_cast<std::vector<HMONITOR>*>(data)->emplace_back(monitor);
return true;
}
bool allMonitorsHaveSameDpiScaling()
{
std::vector<HMONITOR> monitors;
EnumDisplayMonitors(NULL, NULL, saveDisplayToVector, reinterpret_cast<LPARAM>(&monitors));
if (monitors.size() < 2)
{
return true;
}
UINT firstMonitorDpiX;
UINT firstMonitorDpiY;
if (S_OK != GetDpiForMonitor(monitors[0], MDT_EFFECTIVE_DPI, &firstMonitorDpiX, &firstMonitorDpiY))
{
return false;
}
for (int i = 1; i < monitors.size(); i++)
{
UINT iteratedMonitorDpiX;
UINT iteratedMonitorDpiY;
if (S_OK != GetDpiForMonitor(monitors[i], MDT_EFFECTIVE_DPI, &iteratedMonitorDpiX, &iteratedMonitorDpiY) ||
iteratedMonitorDpiX != firstMonitorDpiX)
{
return false;
}
}
return true;
}
RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept
{ {
// Take care of 1px border // Take care of 1px border
@ -101,7 +143,7 @@ RECT Zone::ComputeActualZoneRect(HWND window, HWND zoneWindow) noexcept
const auto taskbar_top_size = std::abs(mi.rcMonitor.top - mi.rcWork.top); const auto taskbar_top_size = std::abs(mi.rcMonitor.top - mi.rcWork.top);
OffsetRect(&newWindowRect, -taskbar_left_size, -taskbar_top_size); OffsetRect(&newWindowRect, -taskbar_left_size, -taskbar_top_size);
if (accountForUnawareness && MonitorInfo::GetMonitorsCount() > 1) if (accountForUnawareness && !allMonitorsHaveSameDpiScaling())
{ {
newWindowRect.left = max(mi.rcMonitor.left, newWindowRect.left); newWindowRect.left = max(mi.rcMonitor.left, newWindowRect.left);
newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size, newWindowRect.right); newWindowRect.right = min(mi.rcMonitor.right - taskbar_left_size, newWindowRect.right);

View File

@ -213,7 +213,7 @@ public:
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
RestoreOrginalTransparency() noexcept; RestoreOrginalTransparency() noexcept;
IFACEMETHODIMP_(bool) IFACEMETHODIMP_(bool)
IsDragEnabled() noexcept { return m_dragEnabled; } IsDragEnabled() noexcept;
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
MoveWindowIntoZoneByIndex(HWND window, int index) noexcept; MoveWindowIntoZoneByIndex(HWND window, int index) noexcept;
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
@ -223,13 +223,13 @@ public:
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
CycleActiveZoneSet(DWORD vkCode) noexcept; CycleActiveZoneSet(DWORD vkCode) noexcept;
IFACEMETHODIMP_(std::wstring) IFACEMETHODIMP_(std::wstring)
UniqueId() noexcept { return { m_uniqueId }; } UniqueId() noexcept;
IFACEMETHODIMP_(std::wstring) IFACEMETHODIMP_(std::wstring)
WorkAreaKey() noexcept { return { m_workArea }; } WorkAreaKey() noexcept;
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
SaveWindowProcessToZoneIndex(HWND window) noexcept; SaveWindowProcessToZoneIndex(HWND window) noexcept;
IFACEMETHODIMP_(IZoneSet*) IFACEMETHODIMP_(IZoneSet*)
ActiveZoneSet() noexcept { return m_activeZoneSet.get(); } ActiveZoneSet() noexcept;
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ShowZoneWindow() noexcept; ShowZoneWindow() noexcept;
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
@ -267,13 +267,15 @@ private:
static const UINT m_showAnimationDuration = 200; // ms static const UINT m_showAnimationDuration = 200; // ms
static const UINT m_flashDuration = 700; // ms static const UINT m_flashDuration = 700; // ms
HWND draggedWindow = nullptr; HWND m_draggedWindow = nullptr;
long draggedWindowExstyle = 0; long m_draggedWindowExstyle = 0;
COLORREF draggedWindowCrKey = RGB(0, 0, 0); COLORREF m_draggedWindowCrKey = RGB(0, 0, 0);
DWORD draggedWindowDwFlags = 0; DWORD m_draggedWindowDwFlags = 0;
BYTE draggedWindowInitialAlpha = 0; BYTE m_draggedWindowInitialAlpha = 0;
ULONG_PTR gdiplusToken; ULONG_PTR m_gdiplusToken;
mutable std::shared_mutex m_mutex;
}; };
ZoneWindow::ZoneWindow(HINSTANCE hinstance) ZoneWindow::ZoneWindow(HINSTANCE hinstance)
@ -287,14 +289,14 @@ ZoneWindow::ZoneWindow(HINSTANCE hinstance)
RegisterClassExW(&wcex); RegisterClassExW(&wcex);
Gdiplus::GdiplusStartupInput gdiplusStartupInput; Gdiplus::GdiplusStartupInput gdiplusStartupInput;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); Gdiplus::GdiplusStartup(&m_gdiplusToken, &gdiplusStartupInput, NULL);
} }
ZoneWindow::~ZoneWindow() ZoneWindow::~ZoneWindow()
{ {
RestoreOrginalTransparency(); RestoreOrginalTransparency();
Gdiplus::GdiplusShutdown(gdiplusToken); Gdiplus::GdiplusShutdown(m_gdiplusToken);
} }
bool ZoneWindow::Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, bool flashZones, bool newWorkArea) bool ZoneWindow::Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monitor, const std::wstring& uniqueId, bool flashZones, bool newWorkArea)
@ -347,13 +349,24 @@ bool ZoneWindow::Init(IZoneWindowHost* host, HINSTANCE hinstance, HMONITOR monit
IFACEMETHODIMP ZoneWindow::MoveSizeEnter(HWND window, bool dragEnabled) noexcept IFACEMETHODIMP ZoneWindow::MoveSizeEnter(HWND window, bool dragEnabled) noexcept
{ {
if (m_windowMoveSize) std::shared_lock lock(m_mutex);
auto windowMoveSize = m_windowMoveSize;
auto hostTransparentActive = m_host->isMakeDraggedWindowTransparentActive();
lock.unlock();
if (windowMoveSize)
{ {
return E_INVALIDARG; return E_INVALIDARG;
} }
if (m_host->isMakeDraggedWindowTransparentActive()) if (hostTransparentActive)
{ {
decltype(m_draggedWindowExstyle) draggedWindowExstyle;
decltype(m_draggedWindow) draggedWindow;
decltype(m_draggedWindowCrKey) draggedWindowCrKey;
decltype(m_draggedWindowInitialAlpha) draggedWindowInitialAlpha;
decltype(m_draggedWindowDwFlags) draggedWindowDwFlags;
RestoreOrginalTransparency(); RestoreOrginalTransparency();
draggedWindowExstyle = GetWindowLong(window, GWL_EXSTYLE); draggedWindowExstyle = GetWindowLong(window, GWL_EXSTYLE);
@ -366,27 +379,45 @@ IFACEMETHODIMP ZoneWindow::MoveSizeEnter(HWND window, bool dragEnabled) noexcept
GetLayeredWindowAttributes(window, &draggedWindowCrKey, &draggedWindowInitialAlpha, &draggedWindowDwFlags); GetLayeredWindowAttributes(window, &draggedWindowCrKey, &draggedWindowInitialAlpha, &draggedWindowDwFlags);
SetLayeredWindowAttributes(window, 0, (255 * 50) / 100, LWA_ALPHA); SetLayeredWindowAttributes(window, 0, (255 * 50) / 100, LWA_ALPHA);
std::unique_lock writeLock(m_mutex);
m_draggedWindowExstyle = draggedWindowExstyle;
m_draggedWindow = draggedWindow;
m_draggedWindowCrKey = draggedWindowCrKey;
m_draggedWindowInitialAlpha = draggedWindowInitialAlpha;
m_draggedWindowDwFlags = draggedWindowDwFlags;
} }
m_dragEnabled = dragEnabled; {
m_windowMoveSize = window; std::unique_lock writeLock(m_mutex);
m_drawHints = true; m_dragEnabled = dragEnabled;
m_highlightZone = {}; m_windowMoveSize = window;
m_drawHints = true;
m_highlightZone = {};
}
ShowZoneWindow(); ShowZoneWindow();
return S_OK; return S_OK;
} }
IFACEMETHODIMP ZoneWindow::MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled) noexcept IFACEMETHODIMP ZoneWindow::MoveSizeUpdate(POINT const& ptScreen, bool dragEnabled) noexcept
{ {
std::shared_lock lock(m_mutex);
auto window = m_window.get();
lock.unlock();
bool redraw = false; bool redraw = false;
POINT ptClient = ptScreen; POINT ptClient = ptScreen;
MapWindowPoints(nullptr, m_window.get(), &ptClient, 1); MapWindowPoints(nullptr, window, &ptClient, 1);
std::unique_lock writeLock(m_mutex);
m_dragEnabled = dragEnabled; m_dragEnabled = dragEnabled;
if (dragEnabled) if (dragEnabled)
{ {
writeLock.unlock();
auto highlightZone = ZonesFromPoint(ptClient); auto highlightZone = ZonesFromPoint(ptClient);
writeLock.lock();
redraw = (highlightZone != m_highlightZone); redraw = (highlightZone != m_highlightZone);
m_highlightZone = std::move(highlightZone); m_highlightZone = std::move(highlightZone);
} }
@ -396,9 +427,11 @@ IFACEMETHODIMP ZoneWindow::MoveSizeUpdate(POINT const& ptScreen, bool dragEnable
redraw = true; redraw = true;
} }
writeLock.unlock();
if (redraw) if (redraw)
{ {
InvalidateRect(m_window.get(), nullptr, true); InvalidateRect(window, nullptr, true);
} }
return S_OK; return S_OK;
} }
@ -407,30 +440,74 @@ IFACEMETHODIMP ZoneWindow::MoveSizeEnd(HWND window, POINT const& ptScreen) noexc
{ {
RestoreOrginalTransparency(); RestoreOrginalTransparency();
if (m_windowMoveSize != window) std::shared_lock lock(m_mutex);
auto windowMoveSize = m_windowMoveSize;
auto thisWindow = m_window.get();
auto activeZoneSet = m_activeZoneSet;
lock.unlock();
if (windowMoveSize != window)
{ {
return E_INVALIDARG; return E_INVALIDARG;
} }
if (m_activeZoneSet) if (activeZoneSet)
{ {
POINT ptClient = ptScreen; POINT ptClient = ptScreen;
MapWindowPoints(nullptr, m_window.get(), &ptClient, 1); MapWindowPoints(nullptr, thisWindow, &ptClient, 1);
m_activeZoneSet->MoveWindowIntoZoneByPoint(window, m_window.get(), ptClient); activeZoneSet->MoveWindowIntoZoneByPoint(window, thisWindow, ptClient);
SaveWindowProcessToZoneIndex(window); SaveWindowProcessToZoneIndex(window);
} }
Trace::ZoneWindow::MoveSizeEnd(m_activeZoneSet); Trace::ZoneWindow::MoveSizeEnd(activeZoneSet);
HideZoneWindow(); HideZoneWindow();
std::unique_lock writeLock(m_mutex);
m_windowMoveSize = nullptr; m_windowMoveSize = nullptr;
return S_OK; return S_OK;
} }
IFACEMETHODIMP_(bool)
ZoneWindow::IsDragEnabled() noexcept
{
std::shared_lock lock(m_mutex);
return m_dragEnabled;
}
IFACEMETHODIMP_(std::wstring)
ZoneWindow::UniqueId() noexcept
{
std::shared_lock lock(m_mutex);
return m_uniqueId;
}
IFACEMETHODIMP_(std::wstring)
ZoneWindow::WorkAreaKey() noexcept
{
std::shared_lock lock(m_mutex);
return m_workArea;
}
IFACEMETHODIMP_(IZoneSet*)
ZoneWindow::ActiveZoneSet() noexcept
{
std::shared_lock lock(m_mutex);
return m_activeZoneSet.get();
}
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ZoneWindow::RestoreOrginalTransparency() noexcept ZoneWindow::RestoreOrginalTransparency() noexcept
{ {
if (m_host->isMakeDraggedWindowTransparentActive() && draggedWindow != nullptr) std::shared_lock lock(m_mutex);
auto hostTransparentActive = m_host->isMakeDraggedWindowTransparentActive();
auto draggedWindow = m_draggedWindow;
auto draggedWindowCrKey = m_draggedWindowCrKey;
auto draggedWindowInitialAlpha = m_draggedWindowInitialAlpha;
auto draggedWindowDwFlags = m_draggedWindowDwFlags;
auto draggedWindowExstyle = m_draggedWindowExstyle;
lock.unlock();
if (hostTransparentActive && draggedWindow != nullptr)
{ {
SetLayeredWindowAttributes(draggedWindow, draggedWindowCrKey, draggedWindowInitialAlpha, draggedWindowDwFlags); SetLayeredWindowAttributes(draggedWindow, draggedWindowCrKey, draggedWindowInitialAlpha, draggedWindowDwFlags);
SetWindowLong(draggedWindow, GWL_EXSTYLE, draggedWindowExstyle); SetWindowLong(draggedWindow, GWL_EXSTYLE, draggedWindowExstyle);
@ -447,18 +524,28 @@ ZoneWindow::MoveWindowIntoZoneByIndex(HWND window, int index) noexcept
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ZoneWindow::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<int>& indexSet) noexcept ZoneWindow::MoveWindowIntoZoneByIndexSet(HWND window, const std::vector<int>& indexSet) noexcept
{ {
if (m_activeZoneSet) std::shared_lock lock(m_mutex);
auto thisWindow = m_window.get();
auto activeZoneSet = m_activeZoneSet;
lock.unlock();
if (activeZoneSet)
{ {
m_activeZoneSet->MoveWindowIntoZoneByIndexSet(window, m_window.get(), indexSet, false); activeZoneSet->MoveWindowIntoZoneByIndexSet(window, thisWindow, indexSet, false);
} }
} }
IFACEMETHODIMP_(bool) IFACEMETHODIMP_(bool)
ZoneWindow::MoveWindowIntoZoneByDirection(HWND window, DWORD vkCode, bool cycle) noexcept ZoneWindow::MoveWindowIntoZoneByDirection(HWND window, DWORD vkCode, bool cycle) noexcept
{ {
if (m_activeZoneSet) std::shared_lock lock(m_mutex);
auto thisWindow = m_window.get();
auto activeZoneSet = m_activeZoneSet;
lock.unlock();
if (activeZoneSet)
{ {
if (m_activeZoneSet->MoveWindowIntoZoneByDirection(window, m_window.get(), vkCode, cycle)) if (activeZoneSet->MoveWindowIntoZoneByDirection(window, thisWindow, vkCode, cycle))
{ {
SaveWindowProcessToZoneIndex(window); SaveWindowProcessToZoneIndex(window);
return true; return true;
@ -472,9 +559,14 @@ ZoneWindow::CycleActiveZoneSet(DWORD wparam) noexcept
{ {
CycleActiveZoneSetInternal(wparam, Trace::ZoneWindow::InputMode::Keyboard); CycleActiveZoneSetInternal(wparam, Trace::ZoneWindow::InputMode::Keyboard);
if (m_windowMoveSize) std::shared_lock lock(m_mutex);
auto windowMoveSize = m_windowMoveSize;
auto window = m_window.get();
lock.unlock();
if (windowMoveSize)
{ {
InvalidateRect(m_window.get(), nullptr, true); InvalidateRect(window, nullptr, true);
} }
else else
{ {
@ -485,15 +577,19 @@ ZoneWindow::CycleActiveZoneSet(DWORD wparam) noexcept
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ZoneWindow::SaveWindowProcessToZoneIndex(HWND window) noexcept ZoneWindow::SaveWindowProcessToZoneIndex(HWND window) noexcept
{ {
if (m_activeZoneSet) std::shared_lock lock(m_mutex);
auto activeZoneSet = m_activeZoneSet;
lock.unlock();
if (activeZoneSet)
{ {
DWORD zoneIndex = static_cast<DWORD>(m_activeZoneSet->GetZoneIndexFromWindow(window)); DWORD zoneIndex = static_cast<DWORD>(activeZoneSet->GetZoneIndexFromWindow(window));
if (zoneIndex != -1) if (zoneIndex != -1)
{ {
OLECHAR* guidString; OLECHAR* guidString;
if (StringFromCLSID(m_activeZoneSet->Id(), &guidString) == S_OK) if (StringFromCLSID(activeZoneSet->Id(), &guidString) == S_OK)
{ {
JSONHelpers::FancyZonesDataInstance().SetAppLastZone(window, m_uniqueId, guidString, zoneIndex); JSONHelpers::FancyZonesDataInstance().SetAppLastZone(window, UniqueId(), guidString, zoneIndex);
} }
CoTaskMemFree(guidString); CoTaskMemFree(guidString);
@ -504,31 +600,43 @@ ZoneWindow::SaveWindowProcessToZoneIndex(HWND window) noexcept
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ZoneWindow::ShowZoneWindow() noexcept ZoneWindow::ShowZoneWindow() noexcept
{ {
if (m_window) std::shared_lock lock(m_mutex);
auto window = m_window.get();
HWND windowInsertAfter = m_windowMoveSize;
lock.unlock();
if (window)
{ {
m_flashMode = false; {
std::unique_lock writeLock(m_mutex);
m_flashMode = false;
}
UINT flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE; UINT flags = SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE;
HWND windowInsertAfter = m_windowMoveSize;
if (windowInsertAfter == nullptr) if (windowInsertAfter == nullptr)
{ {
windowInsertAfter = HWND_TOPMOST; windowInsertAfter = HWND_TOPMOST;
} }
SetWindowPos(m_window.get(), windowInsertAfter, 0, 0, 0, 0, flags); SetWindowPos(window, windowInsertAfter, 0, 0, 0, 0, flags);
AnimateWindow(m_window.get(), m_showAnimationDuration, AW_BLEND); AnimateWindow(window, m_showAnimationDuration, AW_BLEND);
InvalidateRect(m_window.get(), nullptr, true); InvalidateRect(window, nullptr, true);
} }
} }
IFACEMETHODIMP_(void) IFACEMETHODIMP_(void)
ZoneWindow::HideZoneWindow() noexcept ZoneWindow::HideZoneWindow() noexcept
{ {
if (m_window) std::shared_lock lock(m_mutex);
auto window = m_window.get();
lock.unlock();
if (window)
{ {
ShowWindow(m_window.get(), SW_HIDE); ShowWindow(window, SW_HIDE);
std::unique_lock writeLock(m_mutex);
m_keyLast = 0; m_keyLast = 0;
m_windowMoveSize = nullptr; m_windowMoveSize = nullptr;
m_drawHints = false; m_drawHints = false;
@ -540,27 +648,34 @@ ZoneWindow::HideZoneWindow() noexcept
void ZoneWindow::LoadSettings() noexcept void ZoneWindow::LoadSettings() noexcept
{ {
JSONHelpers::FancyZonesDataInstance().AddDevice(m_uniqueId); JSONHelpers::FancyZonesDataInstance().AddDevice(UniqueId());
} }
void ZoneWindow::InitializeZoneSets(bool newWorkArea) noexcept void ZoneWindow::InitializeZoneSets(bool newWorkArea) noexcept
{ {
std::shared_lock lock(m_mutex);
auto parent = m_host->GetParentZoneWindow(m_monitor); auto parent = m_host->GetParentZoneWindow(m_monitor);
lock.unlock();
if (newWorkArea && parent) if (newWorkArea && parent)
{ {
// Update device info with device info from parent virtual desktop (if empty). // Update device info with device info from parent virtual desktop (if empty).
JSONHelpers::FancyZonesDataInstance().CloneDeviceInfo(parent->UniqueId(), m_uniqueId); JSONHelpers::FancyZonesDataInstance().CloneDeviceInfo(parent->UniqueId(), UniqueId());
} }
CalculateZoneSet(); CalculateZoneSet();
} }
void ZoneWindow::CalculateZoneSet() noexcept void ZoneWindow::CalculateZoneSet() noexcept
{ {
std::unique_lock lock(m_mutex);
auto monitor = m_monitor;
lock.unlock();
const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance(); const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance();
const auto deviceInfoData = fancyZonesData.FindDeviceInfo(m_uniqueId); const auto deviceInfoData = fancyZonesData.FindDeviceInfo(UniqueId());
const auto& activeDeviceId = fancyZonesData.GetActiveDeviceId(); const auto& activeDeviceId = fancyZonesData.GetActiveDeviceId();
if (!activeDeviceId.empty() && activeDeviceId != m_uniqueId) if (!activeDeviceId.empty() && activeDeviceId != UniqueId())
{ {
return; return;
} }
@ -583,11 +698,11 @@ void ZoneWindow::CalculateZoneSet() noexcept
auto zoneSet = MakeZoneSet(ZoneSetConfig( auto zoneSet = MakeZoneSet(ZoneSetConfig(
zoneSetId, zoneSetId,
activeZoneSet.type, activeZoneSet.type,
m_monitor, monitor,
m_workArea)); WorkAreaKey().c_str()));
MONITORINFO monitorInfo{}; MONITORINFO monitorInfo{};
monitorInfo.cbSize = sizeof(monitorInfo); monitorInfo.cbSize = sizeof(monitorInfo);
if (GetMonitorInfoW(m_monitor, &monitorInfo)) if (GetMonitorInfoW(monitor, &monitorInfo))
{ {
bool showSpacing = deviceInfoData->showSpacing; bool showSpacing = deviceInfoData->showSpacing;
int spacing = showSpacing ? deviceInfoData->spacing : 0; int spacing = showSpacing ? deviceInfoData->spacing : 0;
@ -600,30 +715,37 @@ void ZoneWindow::CalculateZoneSet() noexcept
void ZoneWindow::UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept void ZoneWindow::UpdateActiveZoneSet(_In_opt_ IZoneSet* zoneSet) noexcept
{ {
m_activeZoneSet.copy_from(zoneSet); {
std::unique_lock writeLock(m_mutex);
m_activeZoneSet.copy_from(zoneSet);
}
if (m_activeZoneSet) if (zoneSet)
{ {
wil::unique_cotaskmem_string zoneSetId; wil::unique_cotaskmem_string zoneSetId;
if (SUCCEEDED_LOG(StringFromCLSID(m_activeZoneSet->Id(), &zoneSetId))) if (SUCCEEDED_LOG(StringFromCLSID(zoneSet->Id(), &zoneSetId)))
{ {
JSONHelpers::ZoneSetData data{ JSONHelpers::ZoneSetData data{
.uuid = zoneSetId.get(), .uuid = zoneSetId.get(),
.type = m_activeZoneSet->LayoutType() .type = zoneSet->LayoutType()
}; };
JSONHelpers::FancyZonesDataInstance().SetActiveZoneSet(m_uniqueId, data); JSONHelpers::FancyZonesDataInstance().SetActiveZoneSet(UniqueId(), data);
} }
} }
} }
LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
{ {
std::shared_lock lock(m_mutex);
auto window = m_window.get();
lock.unlock();
switch (message) switch (message)
{ {
case WM_NCDESTROY: case WM_NCDESTROY:
{ {
::DefWindowProc(m_window.get(), message, wparam, lparam); ::DefWindowProc(window, message, wparam, lparam);
SetWindowLongPtr(m_window.get(), GWLP_USERDATA, 0); SetWindowLongPtr(window, GWLP_USERDATA, 0);
} }
break; break;
@ -637,14 +759,14 @@ LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
wil::unique_hdc hdc{ reinterpret_cast<HDC>(wparam) }; wil::unique_hdc hdc{ reinterpret_cast<HDC>(wparam) };
if (!hdc) if (!hdc)
{ {
hdc.reset(BeginPaint(m_window.get(), &ps)); hdc.reset(BeginPaint(window, &ps));
} }
OnPaint(hdc); OnPaint(hdc);
if (wparam == 0) if (wparam == 0)
{ {
EndPaint(m_window.get(), &ps); EndPaint(window, &ps);
} }
hdc.release(); hdc.release();
@ -653,7 +775,7 @@ LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
default: default:
{ {
return DefWindowProc(m_window.get(), message, wparam, lparam); return DefWindowProc(window, message, wparam, lparam);
} }
} }
return 0; return 0;
@ -661,8 +783,17 @@ LRESULT ZoneWindow::WndProc(UINT message, WPARAM wparam, LPARAM lparam) noexcept
void ZoneWindow::OnPaint(wil::unique_hdc& hdc) noexcept void ZoneWindow::OnPaint(wil::unique_hdc& hdc) noexcept
{ {
std::shared_lock lock(m_mutex);
auto host = m_host;
auto highlightZone = m_highlightZone;
auto flashMode = m_flashMode;
auto drawHints = m_drawHints;
HWND window = m_window.get();
auto activeZoneSet = m_activeZoneSet;
lock.unlock();
RECT clientRect; RECT clientRect;
GetClientRect(m_window.get(), &clientRect); GetClientRect(window, &clientRect);
wil::unique_hdc hdcMem; wil::unique_hdc hdcMem;
HPAINTBUFFER bufferedPaint = BeginBufferedPaint(hdc.get(), &clientRect, BPBF_TOPDOWNDIB, nullptr, &hdcMem); HPAINTBUFFER bufferedPaint = BeginBufferedPaint(hdc.get(), &clientRect, BPBF_TOPDOWNDIB, nullptr, &hdcMem);
@ -670,17 +801,17 @@ void ZoneWindow::OnPaint(wil::unique_hdc& hdc) noexcept
{ {
ZoneWindowDrawUtils::DrawBackdrop(hdcMem, clientRect); ZoneWindowDrawUtils::DrawBackdrop(hdcMem, clientRect);
if (m_activeZoneSet && m_host) if (activeZoneSet && host)
{ {
ZoneWindowDrawUtils::DrawActiveZoneSet(hdcMem, ZoneWindowDrawUtils::DrawActiveZoneSet(hdcMem,
m_host->GetZoneColor(), host->GetZoneColor(),
m_host->GetZoneBorderColor(), host->GetZoneBorderColor(),
m_host->GetZoneHighlightColor(), host->GetZoneHighlightColor(),
m_host->GetZoneHighlightOpacity(), host->GetZoneHighlightOpacity(),
m_activeZoneSet->GetZones(), activeZoneSet->GetZones(),
m_highlightZone, highlightZone,
m_flashMode, flashMode,
m_drawHints); drawHints);
} }
EndBufferedPaint(bufferedPaint, TRUE); EndBufferedPaint(bufferedPaint, TRUE);
@ -689,43 +820,56 @@ void ZoneWindow::OnPaint(wil::unique_hdc& hdc) noexcept
void ZoneWindow::OnKeyUp(WPARAM wparam) noexcept void ZoneWindow::OnKeyUp(WPARAM wparam) noexcept
{ {
std::shared_lock lock(m_mutex);
auto window = m_window.get();
lock.unlock();
bool fRedraw = false; bool fRedraw = false;
Trace::ZoneWindow::KeyUp(wparam); Trace::ZoneWindow::KeyUp(wparam);
if ((wparam >= '0') && (wparam <= '9')) if ((wparam >= '0') && (wparam <= '9'))
{ {
CycleActiveZoneSetInternal(static_cast<DWORD>(wparam), Trace::ZoneWindow::InputMode::Keyboard); CycleActiveZoneSetInternal(static_cast<DWORD>(wparam), Trace::ZoneWindow::InputMode::Keyboard);
InvalidateRect(m_window.get(), nullptr, true); InvalidateRect(window, nullptr, true);
} }
} }
std::vector<int> ZoneWindow::ZonesFromPoint(POINT pt) noexcept std::vector<int> ZoneWindow::ZonesFromPoint(POINT pt) noexcept
{ {
if (m_activeZoneSet) auto activeZoneSet = ActiveZoneSet();
if (activeZoneSet)
{ {
return m_activeZoneSet->ZonesFromPoint(pt); return activeZoneSet->ZonesFromPoint(pt);
} }
return {}; return {};
} }
void ZoneWindow::CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept void ZoneWindow::CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::InputMode mode) noexcept
{ {
Trace::ZoneWindow::CycleActiveZoneSet(m_activeZoneSet, mode); std::shared_lock lock(m_mutex);
if (m_keyLast != wparam) auto activeZoneSet = m_activeZoneSet;
auto keyLast = m_keyLast;
auto keyCycle = m_keyCycle;
auto zoneSets = m_zoneSets;
auto host = m_host;
lock.unlock();
Trace::ZoneWindow::CycleActiveZoneSet(activeZoneSet, mode);
if (keyLast != wparam)
{ {
m_keyCycle = 0; keyCycle = 0;
} }
m_keyLast = wparam; keyLast = wparam;
bool loopAround = true; bool loopAround = true;
size_t const val = static_cast<size_t>(wparam - L'0'); size_t const val = static_cast<size_t>(wparam - L'0');
size_t i = 0; size_t i = 0;
for (auto zoneSet : m_zoneSets) for (auto zoneSet : zoneSets)
{ {
if (zoneSet->GetZones().size() == val) if (zoneSet->GetZones().size() == val)
{ {
if (i < m_keyCycle) if (i < keyCycle)
{ {
i++; i++;
} }
@ -738,21 +882,25 @@ void ZoneWindow::CycleActiveZoneSetInternal(DWORD wparam, Trace::ZoneWindow::Inp
} }
} }
if ((m_keyCycle > 0) && loopAround) if ((keyCycle > 0) && loopAround)
{ {
// Cycling through a non-empty group and hit the end // Cycling through a non-empty group and hit the end
m_keyCycle = 0; keyCycle = 0;
OnKeyUp(wparam); OnKeyUp(wparam);
} }
else else
{ {
m_keyCycle++; keyCycle++;
} }
if (m_host) if (host)
{ {
m_host->MoveWindowsOnActiveZoneSetChange(); host->MoveWindowsOnActiveZoneSetChange();
} }
std::unique_lock writeLock(m_mutex);
m_keyLast = keyLast;
m_keyCycle = keyCycle;
m_highlightZone = {}; m_highlightZone = {};
} }
@ -764,10 +912,13 @@ void ZoneWindow::FlashZones() noexcept
return; return;
} }
std::unique_lock writeLock(m_mutex);
m_flashMode = true; m_flashMode = true;
auto window = m_window.get();
writeLock.unlock();
ShowWindow(m_window.get(), SW_SHOWNA); ShowWindow(window, SW_SHOWNA);
std::thread([window = m_window.get()]() { std::thread([window]() {
AnimateWindow(window, m_flashDuration, AW_HIDE | AW_BLEND); AnimateWindow(window, m_flashDuration, AW_HIDE | AW_BLEND);
}).detach(); }).detach();
} }

View File

@ -1,4 +1,4 @@
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.Appium; using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows; using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions; using OpenQA.Selenium.Interactions;
@ -161,23 +161,67 @@ namespace PowerToysTests
public void CreateSplitter() public void CreateSplitter()
{ {
OpenCreatorWindow("Columns", "Custom table layout creator", "EditTemplateButton"); OpenCreatorWindow("Columns", "Custom table layout creator", "EditTemplateButton");
WindowsElement gridEditor = session.FindElementByClassName("GridEditor"); WaitSeconds(2);
Assert.IsNotNull(gridEditor);
ReadOnlyCollection<AppiumWebElement> thumbs = gridEditor.FindElementsByClassName("Thumb");
Assert.AreEqual(3, session.FindElementsByClassName("GridZone").Count);
Assert.AreEqual(2, thumbs.Count);
new Actions(session).MoveToElement(thumbs[0]).MoveByOffset(-30, 0).Click().Perform();
Assert.AreEqual(3, gridEditor.FindElementsByClassName("Thumb").Count);
ReadOnlyCollection<WindowsElement> zones = session.FindElementsByClassName("GridZone"); ReadOnlyCollection<WindowsElement> zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(3, zones.Count, "Zones count invalid");
const int defaultSpacing = 16;
int splitPos = zones[0].Rect.Y + zones[0].Rect.Height / 2;
new Actions(session).MoveToElement(zones[0]).Click().Perform();
zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(4, zones.Count); Assert.AreEqual(4, zones.Count);
//check that zone was splitted horizontally //check splitted zone
Assert.AreNotEqual(zones[0].Rect.Height, zones[1].Rect.Height); Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.AreNotEqual(zones[3].Rect.Height, zones[1].Rect.Height); Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - splitPos + defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[1].Rect.Height, zones[2].Rect.Height); Assert.IsTrue(Math.Abs(zones[3].Rect.Top - splitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[3].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);
}
[TestMethod]
public void TestSplitterShiftAfterCreation()
{
OpenCreatorWindow("Columns", "Custom table layout creator", "EditTemplateButton");
WaitSeconds(2);
ReadOnlyCollection<WindowsElement> zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(3, zones.Count, "Zones count invalid");
const int defaultSpacing = 16;
//create first split
int firstSplitPos = zones[0].Rect.Y + zones[0].Rect.Height / 4;
new Actions(session).MoveToElement(zones[0]).MoveByOffset(0, -(zones[0].Rect.Height / 4)).Click().Perform();
zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(4, zones.Count);
Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - firstSplitPos + defaultSpacing / 2) <= 2);
Assert.IsTrue(Math.Abs(zones[3].Rect.Top - firstSplitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[3].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);
//create second split
int secondSplitPos = zones[3].Rect.Y + zones[3].Rect.Height / 2;
int expectedTop = zones[3].Rect.Top;
new Actions(session).MoveToElement(zones[3]).Click().Perform();
zones = session.FindElementsByClassName("GridZone");
Assert.AreEqual(5, zones.Count);
//check first split on same position
Assert.AreEqual(zones[0].Rect.Top, defaultSpacing);
Assert.IsTrue(Math.Abs(zones[0].Rect.Bottom - firstSplitPos + defaultSpacing / 2) <= 2);
//check second split
Assert.AreEqual(zones[3].Rect.Top, expectedTop);
Assert.IsTrue(Math.Abs(zones[3].Rect.Bottom - secondSplitPos + defaultSpacing / 2) <= 2);
Assert.IsTrue(Math.Abs(zones[4].Rect.Top - secondSplitPos - defaultSpacing / 2) <= 2);
Assert.AreEqual(zones[4].Rect.Bottom, Screen.PrimaryScreen.Bounds.Bottom - defaultSpacing);
} }
[TestMethod] [TestMethod]

View File

@ -17,9 +17,11 @@ namespace PowerToysTests
protected static void OpenEditor() protected static void OpenEditor()
{ {
new Actions(session).KeyDown(OpenQA.Selenium.Keys.Command).SendKeys("`").KeyUp(OpenQA.Selenium.Keys.Command).Perform(); new Actions(session).KeyDown(OpenQA.Selenium.Keys.Command).SendKeys("`").KeyUp(OpenQA.Selenium.Keys.Command).Perform();
WaitSeconds(2);
//editorWindow = WaitElementByXPath("//Window[@Name=\"FancyZones Editor\"]"); //editorWindow = WaitElementByXPath("//Window[@Name=\"FancyZones Editor\"]");
editorWindow = WaitElementByName("FancyZones Editor");
//may not find editor by name in 0.16.1 //may not find editor by name in 0.16.1
editorWindow = WaitElementByAccessibilityId("MainWindow1"); //editorWindow = WaitElementByAccessibilityId("MainWindow1");
Assert.IsNotNull(editorWindow, "Couldn't find editor window"); Assert.IsNotNull(editorWindow, "Couldn't find editor window");
} }
@ -57,10 +59,10 @@ namespace PowerToysTests
protected static void OpenCreatorWindow(string tabName, string creatorWindowName, string buttonId = "EditCustomButton") protected static void OpenCreatorWindow(string tabName, string creatorWindowName, string buttonId = "EditCustomButton")
{ {
string elementXPath = "//Text[@Name=\"" + tabName + "\"]"; string elementXPath = "//Text[@Name=\"" + tabName + "\"]";
session.FindElementByXPath(elementXPath).Click(); WaitElementByXPath(elementXPath).Click();
session.FindElementByAccessibilityId(buttonId).Click(); WaitElementByAccessibilityId(buttonId).Click();
WindowsElement creatorWindow = session.FindElementByName(creatorWindowName); WindowsElement creatorWindow = WaitElementByName(creatorWindowName);
Assert.IsNotNull(creatorWindow, "Creator window didn't open"); Assert.IsNotNull(creatorWindow, "Creator window didn't open");
} }