'Stop child NSWindow disappearing when moved to another screen

On macOS Monetrey, when I move a child NSWindow to another screen (by manually dragging it), it disappears. Minimal repro using SwiftUI:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Button("Hello World", action:  openChild)
            .padding(50)
    }
    
    func openChild()  {
        let win = NSWindow();
        let parent = NSApp.mainWindow
        parent?.addChildWindow(win, ordered: NSWindow.OrderingMode.above)
        win.makeKeyAndOrderFront(self)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

(I know this may not be the "correct" SwiftUI way of doing things, it's just to demonstrate the problem in as little code as possible - this happens in low level Obj-C code in reality)

The interesting thing is if you:

  • Show the child window
  • Move it to another screen (it disappears)
  • Select "Window -> Move to [Name of other screen]"

The child window reappears and can now be moved freely between screens.

From user reports I understand that this behavior doesn't occur on Big Sur.

I know there are applications that show child windows and allow them to be moved between screens, what is the correct incantation to allow this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source