Skip to content

Commit

Permalink
Target ideal monitor for fullscreen toggle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Themaister committed Nov 3, 2023
1 parent e1e1585 commit f74497b
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions application/platforms/application_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}
}
Expand Down

0 comments on commit f74497b

Please sign in to comment.