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;
scrY=ev.window.data2;
updateWindow=true;
shallDetectScale=2;
logV("window moved to %dx%d",scrX,scrY);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
@ -6140,7 +6141,38 @@ bool FurnaceGUI::loop() {
if (shallDetectScale) {
if (--shallDetectScale<1) {
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;
} else {
logD("auto-detecting UI scale factor.");
dpiScale=getScaleFactor(videoBackend);
dpiScale=getScaleFactor(videoBackend,sdlWin);
logD("scale factor: %f",dpiScale);
if (dpiScale<0.1f) {
logW("scale what?");

View File

@ -17,4 +17,4 @@
* 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 "macstuff.h"
double getMacDPIScale() {
CGFloat val=[[NSScreen mainScreen] backingScaleFactor];
double getMacDPIScale(void* sysWin, unsigned char isUIKit) {
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;
}

View File

@ -42,14 +42,23 @@ typedef int (*XDS)(void*);
typedef int (*XDW)(void*,int);
#endif
double getScaleFactor(const char* driverHint) {
double getScaleFactor(const char* driverHint, void* windowHint) {
double ret=1.0;
// Windows
#ifdef _WIN32
POINT nullPoint;
nullPoint.x=-1;
nullPoint.y=-1;
if (windowHint!=NULL) {
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);
if (disp==NULL) {
@ -93,12 +102,30 @@ double getScaleFactor(const char* driverHint) {
return ret;
#endif
// macOS - backingScaleFactor
// macOS
#ifdef __APPLE__
if (driverHint==NULL) {
return getMacDPIScale();
} else if (strcmp(driverHint,"cocoa")==0 || strcmp(driverHint,"uikit")==0) {
return getMacDPIScale();
return getMacDPIScale(NULL,false);
} else if (strcmp(driverHint,"cocoa")==0) {
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

View File

@ -17,4 +17,4 @@
* 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.emptyLabel2.c_str(),emptyLabel2,2);
// get scale factor
const char* videoBackend=SDL_GetCurrentVideoDriver();
if (settings.dpiScale>=0.5f) {
logD("setting UI scale factor from config (%f).",settings.dpiScale);
dpiScale=settings.dpiScale;
} else {
logD("auto-detecting UI scale factor.");
dpiScale=getScaleFactor(videoBackend);
logD("scale factor: %f",dpiScale);
if (dpiScale<0.1f) {
logW("scale what?");
dpiScale=1.0f;
if (updateFonts) {
// get scale factor
const char* videoBackend=SDL_GetCurrentVideoDriver();
if (settings.dpiScale>=0.5f) {
logD("setting UI scale factor from config (%f).",settings.dpiScale);
dpiScale=settings.dpiScale;
} else {
logD("auto-detecting UI scale factor.");
dpiScale=getScaleFactor(videoBackend,sdlWin);
logD("scale factor: %f",dpiScale);
if (dpiScale<0.1f) {
logW("scale what?");
dpiScale=1.0f;
}
}
}