From 18dca11dd0428dc1de424bc5b71a4cecaf1d889f Mon Sep 17 00:00:00 2001 From: Pascal Thomet Date: Fri, 27 Jun 2025 13:40:48 +0200 Subject: [PATCH] Backends: GLFW, SDL2: ImplXXX_GetContentScaleXXX() helpers return 1.0f on emscripten / apple / android (#8742, #8733) We can divide platforms into two cases based on how they report screen geometry: - Case 1: Platforms which report screen size in "physical pixels": Windows (for "Dpi aware" apps), Linux (with Wayland) - Case 2: Platforms which report screen size in "density-independent pixels": macOS, iOS, Android, emscripten As a consequence, there are two important things we need to know: - FramebufferScale: The scaling factor FrameBufferSize / ScreenSize - In case 1, the framebuffer size is equal to the screen size and DisplayFramebufferScale=1. - In case 2, the framebuffer size is equal to the screen size multiplied by a factor, for example DisplayFramebufferScale=2. - ContentScale The scaling factor for the content that we will display - In case 1, the content scale will often need to be > 1 (e.g., 2), because we will need to display bigger elements so that they show with a correct physical size on the screen. - In case 2, the content scale is equal to 1 This commit fixes ContentScale for platforms in case 2. --- backends/imgui_impl_glfw.cpp | 5 +++-- backends/imgui_impl_sdl2.cpp | 3 ++- docs/CHANGELOG.txt | 3 +++ imgui.h | 2 +- 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/backends/imgui_impl_glfw.cpp b/backends/imgui_impl_glfw.cpp index d0a75ac78..98e07ce5c 100644 --- a/backends/imgui_impl_glfw.cpp +++ b/backends/imgui_impl_glfw.cpp @@ -29,6 +29,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2025-07-08: Made ImGui_ImplGlfw_GetContentScaleForWindow(), ImGui_ImplGlfw_GetContentScaleForMonitor() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733) // 2025-06-18: Added support for multiple Dear ImGui contexts. (#8676, #8239, #8069) // 2025-06-11: Added ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window) and ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor) helper to facilitate making DPI-aware apps. // 2025-03-10: Map GLFW_KEY_WORLD_1 and GLFW_KEY_WORLD_2 into ImGuiKey_Oem102. @@ -876,7 +877,7 @@ static void ImGui_ImplGlfw_UpdateGamepads() // - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle. float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window) { -#if GLFW_HAS_PER_MONITOR_DPI && !defined(__APPLE__) +#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__)) float x_scale, y_scale; glfwGetWindowContentScale(window, &x_scale, &y_scale); return x_scale; @@ -888,7 +889,7 @@ float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window) float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor) { -#if GLFW_HAS_PER_MONITOR_DPI && !defined(__APPLE__) +#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__)) float x_scale, y_scale; glfwGetMonitorContentScale(monitor, &x_scale, &y_scale); return x_scale; diff --git a/backends/imgui_impl_sdl2.cpp b/backends/imgui_impl_sdl2.cpp index 4dc2dea6e..291edb811 100644 --- a/backends/imgui_impl_sdl2.cpp +++ b/backends/imgui_impl_sdl2.cpp @@ -21,6 +21,7 @@ // CHANGELOG // (minor and older changes stripped away, please see git history for details) +// 2025-07-08: Made ImGui_ImplSDL2_GetContentScaleForWindow(), ImGui_ImplSDL2_GetContentScaleForDisplay() helpers return 1.0f on Emscripten and Android platforms, matching macOS logic. (#8742, #8733) // 2025-06-11: Added ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) and ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) helper to facilitate making DPI-aware apps. // 2025-04-09: Don't attempt to call SDL_CaptureMouse() on drivers where we don't call SDL_GetGlobalMouseState(). (#8561) // 2025-03-21: Fill gamepad inputs and set ImGuiBackendFlags_HasGamepad regardless of ImGuiConfigFlags_NavEnableGamepad being set. @@ -719,7 +720,7 @@ float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window) float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index) { #if SDL_HAS_PER_MONITOR_DPI -#ifndef __APPLE__ +#if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__) float dpi = 0.0f; if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0) return dpi / 96.0f; diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt index 0a3523621..d412df107 100644 --- a/docs/CHANGELOG.txt +++ b/docs/CHANGELOG.txt @@ -61,6 +61,9 @@ Other changes: - Demo: Added "Text -> Font Size" demo section. (#8738) [@Demonese] - CI: Fixed dllimport/dllexport tests. (#8757) [@AidanSun05] - CI: Updated to use latest Windows image + VS2022. +- Backends: GLFW, SDL2 made ImGui_ImplGLFW_GetContentScaleXXX() and + ImGui_ImplSDL2_GetContentScaleXXXX() helpers return 1.0f on Emscripten + and Android platforms, matching macOS logic. (#8742, #8733) [@pthom] - Backends: SDL3: avoid calling SDL_StartTextInput() again if already active. (fixes e.g.: an issue on iOS where the keyboard animation will popup every time the user types a key + probably other things) (#8727) [@morrazzzz] diff --git a/imgui.h b/imgui.h index 0833e1c82..89c2c2e5d 100644 --- a/imgui.h +++ b/imgui.h @@ -29,7 +29,7 @@ // Library Version // (Integer encoded as XYYZZ for use in #if preprocessor conditionals, e.g. '#if IMGUI_VERSION_NUM >= 12345') #define IMGUI_VERSION "1.92.1 WIP" -#define IMGUI_VERSION_NUM 19201 +#define IMGUI_VERSION_NUM 19202 #define IMGUI_HAS_TABLE // Added BeginTable() - from IMGUI_VERSION_NUM >= 18000 #define IMGUI_HAS_TEXTURES // Added ImGuiBackendFlags_RendererHasTextures - from IMGUI_VERSION_NUM >= 19198