Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
4afaff0
Refactor helper and expose it
franciscojma86 Jan 30, 2020
f133955
mostly works but with two helpers
franciscojma86 Jan 31, 2020
098a89b
Now with scaling
franciscojma86 Jan 31, 2020
0ac5ce5
Now with scaling
franciscojma86 Jan 31, 2020
3bc93ab
Format
franciscojma86 Jan 31, 2020
1990c7b
Clean
franciscojma86 Feb 1, 2020
5d53cb2
Commetns
franciscojma86 Feb 1, 2020
869725a
Clean
franciscojma86 Feb 1, 2020
e6aa604
Comment
franciscojma86 Feb 1, 2020
ed6d9a2
Address comments
franciscojma86 Feb 3, 2020
186f56c
Comment
franciscojma86 Feb 3, 2020
7954dbd
Review comments
franciscojma86 Feb 4, 2020
c883807
GN
franciscojma86 Feb 4, 2020
6762376
Add test
franciscojma86 Feb 5, 2020
3b1ffc5
Clean
franciscojma86 Feb 5, 2020
31c39d2
Clean
franciscojma86 Feb 5, 2020
fc9c47c
Merge tests
franciscojma86 Feb 5, 2020
226581e
Comments and format
franciscojma86 Feb 5, 2020
c9e38c8
license
franciscojma86 Feb 6, 2020
9e8ccfb
Comments
franciscojma86 Feb 6, 2020
efa8fcc
Virtual destructors
franciscojma86 Feb 6, 2020
9dfbad6
Move tests
franciscojma86 Feb 6, 2020
6e16a11
Add on font change
franciscojma86 Feb 6, 2020
a4144e7
Remove onclose
franciscojma86 Feb 7, 2020
d693ebb
Remove dpi changed and primary monitor change
franciscojma86 Feb 7, 2020
747b614
Merge branch 'master' into dpi-get
franciscojma86 Feb 7, 2020
5899237
Fix
franciscojma86 Feb 7, 2020
f4a225a
monitor type enum
franciscojma86 Feb 7, 2020
93c1aeb
Use MonitorFromWindow
franciscojma86 Feb 7, 2020
688580c
space
franciscojma86 Feb 7, 2020
838a9c2
Default to primary correctly
franciscojma86 Feb 7, 2020
2a98f8c
License
franciscojma86 Feb 7, 2020
4ba92ef
Most comments
franciscojma86 Feb 10, 2020
57c11aa
Comment
franciscojma86 Feb 10, 2020
20dcea8
Add test
franciscojma86 Feb 10, 2020
6b06bb2
Add test
franciscojma86 Feb 10, 2020
c379a98
Remove test line
franciscojma86 Feb 10, 2020
d8bce83
Format
franciscojma86 Feb 10, 2020
86ebe6c
Clean tests
franciscojma86 Feb 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,8 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flu
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h
FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plugin_registrar_windows.h
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils.h
FILE: ../../../flutter/shell/platform/windows/flutter_windows.cc
FILE: ../../../flutter/shell/platform/windows/key_event_handler.cc
FILE: ../../../flutter/shell/platform/windows/key_event_handler.h
Expand All @@ -1125,10 +1127,9 @@ FILE: ../../../flutter/shell/platform/windows/platform_handler.h
FILE: ../../../flutter/shell/platform/windows/public/flutter_windows.h
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc
FILE: ../../../flutter/shell/platform/windows/text_input_plugin.h
FILE: ../../../flutter/shell/platform/windows/win32_dpi_helper.cc
FILE: ../../../flutter/shell/platform/windows/win32_dpi_helper.h
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.cc
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window.h
FILE: ../../../flutter/shell/platform/windows/win32_flutter_window_unittests.cc
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.cc
FILE: ../../../flutter/shell/platform/windows/win32_task_runner.h
FILE: ../../../flutter/shell/platform/windows/win32_window.cc
Expand Down
7 changes: 5 additions & 2 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ source_set("flutter_windows_source") {
sources = [
"angle_surface_manager.cc",
"angle_surface_manager.h",
"dpi_utils.cc",
"dpi_utils.h",
"flutter_windows.cc",
"key_event_handler.cc",
"key_event_handler.h",
Expand All @@ -49,8 +51,6 @@ source_set("flutter_windows_source") {
"platform_handler.h",
"text_input_plugin.cc",
"text_input_plugin.h",
"win32_dpi_helper.cc",
"win32_dpi_helper.h",
"win32_flutter_window.cc",
"win32_flutter_window.h",
"win32_task_runner.cc",
Expand Down Expand Up @@ -111,6 +111,9 @@ executable("flutter_windows_unittests") {
sources = [
"testing/win32_flutter_window_test.cc",
"testing/win32_flutter_window_test.h",
"testing/win32_window_test.cc",
"testing/win32_window_test.h",
"win32_flutter_window_unittests.cc",
"win32_window_unittests.cc",
]

Expand Down
127 changes: 127 additions & 0 deletions shell/platform/windows/dpi_utils.cc
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) {
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)};
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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider:

if (SUCCEEDED(result)) {
  return dpi_x;
}

so that you're not duplicating the fallback return in two places (which could get out of sync in the future.)

}
return kDefaultDpi;
}

