From 027cfdb685b0d6764405bb5b03f6fc44523f9d2a Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Thu, 12 Mar 2015 10:04:31 +0300 Subject: [PATCH] possible fix for issue #72 - GUI is blurry when using retina displays; theme font size in pt --- examples/example1/src/example1.d | 2 +- src/dlangui/core/types.d | 7 +++++- src/dlangui/graphics/resources.d | 16 +++++++++++-- src/dlangui/platforms/sdl/sdlapp.d | 33 ++++++++++++++++++-------- src/dlangui/platforms/windows/winapp.d | 8 +++++++ views/res/theme_dark.xml | 5 ---- views/res/theme_default.xml | 7 ++---- 7 files changed, 54 insertions(+), 24 deletions(-) diff --git a/examples/example1/src/example1.d b/examples/example1/src/example1.d index fa6b2c8b..aeb49b7d 100644 --- a/examples/example1/src/example1.d +++ b/examples/example1/src/example1.d @@ -251,7 +251,7 @@ extern (C) int UIAppMain(string[] args) { //} // create window - Window window = Platform.instance.createWindow("My Window", null, WindowFlag.Resizable, 700, 500); + Window window = Platform.instance.createWindow("My Window", null, WindowFlag.Resizable, pointsToPixels(600), pointsToPixels(500)); static if (true) { VerticalLayout contentLayout = new VerticalLayout(); diff --git a/src/dlangui/core/types.d b/src/dlangui/core/types.d index b38f12f4..d8197b5f 100644 --- a/src/dlangui/core/types.d +++ b/src/dlangui/core/types.d @@ -181,7 +181,12 @@ private __gshared int PRIVATE_SCREEN_DPI = 96; } @property void SCREEN_DPI(int dpi) { - PRIVATE_SCREEN_DPI = dpi; + if (dpi >= 72 && dpi <= 500) { + if (PRIVATE_SCREEN_DPI != dpi) { + // changed DPI + PRIVATE_SCREEN_DPI = dpi; + } + } } /// one point is 1/72 of inch diff --git a/src/dlangui/graphics/resources.d b/src/dlangui/graphics/resources.d index ea148465..17abd4f6 100644 --- a/src/dlangui/graphics/resources.d +++ b/src/dlangui/graphics/resources.d @@ -271,11 +271,15 @@ class FrameDrawable : Drawable { @property override Rect padding() { return _frameWidths; } } +enum DimensionUnits { + pixels, + points +} /// decode size string, e.g. 1px or 2 or 3pt static uint decodeDimension(string s) { uint value = 0; - int units = 0; + DimensionUnits units = DimensionUnits.pixels; foreach(c; s) { int digit = -1; if (c >='0' && c <= '9') @@ -283,9 +287,17 @@ static uint decodeDimension(string s) { if (digit >= 0) value = value * 10 + digit; else if (c == 't') // just test by containing 't' - for NNNpt - units = 1; // "pt" + units = DimensionUnits.points; // "pt" } // TODO: convert points to pixels + switch(units) { + case DimensionUnits.points: + // convert points to pixels + value = pointsToPixels(value); + break; + default: + break; + } return value; } diff --git a/src/dlangui/platforms/sdl/sdlapp.d b/src/dlangui/platforms/sdl/sdlapp.d index 5c8d3e1d..dfec34f3 100644 --- a/src/dlangui/platforms/sdl/sdlapp.d +++ b/src/dlangui/platforms/sdl/sdlapp.d @@ -180,13 +180,14 @@ class SDLWindow : Window { int w = 0; int h = 0; SDL_GL_GetDrawableSize(_win, &w, &h); - if (w != width || h != height) { - Log.d("SDL_GL_GetDrawableSize returned ", w, "x", h, " while resize event reports ", width, "x", height); + version (Windows) { + // DPI already calculated + } else { + // scale DPI + if (w > width && h > height && width > 0 && height > 0) + SCREEN_DPI = 96 * w / width; } - if (w && h) - onResize(w, h); - else - onResize(width, height); + onResize(std.algorithm.max(width, w), std.algorithm.max(height, h)); } @property uint windowId() { @@ -194,7 +195,6 @@ class SDLWindow : Window { return SDL_GetWindowID(_win); return 0; } - override void show() { Log.d("SDLWindow.show()"); @@ -364,9 +364,7 @@ class SDLWindow : Window { void redraw() { //Log.e("Widget instance count in SDLWindow.redraw: ", Widget.instanceCount()); // check if size has been changed - int w, h; - SDL_GetWindowSize(_win, &w, &h); - onResize(w, h); + fixSize(); if (_enableOpengl) { version(USE_OPENGL) { @@ -1139,6 +1137,21 @@ version (Windows) { try { Runtime.initialize(); + + // call SetProcessDPIAware to support HI DPI - fix by Kapps + auto ulib = LoadLibraryA("user32.dll"); + alias SetProcessDPIAwareFunc = int function(); + auto setDpiFunc = cast(SetProcessDPIAwareFunc)GetProcAddress(ulib, "SetProcessDPIAware"); + if(setDpiFunc) // Should never fail, but just in case... + setDpiFunc(); + + // Get screen DPI + HDC dc = CreateCompatibleDC(NULL); + SCREEN_DPI = GetDeviceCaps(dc, LOGPIXELSY); + DeleteObject(dc); + + //SCREEN_DPI = 96 * 3 / 2; + result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); Log.i("calling Runtime.terminate()"); // commented out to fix hanging runtime.terminate when there are background threads diff --git a/src/dlangui/platforms/windows/winapp.d b/src/dlangui/platforms/windows/winapp.d index 426c2902..41913ea5 100644 --- a/src/dlangui/platforms/windows/winapp.d +++ b/src/dlangui/platforms/windows/winapp.d @@ -868,6 +868,14 @@ int DLANGUIWinMain(void* hInstance, void* hPrevInstance, try { Runtime.initialize(); + + // call SetProcessDPIAware to support HI DPI - fix by Kapps + auto ulib = LoadLibraryA("user32.dll"); + alias SetProcessDPIAwareFunc = int function(); + auto setDpiFunc = cast(SetProcessDPIAwareFunc)GetProcAddress(ulib, "SetProcessDPIAware"); + if(setDpiFunc) // Should never fail, but just in case... + setDpiFunc(); + result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow); // TODO: fix hanging on multithreading app //Runtime.terminate(); diff --git a/views/res/theme_dark.xml b/views/res/theme_dark.xml index 27337e30..b9eb9ad3 100644 --- a/views/res/theme_dark.xml +++ b/views/res/theme_dark.xml @@ -1,6 +1,5 @@