From a61cb185b6dccc76e50f6402d3105ba19e2a6ce4 Mon Sep 17 00:00:00 2001 From: falkTX Date: Fri, 3 Sep 2021 21:52:57 +0100 Subject: [PATCH] Correct external ui scale factor code --- Makefile.plugins.mk | 2 -- distrho/DistrhoUI_macOS.mm | 23 +++++++++++++++++++++++ distrho/src/DistrhoUI.cpp | 32 ++++++++++++++++---------------- 3 files changed, 39 insertions(+), 18 deletions(-) diff --git a/Makefile.plugins.mk b/Makefile.plugins.mk index 51b1faea..d057e28c 100644 --- a/Makefile.plugins.mk +++ b/Makefile.plugins.mk @@ -89,11 +89,9 @@ endif OBJS_DSP = $(FILES_DSP:%=$(BUILD_DIR)/%.o) OBJS_UI = $(FILES_UI:%=$(BUILD_DIR)/%.o) -ifneq ($(UI_TYPE),external) ifeq ($(MACOS),true) OBJS_UI += $(BUILD_DIR)/DistrhoUI_macOS_$(NAME).mm.o endif -endif # --------------------------------------------------------------------------------------------------------------------- # Set plugin binary file targets diff --git a/distrho/DistrhoUI_macOS.mm b/distrho/DistrhoUI_macOS.mm index 39b85c21..5127762d 100644 --- a/distrho/DistrhoUI_macOS.mm +++ b/distrho/DistrhoUI_macOS.mm @@ -19,6 +19,28 @@ #endif #include "src/DistrhoPluginChecks.h" +#include "src/DistrhoDefines.h" + +#if DISTRHO_PLUGIN_HAS_EXTERNAL_UI +#import +#include +#include + +START_NAMESPACE_DISTRHO +double getDesktopScaleFactor(const uintptr_t parentWindowHandle) +{ + // allow custom scale for testing + if (const char* const scale = getenv("DPF_SCALE_FACTOR")) + return std::max(1.0, std::atof(scale)); + + if (NSView* const parentView = (NSView*)parentWindowHandle) + if (NSWindow* const parentWindow = [parentView window]) + return [parentWindow screen].backingScaleFactor; + + return [NSScreen mainScreen].backingScaleFactor; +} +END_NAMESPACE_DISTRHO +#else // DISTRHO_PLUGIN_HAS_EXTERNAL_UI #include "../dgl/Base.hpp" #define DISTRHO_MACOS_NAMESPACE_MACRO_HELPER(DGL_NS, SEP, PUGL_NS, INTERFACE) DGL_NS ## SEP ## PUGL_NS ## SEP ## INTERFACE @@ -33,3 +55,4 @@ #define PuglWrapperView DISTRHO_MACOS_NAMESPACE_MACRO(DGL_NAMESPACE, PUGL_NAMESPACE, WrapperView) #import "src/pugl.mm" +#endif // DISTRHO_PLUGIN_HAS_EXTERNAL_UI diff --git a/distrho/src/DistrhoUI.cpp b/distrho/src/DistrhoUI.cpp index 76182ee0..b02adba1 100644 --- a/distrho/src/DistrhoUI.cpp +++ b/distrho/src/DistrhoUI.cpp @@ -17,17 +17,10 @@ #include "DistrhoUIPrivateData.hpp" #include "src/WindowPrivateData.hpp" #if DISTRHO_PLUGIN_HAS_EXTERNAL_UI -# if defined(DISTRHO_OS_HAIKU) -# elif defined(DISTRHO_OS_MAC) -# define Point CocoaPoint -# define Size CocoaSize -# import -# undef Point -# undef Size -# elif defined(DISTRHO_OS_WINDOWS) +# if defined(DISTRHO_OS_WINDOWS) # define WIN32_LEAN_AND_MEAN # include -# else +# elif defined(HAVE_X11) # include # endif #else @@ -47,15 +40,16 @@ const char* g_nextBundlePath = nullptr; /* ------------------------------------------------------------------------------------------------------------ * get global scale factor */ -static double getDesktopScaleFactor() +#ifdef DISTRHO_OS_MAC +double getDesktopScaleFactor(uintptr_t parentWindowHandle); +#else +static double getDesktopScaleFactor(const uintptr_t parentWindowHandle) { // allow custom scale for testing if (const char* const scale = getenv("DPF_SCALE_FACTOR")) return std::max(1.0, std::atof(scale)); -#if defined(DISTRHO_OS_MAC) - return [NSScreen mainScreen].backingScaleFactor; -#elif defined(DISTRHO_OS_WINDOWS) +#if defined(DISTRHO_OS_WINDOWS) if (const HMODULE Shcore = LoadLibraryA("Shcore.dll")) { typedef HRESULT(WINAPI* PFN_GetProcessDpiAwareness)(HANDLE, DWORD*); @@ -70,8 +64,10 @@ static double getDesktopScaleFactor() if (GetProcessDpiAwareness && GetScaleFactorForMonitor && GetProcessDpiAwareness(NULL, &dpiAware) == 0 && dpiAware != 0) { - // TODO replace with something else - const HMONITOR hMon = MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY); + const HMONITOR hMon = parentWindowHandle != 0 + ? MonitorFromWindow((HWND)parentWindowHandle, MONITOR_DEFAULTTOPRIMARY) + /* TODO replace with something else for parent-less */ + : MonitorFromWindow(nullptr, MONITOR_DEFAULTTOPRIMARY); DWORD scaleFactor = 0; if (GetScaleFactorForMonitor(hMon, &scaleFactor) == 0 && scaleFactor != 0) @@ -114,7 +110,11 @@ static double getDesktopScaleFactor() #endif return 1.0; + + // might be unused + (void)parentWindowHandle; } +#endif // !DISTRHO_OS_MAC #endif @@ -137,7 +137,7 @@ UI::PrivateData::createNextWindow(UI* const ui, const uint width, const uint hei ewData.parentWindowHandle = pData->winId; ewData.width = width; ewData.height = height; - ewData.scaleFactor = pData->scaleFactor != 0.0 ? pData->scaleFactor : getDesktopScaleFactor(); + ewData.scaleFactor = pData->scaleFactor != 0.0 ? pData->scaleFactor : getDesktopScaleFactor(pData->winId); ewData.title = DISTRHO_PLUGIN_NAME; ewData.isStandalone = DISTRHO_UI_IS_STANDALONE; return ewData;