Win32DpiHelper* GetHelper() {
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
23 changes: 23 additions & 0 deletions shell/platform/windows/dpi_utils.h
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.
UINT GetDpiForHWND(HWND hwnd);

/// Returns the DPI of a given monitor. Defaults to 96 if the API is not
/// available.
UINT GetDpiForMonitor(HMONITOR monitor);

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_UTILS_H_
9 changes: 9 additions & 0 deletions shell/platform/windows/flutter_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "flutter/shell/platform/common/cpp/client_wrapper/include/flutter/plugin_registrar.h"
#include "flutter/shell/platform/common/cpp/incoming_message_dispatcher.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/shell/platform/windows/dpi_utils.h"
#include "flutter/shell/platform/windows/key_event_handler.h"
#include "flutter/shell/platform/windows/keyboard_hook_handler.h"
#include "flutter/shell/platform/windows/platform_handler.h"
Expand Down Expand Up @@ -182,6 +183,14 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) {
return view->window->GetWindowHandle();
}

UINT FlutterDesktopGetDpiForHWND(HWND hwnd) {
return flutter::GetDpiForHWND(hwnd);
}

UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor) {
return flutter::GetDpiForMonitor(monitor);
}

FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path,
const char* icu_data_path,
const char** arguments,
Expand Down
9 changes: 9 additions & 0 deletions shell/platform/windows/public/flutter_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ FlutterDesktopProcessMessages(FlutterDesktopViewControllerRef controller);
// Return backing HWND for manipulation in host application.
FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view);

// Gets the DPI for a given |hwnd|, depending on the supported APIs per
// windows version and DPI awareness mode. If nullptr is passed, returns the DPI
// of the primary monitor.
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForHWND(HWND hwnd);

// Gets the DPI for a given |monitor|. If the API is not available, a default
// DPI of 96 is returned.
FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor);

// Runs an instance of a headless Flutter engine.
//
// The |assets_path| is the path to the flutter_assets folder for the Flutter
Expand Down
35 changes: 35 additions & 0 deletions shell/platform/windows/testing/win32_window_test.cc
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
56 changes: 56 additions & 0 deletions shell/platform/windows/testing/win32_window_test.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// 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 <windowsx.h>

#include "flutter/fml/macros.h"
#include "flutter/shell/platform/windows/win32_window.h"

namespace flutter {
namespace testing {

class Win32WindowTest : public Win32Window {
public:
Win32WindowTest();
~Win32WindowTest();

// 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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, missed this before. We don't use FML in the desktop embedding since they aren't part of the public API surface. Also, this macro violates current Google style guide (it's mirroring an old, since-deprecated macro that used to be recommended). Please remove the include of fml, and use direct deletion to avoid copying as is done elsewhere in the desktop embeddings (exampe; note that it should be up with the constructor/destructor, not private as with the macro).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, changed in win32_flutter_window_test.h as well

};

} // namespace testing
} // namespace flutter
Loading