-
Notifications
You must be signed in to change notification settings - Fork 6k
Expose DPI helper functions for Runner apps to use #16313
Changes from 32 commits
4afaff0
f133955
098a89b
0ac5ce5
3bc93ab
1990c7b
5d53cb2
869725a
e6aa604
ed6d9a2
186f56c
7954dbd
c883807
6762376
3b1ffc5
31c39d2
fc9c47c
226581e
c9e38c8
9e8ccfb
efa8fcc
9dfbad6
6e16a11
a4144e7
d693ebb
747b614
5899237
f4a225a
93c1aeb
688580c
838a9c2
2a98f8c
4ba92ef
57c11aa
20dcea8
6b06bb2
c379a98
d8bce83
86ebe6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| #include "dpi_utils.h" | ||
|
|
||
| namespace flutter { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr UINT kDefaultDpi = 96; | ||
|
|
||
| // This is the MDT_EFFECTIVE_DPI value from MONITOR_DPI_TYPE, an enum declared | ||
| // in ShellScalingApi.h. Replicating here to avoid importing the library | ||
| // directly. | ||
| constexpr UINT kEffectiveDpiMonitorType = 0; | ||
|
|
||
| template <typename T> | ||
|
|
||
| /// Retrieves a function |name| from a given |comBaseModule| into |outProc|. | ||
| /// Returns a bool indicating whether the function was found. | ||
| bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { | ||
| outProc = reinterpret_cast<T*>(GetProcAddress(comBaseModule, name)); | ||
| return *outProc != nullptr; | ||
| } | ||
|
|
||
| /// A helper class for abstracting various Windows DPI related functions across | ||
| /// Windows OS versions. | ||
| class Win32DpiHelper { | ||
| public: | ||
| Win32DpiHelper(); | ||
|
|
||
| ~Win32DpiHelper(); | ||
|
|
||
| /// Returns the current DPI. Supports all DPI awareness modes, and is backward | ||
| /// compatible down to Windows Vista. If |hwnd| is nullptr, returns the DPI | ||
| /// for the primary monitor. Otherwise, returns the system's DPI. | ||
| UINT GetDpiForWindow(HWND); | ||
|
|
||
| /// Returns the DPI of a given monitor. Defaults to 96 if the API is not | ||
| /// available. | ||
| UINT GetDpiForMonitor(HMONITOR); | ||
|
|
||
| private: | ||
| using GetDpiForWindow_ = UINT __stdcall(HWND); | ||
| using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, | ||
| UINT dpiType, | ||
| UINT* dpiX, | ||
| UINT* dpiY); | ||
| using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); | ||
|
|
||
| GetDpiForWindow_* get_dpi_for_window_ = nullptr; | ||
| GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; | ||
| EnableNonClientDpiScaling_* enable_non_client_dpi_scaling_ = nullptr; | ||
|
|
||
| HMODULE user32_module_ = nullptr; | ||
| HMODULE shlib_module_ = nullptr; | ||
| bool dpi_for_window_supported_ = false; | ||
| bool dpi_for_monitor_supported_ = false; | ||
| }; | ||
|
|
||
| Win32DpiHelper::Win32DpiHelper() { | ||
| user32_module_ = LoadLibraryA("User32.dll"); | ||
| shlib_module_ = LoadLibraryA("Shcore.dll"); | ||
| if (shlib_module_ == nullptr && user32_module_ == nullptr) { | ||
franciscojma86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| dpi_for_window_supported_ = (AssignProcAddress( | ||
| user32_module_, "GetDpiForWindow", get_dpi_for_window_)); | ||
| dpi_for_monitor_supported_ = AssignProcAddress( | ||
| shlib_module_, "GetDpiForMonitor", get_dpi_for_monitor_); | ||
| } | ||
|
|
||
| Win32DpiHelper::~Win32DpiHelper() { | ||
| if (user32_module_ != nullptr) { | ||
| FreeLibrary(user32_module_); | ||
| } | ||
| if (shlib_module_ != nullptr) { | ||
| FreeLibrary(shlib_module_); | ||
| } | ||
| } | ||
|
|
||
| UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { | ||
| // GetDpiForWindow returns the DPI for any awareness mode. If not available, | ||
| // or no |hwnd| is provided, fallback to a per monitor, system, or default | ||
| // DPI. | ||
| if (dpi_for_window_supported_ && hwnd != nullptr) { | ||
| return get_dpi_for_window_(hwnd); | ||
| } | ||
|
|
||
| if (dpi_for_monitor_supported_) { | ||
| HMONITOR monitor; | ||
| if (hwnd == nullptr) { | ||
| const POINT target_point = {static_cast<LONG>(0), static_cast<LONG>(0)}; | ||
franciscojma86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); | ||
| } else { | ||
| monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); | ||
| } | ||
| return GetDpiForMonitor(monitor); | ||
| } | ||
| HDC hdc = GetDC(hwnd); | ||
| UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); | ||
| ReleaseDC(hwnd, hdc); | ||
| return dpi; | ||
| } | ||
|
|
||
| UINT Win32DpiHelper::GetDpiForMonitor(HMONITOR monitor) { | ||
| if (dpi_for_monitor_supported_) { | ||
| UINT dpi_x = 0, dpi_y = 0; | ||
| HRESULT result = | ||
| get_dpi_for_monitor_(monitor, kEffectiveDpiMonitorType, &dpi_x, &dpi_y); | ||
| return SUCCEEDED(result) ? dpi_x : kDefaultDpi; | ||
|
||
| } | ||
| return kDefaultDpi; | ||
| } | ||
|
|
||
| Win32DpiHelper* GetHelper() { | ||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| static Win32DpiHelper* dpi_helper = new Win32DpiHelper(); | ||
| return dpi_helper; | ||
| } | ||
| } // namespace | ||
|
|
||
| UINT GetDpiForHWND(HWND hwnd) { | ||
| return GetHelper()->GetDpiForWindow(hwnd); | ||
| } | ||
|
|
||
| UINT GetDpiForMonitor(HMONITOR monitor) { | ||
| return GetHelper()->GetDpiForMonitor(monitor); | ||
| } | ||
| } // namespace flutter | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include "Windows.h" | ||
|
|
||
| #ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_UTILS_H_ | ||
| #define FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_UTILS_H_ | ||
|
|
||
| namespace flutter { | ||
|
|
||
| /// Returns the current DPI. Supports all DPI awareness modes, and is backward | ||
| /// compatible down to Windows Vista. If |hwnd| is nullptr, returns the DPI for | ||
| /// the primary monitor. Otherwise, returns the system's DPI. | ||
franciscojma86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| UINT GetDpiForHWND(HWND hwnd); | ||
|
|
||
| /// Returns the DPI of a given monitor. Defaults to 96 if the API is not | ||
| /// available. | ||
| UINT GetDpiForMonitor(HMONITOR monitor); | ||
|
|
||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } // namespace flutter | ||
|
|
||
| #endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_UTILS_H_ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| #include "flutter/shell/platform/windows/testing/win32_window_test.h" | ||
|
|
||
| namespace flutter { | ||
| namespace testing { | ||
|
|
||
| Win32WindowTest::Win32WindowTest() : Win32Window(){}; | ||
|
|
||
| Win32WindowTest::~Win32WindowTest() = default; | ||
|
|
||
| void Win32WindowTest::OnDpiScale(unsigned int dpi){}; | ||
|
|
||
| void Win32WindowTest::OnResize(unsigned int width, unsigned int height) {} | ||
|
|
||
| void Win32WindowTest::OnPointerMove(double x, double y) {} | ||
|
|
||
| void Win32WindowTest::OnPointerDown(double x, double y, UINT button) {} | ||
|
|
||
| void Win32WindowTest::OnPointerUp(double x, double y, UINT button) {} | ||
|
|
||
| void Win32WindowTest::OnPointerLeave() {} | ||
|
|
||
| void Win32WindowTest::OnChar(char32_t code_point) {} | ||
|
|
||
| void Win32WindowTest::OnKey(int key, int scancode, int action, int mods) {} | ||
|
|
||
| void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} | ||
|
|
||
| void Win32WindowTest::OnFontChange() {} | ||
|
|
||
| UINT Win32WindowTest::GetDpi() { | ||
| return GetCurrentDPI(); | ||
| } | ||
|
|
||
| } // namespace testing | ||
| } // namespace flutter |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| #include <windowsx.h> | ||
|
|
||
| #include "flutter/fml/macros.h" | ||
| #include "flutter/shell/platform/windows/win32_window.h" | ||
|
|
||
| namespace flutter { | ||
| namespace testing { | ||
|
|
||
| class Win32WindowTest : public Win32Window { | ||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| Win32WindowTest(); | ||
| ~Win32WindowTest(); | ||
franciscojma86 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Wrapper for GetCurrentDPI() which is a protected method. | ||
| UINT GetDpi(); | ||
|
|
||
| // |Win32Window| | ||
| void OnDpiScale(unsigned int dpi) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnResize(unsigned int width, unsigned int height) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnPointerMove(double x, double y) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnPointerDown(double x, double y, UINT button) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnPointerUp(double x, double y, UINT button) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnPointerLeave() override; | ||
|
|
||
| // |Win32Window| | ||
| void OnChar(char32_t code_point) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnKey(int key, int scancode, int action, int mods) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnScroll(double delta_x, double delta_y) override; | ||
|
|
||
| // |Win32Window| | ||
| void OnFontChange() override; | ||
|
|
||
| private: | ||
| FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); | ||
|
||
| }; | ||
|
|
||
| } // namespace testing | ||
| } // namespace flutter | ||
Uh oh!
There was an error while loading. Please reload this page.