停止 child NSWindow 在移动到另一个屏幕时消失

Stop child NSWindow disappearing when moved to another screen

在 macOS Monetrey 上,当我将 child NSWindow 移动到另一个屏幕(通过手动拖动它)时,它消失了。使用 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()
    }
}

(我知道这可能不是“正确”的 SwiftUI 做事方式,它只是为了用尽可能少的代码来演示问题——这实际上发生在低级 Obj-C 代码中)

有趣的是,如果你:

child window 重新出现,现在可以在屏幕之间自由移动。

根据用户报告,我了解到大苏尔不会发生这种行为。

我知道 应用程序显示 child windows 并允许它们在屏幕之间移动,允许这样做的正确咒语是什么?

我相信您只想添加第二个 NSWindowNSPanel。没有 child window 关系。例如,在 AppKit document-based 应用程序中,您可以覆盖 -[NSDocument makeWindowControllers] 并为一个文档设置多个 window 控制器。

Child windows 用于特殊用例,例如:自动完成列表等功能,您需要 chrome-less window 在文本字段下方有一个 table 视图,移动时会自动跟随 parent window

这是具有此类自动完成功能的 TextEdit window 和相同的 Xcode 调试视图层次结构:

请参阅文档中的注释:

After the childWin is added as a child of the window, it is maintained in relative position indicated by place for subsequent ordering operations involving either window. While this attachment is active, moving childWin will not cause the window to move (as in sliding a drawer in or out), but moving the window will cause childWin to move.

我不认为 child window 曾经打算独立于其 parent window,尤其是不同的屏幕。