[New PowerToy] Add Screen Ruler module for measuring screen contents (#19701)

* [MeasureTool] initial commit

* [chore] clean up needless WindowsTargetPlatformVersion overrides from projects

* [MeasureTool] initial implementation

* Fix build errors

* Update vsconfig for needed Windows 10 SDK versions

* fix spellchecker

* another spellcheck fix

* more spellcheck errors

* Fix measurement being off by 1 on both ends

* UI fixes

* Add feet to crosses

* Remove anti-aliasing, as it's creating artifacts

* Use pixel tolerance from settings

* Tooltip updates

* Restore antialiasing to draw the tooltip

* remove comment for spell check

* Updated icons

* Icon updates

* Improve measurement accuracy and display

* Fix spellchecker

* Add less precise drawing on continuous warning

* Add setting for turning cross feet on

* Swap LMB/RMB for interaction

* Uncheck active tool's RadioButton when it exits

* activation hotkey toggles UI instead of just launching it

* track runner process and exit when it exits

* add proj ref

* toolbar is interactive during measurements

* always open toolbar on the main display

* refactor colors

* refactor edge detection & overlay ui

* refactor overlay ui even more

* simplify state structs

* multimonitor preparation: eliminate global state

* prepare for merge

* spelling

* proper thread termination + minor fixes

* multimonitor: launch tools on all monitors

* multimonitor support: track cursor position

* spell

* fix powertoys!

* ScreenSize -> Box

* add shadow effect for textbox

* spell

* fix debug mode

* dynamic text box size based on text layout metrics

* add mouse wheel to adjust pixel tolerance + per channel detection algorithm setting

* spelling

* fix per channel distance calculations

* update installer deps + spelling

* tool activation telemetry

* update assets and try to fix build

* use × instead of x

* allow multiple measurements with bounds tool with shift-click

* move #define DEBUG_OVERLAY in an appropriate space

* spell-checked

* update issue template + refactor text box drawing

* implement custom renderer and make × semiopaque

* spelling

* pass dpiScale to x renderer

* add sse2neon license

* update OOBE

* move license to NOTICE

* appropriate module preview image

* localization for AutomationPeer

* increase default pixel tolerance from 5 to 30

* add PowerToys.MeasureToolUI.exe to bugreport

* explicitly set texture dims

* clarify continuous capture description

* fix a real spelling error!

* cleanup

* clean up x2

* debug texture

* fix texture access

* fix saveasbitmap

* improve sum of all channel diffs method score calc

* optimize

* ContinuousCapture is enabled by default to avoid confusion

* build fix

* draw captured screen in a non continuous mode

* cast a spell...

* merge fix

* disable stroboscopic effect

* split global/perScreen measure state and minor improvements

* spelling

* fix comment

* primary monitor debug also active for the bounds tool

* dpi from rt for custom renderer

* add comment

* fix off by 1

* make backround convertion success for non continuous mode non-essential

* fix spelling

* overlay window covers taskbar

* fix CI

* revert taskbar covering

* fix CI

* fix ci again

* fix 2

* fix ci

* CI fix

* fix arm ci

* cleanup cursor convertion between coordinate spaces

* fix spelling

* Fix signing

* Fix MeasureToolUI version

* Fix core version

* fix race condition in system internals which happens during concurrent d3d/d2d resource creation

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
Co-authored-by: Niels Laute <niels.laute@live.nl>
This commit is contained in:
Andrey Nekrasov
2022-08-27 02:17:20 +03:00
committed by GitHub
parent 2274e0c67d
commit 78d65a87cd
114 changed files with 5319 additions and 337 deletions

View File

@@ -2,72 +2,53 @@
#include <algorithm>
namespace
Box MonitorInfo::GetScreenSize(const bool includeNonWorkingArea) const
{
// TODO: use compare
bool operator<(const RECT& lhs, const RECT& rhs)
{
auto lhs_tuple = std::make_tuple(lhs.left, lhs.right, lhs.top, lhs.bottom);
auto rhs_tuple = std::make_tuple(rhs.left, rhs.right, rhs.top, rhs.bottom);
return lhs_tuple < rhs_tuple;
}
return includeNonWorkingArea ? Box{ info.rcMonitor } : Box{ info.rcWork };
}
bool operator==(const ScreenSize& lhs, const ScreenSize& rhs)
bool MonitorInfo::IsPrimary() const
{
auto lhs_tuple = std::make_tuple(lhs.rect.left, lhs.rect.right, lhs.rect.top, lhs.rect.bottom);
auto rhs_tuple = std::make_tuple(rhs.rect.left, rhs.rect.right, rhs.rect.top, rhs.rect.bottom);
return lhs_tuple == rhs_tuple;
return static_cast<bool>(info.dwFlags & MONITORINFOF_PRIMARY);
}
MonitorInfo::MonitorInfo(HMONITOR h) :
handle{ h }
{
info.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfoW(handle, &info);
}
static BOOL CALLBACK GetDisplaysEnumCb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
if (GetMonitorInfo(monitor, &monitorInfo))
{
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitorInfo.rcWork);
}
return true;
};
static BOOL CALLBACK GetDisplaysEnumCbWithNonWorkingArea(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
if (GetMonitorInfo(monitor, &monitorInfo))
{
reinterpret_cast<std::vector<MonitorInfo>*>(data)->emplace_back(monitor, monitorInfo.rcMonitor);
}
auto* monitors = reinterpret_cast<std::vector<MonitorInfo>*>(data);
monitors->emplace_back(monitor);
return true;
};
std::vector<MonitorInfo> MonitorInfo::GetMonitors(bool includeNonWorkingArea)
{
std::vector<MonitorInfo> monitors;
EnumDisplayMonitors(NULL, NULL, includeNonWorkingArea ? GetDisplaysEnumCbWithNonWorkingArea : GetDisplaysEnumCb, reinterpret_cast<LPARAM>(&monitors));
std::sort(begin(monitors), end(monitors), [](const MonitorInfo& lhs, const MonitorInfo& rhs) {
return lhs.rect < rhs.rect;
EnumDisplayMonitors(nullptr, nullptr, GetDisplaysEnumCb, reinterpret_cast<LPARAM>(&monitors));
std::sort(begin(monitors), end(monitors), [=](const MonitorInfo& lhs, const MonitorInfo& rhs) {
const auto lhsSize = lhs.GetScreenSize(includeNonWorkingArea);
const auto rhsSize = rhs.GetScreenSize(includeNonWorkingArea);
return lhsSize < rhsSize;
});
return monitors;
}
static BOOL CALLBACK GetPrimaryDisplayEnumCb(HMONITOR monitor, HDC hdc, LPRECT rect, LPARAM data)
{
MONITORINFOEX monitorInfo;
monitorInfo.cbSize = sizeof(MONITORINFOEX);
if (GetMonitorInfo(monitor, &monitorInfo) && (monitorInfo.dwFlags & MONITORINFOF_PRIMARY))
{
reinterpret_cast<MonitorInfo*>(data)->handle = monitor;
reinterpret_cast<MonitorInfo*>(data)->rect = monitorInfo.rcWork;
}
return true;
};
MonitorInfo MonitorInfo::GetPrimaryMonitor()
{
MonitorInfo primary({}, {});
EnumDisplayMonitors(NULL, NULL, GetPrimaryDisplayEnumCb, reinterpret_cast<LPARAM>(&primary));
return primary;
auto monitors = MonitorInfo::GetMonitors(false);
if (monitors.size() > 1)
{
for (auto monitor : monitors)
{
if (monitor.IsPrimary())
return monitor;
}
}
return monitors[0];
}

View File

@@ -1,12 +1,20 @@
#pragma once
#include <Windows.h>
#include <compare>
#include <optional>
#include <vector>
struct ScreenSize
// TODO: merge with FZ::Rect
struct Box
{
explicit ScreenSize(RECT rect) :
rect(rect) {}
RECT rect;
explicit Box(RECT rect = {}) :
rect(rect) {}
Box(const Box&) = default;
Box& operator=(const Box&) = default;
int left() const { return rect.left; }
int right() const { return rect.right; }
int top() const { return rect.top; }
@@ -22,17 +30,31 @@ struct ScreenSize
POINT bottom_left() const { return { rect.left, rect.bottom }; };
POINT bottom_middle() const { return { rect.left + width() / 2, rect.bottom }; };
POINT bottom_right() const { return { rect.right, rect.bottom }; };
inline bool inside(const POINT point) const { return PtInRect(&rect, point); }
inline friend auto operator<=>(const Box& lhs, const Box& rhs)
{
auto lhs_tuple = std::make_tuple(lhs.rect.left, lhs.rect.right, lhs.rect.top, lhs.rect.bottom);
auto rhs_tuple = std::make_tuple(rhs.rect.left, rhs.rect.right, rhs.rect.top, rhs.rect.bottom);
return lhs_tuple <=> rhs_tuple;
}
};
struct MonitorInfo : ScreenSize
class MonitorInfo
{
explicit MonitorInfo(HMONITOR monitor, RECT rect) :
handle(monitor), ScreenSize(rect) {}
HMONITOR handle;
MONITORINFOEX info = {};
public:
explicit MonitorInfo(HMONITOR h);
inline HMONITOR GetHandle() const
{
return handle;
}
Box GetScreenSize(const bool includeNonWorkingArea) const;
bool IsPrimary() const;
// Returns monitor rects ordered from left to right
static std::vector<MonitorInfo> GetMonitors(bool includeNonWorkingArea);
static MonitorInfo GetPrimaryMonitor();
};
bool operator==(const ScreenSize& lhs, const ScreenSize& rhs);

View File

@@ -0,0 +1,30 @@
#pragma once
#include <functional>
#include <shared_mutex>
template<typename StateT>
class Serialized
{
mutable std::shared_mutex m;
StateT s;
public:
void Read(std::function<void(const StateT&)> fn) const
{
std::shared_lock lock{ m };
fn(s);
}
void Access(std::function<void(StateT&)> fn)
{
std::unique_lock lock{ m };
fn(s);
}
void Reset()
{
std::unique_lock lock{ m };
s = {};
}
};

View File

@@ -25,7 +25,7 @@ inline std::optional<std::wstring> get_last_error_message(const DWORD dw)
inline std::wstring get_last_error_or_default(const DWORD dw)
{
auto message = get_last_error_message(dw);
return message.has_value() ? message.value() : L"";
return message.has_value() ? *message : L"";
}
inline void show_last_error_message(const wchar_t* functionName, DWORD dw, const wchar_t* errorTitle)

View File

@@ -8,24 +8,24 @@
#include <optional>
// Initializes and runs windows message loop
inline int run_message_loop(const bool until_idle = false, const std::optional<uint32_t> timeout_seconds = {})
inline int run_message_loop(const bool until_idle = false, const std::optional<uint32_t> timeout_ms = {})
{
MSG msg{};
bool stop = false;
UINT_PTR timerId = 0;
if (timeout_seconds.has_value())
if (timeout_ms.has_value())
{
timerId = SetTimer(nullptr, 0, *timeout_seconds * 1000, nullptr);
timerId = SetTimer(nullptr, 0, *timeout_ms, nullptr);
}
while (!stop && GetMessageW(&msg, nullptr, 0, 0))
while (!stop && (until_idle ? PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE) : GetMessageW(&msg, nullptr, 0, 0)))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
stop = until_idle && !PeekMessageW(&msg, nullptr, 0, 0, PM_NOREMOVE);
stop = stop || (msg.message == WM_TIMER && msg.wParam == timerId);
}
if (timeout_seconds.has_value())
if (timeout_ms.has_value())
{
KillTimer(nullptr, timerId);
}
@@ -55,3 +55,24 @@ inline bool is_system_window(HWND hwnd, const char* class_name)
}
return false;
}
template<typename T>
inline T GetWindowCreateParam(LPARAM lparam)
{
static_assert(sizeof(T) <= sizeof(void*));
T data{ (T)(reinterpret_cast<CREATESTRUCT*>(lparam)->lpCreateParams) };
return data;
}
template<typename T>
inline void StoreWindowParam(HWND window, T data)
{
static_assert(sizeof(T) <= sizeof(void*));
SetWindowLongPtrW(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(data));
}
template<typename T>
inline T GetWindowParam(HWND window)
{
return (T)GetWindowLongPtrW(window, GWLP_USERDATA);
}