From 4afaff0cd58926e93f0d42fe9e30d77177e0b1b1 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 30 Jan 2020 13:42:22 -0800 Subject: [PATCH 01/38] Refactor helper and expose it --- .../client_wrapper/flutter_view_controller.cc | 3 + .../include/flutter/flutter_view_controller.h | 2 + shell/platform/windows/flutter_windows.cc | 6 ++ .../platform/windows/public/flutter_windows.h | 1 + shell/platform/windows/win32_dpi_helper.cc | 67 +++++++++++++------ shell/platform/windows/win32_dpi_helper.h | 12 ++++ shell/platform/windows/win32_window.cc | 7 ++ shell/platform/windows/window_state.h | 3 + 8 files changed, 80 insertions(+), 21 deletions(-) diff --git a/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 92a853862d77c..1d95c4c5234e7 100644 --- a/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -57,4 +57,7 @@ FlutterDesktopPluginRegistrarRef FlutterViewController::GetRegistrarForPlugin( return FlutterDesktopGetPluginRegistrar(controller_, plugin_name.c_str()); } + UINT FlutterViewController::GetViewDpi() { + return FlutterDesktopViewGetDpiForView(controller_, view()->GetNativeWindow()); + } } // namespace flutter diff --git a/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index c72b285227240..f8a278cbdb9b6 100644 --- a/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -51,6 +51,8 @@ class FlutterViewController : public PluginRegistry { FlutterViewController& operator=(FlutterViewController const&) = delete; FlutterView* view() { return view_.get(); } + UINT GetViewDpi(); + // Processes any pending events in the Flutter engine, and returns the // nanosecond delay until the next scheduled event (or max, if none). diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 67ab22a5c937f..a2e4af2b8bb5f 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -19,6 +19,7 @@ #include "flutter/shell/platform/windows/keyboard_hook_handler.h" #include "flutter/shell/platform/windows/platform_handler.h" #include "flutter/shell/platform/windows/text_input_plugin.h" +#include "flutter/shell/platform/windows/win32_dpi_helper.h" #include "flutter/shell/platform/windows/win32_flutter_window.h" #include "flutter/shell/platform/windows/win32_task_runner.h" #include "flutter/shell/platform/windows/window_state.h" @@ -149,6 +150,7 @@ FlutterDesktopViewControllerRef FlutterDesktopCreateViewController( } state->view->SetState(engine_state->engine); state->engine_state = std::move(engine_state); + state->dpi_helper = std::make_unique(); return state; } @@ -182,6 +184,10 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { return view->window->GetWindowHandle(); } +UINT FlutterDesktopViewGetDpiForView(FlutterDesktopViewControllerRef controller, HWND hwnd) { + return controller->dpi_helper->GetDpi(hwnd); +} + FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, const char* icu_data_path, const char** arguments, diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index 66c8287ab16fa..61aabd1357f4f 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -76,6 +76,7 @@ FlutterDesktopProcessMessages(FlutterDesktopViewControllerRef controller); // Return backing HWND for manipulation in host application. FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); +FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(FlutterDesktopViewControllerRef controller, HWND hwnd); // Runs an instance of a headless Flutter engine. // diff --git a/shell/platform/windows/win32_dpi_helper.cc b/shell/platform/windows/win32_dpi_helper.cc index 40a79e72c31d9..7d8ce2b978ede 100644 --- a/shell/platform/windows/win32_dpi_helper.cc +++ b/shell/platform/windows/win32_dpi_helper.cc @@ -1,5 +1,6 @@ #include "flutter/shell/platform/windows/win32_dpi_helper.h" +#include namespace flutter { namespace { @@ -19,24 +20,6 @@ Win32DpiHelper::Win32DpiHelper() { if (user32_module_ == nullptr) { return; } - - if (AssignProcAddress(user32_module_, "GetDpiForWindow", - get_dpi_for_window_)) { - dpi_for_window_supported_ = true; - return; - } - - shlib_module_ = LoadLibraryA("Shcore.dll"); - if (shlib_module_ == nullptr) { - return; - } - - if (AssignProcAddress(shlib_module_, "GetDpiForMonitor", - get_dpi_for_monitor_) && - AssignProcAddress(user32_module_, "MonitorFromWindow", - monitor_from_window_)) { - dpi_for_monitor_supported_ = true; - } } Win32DpiHelper::~Win32DpiHelper() { @@ -48,20 +31,62 @@ Win32DpiHelper::~Win32DpiHelper() { } } +BOOL Win32DpiHelper::IsDpiPerWindowSupportedForWindow(HWND hwnd) { + if (!dpi_for_window_api_loaded_) { + dpi_for_window_supported_ = + (AssignProcAddress(user32_module_, "GetDpiForWindow", + get_dpi_for_window_) && + AssignProcAddress(user32_module_, "GetWindowDpiAwarenessContext", + get_window_dpi_awareness_context_) && + AssignProcAddress(user32_module_, "AreDpiAwarenessContextsEqual", + are_dpi_awareness_contexts_equal)); + dpi_for_window_api_loaded_ = true; + } + if (!dpi_for_window_supported_) { + return false; + } + + DPI_AWARENESS_CONTEXT context = get_window_dpi_awareness_context_(hwnd); + return are_dpi_awareness_contexts_equal( + context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); +} + +BOOL Win32DpiHelper::IsDpiPerMonitorSupported() { + if (!dpi_for_monitor_api_loaded_) { + shlib_module_ = LoadLibraryA("Shcore.dll"); + if (shlib_module_ == nullptr) { + return false; + } + dpi_for_monitor_supported_ = + (AssignProcAddress(shlib_module_, "GetDpiForMonitor", + get_dpi_for_monitor_) && + AssignProcAddress(user32_module_, "MonitorFromWindow", + monitor_from_window_)); + dpi_for_monitor_api_loaded_ = true; + } + return dpi_for_monitor_supported_; +} + UINT Win32DpiHelper::GetDpi(HWND hwnd) { // GetDpiForWindow returns the DPI for any awareness mode. If not available, // fallback to a per monitor, system, or default DPI. - if (dpi_for_window_supported_) { + if (hwnd != nullptr && IsDpiPerWindowSupportedForWindow(hwnd)) { + std::cerr << "v2\n"; return get_dpi_for_window_(hwnd); } - if (dpi_for_monitor_supported_) { - HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); + if (IsDpiPerMonitorSupported()) { + std::cerr << "monitor\n"; + + DWORD monitor_flag = + hwnd == nullptr ? MONITOR_DEFAULTTOPRIMARY : MONITOR_DEFAULTTONEAREST; + HMONITOR monitor = monitor_from_window_(hwnd, monitor_flag); UINT dpi_x = 0, dpi_y = 0; HRESULT result = get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); return SUCCEEDED(result) ? dpi_x : kDefaultDpi; } + std::cerr << "system\n"; HDC hdc = GetDC(hwnd); UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); diff --git a/shell/platform/windows/win32_dpi_helper.h b/shell/platform/windows/win32_dpi_helper.h index 6ded84836713f..63a1d357e4b29 100644 --- a/shell/platform/windows/win32_dpi_helper.h +++ b/shell/platform/windows/win32_dpi_helper.h @@ -23,21 +23,33 @@ class Win32DpiHelper { UINT GetDpi(HWND); private: + BOOL IsDpiPerWindowSupportedForWindow(HWND hwnd); + BOOL IsDpiPerMonitorSupported(); + using GetDpiForWindow_ = UINT __stdcall(HWND); using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, UINT* dpiX, UINT* dpiY); using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); + using GetWindowDpiAwarenessContext_ = + DPI_AWARENESS_CONTEXT __stdcall(HWND hwnd); + using AreDpiAwarenessContextsEqual_ = + BOOL __stdcall(DPI_AWARENESS_CONTEXT dpiContextA, + DPI_AWARENESS_CONTEXT dpiContextB); GetDpiForWindow_* get_dpi_for_window_ = nullptr; GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; MonitorFromWindow_* monitor_from_window_ = nullptr; + GetWindowDpiAwarenessContext_* get_window_dpi_awareness_context_ = nullptr; + AreDpiAwarenessContextsEqual_* are_dpi_awareness_contexts_equal = nullptr; HMODULE user32_module_ = nullptr; HMODULE shlib_module_ = nullptr; bool dpi_for_window_supported_ = false; bool dpi_for_monitor_supported_ = false; + bool dpi_for_window_api_loaded_ = false; + bool dpi_for_monitor_api_loaded_ = false; }; } // namespace flutter diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index e499bfbd129f8..ccc9095a56c13 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -4,6 +4,8 @@ #include "flutter/shell/platform/windows/win32_window.h" +#include + namespace flutter { Win32Window::Win32Window() {} @@ -75,6 +77,9 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, auto that = static_cast(cs->lpCreateParams); that->current_dpi_ = that->dpi_helper_->GetDpi(window); + std::cerr << "Current dpi" << std::endl; + std::cerr << that->current_dpi_ << std::endl; + that->window_handle_ = window; } else if (Win32Window* that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); @@ -268,6 +273,8 @@ Win32Window::HandleDpiChange(HWND hwnd, // hence call function to get DPI if needed. if (uDpi == 0) { uDpi = dpi_helper_->GetDpi(hwnd); + std::cerr << "New dpi" << std::endl; + std::cerr << uDpi << std::endl; } current_dpi_ = uDpi; window->OnDpiScale(uDpi); diff --git a/shell/platform/windows/window_state.h b/shell/platform/windows/window_state.h index 254ab46c2ead9..30bb39524f2bd 100644 --- a/shell/platform/windows/window_state.h +++ b/shell/platform/windows/window_state.h @@ -12,6 +12,7 @@ #include "flutter/shell/platform/windows/keyboard_hook_handler.h" #include "flutter/shell/platform/windows/platform_handler.h" #include "flutter/shell/platform/windows/text_input_plugin.h" +#include "flutter/shell/platform/windows/win32_dpi_helper.h" #include "flutter/shell/platform/windows/win32_task_runner.h" struct flutter::Win32FlutterWindow; @@ -27,6 +28,8 @@ struct FlutterDesktopViewControllerState { // The window handle given to API clients. std::unique_ptr view_wrapper; + + std::unique_ptr dpi_helper; }; // Opaque reference for the native windows itself. This is separate from the From f133955f2481f03408a95c0be6d66f9122cd57dc Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 30 Jan 2020 16:25:04 -0800 Subject: [PATCH 02/38] mostly works but with two helpers --- .../windows/client_wrapper/flutter_view_controller.cc | 4 ---- .../client_wrapper/include/flutter/flutter_view_controller.h | 2 -- shell/platform/windows/flutter_windows.cc | 5 +++-- shell/platform/windows/public/flutter_windows.h | 3 ++- shell/platform/windows/win32_dpi_helper.cc | 4 +--- 5 files changed, 6 insertions(+), 12 deletions(-) diff --git a/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 1d95c4c5234e7..296b13e805c47 100644 --- a/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -56,8 +56,4 @@ FlutterDesktopPluginRegistrarRef FlutterViewController::GetRegistrarForPlugin( } return FlutterDesktopGetPluginRegistrar(controller_, plugin_name.c_str()); } - - UINT FlutterViewController::GetViewDpi() { - return FlutterDesktopViewGetDpiForView(controller_, view()->GetNativeWindow()); - } } // namespace flutter diff --git a/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h b/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h index f8a278cbdb9b6..c72b285227240 100644 --- a/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h +++ b/shell/platform/windows/client_wrapper/include/flutter/flutter_view_controller.h @@ -51,8 +51,6 @@ class FlutterViewController : public PluginRegistry { FlutterViewController& operator=(FlutterViewController const&) = delete; FlutterView* view() { return view_.get(); } - UINT GetViewDpi(); - // Processes any pending events in the Flutter engine, and returns the // nanosecond delay until the next scheduled event (or max, if none). diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index a2e4af2b8bb5f..da9dd9bc96032 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -184,8 +184,9 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { return view->window->GetWindowHandle(); } -UINT FlutterDesktopViewGetDpiForView(FlutterDesktopViewControllerRef controller, HWND hwnd) { - return controller->dpi_helper->GetDpi(hwnd); +UINT FlutterDesktopViewGetDpiForView(HWND hwnd) { + static flutter::Win32DpiHelper* dpi_helper = new flutter::Win32DpiHelper(); + return dpi_helper->GetDpi(hwnd); } FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index 61aabd1357f4f..958d7464acf53 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -76,7 +76,8 @@ FlutterDesktopProcessMessages(FlutterDesktopViewControllerRef controller); // Return backing HWND for manipulation in host application. FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); -FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(FlutterDesktopViewControllerRef controller, HWND hwnd); + +FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(HWND hwnd); // Runs an instance of a headless Flutter engine. // diff --git a/shell/platform/windows/win32_dpi_helper.cc b/shell/platform/windows/win32_dpi_helper.cc index 7d8ce2b978ede..9fef9de0fe7a7 100644 --- a/shell/platform/windows/win32_dpi_helper.cc +++ b/shell/platform/windows/win32_dpi_helper.cc @@ -78,9 +78,7 @@ UINT Win32DpiHelper::GetDpi(HWND hwnd) { if (IsDpiPerMonitorSupported()) { std::cerr << "monitor\n"; - DWORD monitor_flag = - hwnd == nullptr ? MONITOR_DEFAULTTOPRIMARY : MONITOR_DEFAULTTONEAREST; - HMONITOR monitor = monitor_from_window_(hwnd, monitor_flag); + HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); UINT dpi_x = 0, dpi_y = 0; HRESULT result = get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); From 098a89bb0b43def02bdb842392ed3108a1333648 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 11:25:21 -0800 Subject: [PATCH 03/38] Now with scaling --- ci/licenses_golden/licenses_flutter | 4 +- shell/platform/windows/BUILD.gn | 4 +- shell/platform/windows/flutter_windows.cc | 11 ++- .../platform/windows/public/flutter_windows.h | 1 + shell/platform/windows/win32_dpi_helper.cc | 94 ------------------- shell/platform/windows/win32_dpi_helper.h | 57 ----------- shell/platform/windows/win32_window.cc | 12 ++- shell/platform/windows/win32_window.h | 6 -- shell/platform/windows/window_state.h | 3 - 9 files changed, 19 insertions(+), 173 deletions(-) delete mode 100644 shell/platform/windows/win32_dpi_helper.cc delete mode 100644 shell/platform/windows/win32_dpi_helper.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 5080f5b095eaa..0fb09282b57c3 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1108,6 +1108,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.h +FILE: ../../../flutter/shell/platform/windows/dpi_utils.cc 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 @@ -1117,8 +1119,6 @@ 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_task_runner.cc diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 36da0da25323f..8c27b050d8853 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -43,6 +43,8 @@ source_set("flutter_windows_source") { sources = [ "angle_surface_manager.cc", "angle_surface_manager.h", + "dpi_utils.h", + "dpi_utils.cc", "flutter_windows.cc", "key_event_handler.cc", "key_event_handler.h", @@ -51,8 +53,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", diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index da9dd9bc96032..023947521d8e7 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -19,7 +19,7 @@ #include "flutter/shell/platform/windows/keyboard_hook_handler.h" #include "flutter/shell/platform/windows/platform_handler.h" #include "flutter/shell/platform/windows/text_input_plugin.h" -#include "flutter/shell/platform/windows/win32_dpi_helper.h" +#include "flutter/shell/platform/windows/dpi_utils.h" #include "flutter/shell/platform/windows/win32_flutter_window.h" #include "flutter/shell/platform/windows/win32_task_runner.h" #include "flutter/shell/platform/windows/window_state.h" @@ -150,7 +150,6 @@ FlutterDesktopViewControllerRef FlutterDesktopCreateViewController( } state->view->SetState(engine_state->engine); state->engine_state = std::move(engine_state); - state->dpi_helper = std::make_unique(); return state; } @@ -185,10 +184,14 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { } UINT FlutterDesktopViewGetDpiForView(HWND hwnd) { - static flutter::Win32DpiHelper* dpi_helper = new flutter::Win32DpiHelper(); - return dpi_helper->GetDpi(hwnd); + return flutter::GetDpiForView(hwnd); } + BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd) { + return flutter::EnableNonClientDpiScaling(hwnd); + } + + FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, const char* icu_data_path, const char** arguments, diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index 958d7464acf53..b87a5d6a130f8 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -78,6 +78,7 @@ FlutterDesktopProcessMessages(FlutterDesktopViewControllerRef controller); FLUTTER_EXPORT HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view); FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(HWND hwnd); +FLUTTER_EXPORT BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd); // Runs an instance of a headless Flutter engine. // diff --git a/shell/platform/windows/win32_dpi_helper.cc b/shell/platform/windows/win32_dpi_helper.cc deleted file mode 100644 index 9fef9de0fe7a7..0000000000000 --- a/shell/platform/windows/win32_dpi_helper.cc +++ /dev/null @@ -1,94 +0,0 @@ -#include "flutter/shell/platform/windows/win32_dpi_helper.h" - -#include -namespace flutter { - -namespace { - -constexpr UINT kDefaultDpi = 96; - -template -bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { - outProc = reinterpret_cast(GetProcAddress(comBaseModule, name)); - return *outProc != nullptr; -} - -} // namespace - -Win32DpiHelper::Win32DpiHelper() { - user32_module_ = LoadLibraryA("User32.dll"); - if (user32_module_ == nullptr) { - return; - } -} - -Win32DpiHelper::~Win32DpiHelper() { - if (user32_module_ != nullptr) { - FreeLibrary(user32_module_); - } - if (shlib_module_ != nullptr) { - FreeLibrary(shlib_module_); - } -} - -BOOL Win32DpiHelper::IsDpiPerWindowSupportedForWindow(HWND hwnd) { - if (!dpi_for_window_api_loaded_) { - dpi_for_window_supported_ = - (AssignProcAddress(user32_module_, "GetDpiForWindow", - get_dpi_for_window_) && - AssignProcAddress(user32_module_, "GetWindowDpiAwarenessContext", - get_window_dpi_awareness_context_) && - AssignProcAddress(user32_module_, "AreDpiAwarenessContextsEqual", - are_dpi_awareness_contexts_equal)); - dpi_for_window_api_loaded_ = true; - } - if (!dpi_for_window_supported_) { - return false; - } - - DPI_AWARENESS_CONTEXT context = get_window_dpi_awareness_context_(hwnd); - return are_dpi_awareness_contexts_equal( - context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); -} - -BOOL Win32DpiHelper::IsDpiPerMonitorSupported() { - if (!dpi_for_monitor_api_loaded_) { - shlib_module_ = LoadLibraryA("Shcore.dll"); - if (shlib_module_ == nullptr) { - return false; - } - dpi_for_monitor_supported_ = - (AssignProcAddress(shlib_module_, "GetDpiForMonitor", - get_dpi_for_monitor_) && - AssignProcAddress(user32_module_, "MonitorFromWindow", - monitor_from_window_)); - dpi_for_monitor_api_loaded_ = true; - } - return dpi_for_monitor_supported_; -} - -UINT Win32DpiHelper::GetDpi(HWND hwnd) { - // GetDpiForWindow returns the DPI for any awareness mode. If not available, - // fallback to a per monitor, system, or default DPI. - if (hwnd != nullptr && IsDpiPerWindowSupportedForWindow(hwnd)) { - std::cerr << "v2\n"; - return get_dpi_for_window_(hwnd); - } - - if (IsDpiPerMonitorSupported()) { - std::cerr << "monitor\n"; - - HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); - UINT dpi_x = 0, dpi_y = 0; - HRESULT result = - get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); - return SUCCEEDED(result) ? dpi_x : kDefaultDpi; - } - std::cerr << "system\n"; - - HDC hdc = GetDC(hwnd); - UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); - ReleaseDC(hwnd, hdc); - return dpi; -} -} // namespace flutter diff --git a/shell/platform/windows/win32_dpi_helper.h b/shell/platform/windows/win32_dpi_helper.h deleted file mode 100644 index 63a1d357e4b29..0000000000000 --- a/shell/platform/windows/win32_dpi_helper.h +++ /dev/null @@ -1,57 +0,0 @@ -// 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. - -#ifndef FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_HELPER_H_ -#define FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_HELPER_H_ - -#include -#include - -namespace flutter { - -/// 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. - UINT GetDpi(HWND); - - private: - BOOL IsDpiPerWindowSupportedForWindow(HWND hwnd); - BOOL IsDpiPerMonitorSupported(); - - using GetDpiForWindow_ = UINT __stdcall(HWND); - using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, - MONITOR_DPI_TYPE dpiType, - UINT* dpiX, - UINT* dpiY); - using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); - using GetWindowDpiAwarenessContext_ = - DPI_AWARENESS_CONTEXT __stdcall(HWND hwnd); - using AreDpiAwarenessContextsEqual_ = - BOOL __stdcall(DPI_AWARENESS_CONTEXT dpiContextA, - DPI_AWARENESS_CONTEXT dpiContextB); - - GetDpiForWindow_* get_dpi_for_window_ = nullptr; - GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; - MonitorFromWindow_* monitor_from_window_ = nullptr; - GetWindowDpiAwarenessContext_* get_window_dpi_awareness_context_ = nullptr; - AreDpiAwarenessContextsEqual_* are_dpi_awareness_contexts_equal = nullptr; - - HMODULE user32_module_ = nullptr; - HMODULE shlib_module_ = nullptr; - bool dpi_for_window_supported_ = false; - bool dpi_for_monitor_supported_ = false; - bool dpi_for_window_api_loaded_ = false; - bool dpi_for_monitor_api_loaded_ = false; -}; - -} // namespace flutter - -#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_HELPER_H_ diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index ccc9095a56c13..20e84f5d76d95 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -4,11 +4,16 @@ #include "flutter/shell/platform/windows/win32_window.h" +#include "dpi_utils.h" #include namespace flutter { -Win32Window::Win32Window() {} +Win32Window::Win32Window() { + current_dpi_ = GetDpiForView(nullptr); + std::cerr << "Current dpi" << std::endl; + std::cerr << current_dpi_ << std::endl; +} Win32Window::~Win32Window() { Destroy(); @@ -76,9 +81,6 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - that->current_dpi_ = that->dpi_helper_->GetDpi(window); - std::cerr << "Current dpi" << std::endl; - std::cerr << that->current_dpi_ << std::endl; that->window_handle_ = window; } else if (Win32Window* that = GetThisFromHandle(window)) { @@ -272,7 +274,7 @@ Win32Window::HandleDpiChange(HWND hwnd, // The DPI is only passed for DPI change messages on top level windows, // hence call function to get DPI if needed. if (uDpi == 0) { - uDpi = dpi_helper_->GetDpi(hwnd); + uDpi = GetDpiForView(hwnd); std::cerr << "New dpi" << std::endl; std::cerr << uDpi << std::endl; } diff --git a/shell/platform/windows/win32_window.h b/shell/platform/windows/win32_window.h index 63a00c355dea6..c24cab78d3da9 100644 --- a/shell/platform/windows/win32_window.h +++ b/shell/platform/windows/win32_window.h @@ -11,7 +11,6 @@ #include #include -#include "flutter/shell/platform/windows/win32_dpi_helper.h" namespace flutter { @@ -172,11 +171,6 @@ class Win32Window { // Member variable to hold the window title. std::wstring window_class_name_; - // Member variable referencing an instance of dpi_helper used to abstract some - // aspects of win32 High DPI handling across different OS versions. - std::unique_ptr dpi_helper_ = - std::make_unique(); - // Set to true to be notified when the mouse leaves the window. bool tracking_mouse_leave_ = false; diff --git a/shell/platform/windows/window_state.h b/shell/platform/windows/window_state.h index 30bb39524f2bd..254ab46c2ead9 100644 --- a/shell/platform/windows/window_state.h +++ b/shell/platform/windows/window_state.h @@ -12,7 +12,6 @@ #include "flutter/shell/platform/windows/keyboard_hook_handler.h" #include "flutter/shell/platform/windows/platform_handler.h" #include "flutter/shell/platform/windows/text_input_plugin.h" -#include "flutter/shell/platform/windows/win32_dpi_helper.h" #include "flutter/shell/platform/windows/win32_task_runner.h" struct flutter::Win32FlutterWindow; @@ -28,8 +27,6 @@ struct FlutterDesktopViewControllerState { // The window handle given to API clients. std::unique_ptr view_wrapper; - - std::unique_ptr dpi_helper; }; // Opaque reference for the native windows itself. This is separate from the From 0ac5ce502bb6d3a0bc4320c380866e1d8bf202ee Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 11:28:23 -0800 Subject: [PATCH 04/38] Now with scaling --- shell/platform/windows/dpi_utils.cc | 170 ++++++++++++++++++++++++++++ shell/platform/windows/dpi_utils.h | 17 +++ 2 files changed, 187 insertions(+) create mode 100644 shell/platform/windows/dpi_utils.cc create mode 100644 shell/platform/windows/dpi_utils.h diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc new file mode 100644 index 0000000000000..7d2ecd32b5cf3 --- /dev/null +++ b/shell/platform/windows/dpi_utils.cc @@ -0,0 +1,170 @@ +#include "dpi_utils.h" + +#include + +#include + +namespace flutter { + +namespace { + +constexpr UINT kDefaultDpi = 96; + +template +bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { + outProc = reinterpret_cast(GetProcAddress(comBaseModule, name)); + return *outProc != nullptr; +} + +} // namespace + +/// 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. + UINT GetDpi(HWND); + BOOL EnableNonClientDpiScaling(HWND hwnd); + + private: + BOOL IsDpiPerWindowSupportedForWindow(HWND hwnd); + BOOL IsDpiPerMonitorSupported(); + + using GetDpiForWindow_ = UINT __stdcall(HWND); + using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, + MONITOR_DPI_TYPE dpiType, + UINT* dpiX, + UINT* dpiY); + using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); + using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); + using GetWindowDpiAwarenessContext_ = + DPI_AWARENESS_CONTEXT __stdcall(HWND hwnd); + using AreDpiAwarenessContextsEqual_ = + BOOL __stdcall(DPI_AWARENESS_CONTEXT dpiContextA, + DPI_AWARENESS_CONTEXT dpiContextB); + + GetDpiForWindow_* get_dpi_for_window_ = nullptr; + GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; + MonitorFromWindow_* monitor_from_window_ = nullptr; + EnableNonClientDpiScaling_* enable_non_client_dpi_scaling_ = nullptr; + GetWindowDpiAwarenessContext_* get_window_dpi_awareness_context_ = nullptr; + AreDpiAwarenessContextsEqual_* are_dpi_awareness_contexts_equal = nullptr; + + HMODULE user32_module_ = nullptr; + HMODULE shlib_module_ = nullptr; + bool dpi_for_window_supported_ = false; + bool dpi_for_monitor_supported_ = false; + bool dpi_for_window_api_loaded_ = false; + bool dpi_for_monitor_api_loaded_ = false; +}; + +Win32DpiHelper::Win32DpiHelper() { + user32_module_ = LoadLibraryA("User32.dll"); + if (user32_module_ == nullptr) { + return; + } +} + +Win32DpiHelper::~Win32DpiHelper() { + if (user32_module_ != nullptr) { + FreeLibrary(user32_module_); + } + if (shlib_module_ != nullptr) { + FreeLibrary(shlib_module_); + } +} + +BOOL Win32DpiHelper::IsDpiPerWindowSupportedForWindow(HWND hwnd) { + if (!dpi_for_window_api_loaded_) { + dpi_for_window_supported_ = + (AssignProcAddress(user32_module_, "GetDpiForWindow", + get_dpi_for_window_) && + AssignProcAddress(user32_module_, "GetWindowDpiAwarenessContext", + get_window_dpi_awareness_context_) && + AssignProcAddress(user32_module_, "AreDpiAwarenessContextsEqual", + are_dpi_awareness_contexts_equal)); + dpi_for_window_api_loaded_ = true; + } + if (!dpi_for_window_supported_) { + return false; + } + DPI_AWARENESS_CONTEXT context = get_window_dpi_awareness_context_(hwnd); + std::cerr << context << std::endl; + + bool is_v2 = are_dpi_awareness_contexts_equal( + context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); + if (!is_v2) { + AssignProcAddress(user32_module_, "EnableNonClientDpiScaling", + enable_non_client_dpi_scaling_); + } + return is_v2; +} + +BOOL Win32DpiHelper::IsDpiPerMonitorSupported() { + if (!dpi_for_monitor_api_loaded_) { + shlib_module_ = LoadLibraryA("Shcore.dll"); + if (shlib_module_ == nullptr) { + return false; + } + dpi_for_monitor_supported_ = + (AssignProcAddress(shlib_module_, "GetDpiForMonitor", + get_dpi_for_monitor_) && + AssignProcAddress(user32_module_, "MonitorFromWindow", + monitor_from_window_)); + dpi_for_monitor_api_loaded_ = true; + } + return dpi_for_monitor_supported_; +} + +BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { + if (enable_non_client_dpi_scaling_ == nullptr) { + std::cerr << "Not loaded\n"; + return false; + } + return enable_non_client_dpi_scaling_(hwnd); +} + +UINT Win32DpiHelper::GetDpi(HWND hwnd) { + // GetDpiForWindow returns the DPI for any awareness mode. If not available, + // fallback to a per monitor, system, or default DPI. + if (IsDpiPerWindowSupportedForWindow(hwnd) && hwnd != nullptr) { + std::cerr << "v2\n"; + return get_dpi_for_window_(hwnd); + } + + if (IsDpiPerMonitorSupported()) { + std::cerr << "monitor\n"; + + HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); + UINT dpi_x = 0, dpi_y = 0; + HRESULT result = + get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); + return SUCCEEDED(result) ? dpi_x : kDefaultDpi; + } + std::cerr << "system\n"; + + HDC hdc = GetDC(hwnd); + UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); + ReleaseDC(hwnd, hdc); + return dpi; +} + +Win32DpiHelper* GetHelper() { + static Win32DpiHelper* dpi_helper = new Win32DpiHelper(); + return dpi_helper; +} + +UINT GetDpiForView(HWND hwnd) { + return GetHelper()->GetDpi(hwnd); +} + +BOOL EnableNonClientDpiScaling(HWND hwnd) { + return GetHelper()->EnableNonClientDpiScaling(hwnd); +} + +} // namespace flutter diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h new file mode 100644 index 0000000000000..47e69c1710c9a --- /dev/null +++ b/shell/platform/windows/dpi_utils.h @@ -0,0 +1,17 @@ +// 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 { + +UINT GetDpiForView(HWND hwnd); +BOOL EnableNonClientDpiScaling(HWND hwnd); + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DPI_UTILS_H_ From 3bc93ab0ec665d03741eb0809a4886c8c29854cc Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 13:54:04 -0800 Subject: [PATCH 05/38] Format --- shell/platform/windows/dpi_utils.cc | 17 +++++------------ shell/platform/windows/flutter_windows.cc | 9 ++++----- shell/platform/windows/public/flutter_windows.h | 9 +++++++-- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 7d2ecd32b5cf3..646b6125c1265 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -2,8 +2,6 @@ #include -#include - namespace flutter { namespace { @@ -29,6 +27,9 @@ class Win32DpiHelper { /// Returns the current DPI. Supports all DPI awareness modes, and is backward /// compatible down to Windows Vista. UINT GetDpi(HWND); + + /// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only + /// supported or Per-Monitor V1 DPI awareness mode. BOOL EnableNonClientDpiScaling(HWND hwnd); private: @@ -94,8 +95,6 @@ BOOL Win32DpiHelper::IsDpiPerWindowSupportedForWindow(HWND hwnd) { return false; } DPI_AWARENESS_CONTEXT context = get_window_dpi_awareness_context_(hwnd); - std::cerr << context << std::endl; - bool is_v2 = are_dpi_awareness_contexts_equal( context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); if (!is_v2) { @@ -123,7 +122,6 @@ BOOL Win32DpiHelper::IsDpiPerMonitorSupported() { BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { if (enable_non_client_dpi_scaling_ == nullptr) { - std::cerr << "Not loaded\n"; return false; } return enable_non_client_dpi_scaling_(hwnd); @@ -131,23 +129,19 @@ BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { UINT Win32DpiHelper::GetDpi(HWND hwnd) { // GetDpiForWindow returns the DPI for any awareness mode. If not available, - // fallback to a per monitor, system, or default DPI. + // or no |hwnd| is provided. fallback to a per monitor, system, or default + // DPI. if (IsDpiPerWindowSupportedForWindow(hwnd) && hwnd != nullptr) { - std::cerr << "v2\n"; return get_dpi_for_window_(hwnd); } if (IsDpiPerMonitorSupported()) { - std::cerr << "monitor\n"; - HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); UINT dpi_x = 0, dpi_y = 0; HRESULT result = get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); return SUCCEEDED(result) ? dpi_x : kDefaultDpi; } - std::cerr << "system\n"; - HDC hdc = GetDC(hwnd); UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); ReleaseDC(hwnd, hdc); @@ -166,5 +160,4 @@ UINT GetDpiForView(HWND hwnd) { BOOL EnableNonClientDpiScaling(HWND hwnd) { return GetHelper()->EnableNonClientDpiScaling(hwnd); } - } // namespace flutter diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 023947521d8e7..4f2a284b3b746 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -15,11 +15,11 @@ #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" #include "flutter/shell/platform/windows/text_input_plugin.h" -#include "flutter/shell/platform/windows/dpi_utils.h" #include "flutter/shell/platform/windows/win32_flutter_window.h" #include "flutter/shell/platform/windows/win32_task_runner.h" #include "flutter/shell/platform/windows/window_state.h" @@ -187,10 +187,9 @@ UINT FlutterDesktopViewGetDpiForView(HWND hwnd) { return flutter::GetDpiForView(hwnd); } - BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd) { - return flutter::EnableNonClientDpiScaling(hwnd); - } - +BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd) { + return flutter::EnableNonClientDpiScaling(hwnd); +} FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, const char* icu_data_path, diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index b87a5d6a130f8..f20b92ded6ecb 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -8,12 +8,11 @@ #include #include +#include "Windows.h" #include "flutter_export.h" #include "flutter_messenger.h" #include "flutter_plugin_registrar.h" -#include "Windows.h" - #if defined(__cplusplus) extern "C" { #endif @@ -77,7 +76,13 @@ 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 nearest monitor. FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(HWND hwnd); + +// Use only for apps that support Per-Monitor V1 awereness mode. Not necessary +// for Per-Monitor V2. FLUTTER_EXPORT BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd); // Runs an instance of a headless Flutter engine. From 1990c7b335cdb191c6762e54d0f578d4e39f59a4 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 16:22:09 -0800 Subject: [PATCH 06/38] Clean --- shell/platform/windows/dpi_utils.cc | 74 +++++-------------- shell/platform/windows/dpi_utils.h | 2 +- shell/platform/windows/flutter_windows.cc | 4 +- .../platform/windows/public/flutter_windows.h | 2 +- shell/platform/windows/win32_window.cc | 14 ++-- 5 files changed, 28 insertions(+), 68 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 646b6125c1265..721c7eed32147 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -29,13 +29,10 @@ class Win32DpiHelper { UINT GetDpi(HWND); /// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only - /// supported or Per-Monitor V1 DPI awareness mode. + /// supported on Per-Monitor V1 DPI awareness mode. BOOL EnableNonClientDpiScaling(HWND hwnd); private: - BOOL IsDpiPerWindowSupportedForWindow(HWND hwnd); - BOOL IsDpiPerMonitorSupported(); - using GetDpiForWindow_ = UINT __stdcall(HWND); using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, MONITOR_DPI_TYPE dpiType, @@ -43,32 +40,35 @@ class Win32DpiHelper { UINT* dpiY); using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); - using GetWindowDpiAwarenessContext_ = - DPI_AWARENESS_CONTEXT __stdcall(HWND hwnd); - using AreDpiAwarenessContextsEqual_ = - BOOL __stdcall(DPI_AWARENESS_CONTEXT dpiContextA, - DPI_AWARENESS_CONTEXT dpiContextB); GetDpiForWindow_* get_dpi_for_window_ = nullptr; GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; MonitorFromWindow_* monitor_from_window_ = nullptr; EnableNonClientDpiScaling_* enable_non_client_dpi_scaling_ = nullptr; - GetWindowDpiAwarenessContext_* get_window_dpi_awareness_context_ = nullptr; - AreDpiAwarenessContextsEqual_* are_dpi_awareness_contexts_equal = nullptr; HMODULE user32_module_ = nullptr; HMODULE shlib_module_ = nullptr; bool dpi_for_window_supported_ = false; bool dpi_for_monitor_supported_ = false; - bool dpi_for_window_api_loaded_ = false; - bool dpi_for_monitor_api_loaded_ = false; }; Win32DpiHelper::Win32DpiHelper() { user32_module_ = LoadLibraryA("User32.dll"); - if (user32_module_ == nullptr) { + 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_) && + AssignProcAddress(user32_module_, "EnableNonClientDpiScaling", + enable_non_client_dpi_scaling_)); + dpi_for_monitor_supported_ = + (AssignProcAddress(shlib_module_, "GetDpiForMonitor", + get_dpi_for_monitor_) && + AssignProcAddress(user32_module_, "MonitorFromWindow", + monitor_from_window_)); } Win32DpiHelper::~Win32DpiHelper() { @@ -80,46 +80,6 @@ Win32DpiHelper::~Win32DpiHelper() { } } -BOOL Win32DpiHelper::IsDpiPerWindowSupportedForWindow(HWND hwnd) { - if (!dpi_for_window_api_loaded_) { - dpi_for_window_supported_ = - (AssignProcAddress(user32_module_, "GetDpiForWindow", - get_dpi_for_window_) && - AssignProcAddress(user32_module_, "GetWindowDpiAwarenessContext", - get_window_dpi_awareness_context_) && - AssignProcAddress(user32_module_, "AreDpiAwarenessContextsEqual", - are_dpi_awareness_contexts_equal)); - dpi_for_window_api_loaded_ = true; - } - if (!dpi_for_window_supported_) { - return false; - } - DPI_AWARENESS_CONTEXT context = get_window_dpi_awareness_context_(hwnd); - bool is_v2 = are_dpi_awareness_contexts_equal( - context, DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2); - if (!is_v2) { - AssignProcAddress(user32_module_, "EnableNonClientDpiScaling", - enable_non_client_dpi_scaling_); - } - return is_v2; -} - -BOOL Win32DpiHelper::IsDpiPerMonitorSupported() { - if (!dpi_for_monitor_api_loaded_) { - shlib_module_ = LoadLibraryA("Shcore.dll"); - if (shlib_module_ == nullptr) { - return false; - } - dpi_for_monitor_supported_ = - (AssignProcAddress(shlib_module_, "GetDpiForMonitor", - get_dpi_for_monitor_) && - AssignProcAddress(user32_module_, "MonitorFromWindow", - monitor_from_window_)); - dpi_for_monitor_api_loaded_ = true; - } - return dpi_for_monitor_supported_; -} - BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { if (enable_non_client_dpi_scaling_ == nullptr) { return false; @@ -131,11 +91,11 @@ UINT Win32DpiHelper::GetDpi(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 (IsDpiPerWindowSupportedForWindow(hwnd) && hwnd != nullptr) { + if (dpi_for_window_supported_ && hwnd != nullptr) { return get_dpi_for_window_(hwnd); } - if (IsDpiPerMonitorSupported()) { + if (dpi_for_monitor_supported_) { HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); UINT dpi_x = 0, dpi_y = 0; HRESULT result = @@ -153,7 +113,7 @@ Win32DpiHelper* GetHelper() { return dpi_helper; } -UINT GetDpiForView(HWND hwnd) { +UINT GetDpiForHWND(HWND hwnd) { return GetHelper()->GetDpi(hwnd); } diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h index 47e69c1710c9a..5baa21c454e27 100644 --- a/shell/platform/windows/dpi_utils.h +++ b/shell/platform/windows/dpi_utils.h @@ -9,7 +9,7 @@ namespace flutter { -UINT GetDpiForView(HWND hwnd); +UINT GetDpiForHWND(HWND hwnd); BOOL EnableNonClientDpiScaling(HWND hwnd); } // namespace flutter diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 4f2a284b3b746..9a9c4118a206c 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -183,8 +183,8 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { return view->window->GetWindowHandle(); } -UINT FlutterDesktopViewGetDpiForView(HWND hwnd) { - return flutter::GetDpiForView(hwnd); +UINT FlutterDesktopViewGetDpiForHWND(HWND hwnd) { + return flutter::GetDpiForHWND(hwnd); } BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd) { diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index f20b92ded6ecb..06322dfa1e042 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -79,7 +79,7 @@ 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 nearest monitor. -FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForView(HWND hwnd); +FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForHWND(HWND hwnd); // Use only for apps that support Per-Monitor V1 awereness mode. Not necessary // for Per-Monitor V2. diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index 20e84f5d76d95..cb0bec81eee5f 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -4,15 +4,17 @@ #include "flutter/shell/platform/windows/win32_window.h" -#include "dpi_utils.h" #include +#include "dpi_utils.h" + namespace flutter { Win32Window::Win32Window() { - current_dpi_ = GetDpiForView(nullptr); - std::cerr << "Current dpi" << std::endl; - std::cerr << current_dpi_ << std::endl; + // Calling GetDpiForView with no HNWD returns the DPI from the nearest + // monitor, which is the best option as an initial DPI. If Per-Monitor V2 is + // supported, |current_dpi_| should be updated in the WM_DPICHANGED message. + current_dpi_ = GetDpiForHWND(nullptr); } Win32Window::~Win32Window() { @@ -274,9 +276,7 @@ Win32Window::HandleDpiChange(HWND hwnd, // The DPI is only passed for DPI change messages on top level windows, // hence call function to get DPI if needed. if (uDpi == 0) { - uDpi = GetDpiForView(hwnd); - std::cerr << "New dpi" << std::endl; - std::cerr << uDpi << std::endl; + uDpi = GetDpiForHWND(hwnd); } current_dpi_ = uDpi; window->OnDpiScale(uDpi); From 5d53cb2e1b62cc14786b5449bb7cf8e78c0e1ef9 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 16:36:18 -0800 Subject: [PATCH 07/38] Commetns --- shell/platform/windows/dpi_utils.cc | 3 ++- shell/platform/windows/dpi_utils.h | 6 ++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 721c7eed32147..7915c99b41edd 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -25,7 +25,8 @@ class Win32DpiHelper { ~Win32DpiHelper(); /// Returns the current DPI. Supports all DPI awareness modes, and is backward - /// compatible down to Windows Vista. + /// compatible down to Windows Vista. If |hwnd| is nullptr, returns the DPI + /// for the nearest monitor is available. Otherwise, returns the system's DPI. UINT GetDpi(HWND); /// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h index 5baa21c454e27..f460a5224a6c7 100644 --- a/shell/platform/windows/dpi_utils.h +++ b/shell/platform/windows/dpi_utils.h @@ -9,7 +9,13 @@ 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 nearest monitor is available. Otherwise, returns the system's DPI. UINT GetDpiForHWND(HWND hwnd); + +/// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only +/// supported on Per-Monitor V1 DPI awareness mode. BOOL EnableNonClientDpiScaling(HWND hwnd); } // namespace flutter From 869725aa1ae17237c52fb56ce1d8c57957c5b2f2 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 16:40:34 -0800 Subject: [PATCH 08/38] Clean --- .../platform/windows/client_wrapper/flutter_view_controller.cc | 1 + shell/platform/windows/public/flutter_windows.h | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/shell/platform/windows/client_wrapper/flutter_view_controller.cc b/shell/platform/windows/client_wrapper/flutter_view_controller.cc index 296b13e805c47..92a853862d77c 100644 --- a/shell/platform/windows/client_wrapper/flutter_view_controller.cc +++ b/shell/platform/windows/client_wrapper/flutter_view_controller.cc @@ -56,4 +56,5 @@ FlutterDesktopPluginRegistrarRef FlutterViewController::GetRegistrarForPlugin( } return FlutterDesktopGetPluginRegistrar(controller_, plugin_name.c_str()); } + } // namespace flutter diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index 06322dfa1e042..ca675c17155b7 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -8,11 +8,12 @@ #include #include -#include "Windows.h" #include "flutter_export.h" #include "flutter_messenger.h" #include "flutter_plugin_registrar.h" +#include "Windows.h" + #if defined(__cplusplus) extern "C" { #endif From e6aa6047f168bb935681d93bff29aa25632b3017 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 17:09:11 -0800 Subject: [PATCH 09/38] Comment --- shell/platform/windows/win32_window.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index cb0bec81eee5f..773421981e4ad 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -4,14 +4,12 @@ #include "flutter/shell/platform/windows/win32_window.h" -#include - #include "dpi_utils.h" namespace flutter { Win32Window::Win32Window() { - // Calling GetDpiForView with no HNWD returns the DPI from the nearest + // Calling GetDpiForHWND with no HNWD returns the DPI from the nearest // monitor, which is the best option as an initial DPI. If Per-Monitor V2 is // supported, |current_dpi_| should be updated in the WM_DPICHANGED message. current_dpi_ = GetDpiForHWND(nullptr); From ed6d9a2fef38ee3e163ff611c2e7a95d62c25038 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 3 Feb 2020 14:45:55 -0800 Subject: [PATCH 10/38] Address comments --- shell/platform/windows/dpi_utils.cc | 55 +++++++++---------- shell/platform/windows/dpi_utils.h | 6 +- shell/platform/windows/flutter_windows.cc | 4 +- .../platform/windows/public/flutter_windows.h | 8 +-- 4 files changed, 36 insertions(+), 37 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 7915c99b41edd..e90ea4a021b74 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -9,13 +9,14 @@ namespace { constexpr UINT kDefaultDpi = 96; template + +/// Retrieves a function named |name| from a given module in |comBaseModule| +/// into |outProc|. Returns a bool indicating whether the function was found. bool AssignProcAddress(HMODULE comBaseModule, const char* name, T*& outProc) { outProc = reinterpret_cast(GetProcAddress(comBaseModule, name)); return *outProc != nullptr; } -} // namespace - /// A helper class for abstracting various Windows DPI related functions across /// Windows OS versions. class Win32DpiHelper { @@ -27,11 +28,11 @@ class 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 nearest monitor is available. Otherwise, returns the system's DPI. - UINT GetDpi(HWND); + UINT GetDpiForWindow(HWND); - /// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only - /// supported on Per-Monitor V1 DPI awareness mode. - BOOL EnableNonClientDpiScaling(HWND 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); @@ -60,11 +61,8 @@ Win32DpiHelper::Win32DpiHelper() { return; } - dpi_for_window_supported_ = - (AssignProcAddress(user32_module_, "GetDpiForWindow", - get_dpi_for_window_) && - AssignProcAddress(user32_module_, "EnableNonClientDpiScaling", - enable_non_client_dpi_scaling_)); + dpi_for_window_supported_ = (AssignProcAddress( + user32_module_, "GetDpiForWindow", get_dpi_for_window_)); dpi_for_monitor_supported_ = (AssignProcAddress(shlib_module_, "GetDpiForMonitor", get_dpi_for_monitor_) && @@ -81,27 +79,17 @@ Win32DpiHelper::~Win32DpiHelper() { } } -BOOL Win32DpiHelper::EnableNonClientDpiScaling(HWND hwnd) { - if (enable_non_client_dpi_scaling_ == nullptr) { - return false; - } - return enable_non_client_dpi_scaling_(hwnd); -} - -UINT Win32DpiHelper::GetDpi(HWND hwnd) { +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 + // 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 = monitor_from_window_(hwnd, MONITOR_DEFAULTTONEAREST); - UINT dpi_x = 0, dpi_y = 0; - HRESULT result = - get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); - return SUCCEEDED(result) ? dpi_x : kDefaultDpi; + HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTOPRIMARY); + return GetDpiForMonitor(monitor); } HDC hdc = GetDC(hwnd); UINT dpi = GetDeviceCaps(hdc, LOGPIXELSX); @@ -109,16 +97,27 @@ UINT Win32DpiHelper::GetDpi(HWND hwnd) { 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, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); + return SUCCEEDED(result) ? dpi_x : kDefaultDpi; + } + return kDefaultDpi; +} + Win32DpiHelper* GetHelper() { static Win32DpiHelper* dpi_helper = new Win32DpiHelper(); return dpi_helper; } +} // namespace UINT GetDpiForHWND(HWND hwnd) { - return GetHelper()->GetDpi(hwnd); + return GetHelper()->GetDpiForWindow(hwnd); } -BOOL EnableNonClientDpiScaling(HWND hwnd) { - return GetHelper()->EnableNonClientDpiScaling(hwnd); +UINT GetDpiForMonitor(HMONITOR monitor) { + return GetHelper()->GetDpiForMonitor(monitor); } } // namespace flutter diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h index f460a5224a6c7..ea3e397951dcc 100644 --- a/shell/platform/windows/dpi_utils.h +++ b/shell/platform/windows/dpi_utils.h @@ -14,9 +14,9 @@ namespace flutter { /// the nearest monitor is available. Otherwise, returns the system's DPI. UINT GetDpiForHWND(HWND hwnd); -/// Enables scaling of non-client UI (scrolling bars, title bars, etc). Only -/// supported on Per-Monitor V1 DPI awareness mode. -BOOL EnableNonClientDpiScaling(HWND hwnd); +/// Returns the DPI of a given monitor. Defaults to 96 if the API is not +/// available. +UINT GetDpiForMonitor(HMONITOR monitor); } // namespace flutter diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index 9a9c4118a206c..e090714f9a4b0 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -187,8 +187,8 @@ UINT FlutterDesktopViewGetDpiForHWND(HWND hwnd) { return flutter::GetDpiForHWND(hwnd); } -BOOL FlutterDesktopEnableNonClientDpiScaling(HWND hwnd) { - return flutter::EnableNonClientDpiScaling(hwnd); +FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForMonitor(HMONITOR monitor) { + return flutter::GetDpiForMonitor(monitor); } FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index ca675c17155b7..a0598fa1d7320 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -79,12 +79,12 @@ 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 nearest monitor. +// of the primary monitor. FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForHWND(HWND hwnd); -// Use only for apps that support Per-Monitor V1 awereness mode. Not necessary -// for Per-Monitor V2. -FLUTTER_EXPORT BOOL FlutterDesktopEnableNonClientDpiScaling(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 FlutterDesktopViewGetDpiForMonitor(HMONITOR monitor); // Runs an instance of a headless Flutter engine. // From 186f56cbf6fdae6e8c0ecaf6ce72ec461ac12336 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 3 Feb 2020 14:50:37 -0800 Subject: [PATCH 11/38] Comment --- shell/platform/windows/dpi_utils.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index e90ea4a021b74..053ff4236cc8b 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -10,8 +10,8 @@ constexpr UINT kDefaultDpi = 96; template -/// Retrieves a function named |name| from a given module in |comBaseModule| -/// into |outProc|. Returns a bool indicating whether the function was found. +/// 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(GetProcAddress(comBaseModule, name)); return *outProc != nullptr; From 7954dbd5c2e170de22a6d4f8809c6d993da4ec54 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Tue, 4 Feb 2020 13:10:36 -0800 Subject: [PATCH 12/38] Review comments --- shell/platform/windows/dpi_utils.h | 2 +- shell/platform/windows/flutter_windows.cc | 4 ++-- shell/platform/windows/public/flutter_windows.h | 4 ++-- shell/platform/windows/win32_window.h | 1 - 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h index ea3e397951dcc..3895a2f2454be 100644 --- a/shell/platform/windows/dpi_utils.h +++ b/shell/platform/windows/dpi_utils.h @@ -11,7 +11,7 @@ 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 nearest monitor is available. Otherwise, returns the system's DPI. +/// 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 diff --git a/shell/platform/windows/flutter_windows.cc b/shell/platform/windows/flutter_windows.cc index e090714f9a4b0..58434a35fd253 100644 --- a/shell/platform/windows/flutter_windows.cc +++ b/shell/platform/windows/flutter_windows.cc @@ -183,11 +183,11 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef view) { return view->window->GetWindowHandle(); } -UINT FlutterDesktopViewGetDpiForHWND(HWND hwnd) { +UINT FlutterDesktopGetDpiForHWND(HWND hwnd) { return flutter::GetDpiForHWND(hwnd); } -FLUTTER_EXPORT UINT FlutterDesktopViewGetDpiForMonitor(HMONITOR monitor) { +UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor) { return flutter::GetDpiForMonitor(monitor); } diff --git a/shell/platform/windows/public/flutter_windows.h b/shell/platform/windows/public/flutter_windows.h index a0598fa1d7320..7626be1239c35 100644 --- a/shell/platform/windows/public/flutter_windows.h +++ b/shell/platform/windows/public/flutter_windows.h @@ -80,11 +80,11 @@ 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 FlutterDesktopViewGetDpiForHWND(HWND hwnd); +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 FlutterDesktopViewGetDpiForMonitor(HMONITOR monitor); +FLUTTER_EXPORT UINT FlutterDesktopGetDpiForMonitor(HMONITOR monitor); // Runs an instance of a headless Flutter engine. // diff --git a/shell/platform/windows/win32_window.h b/shell/platform/windows/win32_window.h index c24cab78d3da9..b0a1e10b3fa3a 100644 --- a/shell/platform/windows/win32_window.h +++ b/shell/platform/windows/win32_window.h @@ -11,7 +11,6 @@ #include #include - namespace flutter { // Struct holding the mouse state. The engine doesn't keep track of which mouse From c88380752dfe5d76cbcbdc5953ac9402b54b0f07 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Tue, 4 Feb 2020 13:34:44 -0800 Subject: [PATCH 13/38] GN --- shell/platform/windows/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 8c27b050d8853..df7839784e6fd 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -43,8 +43,8 @@ source_set("flutter_windows_source") { sources = [ "angle_surface_manager.cc", "angle_surface_manager.h", - "dpi_utils.h", "dpi_utils.cc", + "dpi_utils.h", "flutter_windows.cc", "key_event_handler.cc", "key_event_handler.h", From 676237643f541b6a209229ba3c53be364b6005a4 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 14:13:45 -0800 Subject: [PATCH 14/38] Add test --- ci/licenses_golden/licenses_flutter | 2 +- shell/platform/windows/BUILD.gn | 5 +- .../testing/stub_flutter_windows_api.cc | 7 +++ .../testing/stub_flutter_windows_api.h | 3 + .../testing/win32_flutter_window_test.cc | 1 + .../windows/testing/win32_window_test.cc | 41 +++++++++++++ .../windows/testing/win32_window_test.h | 58 +++++++++++++++++++ ...s.cc => win32_flutter_window_unittests.cc} | 0 .../platform/windows/win32_window_unittest.cc | 14 +++++ testing/run_tests.py | 1 + 10 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 shell/platform/windows/testing/win32_window_test.cc create mode 100644 shell/platform/windows/testing/win32_window_test.h rename shell/platform/windows/{win32_window_unittests.cc => win32_flutter_window_unittests.cc} (100%) create mode 100644 shell/platform/windows/win32_window_unittest.cc diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 0fb09282b57c3..964ae44d29eab 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1125,7 +1125,7 @@ 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 FILE: ../../../flutter/shell/platform/windows/win32_window.h -FILE: ../../../flutter/shell/platform/windows/win32_window_unittests.cc +FILE: ../../../flutter/shell/platform/windows/win32_flutter_window_unittests.cc FILE: ../../../flutter/shell/platform/windows/window_state.h FILE: ../../../flutter/shell/version/version.cc FILE: ../../../flutter/shell/version/version.h diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index df7839784e6fd..4be5ca2d0415c 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -113,7 +113,10 @@ executable("flutter_windows_unittests") { sources = [ "testing/win32_flutter_window_test.cc", "testing/win32_flutter_window_test.h", - "win32_window_unittests.cc", + "testing/win32_window_unittest.cc", + "testing/win32_window_unittest.h", + "win32_window_unittest.cc", + "win32_flutter_window_unittests.cc", ] public_configs = [ "$flutter_root:config" ] diff --git a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc index 07dfb8bd7bbbf..b8c00111eda6e 100644 --- a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc +++ b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc @@ -78,6 +78,13 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef controller) { return reinterpret_cast(-1); } +UINT FlutterDesktopGetDpiForHWND(HWND hwnd) { + if (s_stub_implementation) { + return s_stub_implementation->GetDpiForHWND(); + } + return 0; +} + FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, const char* icu_data_path, const char** arguments, diff --git a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h index 0fd74c5b275f5..c54bcd400c6bc 100644 --- a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h +++ b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h @@ -49,6 +49,9 @@ class StubFlutterWindowsApi { // Called for FlutterDesktopViewGetHWND. virtual HWND ViewGetHWND() { return reinterpret_cast(1); } + // Called for FlutterDesktopGetDpiForHWND. + virtual UINT GetDpiForHWND() { return 1; } + // Called for FlutterDesktopRunEngine. virtual FlutterDesktopEngineRef RunEngine(const char* assets_path, const char* icu_data_path, diff --git a/shell/platform/windows/testing/win32_flutter_window_test.cc b/shell/platform/windows/testing/win32_flutter_window_test.cc index 080262ae4bf88..cdb06ede12365 100644 --- a/shell/platform/windows/testing/win32_flutter_window_test.cc +++ b/shell/platform/windows/testing/win32_flutter_window_test.cc @@ -1,4 +1,5 @@ #include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" + #include namespace flutter { diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc new file mode 100644 index 0000000000000..bf651047290dd --- /dev/null +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -0,0 +1,41 @@ +#include "flutter/shell/platform/windows/testing/win32_window_unittest.h" + +#include + +namespace flutter { +namespace testing { + +Win32WindowTest::Win32WindowTest() : Win32Window(){}; + +Win32WindowTest::~Win32WindowTest() = default; + +void Win32WindowTest::OnDpiScale(unsigned int dpi){}; + +// When DesktopWindow notifies that a WM_Size message has come in +// lets FlutterEngine know about the new size. +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::OnClose() {} + +void Win32WindowTest::OnFontChange() {} + +UINT Win32WindowTest::GetDpi() { + return GetCurrentDPI(); +} + +} // namespace testing +} // namespace flutter diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h new file mode 100644 index 0000000000000..684b87357f40c --- /dev/null +++ b/shell/platform/windows/testing/win32_window_test.h @@ -0,0 +1,58 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "flutter/shell/platform/windows/win32_window.h" + +namespace flutter { +namespace testing { + +class Win32WindowTest : public Win32Window { + public: + Win32WindowTest(); + ~Win32WindowTest(); + + 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 OnClose(); + + // |Win32Window| + void OnFontChange() override; + + private: + FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); +}; + +} // namespace testing +} // namespace flutter diff --git a/shell/platform/windows/win32_window_unittests.cc b/shell/platform/windows/win32_flutter_window_unittests.cc similarity index 100% rename from shell/platform/windows/win32_window_unittests.cc rename to shell/platform/windows/win32_flutter_window_unittests.cc diff --git a/shell/platform/windows/win32_window_unittest.cc b/shell/platform/windows/win32_window_unittest.cc new file mode 100644 index 0000000000000..56f504bfaf50f --- /dev/null +++ b/shell/platform/windows/win32_window_unittest.cc @@ -0,0 +1,14 @@ +#include "flutter/shell/platform/windows/testing/win32_window_unittest.h" + +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(Win32WindowTest, GetDpiAfterCreate) { + Win32WindowTest window; + ASSERT_TRUE(window.GetDpi() > 0); +} + +} // namespace testing +} // namespace flutter diff --git a/testing/run_tests.py b/testing/run_tests.py index cb11932eae934..931215d8bb60c 100755 --- a/testing/run_tests.py +++ b/testing/run_tests.py @@ -357,6 +357,7 @@ def main(): default=False, help='Show extra dart snapshot logging.') args = parser.parse_args() + print args, '=====' if args.type == 'all': types = ['engine', 'dart', 'benchmarks', 'java', 'font-subset'] From 3b1ffc5f2b3360fc1a17f9b2554692ffaaa0bf9e Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 14:23:17 -0800 Subject: [PATCH 15/38] Clean --- shell/platform/windows/BUILD.gn | 6 +++--- .../client_wrapper/testing/stub_flutter_windows_api.cc | 7 ------- .../client_wrapper/testing/stub_flutter_windows_api.h | 3 --- shell/platform/windows/testing/win32_window_test.cc | 2 +- ...{win32_window_unittest.cc => win32_window_unittests.cc} | 2 +- 5 files changed, 5 insertions(+), 15 deletions(-) rename shell/platform/windows/{win32_window_unittest.cc => win32_window_unittests.cc} (75%) diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 4be5ca2d0415c..b75a3179360eb 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -113,9 +113,9 @@ executable("flutter_windows_unittests") { sources = [ "testing/win32_flutter_window_test.cc", "testing/win32_flutter_window_test.h", - "testing/win32_window_unittest.cc", - "testing/win32_window_unittest.h", - "win32_window_unittest.cc", + "testing/win32_window_test.cc", + "testing/win32_window_test.h", + "win32_window_unittests.cc", "win32_flutter_window_unittests.cc", ] diff --git a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc index b8c00111eda6e..07dfb8bd7bbbf 100644 --- a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc +++ b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.cc @@ -78,13 +78,6 @@ HWND FlutterDesktopViewGetHWND(FlutterDesktopViewRef controller) { return reinterpret_cast(-1); } -UINT FlutterDesktopGetDpiForHWND(HWND hwnd) { - if (s_stub_implementation) { - return s_stub_implementation->GetDpiForHWND(); - } - return 0; -} - FlutterDesktopEngineRef FlutterDesktopRunEngine(const char* assets_path, const char* icu_data_path, const char** arguments, diff --git a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h index c54bcd400c6bc..0fd74c5b275f5 100644 --- a/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h +++ b/shell/platform/windows/client_wrapper/testing/stub_flutter_windows_api.h @@ -49,9 +49,6 @@ class StubFlutterWindowsApi { // Called for FlutterDesktopViewGetHWND. virtual HWND ViewGetHWND() { return reinterpret_cast(1); } - // Called for FlutterDesktopGetDpiForHWND. - virtual UINT GetDpiForHWND() { return 1; } - // Called for FlutterDesktopRunEngine. virtual FlutterDesktopEngineRef RunEngine(const char* assets_path, const char* icu_data_path, diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index bf651047290dd..ed889d8ea78b1 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -1,4 +1,4 @@ -#include "flutter/shell/platform/windows/testing/win32_window_unittest.h" +#include "flutter/shell/platform/windows/testing/win32_window_test.h" #include diff --git a/shell/platform/windows/win32_window_unittest.cc b/shell/platform/windows/win32_window_unittests.cc similarity index 75% rename from shell/platform/windows/win32_window_unittest.cc rename to shell/platform/windows/win32_window_unittests.cc index 56f504bfaf50f..38e62eda77eae 100644 --- a/shell/platform/windows/win32_window_unittest.cc +++ b/shell/platform/windows/win32_window_unittests.cc @@ -1,4 +1,4 @@ -#include "flutter/shell/platform/windows/testing/win32_window_unittest.h" +#include "flutter/shell/platform/windows/testing/win32_window_test.h" #include "gtest/gtest.h" From 31c39d23576e616df8a78827e8355e66bec221d3 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 14:40:56 -0800 Subject: [PATCH 16/38] Clean --- ci/licenses_golden/licenses_flutter | 3 ++- shell/platform/windows/dpi_utils.cc | 2 +- shell/platform/windows/testing/win32_flutter_window_test.cc | 1 - testing/run_tests.py | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 964ae44d29eab..586168242bb42 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1121,11 +1121,12 @@ FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc FILE: ../../../flutter/shell/platform/windows/text_input_plugin.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 FILE: ../../../flutter/shell/platform/windows/win32_window.h -FILE: ../../../flutter/shell/platform/windows/win32_flutter_window_unittests.cc +FILE: ../../../flutter/shell/platform/windows/win32_window_unittests.cc FILE: ../../../flutter/shell/platform/windows/window_state.h FILE: ../../../flutter/shell/version/version.cc FILE: ../../../flutter/shell/version/version.h diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 053ff4236cc8b..c318534804dff 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -27,7 +27,7 @@ class 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 nearest monitor is available. Otherwise, returns the system's 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 diff --git a/shell/platform/windows/testing/win32_flutter_window_test.cc b/shell/platform/windows/testing/win32_flutter_window_test.cc index cdb06ede12365..080262ae4bf88 100644 --- a/shell/platform/windows/testing/win32_flutter_window_test.cc +++ b/shell/platform/windows/testing/win32_flutter_window_test.cc @@ -1,5 +1,4 @@ #include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" - #include namespace flutter { diff --git a/testing/run_tests.py b/testing/run_tests.py index 931215d8bb60c..cb11932eae934 100755 --- a/testing/run_tests.py +++ b/testing/run_tests.py @@ -357,7 +357,6 @@ def main(): default=False, help='Show extra dart snapshot logging.') args = parser.parse_args() - print args, '=====' if args.type == 'all': types = ['engine', 'dart', 'benchmarks', 'java', 'font-subset'] From fc9c47c97725e8c5dc2981cbb337f41400999b1f Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 15:20:42 -0800 Subject: [PATCH 17/38] Merge tests --- ci/licenses_golden/licenses_flutter | 1 - shell/platform/windows/BUILD.gn | 3 -- .../testing/win32_flutter_window_test.cc | 20 ------------ .../testing/win32_flutter_window_test.h | 31 ------------------- .../windows/testing/win32_window_test.cc | 10 +++++- .../windows/testing/win32_window_test.h | 6 ++++ .../windows/win32_flutter_window_unittests.cc | 22 ------------- .../windows/win32_window_unittests.cc | 20 ++++++++++-- 8 files changed, 33 insertions(+), 80 deletions(-) delete mode 100644 shell/platform/windows/testing/win32_flutter_window_test.cc delete mode 100644 shell/platform/windows/testing/win32_flutter_window_test.h delete mode 100644 shell/platform/windows/win32_flutter_window_unittests.cc diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 586168242bb42..0fb09282b57c3 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1121,7 +1121,6 @@ FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc FILE: ../../../flutter/shell/platform/windows/text_input_plugin.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 diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index b75a3179360eb..1d156979e5a65 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -111,12 +111,9 @@ executable("flutter_windows_unittests") { testonly = true sources = [ - "testing/win32_flutter_window_test.cc", - "testing/win32_flutter_window_test.h", "testing/win32_window_test.cc", "testing/win32_window_test.h", "win32_window_unittests.cc", - "win32_flutter_window_unittests.cc", ] public_configs = [ "$flutter_root:config" ] diff --git a/shell/platform/windows/testing/win32_flutter_window_test.cc b/shell/platform/windows/testing/win32_flutter_window_test.cc deleted file mode 100644 index 080262ae4bf88..0000000000000 --- a/shell/platform/windows/testing/win32_flutter_window_test.cc +++ /dev/null @@ -1,20 +0,0 @@ -#include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" -#include - -namespace flutter { -namespace testing { - -Win32FlutterWindowTest::Win32FlutterWindowTest(int width, int height) - : Win32FlutterWindow(width, height){}; - -Win32FlutterWindowTest::~Win32FlutterWindowTest() = default; - -void Win32FlutterWindowTest::OnFontChange() { - on_font_change_called_ = true; -} - -bool Win32FlutterWindowTest::OnFontChangeWasCalled() { - return on_font_change_called_; -} -} // namespace testing -} // namespace flutter diff --git a/shell/platform/windows/testing/win32_flutter_window_test.h b/shell/platform/windows/testing/win32_flutter_window_test.h deleted file mode 100644 index 361a6960a5fc6..0000000000000 --- a/shell/platform/windows/testing/win32_flutter_window_test.h +++ /dev/null @@ -1,31 +0,0 @@ -// 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 - -#include "flutter/fml/macros.h" -#include "flutter/shell/platform/windows/win32_flutter_window.h" - -namespace flutter { -namespace testing { - -class Win32FlutterWindowTest : public Win32FlutterWindow { - public: - Win32FlutterWindowTest(int width, int height); - - ~Win32FlutterWindowTest(); - - // |Win32Window| - void OnFontChange() override; - - bool OnFontChangeWasCalled(); - - private: - bool on_font_change_called_ = false; - - FML_DISALLOW_COPY_AND_ASSIGN(Win32FlutterWindowTest); -}; - -} // namespace testing -} // namespace flutter diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index ed889d8ea78b1..2eecafb586232 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -31,11 +31,19 @@ void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} void Win32WindowTest::OnClose() {} -void Win32WindowTest::OnFontChange() {} +void Win32WindowTest::OnFontChange() { + std::cerr << "Getting called ======\n"; + on_font_change_called_ = true; +} UINT Win32WindowTest::GetDpi() { return GetCurrentDPI(); } +bool Win32WindowTest::OnFontChangeWasCalled() { + std::cerr << "on font changed called====s\n"; + return on_font_change_called_; +} + } // namespace testing } // namespace flutter diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 684b87357f40c..6e815313bb4cf 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -15,8 +15,12 @@ class Win32WindowTest : public Win32Window { Win32WindowTest(); ~Win32WindowTest(); + // Wrapper for GetCurrentDPI() which is a protected method. UINT GetDpi(); + // Returns a bool whether OnFontChange() was called. + bool OnFontChangeWasCalled(); + // |Win32Window| void OnDpiScale(unsigned int dpi) override; @@ -51,6 +55,8 @@ class Win32WindowTest : public Win32Window { void OnFontChange() override; private: + bool on_font_change_called_ = false; + FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); }; diff --git a/shell/platform/windows/win32_flutter_window_unittests.cc b/shell/platform/windows/win32_flutter_window_unittests.cc deleted file mode 100644 index d04458492dff1..0000000000000 --- a/shell/platform/windows/win32_flutter_window_unittests.cc +++ /dev/null @@ -1,22 +0,0 @@ -#include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" -#include "gtest/gtest.h" - -namespace flutter { -namespace testing { - -TEST(Win32FlutterWindowTest, CreateDestroy) { - Win32FlutterWindowTest window(800, 600); - ASSERT_TRUE(TRUE); -} - -TEST(Win32FlutterWindowTest, CanFontChange) { - Win32FlutterWindowTest window(800, 600); - HWND hwnd = window.GetWindowHandle(); - LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); - ASSERT_EQ(result, 0); - ASSERT_TRUE(window.OnFontChangeWasCalled()); - ASSERT_TRUE(TRUE); -} - -} // namespace testing -} // namespace flutter diff --git a/shell/platform/windows/win32_window_unittests.cc b/shell/platform/windows/win32_window_unittests.cc index 38e62eda77eae..24b9d515d716c 100644 --- a/shell/platform/windows/win32_window_unittests.cc +++ b/shell/platform/windows/win32_window_unittests.cc @@ -1,14 +1,30 @@ #include "flutter/shell/platform/windows/testing/win32_window_test.h" - #include "gtest/gtest.h" namespace flutter { namespace testing { +TEST(Win32WindowTest, CreateDestroy) { + Win32WindowTest window; + ASSERT_TRUE(TRUE); +} + +TEST(Win32WindowTest, CanFontChange) { + Win32WindowTest window; + window.InitializeChild("FLUTTERVIEW", 10, 10); + + HWND hwnd = window.GetWindowHandle(); + LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); + ASSERT_EQ(result, 0); + ASSERT_TRUE(window.OnFontChangeWasCalled()); + ASSERT_TRUE(TRUE); + + window.Destroy(); +} + TEST(Win32WindowTest, GetDpiAfterCreate) { Win32WindowTest window; ASSERT_TRUE(window.GetDpi() > 0); } - } // namespace testing } // namespace flutter From 226581ecf453c5c4010af0144c70cd620faf851e Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 15:22:29 -0800 Subject: [PATCH 18/38] Comments and format --- shell/platform/windows/testing/win32_window_test.cc | 4 ---- shell/platform/windows/testing/win32_window_test.h | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index 2eecafb586232..03b0d42a88d5e 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -1,7 +1,5 @@ #include "flutter/shell/platform/windows/testing/win32_window_test.h" -#include - namespace flutter { namespace testing { @@ -32,7 +30,6 @@ void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} void Win32WindowTest::OnClose() {} void Win32WindowTest::OnFontChange() { - std::cerr << "Getting called ======\n"; on_font_change_called_ = true; } @@ -41,7 +38,6 @@ UINT Win32WindowTest::GetDpi() { } bool Win32WindowTest::OnFontChangeWasCalled() { - std::cerr << "on font changed called====s\n"; return on_font_change_called_; } diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 6e815313bb4cf..07f4fe841f777 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -55,7 +55,7 @@ class Win32WindowTest : public Win32Window { void OnFontChange() override; private: - bool on_font_change_called_ = false; + bool on_font_change_called_ = false; FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); }; From c9e38c8433dee6eee1ad69bc98e92520504ff62c Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 16:05:18 -0800 Subject: [PATCH 19/38] license --- ci/licenses_golden/licenses_flutter | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 0fb09282b57c3..a933c9d6b6f39 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1108,8 +1108,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.h 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 From 9e8ccfb754f8b3346620194540ec5e2b4e81140a Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 22:36:17 -0800 Subject: [PATCH 20/38] Comments --- shell/platform/windows/testing/win32_window_test.h | 2 +- shell/platform/windows/win32_window.cc | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 07f4fe841f777..7b6f9f016f5cf 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -49,7 +49,7 @@ class Win32WindowTest : public Win32Window { void OnScroll(double delta_x, double delta_y) override; // |Win32Window| - void OnClose(); + void OnClose() override; // |Win32Window| void OnFontChange() override; diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index 773421981e4ad..bd8907573be30 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -9,7 +9,7 @@ namespace flutter { Win32Window::Win32Window() { - // Calling GetDpiForHWND with no HNWD returns the DPI from the nearest + // Calling GetDpiForHWND with no HNWD returns the DPI from the primary // monitor, which is the best option as an initial DPI. If Per-Monitor V2 is // supported, |current_dpi_| should be updated in the WM_DPICHANGED message. current_dpi_ = GetDpiForHWND(nullptr); @@ -81,7 +81,6 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - that->window_handle_ = window; } else if (Win32Window* that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); From efa8fcc7907ea858088e61cdf7aa113e4d907720 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 14:45:31 -0800 Subject: [PATCH 21/38] Virtual destructors --- .../testing/win32_flutter_window_test.cc | 19 ++++++++++++ .../testing/win32_flutter_window_test.h | 31 +++++++++++++++++++ .../platform/windows/win32_flutter_window.cc | 8 ++--- shell/platform/windows/win32_flutter_window.h | 11 +++---- shell/platform/windows/win32_window.cc | 5 --- shell/platform/windows/win32_window.h | 8 ++--- .../windows/win32_window_unittests.cc | 3 -- 7 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 shell/platform/windows/testing/win32_flutter_window_test.cc create mode 100644 shell/platform/windows/testing/win32_flutter_window_test.h diff --git a/shell/platform/windows/testing/win32_flutter_window_test.cc b/shell/platform/windows/testing/win32_flutter_window_test.cc new file mode 100644 index 0000000000000..bae1d9cd8d5b3 --- /dev/null +++ b/shell/platform/windows/testing/win32_flutter_window_test.cc @@ -0,0 +1,19 @@ +#include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" + +namespace flutter { +namespace testing { + +Win32FlutterWindowTest::Win32FlutterWindowTest(int width, int height) + : Win32FlutterWindow(width, height){}; + +Win32FlutterWindowTest::~Win32FlutterWindowTest() = default; + +void Win32FlutterWindowTest::OnFontChange() { + on_font_change_called_ = true; +} + +bool Win32FlutterWindowTest::OnFontChangeWasCalled() { + return on_font_change_called_; +} +} // namespace testing +} // namespace flutter diff --git a/shell/platform/windows/testing/win32_flutter_window_test.h b/shell/platform/windows/testing/win32_flutter_window_test.h new file mode 100644 index 0000000000000..24a53f7c529c6 --- /dev/null +++ b/shell/platform/windows/testing/win32_flutter_window_test.h @@ -0,0 +1,31 @@ +// 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 + +#include "flutter/fml/macros.h" +#include "flutter/shell/platform/windows/win32_flutter_window.h" + +namespace flutter { +namespace testing { + +class Win32FlutterWindowTest : public Win32FlutterWindow { + public: + Win32FlutterWindowTest(int width, int height); + + virtual ~Win32FlutterWindowTest(); + + // |Win32Window| + void OnFontChange() override; + + bool OnFontChangeWasCalled(); + + private: + bool on_font_change_called_ = false; + + FML_DISALLOW_COPY_AND_ASSIGN(Win32FlutterWindowTest); +}; + +} // namespace testing +} // namespace flutter diff --git a/shell/platform/windows/win32_flutter_window.cc b/shell/platform/windows/win32_flutter_window.cc index b5d8de160a60d..a3c7fcd11c91e 100644 --- a/shell/platform/windows/win32_flutter_window.cc +++ b/shell/platform/windows/win32_flutter_window.cc @@ -4,7 +4,7 @@ namespace flutter { -// the Windows DPI system is based on this +// The Windows DPI system is based on this // constant for machines running at 100% scaling. constexpr int base_dpi = 96; @@ -15,7 +15,6 @@ Win32FlutterWindow::Win32FlutterWindow(int width, int height) { Win32FlutterWindow::~Win32FlutterWindow() { DestroyRenderSurface(); - Win32Window::Destroy(); } FlutterDesktopViewControllerRef Win32FlutterWindow::CreateWin32FlutterWindow( @@ -177,9 +176,7 @@ void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) { } } -void Win32FlutterWindow::OnClose() { - messageloop_running_ = false; -} +void Win32FlutterWindow::OnClose() {} void Win32FlutterWindow::OnFontChange() { if (engine_ == nullptr) { @@ -365,5 +362,4 @@ void Win32FlutterWindow::DestroyRenderSurface() { } render_surface = EGL_NO_SURFACE; } - } // namespace flutter diff --git a/shell/platform/windows/win32_flutter_window.h b/shell/platform/windows/win32_flutter_window.h index 4d08b4117dabf..6611d1493d4cb 100644 --- a/shell/platform/windows/win32_flutter_window.h +++ b/shell/platform/windows/win32_flutter_window.h @@ -33,7 +33,7 @@ class Win32FlutterWindow : public Win32Window { // Create flutter Window for use as child window Win32FlutterWindow(int width, int height); - ~Win32FlutterWindow(); + virtual ~Win32FlutterWindow(); static FlutterDesktopViewControllerRef Win32FlutterWindow::CreateWin32FlutterWindow(int width, int height); @@ -85,9 +85,6 @@ class Win32FlutterWindow : public Win32Window { // Create a surface for Flutter engine to render into. void CreateRenderSurface(); - // Destroy current rendering surface if one has been allocated. - void DestroyRenderSurface(); - // Callbacks for clearing context, settings context and swapping buffers. bool ClearContext(); bool MakeCurrent(); @@ -99,6 +96,9 @@ class Win32FlutterWindow : public Win32Window { void SendWindowMetrics(); private: + // Destroy current rendering surface if one has been allocated. + void DestroyRenderSurface(); + // Reports a mouse movement to Flutter engine. void SendPointerMove(double x, double y); @@ -174,9 +174,6 @@ class Win32FlutterWindow : public Win32Window { // should we forword input messages or not bool process_events_ = false; - - // flag indicating if the message loop should be running - bool messageloop_running_ = false; }; } // namespace flutter diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index bd8907573be30..1be6de36b56e5 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -118,10 +118,6 @@ Win32Window::MessageHandler(HWND hwnd, case kWmDpiChangedBeforeParent: return HandleDpiChange(window_handle_, wparam, lparam, false); break; - case WM_DESTROY: - window->OnClose(); - return 0; - break; case WM_SIZE: width = LOWORD(lparam); height = HIWORD(lparam); @@ -254,7 +250,6 @@ void Win32Window::Destroy() { DestroyWindow(window_handle_); window_handle_ = nullptr; } - UnregisterClass(window_class_name_.c_str(), nullptr); } diff --git a/shell/platform/windows/win32_window.h b/shell/platform/windows/win32_window.h index b0a1e10b3fa3a..98caacbb1e37c 100644 --- a/shell/platform/windows/win32_window.h +++ b/shell/platform/windows/win32_window.h @@ -35,7 +35,7 @@ struct MouseState { class Win32Window { public: Win32Window(); - ~Win32Window(); + virtual ~Win32Window(); // Initializes as a child window with size using |width| and |height| and // |title| to identify the windowclass. Does not show window, window must be @@ -44,9 +44,6 @@ class Win32Window { unsigned int width, unsigned int height); - // Release OS resources asociated with window. - virtual void Destroy(); - HWND GetWindowHandle(); protected: @@ -148,6 +145,9 @@ class Win32Window { void SetMouseButtons(uint64_t buttons) { mouse_state_.buttons = buttons; } private: + // Release OS resources asociated with window. + void Destroy(); + // Activates tracking for a "mouse leave" event. void TrackMouseLeaveEvent(HWND hwnd); diff --git a/shell/platform/windows/win32_window_unittests.cc b/shell/platform/windows/win32_window_unittests.cc index 24b9d515d716c..576fe12349201 100644 --- a/shell/platform/windows/win32_window_unittests.cc +++ b/shell/platform/windows/win32_window_unittests.cc @@ -17,9 +17,6 @@ TEST(Win32WindowTest, CanFontChange) { LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); ASSERT_EQ(result, 0); ASSERT_TRUE(window.OnFontChangeWasCalled()); - ASSERT_TRUE(TRUE); - - window.Destroy(); } TEST(Win32WindowTest, GetDpiAfterCreate) { From 9dfbad6c439590e8ab5d40b3da944c9572ba427c Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 15:15:43 -0800 Subject: [PATCH 22/38] Move tests --- shell/platform/windows/BUILD.gn | 3 +++ .../windows/testing/win32_window_test.cc | 10 --------- .../windows/testing/win32_window_test.h | 5 ----- .../windows/win32_flutter_window_unittests.cc | 21 +++++++++++++++++++ .../windows/win32_window_unittests.cc | 10 --------- 5 files changed, 24 insertions(+), 25 deletions(-) create mode 100644 shell/platform/windows/win32_flutter_window_unittests.cc diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 1d156979e5a65..eacc7aedcc08d 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -111,8 +111,11 @@ executable("flutter_windows_unittests") { testonly = true 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", ] diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index 03b0d42a88d5e..65e55b0dd6e92 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -9,8 +9,6 @@ Win32WindowTest::~Win32WindowTest() = default; void Win32WindowTest::OnDpiScale(unsigned int dpi){}; -// When DesktopWindow notifies that a WM_Size message has come in -// lets FlutterEngine know about the new size. void Win32WindowTest::OnResize(unsigned int width, unsigned int height) {} void Win32WindowTest::OnPointerMove(double x, double y) {} @@ -29,17 +27,9 @@ void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} void Win32WindowTest::OnClose() {} -void Win32WindowTest::OnFontChange() { - on_font_change_called_ = true; -} - UINT Win32WindowTest::GetDpi() { return GetCurrentDPI(); } -bool Win32WindowTest::OnFontChangeWasCalled() { - return on_font_change_called_; -} - } // namespace testing } // namespace flutter diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 7b6f9f016f5cf..925f7178574e6 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -18,9 +18,6 @@ class Win32WindowTest : public Win32Window { // Wrapper for GetCurrentDPI() which is a protected method. UINT GetDpi(); - // Returns a bool whether OnFontChange() was called. - bool OnFontChangeWasCalled(); - // |Win32Window| void OnDpiScale(unsigned int dpi) override; @@ -55,8 +52,6 @@ class Win32WindowTest : public Win32Window { void OnFontChange() override; private: - bool on_font_change_called_ = false; - FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); }; diff --git a/shell/platform/windows/win32_flutter_window_unittests.cc b/shell/platform/windows/win32_flutter_window_unittests.cc new file mode 100644 index 0000000000000..dece5a280f807 --- /dev/null +++ b/shell/platform/windows/win32_flutter_window_unittests.cc @@ -0,0 +1,21 @@ +#include "flutter/shell/platform/windows/testing/win32_flutter_window_test.h" +#include "gtest/gtest.h" + +namespace flutter { +namespace testing { + +TEST(Win32FlutterWindowTest, CreateDestroy) { + Win32FlutterWindowTest window(800, 600); + ASSERT_TRUE(TRUE); +} + +TEST(Win32FlutterWindowTest, CanFontChange) { + Win32FlutterWindowTest window(800, 600); + HWND hwnd = window.GetWindowHandle(); + LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); + ASSERT_EQ(result, 0); + ASSERT_TRUE(window.OnFontChangeWasCalled()); +} + +} // namespace testing +} // namespace flutter diff --git a/shell/platform/windows/win32_window_unittests.cc b/shell/platform/windows/win32_window_unittests.cc index 576fe12349201..978bec4fc6462 100644 --- a/shell/platform/windows/win32_window_unittests.cc +++ b/shell/platform/windows/win32_window_unittests.cc @@ -9,16 +9,6 @@ TEST(Win32WindowTest, CreateDestroy) { ASSERT_TRUE(TRUE); } -TEST(Win32WindowTest, CanFontChange) { - Win32WindowTest window; - window.InitializeChild("FLUTTERVIEW", 10, 10); - - HWND hwnd = window.GetWindowHandle(); - LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); - ASSERT_EQ(result, 0); - ASSERT_TRUE(window.OnFontChangeWasCalled()); -} - TEST(Win32WindowTest, GetDpiAfterCreate) { Win32WindowTest window; ASSERT_TRUE(window.GetDpi() > 0); From 6e16a110d507fca7cee71b16c1d6710497ed49d8 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 15:42:42 -0800 Subject: [PATCH 23/38] Add on font change --- shell/platform/windows/testing/win32_window_test.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index 65e55b0dd6e92..881694fb5afa4 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -27,6 +27,8 @@ void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} void Win32WindowTest::OnClose() {} +void Win32WindowTest::OnFontChange() {} + UINT Win32WindowTest::GetDpi() { return GetCurrentDPI(); } From a4144e73990b71837bf3b437b4df38e701d904ac Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 16:03:48 -0800 Subject: [PATCH 24/38] Remove onclose --- shell/platform/windows/testing/win32_window_test.cc | 2 -- shell/platform/windows/testing/win32_window_test.h | 3 --- shell/platform/windows/win32_flutter_window.cc | 2 -- shell/platform/windows/win32_flutter_window.h | 3 --- shell/platform/windows/win32_window.h | 3 --- 5 files changed, 13 deletions(-) diff --git a/shell/platform/windows/testing/win32_window_test.cc b/shell/platform/windows/testing/win32_window_test.cc index 881694fb5afa4..13a70e039647c 100644 --- a/shell/platform/windows/testing/win32_window_test.cc +++ b/shell/platform/windows/testing/win32_window_test.cc @@ -25,8 +25,6 @@ void Win32WindowTest::OnKey(int key, int scancode, int action, int mods) {} void Win32WindowTest::OnScroll(double delta_x, double delta_y) {} -void Win32WindowTest::OnClose() {} - void Win32WindowTest::OnFontChange() {} UINT Win32WindowTest::GetDpi() { diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 925f7178574e6..c1a715806ba19 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -45,9 +45,6 @@ class Win32WindowTest : public Win32Window { // |Win32Window| void OnScroll(double delta_x, double delta_y) override; - // |Win32Window| - void OnClose() override; - // |Win32Window| void OnFontChange() override; diff --git a/shell/platform/windows/win32_flutter_window.cc b/shell/platform/windows/win32_flutter_window.cc index a3c7fcd11c91e..803732a20754f 100644 --- a/shell/platform/windows/win32_flutter_window.cc +++ b/shell/platform/windows/win32_flutter_window.cc @@ -176,8 +176,6 @@ void Win32FlutterWindow::OnScroll(double delta_x, double delta_y) { } } -void Win32FlutterWindow::OnClose() {} - void Win32FlutterWindow::OnFontChange() { if (engine_ == nullptr) { return; diff --git a/shell/platform/windows/win32_flutter_window.h b/shell/platform/windows/win32_flutter_window.h index 6611d1493d4cb..5e43ca4f34912 100644 --- a/shell/platform/windows/win32_flutter_window.h +++ b/shell/platform/windows/win32_flutter_window.h @@ -65,9 +65,6 @@ class Win32FlutterWindow : public Win32Window { // |Win32Window| void OnScroll(double delta_x, double delta_y) override; - // |Win32Window| - void OnClose(); - // |Win32Window| void OnFontChange() override; diff --git a/shell/platform/windows/win32_window.h b/shell/platform/windows/win32_window.h index 98caacbb1e37c..dfb5928048168 100644 --- a/shell/platform/windows/win32_window.h +++ b/shell/platform/windows/win32_window.h @@ -111,9 +111,6 @@ class Win32Window { // Called when mouse scrollwheel input occurs. virtual void OnScroll(double delta_x, double delta_y) = 0; - // Called when the user closes the Windows. - virtual void OnClose() = 0; - // Called when the system font change. virtual void OnFontChange() = 0; From d693ebb44b76eee4a38a3570136090472c339e22 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 16:28:06 -0800 Subject: [PATCH 25/38] Remove dpi changed and primary monitor change --- shell/platform/windows/win32_window.cc | 52 +++++--------------------- 1 file changed, 9 insertions(+), 43 deletions(-) diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index 1be6de36b56e5..d379e18940f2a 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -9,10 +9,12 @@ namespace flutter { Win32Window::Win32Window() { - // Calling GetDpiForHWND with no HNWD returns the DPI from the primary - // monitor, which is the best option as an initial DPI. If Per-Monitor V2 is - // supported, |current_dpi_| should be updated in the WM_DPICHANGED message. - current_dpi_ = GetDpiForHWND(nullptr); + // Get the DPI of the primary monitor as the initial DPI. If Per-Monitor V2 is + // supported, |current_dpi_| should be updated in the + // kWmDpiChangedBeforeParent message. + const POINT target_point = {static_cast(0), static_cast(0)}; + HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); + current_dpi_ = GetDpiForMonitor(monitor); } Win32Window::~Win32Window() { @@ -112,12 +114,10 @@ Win32Window::MessageHandler(HWND hwnd, UINT button_pressed = 0; if (window != nullptr) { switch (message) { - case WM_DPICHANGED: - return HandleDpiChange(window_handle_, wparam, lparam, true); - break; case kWmDpiChangedBeforeParent: - return HandleDpiChange(window_handle_, wparam, lparam, false); - break; + current_dpi_ = GetDpiForHWND(window_handle_); + window->OnDpiScale(current_dpi_); + return 0; case WM_SIZE: width = LOWORD(lparam); height = HIWORD(lparam); @@ -253,40 +253,6 @@ void Win32Window::Destroy() { UnregisterClass(window_class_name_.c_str(), nullptr); } -// DPI Change handler. on WM_DPICHANGE resize the window -LRESULT -Win32Window::HandleDpiChange(HWND hwnd, - WPARAM wparam, - LPARAM lparam, - bool toplevel) { - if (hwnd != nullptr) { - auto window = - reinterpret_cast(GetWindowLongPtr(hwnd, GWLP_USERDATA)); - - UINT uDpi = HIWORD(wparam); - - // The DPI is only passed for DPI change messages on top level windows, - // hence call function to get DPI if needed. - if (uDpi == 0) { - uDpi = GetDpiForHWND(hwnd); - } - current_dpi_ = uDpi; - window->OnDpiScale(uDpi); - - if (toplevel) { - // Resize the window only for toplevel windows which have a suggested - // size. - auto lprcNewScale = reinterpret_cast(lparam); - LONG newWidth = lprcNewScale->right - lprcNewScale->left; - LONG newHeight = lprcNewScale->bottom - lprcNewScale->top; - - SetWindowPos(hwnd, nullptr, lprcNewScale->left, lprcNewScale->top, - newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); - } - } - return 0; -} - void Win32Window::HandleResize(UINT width, UINT height) { current_width_ = width; current_height_ = height; From 589923751ecd9d9ab631c9f9615ab23435a48a0c Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 6 Feb 2020 17:31:27 -0800 Subject: [PATCH 26/38] Fix --- shell/platform/windows/win32_window_unittests.cc | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/shell/platform/windows/win32_window_unittests.cc b/shell/platform/windows/win32_window_unittests.cc index 812643c3f6a0c..0adc0b0388389 100644 --- a/shell/platform/windows/win32_window_unittests.cc +++ b/shell/platform/windows/win32_window_unittests.cc @@ -9,18 +9,10 @@ TEST(Win32WindowTest, CreateDestroy) { ASSERT_TRUE(TRUE); } -<<<<<<< HEAD TEST(Win32WindowTest, GetDpiAfterCreate) { Win32WindowTest window; ASSERT_TRUE(window.GetDpi() > 0); -======= -TEST(Win32FlutterWindowTest, CanFontChange) { - Win32FlutterWindowTest window(800, 600); - HWND hwnd = window.GetWindowHandle(); - LRESULT result = SendMessage(hwnd, WM_FONTCHANGE, NULL, NULL); - ASSERT_EQ(result, 0); - ASSERT_TRUE(window.OnFontChangeWasCalled()); ->>>>>>> master } + } // namespace testing } // namespace flutter From f4a225a28fd32dc86d946c61eab157a3b3705b08 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 08:41:58 -0800 Subject: [PATCH 27/38] monitor type enum --- shell/platform/windows/dpi_utils.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index c318534804dff..33104e36a4090 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -1,13 +1,16 @@ #include "dpi_utils.h" -#include - 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 /// Retrieves a function |name| from a given |comBaseModule| into |outProc|. @@ -37,7 +40,7 @@ class Win32DpiHelper { private: using GetDpiForWindow_ = UINT __stdcall(HWND); using GetDpiForMonitor_ = HRESULT __stdcall(HMONITOR hmonitor, - MONITOR_DPI_TYPE dpiType, + UINT dpiType, UINT* dpiX, UINT* dpiY); using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); @@ -101,7 +104,7 @@ UINT Win32DpiHelper::GetDpiForMonitor(HMONITOR monitor) { if (dpi_for_monitor_supported_) { UINT dpi_x = 0, dpi_y = 0; HRESULT result = - get_dpi_for_monitor_(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); + get_dpi_for_monitor_(monitor, kEffectiveDpiMonitorType, &dpi_x, &dpi_y); return SUCCEEDED(result) ? dpi_x : kDefaultDpi; } return kDefaultDpi; From 93c1aebe31b60f1869c8410e98bb76bc4e61d38a Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 08:48:56 -0800 Subject: [PATCH 28/38] Use MonitorFromWindow --- shell/platform/windows/dpi_utils.cc | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 33104e36a4090..d775844aa5c90 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -43,12 +43,10 @@ class Win32DpiHelper { UINT dpiType, UINT* dpiX, UINT* dpiY); - using MonitorFromWindow_ = HMONITOR __stdcall(HWND hwnd, DWORD dwFlags); using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); GetDpiForWindow_* get_dpi_for_window_ = nullptr; GetDpiForMonitor_* get_dpi_for_monitor_ = nullptr; - MonitorFromWindow_* monitor_from_window_ = nullptr; EnableNonClientDpiScaling_* enable_non_client_dpi_scaling_ = nullptr; HMODULE user32_module_ = nullptr; @@ -66,11 +64,8 @@ Win32DpiHelper::Win32DpiHelper() { dpi_for_window_supported_ = (AssignProcAddress( user32_module_, "GetDpiForWindow", get_dpi_for_window_)); - dpi_for_monitor_supported_ = - (AssignProcAddress(shlib_module_, "GetDpiForMonitor", - get_dpi_for_monitor_) && - AssignProcAddress(user32_module_, "MonitorFromWindow", - monitor_from_window_)); + dpi_for_monitor_supported_ = AssignProcAddress( + shlib_module_, "GetDpiForMonitor", get_dpi_for_monitor_); } Win32DpiHelper::~Win32DpiHelper() { @@ -91,7 +86,7 @@ UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { } if (dpi_for_monitor_supported_) { - HMONITOR monitor = monitor_from_window_(hwnd, MONITOR_DEFAULTTOPRIMARY); + HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); return GetDpiForMonitor(monitor); } HDC hdc = GetDC(hwnd); From 688580cb3764ed8b0b0fee4b80b482f474b51532 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 08:56:20 -0800 Subject: [PATCH 29/38] space --- shell/platform/windows/win32_window.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index d379e18940f2a..626df9ce9dc34 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -250,6 +250,7 @@ void Win32Window::Destroy() { DestroyWindow(window_handle_); window_handle_ = nullptr; } + UnregisterClass(window_class_name_.c_str(), nullptr); } From 838a9c2721d1c5611535ecc400e4e57f42a8dea0 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 09:40:17 -0800 Subject: [PATCH 30/38] Default to primary correctly --- shell/platform/windows/dpi_utils.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index d775844aa5c90..b0074f8a62d8a 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -86,7 +86,13 @@ UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { } if (dpi_for_monitor_supported_) { - HMONITOR monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); + HMONITOR monitor; + if (hwnd == nullptr) { + const POINT target_point = {static_cast(0), static_cast(0)}; + monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); + } else { + monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); + } return GetDpiForMonitor(monitor); } HDC hdc = GetDC(hwnd); From 2a98f8c9e3969ad227a47719ee5569fd1410dea6 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 10:41:34 -0800 Subject: [PATCH 31/38] License --- ci/licenses_golden/licenses_flutter | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 46781e3ed3507..da25e74f8e907 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1129,6 +1129,7 @@ FILE: ../../../flutter/shell/platform/windows/text_input_plugin.cc FILE: ../../../flutter/shell/platform/windows/text_input_plugin.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 From 4ba92efac0674cef6bd29fc039617523f9a16d83 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 13:43:28 -0800 Subject: [PATCH 32/38] Most comments --- shell/platform/windows/dpi_utils.cc | 22 +++++++++---------- shell/platform/windows/dpi_utils.h | 7 +++--- .../testing/win32_flutter_window_test.h | 1 + .../windows/testing/win32_window_test.h | 2 ++ shell/platform/windows/win32_window.cc | 4 +--- 5 files changed, 19 insertions(+), 17 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index b0074f8a62d8a..d54255d07b676 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -56,16 +56,14 @@ class Win32DpiHelper { }; Win32DpiHelper::Win32DpiHelper() { - user32_module_ = LoadLibraryA("User32.dll"); - shlib_module_ = LoadLibraryA("Shcore.dll"); - if (shlib_module_ == nullptr && user32_module_ == nullptr) { - return; + 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_); } - - 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() { @@ -88,7 +86,7 @@ UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { if (dpi_for_monitor_supported_) { HMONITOR monitor; if (hwnd == nullptr) { - const POINT target_point = {static_cast(0), static_cast(0)}; + const POINT target_point = {0, 0}; monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); } else { monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); @@ -106,7 +104,9 @@ UINT Win32DpiHelper::GetDpiForMonitor(HMONITOR monitor) { 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; + if (SUCCEEDED(result)) { + return dpi_x; + } } return kDefaultDpi; } diff --git a/shell/platform/windows/dpi_utils.h b/shell/platform/windows/dpi_utils.h index 3895a2f2454be..1c14994b61235 100644 --- a/shell/platform/windows/dpi_utils.h +++ b/shell/platform/windows/dpi_utils.h @@ -9,9 +9,10 @@ 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. +/// 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 diff --git a/shell/platform/windows/testing/win32_flutter_window_test.h b/shell/platform/windows/testing/win32_flutter_window_test.h index 24a53f7c529c6..98ee519def32b 100644 --- a/shell/platform/windows/testing/win32_flutter_window_test.h +++ b/shell/platform/windows/testing/win32_flutter_window_test.h @@ -10,6 +10,7 @@ namespace flutter { namespace testing { +/// Test class for Win32FlutterWindow. class Win32FlutterWindowTest : public Win32FlutterWindow { public: Win32FlutterWindowTest(int width, int height); diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index c1a715806ba19..4bb310e036b2a 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -10,6 +10,8 @@ namespace flutter { namespace testing { +/// Test class for the Win32Window base class. Used to access protected methods +/// for testing. class Win32WindowTest : public Win32Window { public: Win32WindowTest(); diff --git a/shell/platform/windows/win32_window.cc b/shell/platform/windows/win32_window.cc index 626df9ce9dc34..0abafb562fbf1 100644 --- a/shell/platform/windows/win32_window.cc +++ b/shell/platform/windows/win32_window.cc @@ -12,9 +12,7 @@ Win32Window::Win32Window() { // Get the DPI of the primary monitor as the initial DPI. If Per-Monitor V2 is // supported, |current_dpi_| should be updated in the // kWmDpiChangedBeforeParent message. - const POINT target_point = {static_cast(0), static_cast(0)}; - HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); - current_dpi_ = GetDpiForMonitor(monitor); + current_dpi_ = GetDpiForHWND(nullptr); } Win32Window::~Win32Window() { From 57c11aa7dc6ff4dfa6ac7d9f2bcb9423305fdf1b Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 13:46:12 -0800 Subject: [PATCH 33/38] Comment --- shell/platform/windows/dpi_utils.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index d54255d07b676..42b1ed0372ddc 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -28,9 +28,10 @@ class 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. + /// 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 From 20dcea8f3ae0bc92f28d40e675c60700df5ab763 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 14:34:02 -0800 Subject: [PATCH 34/38] Add test --- ci/licenses_golden/licenses_flutter | 1 + shell/platform/windows/BUILD.gn | 1 + 2 files changed, 2 insertions(+) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index da25e74f8e907..bbd6348acf5e8 100644 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -1118,6 +1118,7 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plu 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/dpi_utils_unittests.cc 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 diff --git a/shell/platform/windows/BUILD.gn b/shell/platform/windows/BUILD.gn index 6b5eee870bc38..fee227ea7c9e0 100644 --- a/shell/platform/windows/BUILD.gn +++ b/shell/platform/windows/BUILD.gn @@ -109,6 +109,7 @@ executable("flutter_windows_unittests") { testonly = true sources = [ + "dpi_utils_unittests.cc", "testing/win32_flutter_window_test.cc", "testing/win32_flutter_window_test.h", "testing/win32_window_test.cc", From 6b06bb2c1ee35ef545665f1484e82b98852fbb9d Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 14:36:34 -0800 Subject: [PATCH 35/38] Add test --- shell/platform/windows/dpi_utils_unittests.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 shell/platform/windows/dpi_utils_unittests.cc diff --git a/shell/platform/windows/dpi_utils_unittests.cc b/shell/platform/windows/dpi_utils_unittests.cc new file mode 100644 index 0000000000000..335a4b5d765a3 --- /dev/null +++ b/shell/platform/windows/dpi_utils_unittests.cc @@ -0,0 +1,18 @@ +#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, EqualDpis) { + ASSERT_EQ(GetDpiForHWND(nullptr), GetDpiForMonitor(nullptr)); + ASSERT_EQ(GetDpiForHWND(nullptr), 96); +}; + +} // namespace testing +} // namespace flutter From c379a981839fa10c6b07870adcb4a2362e7e7edc Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 14:37:29 -0800 Subject: [PATCH 36/38] Remove test line --- shell/platform/windows/dpi_utils_unittests.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/shell/platform/windows/dpi_utils_unittests.cc b/shell/platform/windows/dpi_utils_unittests.cc index 335a4b5d765a3..ba1449da4adac 100644 --- a/shell/platform/windows/dpi_utils_unittests.cc +++ b/shell/platform/windows/dpi_utils_unittests.cc @@ -11,7 +11,6 @@ TEST(DpiUtilsTest, NonZero) { TEST(DpiUtilsTest, EqualDpis) { ASSERT_EQ(GetDpiForHWND(nullptr), GetDpiForMonitor(nullptr)); - ASSERT_EQ(GetDpiForHWND(nullptr), 96); }; } // namespace testing From d8bce83ef01d2c0b18f4def0f4d53d216029768d Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Mon, 10 Feb 2020 14:49:42 -0800 Subject: [PATCH 37/38] Format --- shell/platform/windows/dpi_utils_unittests.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/windows/dpi_utils_unittests.cc b/shell/platform/windows/dpi_utils_unittests.cc index ba1449da4adac..66de44e96eb2a 100644 --- a/shell/platform/windows/dpi_utils_unittests.cc +++ b/shell/platform/windows/dpi_utils_unittests.cc @@ -5,8 +5,8 @@ namespace flutter { namespace testing { TEST(DpiUtilsTest, NonZero) { - ASSERT_GT(GetDpiForHWND(nullptr),0); - ASSERT_GT(GetDpiForMonitor(nullptr),0); + ASSERT_GT(GetDpiForHWND(nullptr), 0); + ASSERT_GT(GetDpiForMonitor(nullptr), 0); }; TEST(DpiUtilsTest, EqualDpis) { From 86ebe6ce400ac182871f8365a2577626a12a91e0 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Tue, 11 Feb 2020 09:27:37 -0800 Subject: [PATCH 38/38] Clean tests --- shell/platform/windows/dpi_utils.cc | 13 +++++++------ shell/platform/windows/dpi_utils_unittests.cc | 8 ++++++-- .../windows/testing/win32_flutter_window_test.h | 8 ++++---- shell/platform/windows/testing/win32_window_test.h | 10 +++++----- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/shell/platform/windows/dpi_utils.cc b/shell/platform/windows/dpi_utils.cc index 42b1ed0372ddc..20b706ef2fa95 100644 --- a/shell/platform/windows/dpi_utils.cc +++ b/shell/platform/windows/dpi_utils.cc @@ -85,11 +85,8 @@ UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { } if (dpi_for_monitor_supported_) { - HMONITOR monitor; - if (hwnd == nullptr) { - const POINT target_point = {0, 0}; - monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); - } else { + HMONITOR monitor = nullptr; + if (hwnd != nullptr) { monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); } return GetDpiForMonitor(monitor); @@ -102,6 +99,10 @@ UINT Win32DpiHelper::GetDpiForWindow(HWND hwnd) { 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); + } UINT dpi_x = 0, dpi_y = 0; HRESULT result = get_dpi_for_monitor_(monitor, kEffectiveDpiMonitorType, &dpi_x, &dpi_y); @@ -110,7 +111,7 @@ UINT Win32DpiHelper::GetDpiForMonitor(HMONITOR monitor) { } } return kDefaultDpi; -} +} // namespace Win32DpiHelper* GetHelper() { static Win32DpiHelper* dpi_helper = new Win32DpiHelper(); diff --git a/shell/platform/windows/dpi_utils_unittests.cc b/shell/platform/windows/dpi_utils_unittests.cc index 66de44e96eb2a..d69da0588827b 100644 --- a/shell/platform/windows/dpi_utils_unittests.cc +++ b/shell/platform/windows/dpi_utils_unittests.cc @@ -1,3 +1,5 @@ +#include + #include "flutter/shell/platform/windows/dpi_utils.h" #include "gtest/gtest.h" @@ -9,8 +11,10 @@ TEST(DpiUtilsTest, NonZero) { ASSERT_GT(GetDpiForMonitor(nullptr), 0); }; -TEST(DpiUtilsTest, EqualDpis) { - ASSERT_EQ(GetDpiForHWND(nullptr), GetDpiForMonitor(nullptr)); +TEST(DpiUtilsTest, NullHwndUsesPrimaryMonitor) { + const POINT target_point = {0, 0}; + HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTOPRIMARY); + ASSERT_EQ(GetDpiForHWND(nullptr), GetDpiForMonitor(monitor)); }; } // namespace testing diff --git a/shell/platform/windows/testing/win32_flutter_window_test.h b/shell/platform/windows/testing/win32_flutter_window_test.h index 98ee519def32b..6da56d9383a5b 100644 --- a/shell/platform/windows/testing/win32_flutter_window_test.h +++ b/shell/platform/windows/testing/win32_flutter_window_test.h @@ -4,7 +4,6 @@ #include -#include "flutter/fml/macros.h" #include "flutter/shell/platform/windows/win32_flutter_window.h" namespace flutter { @@ -14,9 +13,12 @@ namespace testing { class Win32FlutterWindowTest : public Win32FlutterWindow { public: Win32FlutterWindowTest(int width, int height); - virtual ~Win32FlutterWindowTest(); + // Prevent copying. + Win32FlutterWindowTest(Win32FlutterWindowTest const&) = delete; + Win32FlutterWindowTest& operator=(Win32FlutterWindowTest const&) = delete; + // |Win32Window| void OnFontChange() override; @@ -24,8 +26,6 @@ class Win32FlutterWindowTest : public Win32FlutterWindow { private: bool on_font_change_called_ = false; - - FML_DISALLOW_COPY_AND_ASSIGN(Win32FlutterWindowTest); }; } // namespace testing diff --git a/shell/platform/windows/testing/win32_window_test.h b/shell/platform/windows/testing/win32_window_test.h index 4bb310e036b2a..ccefdfe717dbf 100644 --- a/shell/platform/windows/testing/win32_window_test.h +++ b/shell/platform/windows/testing/win32_window_test.h @@ -4,7 +4,6 @@ #include -#include "flutter/fml/macros.h" #include "flutter/shell/platform/windows/win32_window.h" namespace flutter { @@ -15,7 +14,11 @@ namespace testing { class Win32WindowTest : public Win32Window { public: Win32WindowTest(); - ~Win32WindowTest(); + virtual ~Win32WindowTest(); + + // Prevent copying. + Win32WindowTest(Win32WindowTest const&) = delete; + Win32WindowTest& operator=(Win32WindowTest const&) = delete; // Wrapper for GetCurrentDPI() which is a protected method. UINT GetDpi(); @@ -49,9 +52,6 @@ class Win32WindowTest : public Win32Window { // |Win32Window| void OnFontChange() override; - - private: - FML_DISALLOW_COPY_AND_ASSIGN(Win32WindowTest); }; } // namespace testing