| 76 | } |
| 77 | |
| 78 | GLFWmonitor* WindowController::currentMonitor() { |
| 79 | if (!window) { |
| 80 | return glfwGetPrimaryMonitor(); |
| 81 | } |
| 82 | |
| 83 | if (GLFWmonitor* fullscreenMonitor = glfwGetWindowMonitor(window)) { |
| 84 | return fullscreenMonitor; |
| 85 | } |
| 86 | |
| 87 | int windowX = 0; |
| 88 | int windowY = 0; |
| 89 | int windowWidth = 0; |
| 90 | int windowHeight = 0; |
| 91 | glfwGetWindowPos(window, &windowX, &windowY); |
| 92 | glfwGetWindowSize(window, &windowWidth, &windowHeight); |
| 93 | |
| 94 | const int centerX = windowX + windowWidth / 2; |
| 95 | const int centerY = windowY + windowHeight / 2; |
| 96 | |
| 97 | int monitorCount = 0; |
| 98 | GLFWmonitor** monitors = glfwGetMonitors(&monitorCount); |
| 99 | if (!monitors || monitorCount <= 0) { |
| 100 | return glfwGetPrimaryMonitor(); |
| 101 | } |
| 102 | |
| 103 | GLFWmonitor* bestMonitor = monitors[0]; |
| 104 | int bestOverlap = -1; |
| 105 | |
| 106 | for (int i = 0; i < monitorCount; ++i) { |
| 107 | int monitorX = 0; |
| 108 | int monitorY = 0; |
| 109 | glfwGetMonitorPos(monitors[i], &monitorX, &monitorY); |
| 110 | const GLFWvidmode* mode = glfwGetVideoMode(monitors[i]); |
| 111 | if (!mode) { |
| 112 | continue; |
| 113 | } |
| 114 | |
| 115 | if (centerX >= monitorX && centerX < monitorX + mode->width && |
| 116 | centerY >= monitorY && centerY < monitorY + mode->height) { |
| 117 | return monitors[i]; |
| 118 | } |
| 119 | |
| 120 | const int overlapWidth = |
| 121 | std::max(0, std::min(windowX + windowWidth, monitorX + mode->width) - std::max(windowX, monitorX)); |
| 122 | const int overlapHeight = |
| 123 | std::max(0, std::min(windowY + windowHeight, monitorY + mode->height) - std::max(windowY, monitorY)); |
| 124 | const int overlap = overlapWidth * overlapHeight; |
| 125 | |
| 126 | if (overlap > bestOverlap) { |
| 127 | bestOverlap = overlap; |
| 128 | bestMonitor = monitors[i]; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return bestMonitor; |
| 133 | } |
| 134 | |
| 135 | void WindowController::windowPosCallback(GLFWwindow* callbackWindow, int x, int y) { |
nothing calls this directly
no outgoing calls
no test coverage detected