mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-03 09:46:54 +02:00
common: refactor common library pt2 (#8588)
- remove common lib - split settings, remove common-md - move ipc interop/kb_layout to interop - rename core -> settings, settings -> old_settings - os-detect header-only; interop -> PowerToysInterop - split notifications, move single-use headers where they're used - winstore lib - rename com utils - rename Updating and Telemetry projects - rename core -> settings-ui and remove examples folder - rename settings-ui folder + consisent common/version include
This commit is contained in:
44
src/common/Display/Display.vcxproj
Normal file
44
src/common/Display/Display.vcxproj
Normal file
@@ -0,0 +1,44 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>16.0</VCProjectVersion>
|
||||
<ProjectGuid>{CABA8DFB-823B-4BF2-93AC-3F31984150D9}</ProjectGuid>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<RootNamespace>Display</RootNamespace>
|
||||
<ProjectName>Display</ProjectName>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Label="Configuration">
|
||||
<ConfigurationType>StaticLibrary</ConfigurationType>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup>
|
||||
<ClCompile>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
<AdditionalIncludeDirectories>..\..\..\;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
|
||||
<PreprocessorDefinitions>_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<TreatWarningAsError>true</TreatWarningAsError>
|
||||
<PrecompiledHeader>NotUsing</PrecompiledHeader>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="monitors.h" />
|
||||
<ClInclude Include="dpi_aware.h" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="monitors.cpp" />
|
||||
<ClCompile Include="dpi_aware.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
</Project>
|
||||
93
src/common/Display/dpi_aware.cpp
Normal file
93
src/common/Display/dpi_aware.cpp
Normal file
@@ -0,0 +1,93 @@
|
||||
#include "dpi_aware.h"
|
||||
#include "monitors.h"
|
||||
#include <ShellScalingApi.h>
|
||||
#include <array>
|
||||
|
||||
#pragma comment(lib, "shcore.lib")
|
||||
|
||||
namespace DPIAware
|
||||
{
|
||||
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT& dpi_x, UINT& dpi_y)
|
||||
{
|
||||
auto monitor_handle = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
|
||||
dpi_x = 0;
|
||||
dpi_y = 0;
|
||||
if (monitor_handle != nullptr)
|
||||
{
|
||||
return GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y);
|
||||
}
|
||||
else
|
||||
{
|
||||
return E_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
HRESULT GetScreenDPIForPoint(POINT p, UINT& dpi_x, UINT& dpi_y)
|
||||
{
|
||||
auto monitor_handle = MonitorFromPoint(p, MONITOR_DEFAULTTONEAREST);
|
||||
dpi_x = 0;
|
||||
dpi_y = 0;
|
||||
if (monitor_handle != nullptr)
|
||||
{
|
||||
return GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y);
|
||||
}
|
||||
else
|
||||
{
|
||||
return E_FAIL;
|
||||
}
|
||||
}
|
||||
|
||||
void Convert(HMONITOR monitor_handle, int& width, int& height)
|
||||
{
|
||||
if (monitor_handle == NULL)
|
||||
{
|
||||
const POINT ptZero = { 0, 0 };
|
||||
monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
|
||||
}
|
||||
|
||||
UINT dpi_x, dpi_y;
|
||||
if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK)
|
||||
{
|
||||
width = width * static_cast<int>(dpi_x) / DEFAULT_DPI;
|
||||
height = height * static_cast<int>(dpi_y) / DEFAULT_DPI;
|
||||
}
|
||||
}
|
||||
|
||||
void InverseConvert(HMONITOR monitor_handle, int& width, int& height)
|
||||
{
|
||||
if (monitor_handle == NULL)
|
||||
{
|
||||
const POINT ptZero = { 0, 0 };
|
||||
monitor_handle = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
|
||||
}
|
||||
|
||||
UINT dpi_x, dpi_y;
|
||||
if (GetDpiForMonitor(monitor_handle, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y) == S_OK)
|
||||
{
|
||||
width = width * DEFAULT_DPI / static_cast<int>(dpi_x);
|
||||
height = height * DEFAULT_DPI / static_cast<int>(dpi_y);
|
||||
}
|
||||
}
|
||||
|
||||
void EnableDPIAwarenessForThisProcess()
|
||||
{
|
||||
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
|
||||
}
|
||||
|
||||
AwarenessLevel GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value)
|
||||
{
|
||||
const std::array levels{ DPI_AWARENESS_CONTEXT_UNAWARE,
|
||||
DPI_AWARENESS_CONTEXT_SYSTEM_AWARE,
|
||||
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE,
|
||||
DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2,
|
||||
DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED };
|
||||
for (size_t i = 0; i < size(levels); ++i)
|
||||
{
|
||||
if (AreDpiAwarenessContextsEqual(levels[i], system_returned_value))
|
||||
{
|
||||
return static_cast<AwarenessLevel>(i);
|
||||
}
|
||||
}
|
||||
return AwarenessLevel::UNAWARE;
|
||||
}
|
||||
}
|
||||
25
src/common/Display/dpi_aware.h
Normal file
25
src/common/Display/dpi_aware.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <Windows.h>
|
||||
|
||||
namespace DPIAware
|
||||
{
|
||||
constexpr inline int DEFAULT_DPI = 96;
|
||||
|
||||
HRESULT GetScreenDPIForWindow(HWND hwnd, UINT& dpi_x, UINT& dpi_y);
|
||||
HRESULT GetScreenDPIForPoint(POINT p, UINT& dpi_x, UINT& dpi_y);
|
||||
void Convert(HMONITOR monitor_handle, int& width, int& height);
|
||||
void InverseConvert(HMONITOR monitor_handle, int& width, int& height);
|
||||
void EnableDPIAwarenessForThisProcess();
|
||||
|
||||
enum AwarenessLevel
|
||||
{
|
||||
UNAWARE,
|
||||
SYSTEM_AWARE,
|
||||
PER_MONITOR_AWARE,
|
||||
PER_MONITOR_AWARE_V2,
|
||||
UNAWARE_GDISCALED
|
||||
};
|
||||
AwarenessLevel GetAwarenessLevel(DPI_AWARENESS_CONTEXT system_returned_value);
|
||||
};
|
||||
73
src/common/Display/monitors.cpp
Normal file
73
src/common/Display/monitors.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "monitors.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
|
||||
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 rhs_tuple = std::make_tuple(rhs.rect.left, rhs.rect.right, rhs.rect.top, rhs.rect.bottom);
|
||||
return lhs_tuple == rhs_tuple;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
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;
|
||||
});
|
||||
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;
|
||||
}
|
||||
38
src/common/Display/monitors.h
Normal file
38
src/common/Display/monitors.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
#include <Windows.h>
|
||||
#include <vector>
|
||||
|
||||
struct ScreenSize
|
||||
{
|
||||
explicit ScreenSize(RECT rect) :
|
||||
rect(rect) {}
|
||||
RECT rect;
|
||||
int left() const { return rect.left; }
|
||||
int right() const { return rect.right; }
|
||||
int top() const { return rect.top; }
|
||||
int bottom() const { return rect.bottom; }
|
||||
int height() const { return rect.bottom - rect.top; };
|
||||
int width() const { return rect.right - rect.left; };
|
||||
POINT top_left() const { return { rect.left, rect.top }; };
|
||||
POINT top_middle() const { return { rect.left + width() / 2, rect.top }; };
|
||||
POINT top_right() const { return { rect.right, rect.top }; };
|
||||
POINT middle_left() const { return { rect.left, rect.top + height() / 2 }; };
|
||||
POINT middle() const { return { rect.left + width() / 2, rect.top + height() / 2 }; };
|
||||
POINT middle_right() const { return { rect.right, rect.top + height() / 2 }; };
|
||||
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 }; };
|
||||
};
|
||||
|
||||
struct MonitorInfo : ScreenSize
|
||||
{
|
||||
explicit MonitorInfo(HMONITOR monitor, RECT rect) :
|
||||
handle(monitor), ScreenSize(rect) {}
|
||||
HMONITOR handle;
|
||||
|
||||
// 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);
|
||||
Reference in New Issue
Block a user