@@ -1971,6 +1971,40 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window)
19711971 return NSScreen .mainScreen ;
19721972}
19731973
1974+ static NSScreen *
1975+ active_screen (void ) {
1976+ NSPoint mouseLocation = [NSEvent mouseLocation ];
1977+ NSArray <NSScreen *> *screens = [NSScreen screens ];
1978+ for (NSScreen *screen in screens) {
1979+ if (NSPointInRect (mouseLocation, [screen frame ])) {
1980+ return screen;
1981+ }
1982+ }
1983+ // As a fallback, return the main screen
1984+ return [NSScreen mainScreen ];
1985+ }
1986+
1987+ static bool
1988+ is_same_screen (NSScreen *screenA, NSScreen * screenB) {
1989+ if (screenA == screenB) return true ;
1990+ NSDictionary <NSDeviceDescriptionKey , id > *deviceDescriptionA = [screenA deviceDescription ];
1991+ NSDictionary <NSDeviceDescriptionKey , id > *deviceDescriptionB = [screenB deviceDescription ];
1992+ NSNumber *screenNumberA = deviceDescriptionA[@" NSScreenNumber" ];
1993+ NSNumber *screenNumberB = deviceDescriptionB[@" NSScreenNumber" ];
1994+ return [screenNumberA isEqualToNumber: screenNumberB];
1995+ }
1996+
1997+ static void
1998+ move_window_to_screen (_GLFWwindow *window, NSScreen *target) {
1999+ NSRect screenFrame = [target visibleFrame ];
2000+ NSRect windowFrame = [window->ns.object frame ];
2001+ CGFloat newX = NSMidX (screenFrame) - (windowFrame.size .width / 2.0 );
2002+ CGFloat newY = NSMidY (screenFrame) - (windowFrame.size .height / 2.0 );
2003+ NSRect newWindowFrame = NSMakeRect (newX, newY, windowFrame.size .width , windowFrame.size .height );
2004+ [window->ns.object setFrame: newWindowFrame display: NO animate: NO ];
2005+ if (window->ns .layer_shell .is_active ) _glfwPlatformSetLayerShellConfig (window, NULL );
2006+ }
2007+
19742008const GLFWLayerShellConfig*
19752009_glfwPlatformGetLayerShellConfig (_GLFWwindow *window) {
19762010 return &window->ns .layer_shell .config ;
@@ -2257,10 +2291,18 @@ void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
22572291 }
22582292}
22592293
2260- void _glfwPlatformShowWindow (_GLFWwindow* window)
2294+ void _glfwPlatformShowWindow (_GLFWwindow* window, bool move_to_active_screen )
22612295{
22622296 const bool is_background = window->ns .layer_shell .is_active && window->ns .layer_shell .config .type == GLFW_LAYER_SHELL_BACKGROUND;
22632297 NSWindow *nw = window->ns .object ;
2298+ if (move_to_active_screen) {
2299+ NSScreen *current_screen = screen_for_window_center (window);
2300+ NSScreen *target_screen = active_screen ();
2301+ if (!is_same_screen (current_screen, target_screen)) {
2302+ debug_rendering (" Moving OS window %llu to active screen\n " , window->id );
2303+ move_window_to_screen (window, target_screen);
2304+ }
2305+ }
22642306 if (is_background) {
22652307 [nw orderBack: nil ];
22662308 } else {
0 commit comments