From 4041ff10f50d4b6dc5c14b18f180b2738cbbadd0 Mon Sep 17 00:00:00 2001 From: Jaakko Keränen Date: Wed, 30 Sep 2020 15:12:21 +0300 Subject: Windows: Initial/minimum window size vs. UI scale factor --- src/app.c | 7 +++++++ src/ui/window.c | 20 +++++--------------- src/win32.c | 17 +++++++++++++++++ src/win32.h | 5 +++-- 4 files changed, 32 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/app.c b/src/app.c index d1732bf9..ff5ec9b7 100644 --- a/src/app.c +++ b/src/app.c @@ -57,6 +57,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #if defined (iPlatformApple) && !defined (iPlatformIOS) # include "macos.h" #endif +#if defined (iPlatformMsys) +# include "win32.h" +#endif iDeclareType(App) @@ -314,6 +317,10 @@ static void init_App_(iApp *d, int argc, char **argv) { d->commandEcho = checkArgument_CommandLine(&d->args, "echo") != NULL; d->forceSoftwareRender = checkArgument_CommandLine(&d->args, "sw") != NULL; d->initialWindowRect = init_Rect(-1, -1, 900, 560); +#if defined (iPlatformMsys) + /* Must scale by UI scaling factor. */ + mulfv_I2(&d->initialWindowRect.size, desktopDPI_Win32()); +#endif init_Prefs(&d->prefs); setCStr_String(&d->prefs.downloadDir, downloadDir_App_); d->running = iFalse; diff --git a/src/ui/window.c b/src/ui/window.c index c0e8fdad..7d368f42 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -38,7 +38,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "../gmutil.h" #if defined (iPlatformMsys) # include "../win32.h" -# include #endif #if defined (iPlatformApple) && !defined (iPlatformIOS) # include "macos.h" @@ -493,19 +492,8 @@ static void updateRootSize_Window_(iWindow *d) { static float pixelRatio_Window_(const iWindow *d) { #if defined (iPlatformMsys) - /* Query Direct2D for the desktop DPI (not aware of which monitor, though). */ { - float ratio = 1.0f; - ID2D1Factory *d2dFactory = NULL; - HRESULT hr = D2D1CreateFactory( - D2D1_FACTORY_TYPE_SINGLE_THREADED, &IID_ID2D1Factory, NULL, (void **) &d2dFactory); - if (SUCCEEDED(hr)) { - FLOAT dpiX = 96; - FLOAT dpiY = 96; - ID2D1Factory_GetDesktopDpi(d2dFactory, &dpiX, &dpiY); - ratio = (float) (dpiX / 96.0); - ID2D1Factory_Release(d2dFactory); - } - } + iUnused(d); + return desktopDPI_Win32(); #else int dx, x; SDL_GetRendererOutputSize(d->render, &dx, NULL); @@ -556,7 +544,8 @@ void init_Window(iWindow *d, iRect rect) { if (left_Rect(rect) >= 0 || top_Rect(rect) >= 0) { SDL_SetWindowPosition(d->win, left_Rect(rect), top_Rect(rect)); } - SDL_SetWindowMinimumSize(d->win, 400, 250); + const iInt2 minSize = init_I2(400, 250); + SDL_SetWindowMinimumSize(d->win, minSize.x, minSize.y); SDL_SetWindowTitle(d->win, "Lagrange"); /* Some info. */ { SDL_RendererInfo info; @@ -578,6 +567,7 @@ void init_Window(iWindow *d, iRect rect) { d->pixelRatio = pixelRatio_Window_(d); setPixelRatio_Metrics(d->pixelRatio * d->uiScale); #if defined (iPlatformMsys) + SDL_SetWindowMinimumSize(d->win, minSize.x * d->pixelRatio, minSize.y * d->pixelRatio); useExecutableIconResource_SDLWindow(d->win); #endif #if defined (iPlatformLinux) diff --git a/src/win32.c b/src/win32.c index 36043b53..5eb2a0d5 100644 --- a/src/win32.c +++ b/src/win32.c @@ -25,11 +25,28 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #define WIN32_LEAN_AND_MEAN #include +#include void setDPIAware_Win32(void) { SetProcessDPIAware(); } +float desktopDPI_Win32(void) { + /* Query Direct2D for the desktop DPI (not aware of which monitor, though). */ + float ratio = 1.0f; + ID2D1Factory *d2dFactory = NULL; + HRESULT hr = D2D1CreateFactory( + D2D1_FACTORY_TYPE_SINGLE_THREADED, &IID_ID2D1Factory, NULL, (void **) &d2dFactory); + if (SUCCEEDED(hr)) { + FLOAT dpiX = 96; + FLOAT dpiY = 96; + ID2D1Factory_GetDesktopDpi(d2dFactory, &dpiX, &dpiY); + ratio = (float) (dpiX / 96.0); + ID2D1Factory_Release(d2dFactory); + } + return ratio; +} + void useExecutableIconResource_SDLWindow(SDL_Window *win) { HINSTANCE handle = GetModuleHandle(NULL); HICON icon = LoadIcon(handle, "IDI_ICON1"); diff --git a/src/win32.h b/src/win32.h index 33863c8e..b5bc0b45 100644 --- a/src/win32.h +++ b/src/win32.h @@ -23,5 +23,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #pragma once #include -void setDPIAware_Win32(void); -void useExecutableIconResource_SDLWindow(SDL_Window *win); +void setDPIAware_Win32(void); +float desktopDPI_Win32(void); +void useExecutableIconResource_SDLWindow(SDL_Window *win); -- cgit v1.2.3