From c5ca35f06d48e44d809271c8a09b53c16ab423bb Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Thu, 30 Jan 2020 16:24:46 -0800 Subject: [PATCH 01/13] mostly works --- example/windows/main.cpp | 8 +++++++- example/windows/runner.exe.manifest | 2 +- example/windows/win32_window.cc | 23 ++++++++++++++++------- example/windows/win32_window.h | 2 +- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/example/windows/main.cpp b/example/windows/main.cpp index 0aefa0ed8..434a8f8de 100644 --- a/example/windows/main.cpp +++ b/example/windows/main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -78,7 +79,12 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t *command_line, // Create a top-level win32 window to host the Flutter view. Win32Window window; - if (!window.CreateAndShow(kFlutterWindowTitle, origin, size)) { + INT dpi = FlutterDesktopViewGetDpiForView(nullptr); + std::cerr << "App dpi\n"; + std::cerr << dpi << std::endl; + double scale_factor = static_cast(dpi) / 96; + + if (!window.CreateAndShow(kFlutterWindowTitle, origin, size, scale_factor)) { return EXIT_FAILURE; } diff --git a/example/windows/runner.exe.manifest b/example/windows/runner.exe.manifest index 41208def9..c977c4a42 100644 --- a/example/windows/runner.exe.manifest +++ b/example/windows/runner.exe.manifest @@ -17,4 +17,4 @@ - \ No newline at end of file + diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 2c5358404..2fa59013d 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -4,8 +4,10 @@ #include "win32_window.h" + #include "resource.h" #include "shellscalingapi.h" +#include namespace { @@ -28,17 +30,11 @@ Win32Window::Win32Window() {} Win32Window::~Win32Window() { Destroy(); } bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, - const Size &size) { + const Size &size, double scale_factor) { Destroy(); WNDCLASS window_class = RegisterWindowClass(); - HMONITOR defaut_monitor = - MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY); - UINT dpi_x = 0, dpi_y = 0; - GetDpiForMonitor(defaut_monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); - - double scale_factor = static_cast(dpi_x) / kBaseDpi; HWND window = CreateWindow( window_class.lpszClassName, title.c_str(), @@ -100,6 +96,19 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, Destroy(); return 0; + case WM_DPICHANGED: { + std::cerr << "Resizing\n"; + // 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; + } case WM_SIZE: RECT rect; GetClientRect(hwnd, &rect); diff --git a/example/windows/win32_window.h b/example/windows/win32_window.h index c6cf1bb63..01d1c2a7c 100644 --- a/example/windows/win32_window.h +++ b/example/windows/win32_window.h @@ -40,7 +40,7 @@ class Win32Window { // as logical pixels and scale to appropriate for the default monitor. Returns // true if the window was created successfully. bool CreateAndShow(const std::wstring &title, const Point &origin, - const Size &size); + const Size &size, double scale_factor); // Release OS resources asociated with window. void Destroy(); From f05ec501a4aa448aa41695c309ae77668c5a31a7 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 16:59:04 -0800 Subject: [PATCH 02/13] Clean --- example/windows/main.cpp | 14 +++++++++----- example/windows/win32_window.cc | 16 +++++----------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/example/windows/main.cpp b/example/windows/main.cpp index 434a8f8de..a22177b97 100644 --- a/example/windows/main.cpp +++ b/example/windows/main.cpp @@ -13,8 +13,8 @@ // limitations under the License. #include -#include #include +#include #include #include @@ -28,6 +28,10 @@ namespace { +// the Windows DPI system is based on this +// constant for machines running at 100% scaling. +constexpr int kBaseDpi = 96; + // Returns the path of the directory containing this executable, or an empty // string if the directory cannot be found. std::string GetExecutableDirectory() { @@ -79,10 +83,10 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t *command_line, // Create a top-level win32 window to host the Flutter view. Win32Window window; - INT dpi = FlutterDesktopViewGetDpiForView(nullptr); - std::cerr << "App dpi\n"; - std::cerr << dpi << std::endl; - double scale_factor = static_cast(dpi) / 96; + // Send a nullptr since the top-level window hasn't been created. This will + // get the neares monitor's DPI. + INT dpi = FlutterDesktopViewGetDpiForHWND(nullptr); + double scale_factor = static_cast(dpi) / kBaseDpi; if (!window.CreateAndShow(kFlutterWindowTitle, origin, size, scale_factor)) { return EXIT_FAILURE; diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 2fa59013d..cb8fd7075 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -4,17 +4,13 @@ #include "win32_window.h" +#include #include "resource.h" #include "shellscalingapi.h" -#include namespace { -// the Windows DPI system is based on this -// constant for machines running at 100% scaling. -constexpr int kBaseDpi = 96; - constexpr LPCWSTR kClassName = L"CLASSNAME"; // Scale helper to convert logical scaler values to physical using passed in @@ -35,7 +31,6 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, WNDCLASS window_class = RegisterWindowClass(); - HWND window = CreateWindow( window_class.lpszClassName, title.c_str(), WS_OVERLAPPEDWINDOW | WS_VISIBLE, Scale(origin.x, scale_factor), @@ -71,7 +66,7 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - + FlutterDesktopEnableNonClientDpiScaling(window); that->window_handle_ = window; } else if (Win32Window *that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); @@ -96,11 +91,10 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, Destroy(); return 0; - case WM_DPICHANGED: { - std::cerr << "Resizing\n"; + case WM_DPICHANGED: { // Resize the window only for toplevel windows which have a suggested // size. - auto lprcNewScale = reinterpret_cast(lparam); + auto lprcNewScale = reinterpret_cast(lparam); LONG newWidth = lprcNewScale->right - lprcNewScale->left; LONG newHeight = lprcNewScale->bottom - lprcNewScale->top; @@ -108,7 +102,7 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); return 0; - } + } case WM_SIZE: RECT rect; GetClientRect(hwnd, &rect); From dbdacd933f9e6846fcf779f2a06b662e0dd50455 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 31 Jan 2020 17:04:49 -0800 Subject: [PATCH 03/13] Format --- example/windows/main.cpp | 11 +---------- example/windows/win32_window.cc | 10 +++++++++- example/windows/win32_window.h | 2 +- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/example/windows/main.cpp b/example/windows/main.cpp index a22177b97..3623d6dd8 100644 --- a/example/windows/main.cpp +++ b/example/windows/main.cpp @@ -28,10 +28,6 @@ namespace { -// the Windows DPI system is based on this -// constant for machines running at 100% scaling. -constexpr int kBaseDpi = 96; - // Returns the path of the directory containing this executable, or an empty // string if the directory cannot be found. std::string GetExecutableDirectory() { @@ -83,12 +79,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t *command_line, // Create a top-level win32 window to host the Flutter view. Win32Window window; - // Send a nullptr since the top-level window hasn't been created. This will - // get the neares monitor's DPI. - INT dpi = FlutterDesktopViewGetDpiForHWND(nullptr); - double scale_factor = static_cast(dpi) / kBaseDpi; - - if (!window.CreateAndShow(kFlutterWindowTitle, origin, size, scale_factor)) { + if (!window.CreateAndShow(kFlutterWindowTitle, origin, size)) { return EXIT_FAILURE; } diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index cb8fd7075..92c6894cc 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -11,6 +11,10 @@ namespace { +// The Windows DPI system is based on this +// constant for machines running at 100% scaling. +constexpr int kBaseDpi = 96; + constexpr LPCWSTR kClassName = L"CLASSNAME"; // Scale helper to convert logical scaler values to physical using passed in @@ -26,10 +30,14 @@ Win32Window::Win32Window() {} Win32Window::~Win32Window() { Destroy(); } bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, - const Size &size, double scale_factor) { + const Size &size) { Destroy(); WNDCLASS window_class = RegisterWindowClass(); + // Send a nullptr since the top-level window hasn't been created. This will + // get the neares monitor's DPI. + INT dpi = FlutterDesktopViewGetDpiForHWND(nullptr); + double scale_factor = static_cast(dpi) / kBaseDpi; HWND window = CreateWindow( window_class.lpszClassName, title.c_str(), diff --git a/example/windows/win32_window.h b/example/windows/win32_window.h index 01d1c2a7c..c6cf1bb63 100644 --- a/example/windows/win32_window.h +++ b/example/windows/win32_window.h @@ -40,7 +40,7 @@ class Win32Window { // as logical pixels and scale to appropriate for the default monitor. Returns // true if the window was created successfully. bool CreateAndShow(const std::wstring &title, const Point &origin, - const Size &size, double scale_factor); + const Size &size); // Release OS resources asociated with window. void Destroy(); From 596c90cd3f54bafde39ca5a5d88b5ebcb7624c0a Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Tue, 4 Feb 2020 09:24:58 -0800 Subject: [PATCH 04/13] Enable dpi scaling --- example/windows/win32_window.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 92c6894cc..5d10d7461 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -7,7 +7,6 @@ #include #include "resource.h" -#include "shellscalingapi.h" namespace { @@ -17,12 +16,26 @@ constexpr int kBaseDpi = 96; constexpr LPCWSTR kClassName = L"CLASSNAME"; +using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); + // Scale helper to convert logical scaler values to physical using passed in // scale factor int Scale(int source, double scale_factor) { return static_cast(source * scale_factor); } +// Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. +// Appended with FDE to differentiate from the win32 API. +bool FDEEnableNonClientDpiScaling(HWND hwnd) { + HMODULE user32_module_ = LoadLibraryA("User32.dll"); + if (user32_module_) { + return false; + } + auto enable_non_client_dpi_scaling_ = + reinterpret_cast( + GetProcAddress(user32_module_, "EnableNonClientDpiScaling")); + return enable_non_client_dpi_scaling_(hwnd); +} } // namespace Win32Window::Win32Window() {} @@ -74,7 +87,7 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - FlutterDesktopEnableNonClientDpiScaling(window); + FDEEnableNonClientDpiScaling(window); that->window_handle_ = window; } else if (Win32Window *that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); @@ -109,7 +122,7 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, SetWindowPos(hwnd, nullptr, lprcNewScale->left, lprcNewScale->top, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); - return 0; + break; } case WM_SIZE: RECT rect; From c5674805253307065e10cf63d99e5202d9e135b1 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 16:06:37 -0800 Subject: [PATCH 05/13] Comments --- example/windows/main.cpp | 1 - example/windows/win32_window.cc | 23 ++++++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/example/windows/main.cpp b/example/windows/main.cpp index 3623d6dd8..0aefa0ed8 100644 --- a/example/windows/main.cpp +++ b/example/windows/main.cpp @@ -13,7 +13,6 @@ // limitations under the License. #include -#include #include #include diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 5d10d7461..9cf363529 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -26,15 +26,18 @@ int Scale(int source, double scale_factor) { // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. // Appended with FDE to differentiate from the win32 API. -bool FDEEnableNonClientDpiScaling(HWND hwnd) { +void EnableFullDpiSupportIfAvailable (HWND hwnd) { HMODULE user32_module_ = LoadLibraryA("User32.dll"); if (user32_module_) { - return false; + return; } auto enable_non_client_dpi_scaling_ = reinterpret_cast( GetProcAddress(user32_module_, "EnableNonClientDpiScaling")); - return enable_non_client_dpi_scaling_(hwnd); + + FreeLibrary(user32_module_); + + enable_non_client_dpi_scaling_(hwnd); } } // namespace @@ -49,7 +52,7 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, WNDCLASS window_class = RegisterWindowClass(); // Send a nullptr since the top-level window hasn't been created. This will // get the neares monitor's DPI. - INT dpi = FlutterDesktopViewGetDpiForHWND(nullptr); + INT dpi = FlutterDesktopGetDpiForHWND(nullptr); double scale_factor = static_cast(dpi) / kBaseDpi; HWND window = CreateWindow( @@ -87,7 +90,7 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - FDEEnableNonClientDpiScaling(window); + EnableFullDpiSupportIfAvailable(window); that->window_handle_ = window; } else if (Win32Window *that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); @@ -113,13 +116,11 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, return 0; case WM_DPICHANGED: { - // 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; + auto newRectSize = reinterpret_cast(lparam); + LONG newWidth = newRectSize->right - newRectSize->left; + LONG newHeight = newRectSize->bottom - newRectSize->top; - SetWindowPos(hwnd, nullptr, lprcNewScale->left, lprcNewScale->top, + SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); break; From 4045cd9261573d31e09f3f5b61b70309c60683bc Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 16:21:47 -0800 Subject: [PATCH 06/13] Add to testbed --- testbed/windows/win32_window.cc | 44 ++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index 2c5358404..61b8fab03 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -4,8 +4,9 @@ #include "win32_window.h" +#include + #include "resource.h" -#include "shellscalingapi.h" namespace { @@ -15,12 +16,30 @@ constexpr int kBaseDpi = 96; constexpr LPCWSTR kClassName = L"CLASSNAME"; +using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); + // Scale helper to convert logical scaler values to physical using passed in // scale factor int Scale(int source, double scale_factor) { return static_cast(source * scale_factor); } +// Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. +// Appended with FDE to differentiate from the win32 API. +void EnableFullDpiSupportIfAvailable (HWND hwnd) { + HMODULE user32_module_ = LoadLibraryA("User32.dll"); + if (user32_module_) { + return; + } + auto enable_non_client_dpi_scaling_ = + reinterpret_cast( + GetProcAddress(user32_module_, "EnableNonClientDpiScaling")); + + FreeLibrary(user32_module_); + + enable_non_client_dpi_scaling_(hwnd); +} + } // namespace Win32Window::Win32Window() {} @@ -32,13 +51,10 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, Destroy(); WNDCLASS window_class = RegisterWindowClass(); - - HMONITOR defaut_monitor = - MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY); - UINT dpi_x = 0, dpi_y = 0; - GetDpiForMonitor(defaut_monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); - - double scale_factor = static_cast(dpi_x) / kBaseDpi; + // Send a nullptr since the top-level window hasn't been created. This will + // get the neares monitor's DPI. + INT dpi = FlutterDesktopGetDpiForHWND(nullptr); + double scale_factor = static_cast(dpi) / kBaseDpi; HWND window = CreateWindow( window_class.lpszClassName, title.c_str(), @@ -75,7 +91,7 @@ LRESULT CALLBACK Win32Window::WndProc(HWND const window, UINT const message, reinterpret_cast(cs->lpCreateParams)); auto that = static_cast(cs->lpCreateParams); - + EnableFullDpiSupportIfAvailable(window); that->window_handle_ = window; } else if (Win32Window *that = GetThisFromHandle(window)) { return that->MessageHandler(window, message, wparam, lparam); @@ -100,6 +116,16 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, Destroy(); return 0; + case WM_DPICHANGED: { + auto newRectSize = reinterpret_cast(lparam); + LONG newWidth = newRectSize->right - newRectSize->left; + LONG newHeight = newRectSize->bottom - newRectSize->top; + + SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, + newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); + + break; + } case WM_SIZE: RECT rect; GetClientRect(hwnd, &rect); From 95b1c8b62dec79b23566382c744b046deda4a22e Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 16:31:26 -0800 Subject: [PATCH 07/13] Fix window size and format --- example/windows/win32_window.cc | 6 ++--- .../windows/window_size_plugin.cpp | 22 ++++++++++--------- testbed/windows/win32_window.cc | 6 ++--- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 9cf363529..1962514ba 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -26,7 +26,7 @@ int Scale(int source, double scale_factor) { // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. // Appended with FDE to differentiate from the win32 API. -void EnableFullDpiSupportIfAvailable (HWND hwnd) { +void EnableFullDpiSupportIfAvailable(HWND hwnd) { HMODULE user32_module_ = LoadLibraryA("User32.dll"); if (user32_module_) { return; @@ -120,8 +120,8 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, LONG newWidth = newRectSize->right - newRectSize->left; LONG newHeight = newRectSize->bottom - newRectSize->top; - SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, - newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); + SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, + newHeight, SWP_NOZORDER | SWP_NOACTIVATE); break; } diff --git a/plugins/window_size/windows/window_size_plugin.cpp b/plugins/window_size/windows/window_size_plugin.cpp index 423d00695..7a1283ddb 100644 --- a/plugins/window_size/windows/window_size_plugin.cpp +++ b/plugins/window_size/windows/window_size_plugin.cpp @@ -13,17 +13,17 @@ // limitations under the License. #include "window_size_plugin.h" -#include -#include - #include #include #include #include #include +#include +#include + +#include #include #include -#include namespace { @@ -64,9 +64,10 @@ EncodableValue GetPlatformChannelRepresentationForMonitor(HMONITOR monitor) { MONITORINFO info; info.cbSize = sizeof(MONITORINFO); GetMonitorInfo(monitor, &info); - UINT dpi_x, dpi_y; - GetDpiForMonitor(monitor, MDT_EFFECTIVE_DPI, &dpi_x, &dpi_y); - double scale_factor = dpi_x / kBaseDpi; + // Send a nullptr since the top-level window hasn't been created. This will + // get the neares monitor's DPI. + INT dpi = FlutterDesktopGetDpiForHWND(nullptr); + double scale_factor = static_cast(dpi) / kBaseDpi; return EncodableValue(EncodableMap{ {EncodableValue(kFrameKey), GetPlatformChannelRepresentationForRect(info.rcMonitor)}, @@ -94,7 +95,7 @@ EncodableValue GetPlatformChannelRepresentationForWindow(HWND window) { GetWindowRect(window, &frame); HMONITOR window_monitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); // TODO: Support fallback for systems older than Windows 10(1607). - double scale_factor = GetDpiForWindow(window) / kBaseDpi; + double scale_factor = FlutterDesktopGetDpiForHWND(window) / kBaseDpi; return EncodableValue(EncodableMap{ {EncodableValue(kFrameKey), @@ -185,8 +186,9 @@ void WindowSizePlugin::HandleMethodCall( return; } const auto &title = method_call.arguments()->StringValue(); - std::wstring wstr = std::wstring_convert< - std::codecvt_utf8_utf16, wchar_t>{}.from_bytes(title); + std::wstring wstr = + std::wstring_convert, wchar_t>{} + .from_bytes(title); SetWindowText(GetRootWindow(registrar_->GetView()), wstr.c_str()); result->Success(); } else { diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index 61b8fab03..f3bc9287d 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -26,7 +26,7 @@ int Scale(int source, double scale_factor) { // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. // Appended with FDE to differentiate from the win32 API. -void EnableFullDpiSupportIfAvailable (HWND hwnd) { +void EnableFullDpiSupportIfAvailable(HWND hwnd) { HMODULE user32_module_ = LoadLibraryA("User32.dll"); if (user32_module_) { return; @@ -121,8 +121,8 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, LONG newWidth = newRectSize->right - newRectSize->left; LONG newHeight = newRectSize->bottom - newRectSize->top; - SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, - newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); + SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, + newHeight, SWP_NOZORDER | SWP_NOACTIVATE); break; } From 71c445b1949fbed3deae343dbd1955f0ef8d8ea0 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 16:37:06 -0800 Subject: [PATCH 08/13] Return 0 --- example/windows/win32_window.cc | 2 +- testbed/windows/win32_window.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 1962514ba..d9c84fd2f 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -123,7 +123,7 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); - break; + return 0; } case WM_SIZE: RECT rect; diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index f3bc9287d..d7dab4201 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -124,7 +124,7 @@ Win32Window::MessageHandler(HWND hwnd, UINT const message, WPARAM const wparam, SetWindowPos(hwnd, nullptr, newRectSize->left, newRectSize->top, newWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE); - break; + return 0; } case WM_SIZE: RECT rect; From b0245b4b00f657e35e78b4b41e2c35726fe57a32 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 17:36:19 -0800 Subject: [PATCH 09/13] Reveiew --- example/windows/win32_window.cc | 16 ++++++++-------- .../window_size/windows/window_size_plugin.cpp | 10 +++++----- testbed/windows/runner.exe.manifest | 2 +- testbed/windows/win32_window.cc | 17 ++++++++--------- 4 files changed, 22 insertions(+), 23 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index d9c84fd2f..6a9af4482 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -25,19 +25,19 @@ int Scale(int source, double scale_factor) { } // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. -// Appended with FDE to differentiate from the win32 API. +// This API is only needed for PerMonitor V1 awareness mode. void EnableFullDpiSupportIfAvailable(HWND hwnd) { - HMODULE user32_module_ = LoadLibraryA("User32.dll"); - if (user32_module_) { + HMODULE user32_module = LoadLibraryA("User32.dll"); + if (!user32_module) { return; } - auto enable_non_client_dpi_scaling_ = + auto enable_non_client_dpi_scaling = reinterpret_cast( - GetProcAddress(user32_module_, "EnableNonClientDpiScaling")); + GetProcAddress(user32_module, "EnableNonClientDpiScaling")); - FreeLibrary(user32_module_); + enable_non_client_dpi_scaling(hwnd); - enable_non_client_dpi_scaling_(hwnd); + FreeLibrary(user32_module); } } // namespace @@ -51,7 +51,7 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, WNDCLASS window_class = RegisterWindowClass(); // Send a nullptr since the top-level window hasn't been created. This will - // get the neares monitor's DPI. + // get the primary monitor's DPI. INT dpi = FlutterDesktopGetDpiForHWND(nullptr); double scale_factor = static_cast(dpi) / kBaseDpi; diff --git a/plugins/window_size/windows/window_size_plugin.cpp b/plugins/window_size/windows/window_size_plugin.cpp index 7a1283ddb..d74f71a80 100644 --- a/plugins/window_size/windows/window_size_plugin.cpp +++ b/plugins/window_size/windows/window_size_plugin.cpp @@ -13,13 +13,16 @@ // limitations under the License. #include "window_size_plugin.h" +// windows.h must be imported before VersionHelpers.h or it will break +// compilation. +#include + #include #include #include #include #include #include -#include #include #include @@ -64,9 +67,7 @@ EncodableValue GetPlatformChannelRepresentationForMonitor(HMONITOR monitor) { MONITORINFO info; info.cbSize = sizeof(MONITORINFO); GetMonitorInfo(monitor, &info); - // Send a nullptr since the top-level window hasn't been created. This will - // get the neares monitor's DPI. - INT dpi = FlutterDesktopGetDpiForHWND(nullptr); + INT dpi = FlutterDesktopGetDpiForMonitor(monitor); double scale_factor = static_cast(dpi) / kBaseDpi; return EncodableValue(EncodableMap{ {EncodableValue(kFrameKey), @@ -94,7 +95,6 @@ EncodableValue GetPlatformChannelRepresentationForWindow(HWND window) { RECT frame; GetWindowRect(window, &frame); HMONITOR window_monitor = MonitorFromWindow(window, MONITOR_DEFAULTTOPRIMARY); - // TODO: Support fallback for systems older than Windows 10(1607). double scale_factor = FlutterDesktopGetDpiForHWND(window) / kBaseDpi; return EncodableValue(EncodableMap{ diff --git a/testbed/windows/runner.exe.manifest b/testbed/windows/runner.exe.manifest index 41208def9..c977c4a42 100644 --- a/testbed/windows/runner.exe.manifest +++ b/testbed/windows/runner.exe.manifest @@ -17,4 +17,4 @@ - \ No newline at end of file + diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index d7dab4201..b3f47395f 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -25,21 +25,20 @@ int Scale(int source, double scale_factor) { } // Dynamically loads the |EnableNonClientDpiScaling| from the User32 module. -// Appended with FDE to differentiate from the win32 API. +// This API is only needed for PerMonitor V1 awareness mode. void EnableFullDpiSupportIfAvailable(HWND hwnd) { - HMODULE user32_module_ = LoadLibraryA("User32.dll"); - if (user32_module_) { + HMODULE user32_module = LoadLibraryA("User32.dll"); + if (!user32_module) { return; } - auto enable_non_client_dpi_scaling_ = + auto enable_non_client_dpi_scaling = reinterpret_cast( - GetProcAddress(user32_module_, "EnableNonClientDpiScaling")); + GetProcAddress(user32_module, "EnableNonClientDpiScaling")); - FreeLibrary(user32_module_); + enable_non_client_dpi_scaling(hwnd); - enable_non_client_dpi_scaling_(hwnd); + FreeLibrary(user32_module); } - } // namespace Win32Window::Win32Window() {} @@ -52,7 +51,7 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, WNDCLASS window_class = RegisterWindowClass(); // Send a nullptr since the top-level window hasn't been created. This will - // get the neares monitor's DPI. + // get the primary monitor's DPI. INT dpi = FlutterDesktopGetDpiForHWND(nullptr); double scale_factor = static_cast(dpi) / kBaseDpi; From c05e593d78aad82aa54a2253c5401a3ad0d29f9c Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Wed, 5 Feb 2020 17:57:43 -0800 Subject: [PATCH 10/13] Remove direct linking --- example/windows/Runner.vcxproj | 4 ++-- plugins/file_chooser/windows/file_chooser.vcxproj | 2 +- plugins/window_size/windows/window_size.vcxproj | 6 +++--- testbed/windows/Runner.vcxproj | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/example/windows/Runner.vcxproj b/example/windows/Runner.vcxproj index cf0ab714f..0d48e0cd1 100644 --- a/example/windows/Runner.vcxproj +++ b/example/windows/Runner.vcxproj @@ -73,7 +73,7 @@ _MBCS;%(PreprocessorDefinitions) - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) @@ -120,7 +120,7 @@ true true - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) diff --git a/plugins/file_chooser/windows/file_chooser.vcxproj b/plugins/file_chooser/windows/file_chooser.vcxproj index 270a8c24d..6a8c75062 100644 --- a/plugins/file_chooser/windows/file_chooser.vcxproj +++ b/plugins/file_chooser/windows/file_chooser.vcxproj @@ -163,4 +163,4 @@ - \ No newline at end of file + diff --git a/plugins/window_size/windows/window_size.vcxproj b/plugins/window_size/windows/window_size.vcxproj index 99ccf74e7..510e66559 100644 --- a/plugins/window_size/windows/window_size.vcxproj +++ b/plugins/window_size/windows/window_size.vcxproj @@ -82,7 +82,7 @@ Windows true - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) @@ -124,7 +124,7 @@ true true true - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) @@ -163,4 +163,4 @@ - \ No newline at end of file + diff --git a/testbed/windows/Runner.vcxproj b/testbed/windows/Runner.vcxproj index cf0ab714f..0d48e0cd1 100644 --- a/testbed/windows/Runner.vcxproj +++ b/testbed/windows/Runner.vcxproj @@ -73,7 +73,7 @@ _MBCS;%(PreprocessorDefinitions) - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) @@ -120,7 +120,7 @@ true true - flutter_windows.dll.lib;Shcore.lib;%(AdditionalDependencies) + flutter_windows.dll.lib;%(AdditionalDependencies) From d5f5dbc570e40b6551e11429b3154ca5cd8e876e Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 09:07:46 -0800 Subject: [PATCH 11/13] Clean --- example/windows/win32_window.cc | 4 ++-- testbed/windows/win32_window.cc | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 6a9af4482..1aadaa474 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -16,7 +16,7 @@ constexpr int kBaseDpi = 96; constexpr LPCWSTR kClassName = L"CLASSNAME"; -using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); +using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd); // Scale helper to convert logical scaler values to physical using passed in // scale factor @@ -32,7 +32,7 @@ void EnableFullDpiSupportIfAvailable(HWND hwnd) { return; } auto enable_non_client_dpi_scaling = - reinterpret_cast( + reinterpret_cast( GetProcAddress(user32_module, "EnableNonClientDpiScaling")); enable_non_client_dpi_scaling(hwnd); diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index b3f47395f..d5d7faa9d 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -16,7 +16,7 @@ constexpr int kBaseDpi = 96; constexpr LPCWSTR kClassName = L"CLASSNAME"; -using EnableNonClientDpiScaling_ = BOOL __stdcall(HWND hwnd); +using EnableNonClientDpiScaling = BOOL __stdcall(HWND hwnd); // Scale helper to convert logical scaler values to physical using passed in // scale factor @@ -32,7 +32,7 @@ void EnableFullDpiSupportIfAvailable(HWND hwnd) { return; } auto enable_non_client_dpi_scaling = - reinterpret_cast( + reinterpret_cast( GetProcAddress(user32_module, "EnableNonClientDpiScaling")); enable_non_client_dpi_scaling(hwnd); From 77cf246c0a3fffa67c95039b23268fb99a1f851e Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Fri, 7 Feb 2020 09:45:15 -0800 Subject: [PATCH 12/13] Use nearest monitor properly --- example/windows/win32_window.cc | 8 +++++--- testbed/windows/win32_window.cc | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 1aadaa474..9b457bbe7 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -50,9 +50,11 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, Destroy(); WNDCLASS window_class = RegisterWindowClass(); - // Send a nullptr since the top-level window hasn't been created. This will - // get the primary monitor's DPI. - INT dpi = FlutterDesktopGetDpiForHWND(nullptr); + + const POINT target_point = {static_cast(origin.x), + static_cast(origin.y)}; + HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); + UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); double scale_factor = static_cast(dpi) / kBaseDpi; HWND window = CreateWindow( diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index d5d7faa9d..dd6582c88 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -50,9 +50,11 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, Destroy(); WNDCLASS window_class = RegisterWindowClass(); - // Send a nullptr since the top-level window hasn't been created. This will - // get the primary monitor's DPI. - INT dpi = FlutterDesktopGetDpiForHWND(nullptr); + + const POINT target_point = {static_cast(origin.x), + static_cast(origin.y)}; + HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); + UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); double scale_factor = static_cast(dpi) / kBaseDpi; HWND window = CreateWindow( From a49e3516cf81799227dac9f8790eb72c59e9aff6 Mon Sep 17 00:00:00 2001 From: Francisco Madgaleno Date: Tue, 11 Feb 2020 15:04:12 -0800 Subject: [PATCH 13/13] Remove cast --- example/windows/win32_window.cc | 2 +- plugins/window_size/windows/window_size_plugin.cpp | 4 ++-- testbed/windows/win32_window.cc | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/example/windows/win32_window.cc b/example/windows/win32_window.cc index 81e76f976..4c0a33c8c 100644 --- a/example/windows/win32_window.cc +++ b/example/windows/win32_window.cc @@ -55,7 +55,7 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, static_cast(origin.y)}; HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); - double scale_factor = static_cast(dpi) / kBaseDpi; + double scale_factor = dpi / kBaseDpi; HWND window = CreateWindow( window_class.lpszClassName, title.c_str(), diff --git a/plugins/window_size/windows/window_size_plugin.cpp b/plugins/window_size/windows/window_size_plugin.cpp index 1db3e4f81..cac5b3ce8 100644 --- a/plugins/window_size/windows/window_size_plugin.cpp +++ b/plugins/window_size/windows/window_size_plugin.cpp @@ -69,8 +69,8 @@ EncodableValue GetPlatformChannelRepresentationForMonitor(HMONITOR monitor) { MONITORINFO info; info.cbSize = sizeof(MONITORINFO); GetMonitorInfo(monitor, &info); - INT dpi = FlutterDesktopGetDpiForMonitor(monitor); - double scale_factor = static_cast(dpi) / kBaseDpi; + UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); + double scale_factor = dpi / kBaseDpi; return EncodableValue(EncodableMap{ {EncodableValue(kFrameKey), GetPlatformChannelRepresentationForRect(info.rcMonitor)}, diff --git a/testbed/windows/win32_window.cc b/testbed/windows/win32_window.cc index 949ea210f..86b94705c 100644 --- a/testbed/windows/win32_window.cc +++ b/testbed/windows/win32_window.cc @@ -55,7 +55,7 @@ bool Win32Window::CreateAndShow(const std::wstring &title, const Point &origin, static_cast(origin.y)}; HMONITOR monitor = MonitorFromPoint(target_point, MONITOR_DEFAULTTONEAREST); UINT dpi = FlutterDesktopGetDpiForMonitor(monitor); - double scale_factor = static_cast(dpi) / kBaseDpi; + double scale_factor = dpi / kBaseDpi; HWND window = CreateWindow( window_class.lpszClassName, title.c_str(),