GUI: detect UI scale factor when moving window

This commit is contained in:
tildearrow 2023-08-31 01:24:06 -05:00
parent 9caa2f38f4
commit a882d7bcf2
6 changed files with 107 additions and 25 deletions

View File

@ -3491,6 +3491,7 @@ bool FurnaceGUI::loop() {
scrX=ev.window.data1; scrX=ev.window.data1;
scrY=ev.window.data2; scrY=ev.window.data2;
updateWindow=true; updateWindow=true;
shallDetectScale=2;
logV("window moved to %dx%d",scrX,scrY); logV("window moved to %dx%d",scrX,scrY);
break; break;
case SDL_WINDOWEVENT_SIZE_CHANGED: case SDL_WINDOWEVENT_SIZE_CHANGED:
@ -6140,7 +6141,38 @@ bool FurnaceGUI::loop() {
if (shallDetectScale) { if (shallDetectScale) {
if (--shallDetectScale<1) { if (--shallDetectScale<1) {
if (settings.dpiScale<0.5f) { if (settings.dpiScale<0.5f) {
applyUISettings(); const char* videoBackend=SDL_GetCurrentVideoDriver();
double newScale=getScaleFactor(videoBackend,sdlWin);
if (newScale<0.1f) {
logW("scale what?");
newScale=1.0f;
}
if (newScale!=dpiScale) {
logD("auto UI scale changed (%f != %f) - applying settings...",newScale,dpiScale);
ImGui::GetIO().Fonts->Clear();
applyUISettings();
if (rend) rend->destroyFontsTexture();
if (!ImGui::GetIO().Fonts->Build()) {
logE("error while building font atlas!");
showError("error while loading fonts! please check your settings.");
ImGui::GetIO().Fonts->Clear();
mainFont=ImGui::GetIO().Fonts->AddFontDefault();
patFont=mainFont;
bigFont=mainFont;
headFont=mainFont;
if (rend) rend->destroyFontsTexture();
if (!ImGui::GetIO().Fonts->Build()) {
logE("error again while building font atlas!");
} else {
rend->createFontsTexture();
}
} else {
rend->createFontsTexture();
}
}
} }
} }
} }
@ -6368,7 +6400,7 @@ bool FurnaceGUI::init() {
dpiScale=settings.dpiScale; dpiScale=settings.dpiScale;
} else { } else {
logD("auto-detecting UI scale factor."); logD("auto-detecting UI scale factor.");
dpiScale=getScaleFactor(videoBackend); dpiScale=getScaleFactor(videoBackend,sdlWin);
logD("scale factor: %f",dpiScale); logD("scale factor: %f",dpiScale);
if (dpiScale<0.1f) { if (dpiScale<0.1f) {
logW("scale what?"); logW("scale what?");

View File

@ -17,4 +17,4 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
double getMacDPIScale(); double getMacDPIScale(void* sysWin, unsigned char isUIKit);

View File

@ -20,7 +20,28 @@
#include <Cocoa/Cocoa.h> #include <Cocoa/Cocoa.h>
#include "macstuff.h" #include "macstuff.h"
double getMacDPIScale() { double getMacDPIScale(void* sysWin, unsigned char isUIKit) {
CGFloat val=[[NSScreen mainScreen] backingScaleFactor]; NSScreen* screen=nil;
if (sysWin!=NULL) {
if (isUIKit) {
UIWindow* win=(UIWindow*)sysWin;
UIWindowScene* winScene=[win windowScene];
if (winScene!=nil) {
UIScreen* winScreen=[winScene screen];
CGFloat ret=[winScreen scale];
return (double)ret;
}
} else {
NSWindow* win=(NSWindow*)sysWin;
screen=[win screen];
}
}
if (screen==nil) {
screen=[NSScreen mainScreen];
}
if (screen==nil) {
return 1.0;
}
CGFloat val=[screen backingScaleFactor];
return (double)val; return (double)val;
} }

View File

@ -42,14 +42,23 @@ typedef int (*XDS)(void*);
typedef int (*XDW)(void*,int); typedef int (*XDW)(void*,int);
#endif #endif
double getScaleFactor(const char* driverHint) { double getScaleFactor(const char* driverHint, void* windowHint) {
double ret=1.0; double ret=1.0;
// Windows // Windows
#ifdef _WIN32 #ifdef _WIN32
POINT nullPoint; POINT nullPoint;
nullPoint.x=-1; if (windowHint!=NULL) {
nullPoint.y=-1; int px=0;
int py=0;
SDL_GetWindowPosition((SDL_Window*)windowHint,&px,&py);
nullPoint.x=px;
nullPoint.y=py;
} else {
nullPoint.x=-1;
nullPoint.y=-1;
}
HMONITOR disp=MonitorFromPoint(nullPoint,MONITOR_DEFAULTTOPRIMARY); HMONITOR disp=MonitorFromPoint(nullPoint,MONITOR_DEFAULTTOPRIMARY);
if (disp==NULL) { if (disp==NULL) {
@ -93,12 +102,30 @@ double getScaleFactor(const char* driverHint) {
return ret; return ret;
#endif #endif
// macOS - backingScaleFactor // macOS
#ifdef __APPLE__ #ifdef __APPLE__
if (driverHint==NULL) { if (driverHint==NULL) {
return getMacDPIScale(); return getMacDPIScale(NULL,false);
} else if (strcmp(driverHint,"cocoa")==0 || strcmp(driverHint,"uikit")==0) { } else if (strcmp(driverHint,"cocoa")==0) {
return getMacDPIScale(); void* nsWindow=NULL;
SDL_SysWMinfo wmInfo;
if (windowHint!=NULL) {
SDL_VERSION(&info.version)
if (SDL_GetWindowWMInfo((SDL_Window*)windowHint,&wmInfo)==SDL_TRUE) {
nsWindow=wmInfo.cocoa.window;
}
}
return getMacDPIScale(nsWindow,false);
} else if (strcmp(driverHint,"uikit")==0) {
void* uiWindow=NULL;
SDL_SysWMinfo wmInfo;
if (windowHint!=NULL) {
SDL_VERSION(&info.version)
if (SDL_GetWindowWMInfo((SDL_Window*)windowHint,&wmInfo)==SDL_TRUE) {
uiWindow=wmInfo.cocoa.window;
}
}
return getMacDPIScale(uiWindow,true);
} }
#endif #endif

View File

@ -17,4 +17,4 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
double getScaleFactor(const char* driverHint); double getScaleFactor(const char* driverHint, void* windowHint);

View File

@ -4153,18 +4153,20 @@ void FurnaceGUI::applyUISettings(bool updateFonts) {
setupLabel(settings.emptyLabel.c_str(),emptyLabel,3); setupLabel(settings.emptyLabel.c_str(),emptyLabel,3);
setupLabel(settings.emptyLabel2.c_str(),emptyLabel2,2); setupLabel(settings.emptyLabel2.c_str(),emptyLabel2,2);
// get scale factor if (updateFonts) {
const char* videoBackend=SDL_GetCurrentVideoDriver(); // get scale factor
if (settings.dpiScale>=0.5f) { const char* videoBackend=SDL_GetCurrentVideoDriver();
logD("setting UI scale factor from config (%f).",settings.dpiScale); if (settings.dpiScale>=0.5f) {
dpiScale=settings.dpiScale; logD("setting UI scale factor from config (%f).",settings.dpiScale);
} else { dpiScale=settings.dpiScale;
logD("auto-detecting UI scale factor."); } else {
dpiScale=getScaleFactor(videoBackend); logD("auto-detecting UI scale factor.");
logD("scale factor: %f",dpiScale); dpiScale=getScaleFactor(videoBackend,sdlWin);
if (dpiScale<0.1f) { logD("scale factor: %f",dpiScale);
logW("scale what?"); if (dpiScale<0.1f) {
dpiScale=1.0f; logW("scale what?");
dpiScale=1.0f;
}
} }
} }