From 604dbd1e736c88bbba36da4a9422615936b856d7 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Tue, 10 Jan 2023 11:55:36 -0500 Subject: Windows: Make puglGetScaleFactor always return a reasonable value This guesses the likely scale factor when the view hasn't been realized (and so there's no real window to get information for). It may still be wrong for multiple display systems, but will always return some scale factor that "exists" on the system, and should always be correct when there's only one display. --- src/win.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/win.c') diff --git a/src/win.c b/src/win.c index 5defb8d..fa50034 100644 --- a/src/win.c +++ b/src/win.c @@ -136,6 +136,27 @@ puglWinGetWindowExFlags(const PuglView* const view) return WS_EX_NOINHERITLAYOUT | (view->parent ? 0U : WS_EX_APPWINDOW); } +static HWND +puglWinGetWindow(const PuglView* const view) +{ + return view->impl->hwnd ? view->impl->hwnd + : view->parent ? (HWND)view->parent + : view->transientParent ? (HWND)view->transientParent + : NULL; +} + +static HMONITOR +puglWinGetMonitor(const PuglView* const view) +{ + const HWND hwnd = puglWinGetWindow(view); + if (hwnd) { + return MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); + } + + const POINT point = {(long)view->frame.x, (long)view->frame.y}; + return MonitorFromPoint(point, MONITOR_DEFAULTTOPRIMARY); +} + static double puglWinGetViewScaleFactor(const PuglView* const view) { @@ -156,9 +177,7 @@ puglWinGetViewScaleFactor(const PuglView* const view) DWORD scaleFactor = 100; if (GetProcessDpiAwareness && GetScaleFactorForMonitor && !GetProcessDpiAwareness(NULL, &dpiAware) && dpiAware) { - GetScaleFactorForMonitor( - MonitorFromWindow(view->impl->hwnd, MONITOR_DEFAULTTOPRIMARY), - &scaleFactor); + GetScaleFactorForMonitor(puglWinGetMonitor(view), &scaleFactor); } FreeLibrary(shcore); @@ -1174,7 +1193,8 @@ adjustedWindowRect(PuglView* const view, double puglGetScaleFactor(const PuglView* const view) { - return view->impl->scaleFactor; + return view->impl->scaleFactor ? view->impl->scaleFactor + : puglWinGetViewScaleFactor(view); } PuglStatus -- cgit v1.2.1