From f74497b57d75f01ab1234c6b49c59111f943a644 Mon Sep 17 00:00:00 2001 From: Hans-Kristian Arntzen Date: Fri, 3 Nov 2023 16:48:38 +0100 Subject: [PATCH] Target ideal monitor for fullscreen toggle. --- application/platforms/application_glfw.cpp | 33 +++++++++++++++++++--- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/application/platforms/application_glfw.cpp b/application/platforms/application_glfw.cpp index 0cec6b0c..7da548f9 100644 --- a/application/platforms/application_glfw.cpp +++ b/application/platforms/application_glfw.cpp @@ -150,10 +150,35 @@ struct WSIPlatformGLFW : GraniteWSIPlatform else { // Windowed -> fullscreen - auto *primary = glfwGetPrimaryMonitor(); - if (primary) + int count; + + GLFWmonitor **monitors = glfwGetMonitors(&count); + GLFWmonitor *target_monitor = nullptr; + + int xpos, ypos, w, h; + glfwGetWindowPos(window, &xpos, &ypos); + glfwGetWindowSize(window, &w, &h); + xpos += w / 2; + ypos += h / 2; + + for (int i = 0; i < count; i++) + { + int x, y; + // Find monitor where center of window lies within it. Use that monitor as fullscreen target. + glfwGetMonitorWorkarea(monitors[i], &x, &y, &w, &h); + if (xpos >= x && ypos >= y && xpos < x + w && ypos < y + h) + { + target_monitor = monitors[i]; + break; + } + } + + if (!target_monitor) + target_monitor = glfwGetPrimaryMonitor(); + + if (target_monitor) { - auto *mode = glfwGetVideoMode(primary); + auto *mode = glfwGetVideoMode(target_monitor); WSIPlatformGLFW::CachedWindow win; glfwGetWindowPos(window, &win.x, &win.y); glfwGetWindowSize(window, &win.width, &win.height); @@ -163,7 +188,7 @@ struct WSIPlatformGLFW : GraniteWSIPlatform set_hmonitor(MonitorFromWindow(glfwGetWin32Window(window), MONITOR_DEFAULTTOPRIMARY)); }); #endif - glfwSetWindowMonitor(window, primary, 0, 0, mode->width, mode->height, mode->refreshRate); + glfwSetWindowMonitor(window, target_monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } } }