Viewport, Platform, DPI: Back-end store per-monitor DPI information in ImGuiPlatformMonitor. (#1542, #1676)

This commit is contained in:
omar 2018-04-13 18:07:23 +02:00
parent f270d6c52c
commit 950539b768
5 changed files with 29 additions and 17 deletions

View File

@ -35,15 +35,11 @@
#define GLFW_EXPOSE_NATIVE_WIN32 #define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h> // for glfwGetWin32Window #include <GLFW/glfw3native.h> // for glfwGetWin32Window
#endif #endif
#ifdef GLFW_HOVERED #define GLFW_HAS_WINDOW_TOPMOST (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ GLFW_FLOATING
#define GLFW_HAS_GLFW_HOVERED 1 #define GLFW_HAS_WINDOW_HOVERED (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ GLFW_HOVERED
#else #define GLFW_HAS_WINDOW_ALPHA (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwSetWindowOpacity
#define GLFW_HAS_GLFW_HOVERED 0 #define GLFW_HAS_PER_MONITOR_DPI (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3300) // 3.3+ glfwGetMonitorContentScale
#endif #define GLFW_HAS_VULKAN (GLFW_VERSION_MAJOR * 1000 + GLFW_VERSION_MINOR * 100 >= 3200) // 3.2+ glfwCreateWindowSurface
#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+
// Data // Data
enum GlfwClientApi enum GlfwClientApi
@ -530,7 +526,7 @@ static void ImGui_ImplGlfw_UpdateMonitors()
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
int monitors_count = 0; int monitors_count = 0;
GLFWmonitor** glfw_monitors = glfwGetMonitors(&monitors_count); 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++) for (int n = 0; n < monitors_count; n++)
{ {
int x, y; int x, y;
@ -538,6 +534,12 @@ static void ImGui_ImplGlfw_UpdateMonitors()
const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]); const GLFWvidmode* vid_mode = glfwGetVideoMode(glfw_monitors[n]);
platform_io.Monitors[n].Pos = ImVec2((float)x, (float)y); platform_io.Monitors[n].Pos = ImVec2((float)x, (float)y);
platform_io.Monitors[n].Size = ImVec2((float)vid_mode->width, (float)vid_mode->height); 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
} }
} }

View File

@ -35,6 +35,7 @@
#define SDL_HAS_CAPTURE_MOUSE SDL_VERSION_ATLEAST(2,0,4) #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_WINDOW_OPACITY SDL_VERSION_ATLEAST(2,0,5)
#define SDL_HAS_ALWAYS_ON_TOP 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) #define SDL_HAS_VULKAN SDL_VERSION_ATLEAST(2,0,6)
#if !SDL_HAS_VULKAN #if !SDL_HAS_VULKAN
static const Uint32 SDL_WINDOW_VULKAN = 0x10000000; static const Uint32 SDL_WINDOW_VULKAN = 0x10000000;
@ -446,13 +447,19 @@ static void ImGui_ImplSDL2_UpdateMonitors()
{ {
ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO(); ImGuiPlatformIO& platform_io = ImGui::GetPlatformIO();
int display_count = SDL_GetNumVideoDisplays(); 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++) 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_Rect r;
SDL_GetDisplayBounds(n, &r); SDL_GetDisplayBounds(n, &r);
platform_io.Monitors[n].Pos = ImVec2((float)r.x, (float)r.y); platform_io.Monitors[n].Pos = ImVec2((float)r.x, (float)r.y);
platform_io.Monitors[n].Size = ImVec2((float)r.w, (float)r.h); 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
} }
} }

View File

@ -550,11 +550,12 @@ static LRESULT CALLBACK ImGui_ImplWin32_WndProcHandler_PlatformWindow(HWND hWnd,
return DefWindowProc(hWnd, msg, wParam, lParam); 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; ImGuiPlatformMonitor imgui_monitor;
imgui_monitor.Pos = ImVec2((float)rect->left, (float)rect->top); 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.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); ImGui::GetPlatformIO().Monitors.push_back(imgui_monitor);
return TRUE; return TRUE;
} }

View File

@ -14258,7 +14258,7 @@ void ImGui::ShowMetricsWindow(bool* p_open)
for (int i = 0; i < g.PlatformIO.Monitors.Size; i++) for (int i = 0; i < g.PlatformIO.Monitors.Size; i++)
{ {
const ImGuiPlatformMonitor& mon = g.PlatformIO.Monitors[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(); ImGui::TreePop();
} }

View File

@ -1879,6 +1879,8 @@ struct ImGuiPlatformMonitor
{ {
ImVec2 Pos; ImVec2 Pos;
ImVec2 Size; 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 // (Optional) Setup required only if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable) is enabled