From 9f7ebaea5c43b3c5c860de782ed1d0f544124062 Mon Sep 17 00:00:00 2001 From: Haoyu Qiu Date: Mon, 6 Oct 2025 22:48:31 +0800 Subject: [PATCH] Fix editor auto scale on Windows --- editor/settings/editor_settings.cpp | 7 ++++++- platform/windows/display_server_windows.cpp | 22 +++++++++++---------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/editor/settings/editor_settings.cpp b/editor/settings/editor_settings.cpp index 4ab88e4abdc..2255827e92e 100644 --- a/editor/settings/editor_settings.cpp +++ b/editor/settings/editor_settings.cpp @@ -1823,6 +1823,9 @@ float EditorSettings::get_auto_display_scale() { return 1.0; } +#if defined(WINDOWS_ENABLED) + return DisplayServer::get_singleton()->screen_get_dpi(screen) / 96.0; +#else // Use the smallest dimension to use a correct display scale on portrait displays. const int smallest_dimension = MIN(DisplayServer::get_singleton()->screen_get_size(screen).x, DisplayServer::get_singleton()->screen_get_size(screen).y); if (DisplayServer::get_singleton()->screen_get_dpi(screen) >= 192 && smallest_dimension >= 1400) { @@ -1838,7 +1841,9 @@ float EditorSettings::get_auto_display_scale() { return 0.75; } return 1.0; -#endif +#endif // defined(WINDOWS_ENABLED) + +#endif // defined(MACOS_ENABLED) || defined(ANDROID_ENABLED) } // Shortcuts diff --git a/platform/windows/display_server_windows.cpp b/platform/windows/display_server_windows.cpp index 1cce42d57f2..cd6633f6669 100644 --- a/platform/windows/display_server_windows.cpp +++ b/platform/windows/display_server_windows.cpp @@ -1331,17 +1331,17 @@ Rect2i DisplayServerWindows::screen_get_usable_rect(int p_screen) const { } typedef struct { - int count; + int current_index; int screen; int dpi; } EnumDpiData; -static int QueryDpiForMonitor(HMONITOR hmon, MONITOR_DPI_TYPE dpiType = MDT_DEFAULT) { +static int QueryDpiForMonitor(HMONITOR hmon) { int dpiX = 96, dpiY = 96; UINT x = 0, y = 0; if (hmon) { - HRESULT hr = GetDpiForMonitor(hmon, dpiType, &x, &y); + HRESULT hr = GetDpiForMonitor(hmon, MDT_DEFAULT, &x, &y); if (SUCCEEDED(hr) && (x > 0) && (y > 0)) { dpiX = (int)x; dpiY = (int)y; @@ -1367,11 +1367,13 @@ static int QueryDpiForMonitor(HMONITOR hmon, MONITOR_DPI_TYPE dpiType = MDT_DEFA static BOOL CALLBACK _MonitorEnumProcDpi(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) { EnumDpiData *data = (EnumDpiData *)dwData; - if (data->count == data->screen) { - data->dpi = QueryDpiForMonitor(hMonitor); - } - data->count++; + data->current_index++; + + if (data->current_index == data->screen) { + data->dpi = QueryDpiForMonitor(hMonitor); + return FALSE; + } return TRUE; } @@ -1379,11 +1381,11 @@ int DisplayServerWindows::screen_get_dpi(int p_screen) const { _THREAD_SAFE_METHOD_ p_screen = _get_screen_index(p_screen); - int screen_count = get_screen_count(); - ERR_FAIL_INDEX_V(p_screen, screen_count, 72); - EnumDpiData data = { 0, p_screen, 72 }; + EnumDpiData data = { -1, p_screen, 96 }; EnumDisplayMonitors(nullptr, nullptr, _MonitorEnumProcDpi, (LPARAM)&data); + + 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)); return data.dpi; }