Skip to content

Commit ab25051

Browse files
author
Felix "xq" Queißner
committed
Fixes DPI scaling for GLFW windows
1 parent d6c4f53 commit ab25051

1 file changed

Lines changed: 28 additions & 4 deletions

File tree

libs/imgui/backends/imgui_impl_glfw.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,17 @@ void ImGui_ImplGlfw_WindowFocusCallback(GLFWwindow* window, int focused)
499499
io.AddFocusEvent(focused != 0);
500500
}
501501

502+
static void ImGui_ImplGlfw_SetMousePosWithScaling(ImGui_ImplGlfw_Data* bd, GLFWwindow* window, ImGuiIO& io, double x, double y)
503+
{
504+
float scale = ImGui_ImplGlfw_GetContentScaleForWindow(window);
505+
506+
x *= scale;
507+
y *= scale;
508+
509+
io.AddMousePosEvent((float)x, (float)y);
510+
bd->LastValidMousePos = ImVec2((float)x, (float)y);
511+
}
512+
502513
void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
503514
{
504515
ImGui_ImplGlfw_Data* bd = ImGui_ImplGlfw_GetBackendData(window);
@@ -513,8 +524,7 @@ void ImGui_ImplGlfw_CursorPosCallback(GLFWwindow* window, double x, double y)
513524
x += window_x;
514525
y += window_y;
515526
}
516-
io.AddMousePosEvent((float)x, (float)y);
517-
bd->LastValidMousePos = ImVec2((float)x, (float)y);
527+
ImGui_ImplGlfw_SetMousePosWithScaling(bd, window, io, x, y);
518528
}
519529

520530
// Workaround: X11 seems to send spurious Leave/Enter events which would make us lose our position,
@@ -856,8 +866,7 @@ static void ImGui_ImplGlfw_UpdateMouseData()
856866
mouse_x += window_x;
857867
mouse_y += window_y;
858868
}
859-
bd->LastValidMousePos = ImVec2((float)mouse_x, (float)mouse_y);
860-
io.AddMousePosEvent((float)mouse_x, (float)mouse_y);
869+
ImGui_ImplGlfw_SetMousePosWithScaling(bd, window, io, mouse_x, mouse_y);
861870
}
862871
}
863872

@@ -1006,6 +1015,7 @@ static void ImGui_ImplGlfw_UpdateMonitors()
10061015
}
10071016
}
10081017

1018+
10091019
// - On Windows the process needs to be marked DPI-aware!! SDL2 doesn't do it by default. You can call ::SetProcessDPIAware() or call ImGui_ImplWin32_EnableDpiAwareness() from Win32 backend.
10101020
// - Apple platforms use FramebufferScale so we always return 1.0f.
10111021
// - Some accessibility applications are declaring virtual monitors with a DPI of 0.0f, see #7902. We preserve this value for caller to handle.
@@ -1021,6 +1031,19 @@ float ImGui_ImplGlfw_GetContentScaleForWindow(GLFWwindow* window)
10211031
#endif
10221032
}
10231033

1034+
1035+
static float ImGui_ImplGlfw_GetWindowDpiScale(ImGuiViewport* vp)
1036+
{
1037+
if(vp == nullptr) {
1038+
return 1.0;
1039+
}
1040+
GLFWwindow * const window = static_cast<GLFWwindow *>(vp->PlatformHandle);
1041+
if(window == nullptr) {
1042+
return 1.0;
1043+
}
1044+
return ImGui_ImplGlfw_GetContentScaleForWindow(window);
1045+
}
1046+
10241047
float ImGui_ImplGlfw_GetContentScaleForMonitor(GLFWmonitor* monitor)
10251048
{
10261049
#if GLFW_HAS_PER_MONITOR_DPI && !(defined(__APPLE__) || defined(__EMSCRIPTEN__) || defined(__ANDROID__))
@@ -1472,6 +1495,7 @@ static void ImGui_ImplGlfw_InitMultiViewportSupport()
14721495
#if GLFW_HAS_VULKAN
14731496
platform_io.Platform_CreateVkSurface = ImGui_ImplGlfw_CreateVkSurface;
14741497
#endif
1498+
platform_io.Platform_GetWindowDpiScale = ImGui_ImplGlfw_GetWindowDpiScale; // FIXME-DPI
14751499

14761500
// Register main window handle (which is owned by the main application, not by us)
14771501
// This is mostly for simplicity and consistency, so that our code (e.g. mouse handling etc.) can use same logic for main and secondary viewports.

0 commit comments

Comments
 (0)