From 9b9e0a36187a5a2f76add523f1eadfe8bf22dbdc Mon Sep 17 00:00:00 2001 From: falkTX Date: Fri, 13 Aug 2021 19:21:03 +0100 Subject: [PATCH] Implement fetching desktop scale factor on macOS; Tweak d_info size --- dgl/src/WindowPrivateData.cpp | 11 ++++++----- dgl/src/pugl.cpp | 23 +++++++++++++++++++++-- dgl/src/pugl.hpp | 4 ++++ examples/Info/InfoExampleUI.cpp | 7 +++++-- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/dgl/src/WindowPrivateData.cpp b/dgl/src/WindowPrivateData.cpp index 1b4b5c90..ceeb2362 100644 --- a/dgl/src/WindowPrivateData.cpp +++ b/dgl/src/WindowPrivateData.cpp @@ -64,12 +64,13 @@ START_NAMESPACE_DGL static const char* const kWin32SelectedFileCancelled = "__dpf_cancelled__"; #endif -static double getDesktopScaleFactor() +static double getDesktopScaleFactor(const PuglView* const view) { + // allow custom scale for testing if (const char* const scale = getenv("DPF_SCALE_FACTOR")) return std::max(1.0, std::atof(scale)); - return 1.0; + return puglGetDesktopScaleFactor(view); } // ----------------------------------------------------------------------- @@ -84,7 +85,7 @@ Window::PrivateData::PrivateData(Application& a, Window* const s) isClosed(true), isVisible(false), isEmbed(false), - scaleFactor(getDesktopScaleFactor()), + scaleFactor(getDesktopScaleFactor(view)), autoScaling(false), autoScaleFactor(1.0), minWidth(0), @@ -137,7 +138,7 @@ Window::PrivateData::PrivateData(Application& a, Window* const s, isClosed(parentWindowHandle == 0), isVisible(parentWindowHandle != 0), isEmbed(parentWindowHandle != 0), - scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor()), + scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor(view)), autoScaling(false), autoScaleFactor(1.0), minWidth(0), @@ -167,7 +168,7 @@ Window::PrivateData::PrivateData(Application& a, Window* const s, isClosed(parentWindowHandle == 0), isVisible(parentWindowHandle != 0), isEmbed(parentWindowHandle != 0), - scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor()), + scaleFactor(scale != 0.0 ? scale : getDesktopScaleFactor(view)), autoScaling(false), autoScaleFactor(1.0), minWidth(0), diff --git a/dgl/src/pugl.cpp b/dgl/src/pugl.cpp index 523c25f6..3873b297 100644 --- a/dgl/src/pugl.cpp +++ b/dgl/src/pugl.cpp @@ -197,13 +197,32 @@ const char* puglGetWindowTitle(const PuglView* const view) return view->title; } +// -------------------------------------------------------------------------------------------------------------------- +// get global scale factor + +double puglGetDesktopScaleFactor(const PuglView* const view) +{ +#if defined(DISTRHO_OS_MAC) + return (view->impl->window ? [view->impl->window screen] + : [NSScreen mainScreen]).backingScaleFactor; +#else + return 1.0; + + // unused + (void)view; +#endif +} + // -------------------------------------------------------------------------------------------------------------------- // bring view window into the foreground, aka "raise" window -void puglRaiseWindow(PuglView* view) +void puglRaiseWindow(PuglView* const view) { -#if defined(DISTRHO_OS_HAIKU) || defined(DISTRHO_OS_MAC) +#if defined(DISTRHO_OS_HAIKU) // nothing here yet +#elif defined(DISTRHO_OS_MAC) + if (view->impl->window) + [view->impl->window orderFrontRegardless]; #elif defined(DISTRHO_OS_WINDOWS) SetForegroundWindow(view->impl->hwnd); SetActiveWindow(view->impl->hwnd); diff --git a/dgl/src/pugl.hpp b/dgl/src/pugl.hpp index 9ad05dd6..149d6b88 100644 --- a/dgl/src/pugl.hpp +++ b/dgl/src/pugl.hpp @@ -67,6 +67,10 @@ puglGetTransientParent(const PuglView* view); PUGL_API const char* puglGetWindowTitle(const PuglView* view); +// get global scale factor +PUGL_API double +puglGetDesktopScaleFactor(const PuglView* view); + // bring view window into the foreground, aka "raise" window PUGL_API void puglRaiseWindow(PuglView* view); diff --git a/examples/Info/InfoExampleUI.cpp b/examples/Info/InfoExampleUI.cpp index 80d87673..9575ee31 100644 --- a/examples/Info/InfoExampleUI.cpp +++ b/examples/Info/InfoExampleUI.cpp @@ -34,7 +34,7 @@ public: : UI(kInitialWidth, kInitialHeight), fSampleRate(getSampleRate()), fResizable(isResizable()), - fScale(1.0f), + fScale(getScaleFactor()), fResizeHandle(this) { std::memset(fParameters, 0, sizeof(float)*kParameterCount); @@ -46,7 +46,10 @@ public: loadSharedResources(); #endif - setGeometryConstraints(kInitialWidth, kInitialHeight, true); + if (d_isNotEqual(fScale, 1.0f)) + setSize(kInitialWidth * fScale, kInitialHeight * fScale); + + setGeometryConstraints(kInitialWidth * fScale, kInitialHeight * fScale, true); // no need to show resize handle if window is user-resizable if (fResizable)