Skip to content

Commit f7c9306

Browse files
committed
Merge pull request #111326 from timothyqiu/win-dpi-scale
Fix editor auto display scale on Windows
2 parents 597553a + 9f7ebae commit f7c9306

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

editor/settings/editor_settings.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1824,6 +1824,9 @@ float EditorSettings::get_auto_display_scale() {
18241824
return 1.0;
18251825
}
18261826

1827+
#if defined(WINDOWS_ENABLED)
1828+
return DisplayServer::get_singleton()->screen_get_dpi(screen) / 96.0;
1829+
#else
18271830
// Use the smallest dimension to use a correct display scale on portrait displays.
18281831
const int smallest_dimension = MIN(DisplayServer::get_singleton()->screen_get_size(screen).x, DisplayServer::get_singleton()->screen_get_size(screen).y);
18291832
if (DisplayServer::get_singleton()->screen_get_dpi(screen) >= 192 && smallest_dimension >= 1400) {
@@ -1839,7 +1842,9 @@ float EditorSettings::get_auto_display_scale() {
18391842
return 0.75;
18401843
}
18411844
return 1.0;
1842-
#endif
1845+
#endif // defined(WINDOWS_ENABLED)
1846+
1847+
#endif // defined(MACOS_ENABLED) || defined(ANDROID_ENABLED)
18431848
}
18441849

18451850
// Shortcuts

platform/windows/display_server_windows.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,17 +1331,17 @@ Rect2i DisplayServerWindows::screen_get_usable_rect(int p_screen) const {
13311331
}
13321332

13331333
typedef struct {
1334-
int count;
1334+
int current_index;
13351335
int screen;
13361336
int dpi;
13371337
} EnumDpiData;
13381338

1339-
static int QueryDpiForMonitor(HMONITOR hmon, MONITOR_DPI_TYPE dpiType = MDT_DEFAULT) {
1339+
static int QueryDpiForMonitor(HMONITOR hmon) {
13401340
int dpiX = 96, dpiY = 96;
13411341

13421342
UINT x = 0, y = 0;
13431343
if (hmon) {
1344-
HRESULT hr = GetDpiForMonitor(hmon, dpiType, &x, &y);
1344+
HRESULT hr = GetDpiForMonitor(hmon, MDT_DEFAULT, &x, &y);
13451345
if (SUCCEEDED(hr) && (x > 0) && (y > 0)) {
13461346
dpiX = (int)x;
13471347
dpiY = (int)y;
@@ -1367,23 +1367,25 @@ static int QueryDpiForMonitor(HMONITOR hmon, MONITOR_DPI_TYPE dpiType = MDT_DEFA
13671367

13681368
static BOOL CALLBACK _MonitorEnumProcDpi(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
13691369
EnumDpiData *data = (EnumDpiData *)dwData;
1370-
if (data->count == data->screen) {
1370+
1371+
data->current_index++;
1372+
1373+
if (data->current_index == data->screen) {
13711374
data->dpi = QueryDpiForMonitor(hMonitor);
1375+
return FALSE;
13721376
}
1373-
1374-
data->count++;
13751377
return TRUE;
13761378
}
13771379

13781380
int DisplayServerWindows::screen_get_dpi(int p_screen) const {
13791381
_THREAD_SAFE_METHOD_
13801382

13811383
p_screen = _get_screen_index(p_screen);
1382-
int screen_count = get_screen_count();
1383-
ERR_FAIL_INDEX_V(p_screen, screen_count, 72);
13841384

1385-
EnumDpiData data = { 0, p_screen, 72 };
1385+
EnumDpiData data = { -1, p_screen, 96 };
13861386
EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcDpi, (LPARAM)&data);
1387+
1388+
ERR_FAIL_COND_V_MSG(data.current_index < p_screen, 96, vformat("Screen index %d out of range [0, %d].", p_screen, data.current_index));
13871389
return data.dpi;
13881390
}
13891391

0 commit comments

Comments
 (0)