Skip to content

Commit a0dcc29

Browse files
committed
Backends: Glfw, SDL2: ImplXXXGetContentScale shall return 1 on emscripten / apple / android
This is a fix related to #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.
1 parent 22ad62c commit a0dcc29

File tree

2 files changed

+4
-3
lines changed

2 files changed

+4
-3
lines changed

backends/imgui_impl_glfw.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ static void ImGui_ImplGlfw_UpdateGamepads()
876876
// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
877877
float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window)
878878
{
879-
#if GLFW_HAS_PER_MONITOR_DPI && !defined(__APPLE__)
879+
#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
880880
float x_scale, y_scale;
881881
glfwGetWindowContentScale(window, &x_scale, &y_scale);
882882
return x_scale;
@@ -888,9 +888,10 @@ float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window)
888888

889889
float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor)
890890
{
891-
#if GLFW_HAS_PER_MONITOR_DPI && !defined(__APPLE__)
891+
#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
892892
float x_scale, y_scale;
893893
glfwGetMonitorContentScale(monitor, &x_scale, &y_scale);
894+
printf("ImGui_ImplGlfw_GetContentScaleForMonitor : monitor scale = %.2f x %.2f\n", x_scale, y_scale);
894895
return x_scale;
895896
#else
896897
IM_UNUSED(monitor);

backends/imgui_impl_sdl2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ float ImGui_ImplSDL2_GetContentScaleForWindow(SDL_Window* window)
718718
float ImGui_ImplSDL2_GetContentScaleForDisplay(int display_index)
719719
{
720720
#if SDL_HAS_PER_MONITOR_DPI
721-
#ifndef __APPLE__
721+
#if !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && !defined(__ANDROID__)
722722
float dpi = 0.0f;
723723
if (SDL_GetDisplayDPI(display_index, &dpi, nullptr, nullptr) == 0)
724724
return dpi / 96.0f;

0 commit comments

Comments
 (0)