This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expose DPI helper functions for Runner apps to use #16313
Merged
Merged
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
4afaff0
Refactor helper and expose it
franciscojma86 f133955
mostly works but with two helpers
franciscojma86 098a89b
Now with scaling
franciscojma86 0ac5ce5
Now with scaling
franciscojma86 3bc93ab
Format
franciscojma86 1990c7b
Clean
franciscojma86 5d53cb2
Commetns
franciscojma86 869725a
Clean
franciscojma86 e6aa604
Comment
franciscojma86 ed6d9a2
Address comments
franciscojma86 186f56c
Comment
franciscojma86 7954dbd
Review comments
franciscojma86 c883807
GN
franciscojma86 6762376
Add test
franciscojma86 3b1ffc5
Clean
franciscojma86 31c39d2
Clean
franciscojma86 fc9c47c
Merge tests
franciscojma86 226581e
Comments and format
franciscojma86 c9e38c8
license
franciscojma86 9e8ccfb
Comments
franciscojma86 efa8fcc
Virtual destructors
franciscojma86 9dfbad6
Move tests
franciscojma86 6e16a11
Add on font change
franciscojma86 a4144e7
Remove onclose
franciscojma86 d693ebb
Remove dpi changed and primary monitor change
franciscojma86 747b614
Merge branch 'master' into dpi-get
franciscojma86 5899237
Fix
franciscojma86 f4a225a
monitor type enum
franciscojma86 93c1aeb
Use MonitorFromWindow
franciscojma86 688580c
space
franciscojma86 838a9c2
Default to primary correctly
franciscojma86 2a98f8c
License
franciscojma86 4ba92ef
Most comments
franciscojma86 57c11aa
Comment
franciscojma86 20dcea8
Add test
franciscojma86 6b06bb2
Add test
franciscojma86 c379a98
Remove test line
franciscojma86 d8bce83
Format
franciscojma86 86ebe6c
Clean tests
franciscojma86 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| #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 DPI for |hwnd|. Supports all DPI awareness modes, and is | ||
| /// backward compatible down to Windows Vista. If |hwnd| is nullptr, returns | ||
| /// the DPI for the primary monitor. If Per-Monitor DPI awareness is not | ||
| /// available, 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() { | ||
| if ((user32_module_ = LoadLibraryA("User32.dll")) != nullptr) { | ||
| dpi_for_window_supported_ = (AssignProcAddress( | ||
| user32_module_, "GetDpiForWindow", get_dpi_for_window_)); | ||
| } | ||
| if ((shlib_module_ = LoadLibraryA("Shcore.dll")) != nullptr) { | ||
| 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 = nullptr; | ||
| if (hwnd != nullptr) { | ||
| 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_) { | ||
| if (monitor == nullptr) { | ||
| const POINT target_point = {0, 0}; | ||
| monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know that we need this functionality, but I guess it's better for API consistency. |
||
| UINT dpi_x = 0, dpi_y = 0; | ||
| HRESULT result = | ||
| get_dpi_for_monitor_(monitor, kEffectiveDpiMonitorType, &dpi_x, &dpi_y); | ||
| if (SUCCEEDED(result)) { | ||
| return dpi_x; | ||
| } | ||
| } | ||
| return kDefaultDpi; | ||
| } // namespace | ||
|
|
||
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| // 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 DPI for |hwnd|. Supports all DPI awareness modes, and is | ||
| /// backward compatible down to Windows Vista. If |hwnd| is nullptr, returns the | ||
| /// DPI for the primary monitor. If Per-Monitor DPI awareness is not available, | ||
| /// returns the system's DPI. | ||
| 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_ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #include <windows.h> | ||
|
|
||
| #include "flutter/shell/platform/windows/dpi_utils.h" | ||
| #include "gtest/gtest.h" | ||
|
|
||
| namespace flutter { | ||
| namespace testing { | ||
|
|
||
| TEST(DpiUtilsTest, NonZero) { | ||
| ASSERT_GT(GetDpiForHWND(nullptr), 0); | ||
| ASSERT_GT(GetDpiForMonitor(nullptr), 0); | ||
| }; | ||
|
|
||
| TEST(DpiUtilsTest, NullHwndUsesPrimaryMonitor) { | ||
| const POINT target_point = {0, 0}; | ||
| HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); | ||
| ASSERT_EQ(GetDpiForHWND(nullptr), GetDpiForMonitor(monitor)); | ||
| }; | ||
|
|
||
| } // namespace testing | ||
| } // namespace flutter |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // 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/shell/platform/windows/win32_window.h" | ||
|
|
||
| namespace flutter { | ||
| namespace testing { | ||
|
|
||
| /// Test class for the Win32Window base class. Used to access protected methods | ||
| /// for testing. | ||
| class Win32WindowTest : public Win32Window { | ||
franciscojma86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public: | ||
| Win32WindowTest(); | ||
| virtual ~Win32WindowTest(); | ||
|
|
||
| // Prevent copying. | ||
| Win32WindowTest(Win32WindowTest const&) = delete; | ||
| Win32WindowTest& operator=(Win32WindowTest const&) = delete; | ||
|
|
||
| // 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; | ||
| }; | ||
|
|
||
| } // namespace testing | ||
| } // namespace flutter | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.