From 950539b7688e8f7776f7c087d59792765b760c84 Mon Sep 17 00:00:00 2001 From: omar Date: Fri, 13 Apr 2018 18:07:23 +0200 Subject: [PATCH] Viewport, Platform, DPI: Back-end store per-monitor DPI information in ImGuiPlatformMonitor. (#1542, #1676) --- examples/imgui_impl_glfw.cpp | 22 ++++++++++++---------- examples/imgui_impl_sdl2.cpp | 17 ++++++++++++----- examples/imgui_impl_win32.cpp | 3 ++- imgui.cpp | 2 +- imgui.h | 2 ++ 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/examples/imgui_impl_glfw.cpp b/examples/imgui_impl_glfw.cpp index 416e0f99..cd628b71 100644 --- a/examples/imgui_impl_glfw.cpp +++ b/examples/imgui_impl_glfw.cpp @@ -35,15 +35,11 @@ #define GLFW_EXPOSE_NATIVE_WIN32 #include // for glfwGetWin32Window #endif -#ifdef GLFW_HOVERED -#define GLFW_HAS_GLFW_HOVERED 1 -#else -#define GLFW_HAS_GLFW_HOVERED 0 -#endif -#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ -#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ -#define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ - +#define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING +#define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED +#define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity +#define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale +#define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface // Data enum GlfwClientApi @@ -530,7 +526,7 @@ static void ImGui_ImplGlfw_UpdateMonitors() ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); int monitors_count = 0; GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count); - platform_io.Monitors.resize(monitors_count); + platform_io.Monitors.resize(monitors_count, ImGuiPlatformMonitor()); for (int n = 0; n < monitors_count; n++) { int x, y; @@ -538,6 +534,12 @@ static void ImGui_ImplGlfw_UpdateMonitors() const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]); platform_io.Monitors[n].Pos = ImVec2((float)x, (float)y); platform_io.Monitors[n].Size = ImVec2((float)vid_mode->width, (float)vid_mode->height); +#if GLFW_HAS_PER_MONITOR_DPI + // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime. + float x_scale, y_scale; + glfwGetMonitorContentScale(glfw_monitors[n], &x_scale, &y_scale); + platform_io.Monitors[n].DpiScale = x_scale; +#endif } } diff --git a/examples/imgui_impl_sdl2.cpp b/examples/imgui_impl_sdl2.cpp index e01bd79d..09391004 100644 --- a/examples/imgui_impl_sdl2.cpp +++ b/examples/imgui_impl_sdl2.cpp @@ -32,10 +32,11 @@ // SDL #include #include -#define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4) -#define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) -#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) +#define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4) +#define SDL_HAS_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5) +#define SDL_HAS_ALWAYS_ON_TOP SDL_VERSION_ATLEAST(2,0,5) +#define SDL_HAS_PER_MONITOR_DPI SDL_VERSION_ATLEAST(2,0,4) +#define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6) #if !SDL_HAS_VULKAN static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; #endif @@ -446,13 +447,19 @@ static void ImGui_ImplSDL2_UpdateMonitors() { ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); int display_count = SDL_GetNumVideoDisplays(); - platform_io.Monitors.resize(display_count); + platform_io.Monitors.resize(display_count, ImGuiPlatformMonitor()); for (int n = 0; n < display_count; n++) { + // Warning: the validity of monitor DPI information on Windows depends on the application DPI awareness settings, which generally needs to be set in the manifest or at runtime. SDL_Rect r; SDL_GetDisplayBounds(n, &r); platform_io.Monitors[n].Pos = ImVec2((float)r.x, (float)r.y); platform_io.Monitors[n].Size = ImVec2((float)r.w, (float)r.h); +#if SDL_HAS_PER_MONITOR_DPI + float dpi = 0.0f; + SDL_GetDisplayDPI(n, &dpi, NULL, NULL); + platform_io.Monitors[n].DpiScale = dpi / 96.0f; +#endif } } diff --git a/examples/imgui_impl_win32.cpp b/examples/imgui_impl_win32.cpp index efe5b461..1849a2d6 100644 --- a/examples/imgui_impl_win32.cpp +++ b/examples/imgui_impl_win32.cpp @@ -550,11 +550,12 @@ static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd, return DefWindowProc(hWnd, msg, wParam, lParam); } -static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR, HDC, LPRECT rect, LPARAM) +static BOOL CALLBACK ImGui_ImplWin32_UpdateMonitors_EnumFunc(HMONITOR monitor, HDC, LPRECT rect, LPARAM) { ImGuiPlatformMonitor imgui_monitor; imgui_monitor.Pos = ImVec2((float)rect->left, (float)rect->top); imgui_monitor.Size = ImVec2((float)(rect->right - rect->left), (float)(rect->bottom - rect->top)); + imgui_monitor.DpiScale = ImGui_ImplWin32_GetDpiScaleForMonitor(monitor); ImGui::GetPlatformIO().Monitors.push_back(imgui_monitor); return TRUE; } diff --git a/imgui.cpp b/imgui.cpp index e32fb60a..789c5f4f 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -14258,7 +14258,7 @@ void ImGui::ShowMetricsWindow(bool* p_open) for (int i = 0; i < g.PlatformIO.Monitors.Size; i++) { const ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[i]; - ImGui::BulletText("Monitor #%d: Min (%.0f,%.0f) Max (%.0f,%.0f) Size (%.0f,%.0f)", i, mon.Pos.x, mon.Pos.y, mon.Pos.x + mon.Size.x, mon.Pos.y + mon.Size.y, mon.Size.x, mon.Size.y); + ImGui::BulletText("Monitor #%d: DPI %.0f%%, Min (%.0f,%.0f), Max (%.0f,%.0f), Size (%.0f,%.0f)", i, mon.DpiScale * 100.0f, mon.Pos.x, mon.Pos.y, mon.Pos.x + mon.Size.x, mon.Pos.y + mon.Size.y, mon.Size.x, mon.Size.y); } ImGui::TreePop(); } diff --git a/imgui.h b/imgui.h index 6b4781d0..2b4d8871 100644 --- a/imgui.h +++ b/imgui.h @@ -1879,6 +1879,8 @@ struct ImGuiPlatformMonitor { ImVec2 Pos; ImVec2 Size; + float DpiScale; + ImGuiPlatformMonitor() { Pos = ImVec2(0,0); Size = ImVec2(0,0); DpiScale = 1.0f; } }; // (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled