在自定义 NSWindowController 中级联

Cascading in custom NSWindowController

我有一个基于文档的应用程序。我覆盖 NSDocumentmakeWindowControllers 以实例化自定义 window 控制器。它的初始化程序调用其超类的 init(window: NSWindow?),即它不使用任何涉及 nib 文件的初始化程序。

如何进行级联(参见 shouldCascadeWindows)?目前每个 window 在第一个屏幕上的相同位置打开。

我能否以某种方式重用现有的级联逻辑,也许通过在 NSWindowController 上调用某些东西?

如果我必须自己手动实现,我应该如何最好地获取最顶层文档的位置window?文档的潜在许多 windows 中的哪一个应该是计算偏移量的 window?

func cascadeTopLeft(from topLeftPoint: NSPoint) -> NSPoint

Positions the window's top left to a given point.

Parameters

topLeftPoint The new top-left point, in screen coordinates, for the window. When NSZeroPoint, the window is not moved, except as needed to constrain to the visible screen

NSZeroPoint是第一个window的起点。

Return Value The point shifted from top left of the window in screen coordinates.

Discussion

The returned point can be passed to a subsequent invocation of cascadeTopLeft(from:) to position the next window so the title bars of both windows are fully visible.

返回的点是下一个window的起点。

示例(如 TextEdit):

static var cascadingPoint = NSZeroPoint

override func makeWindowControllers() {
    let window = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .titled, backing: .buffered, defer: true)
    Document.cascadingPoint = window.cascadeTopLeft(from: Document.cascadingPoint)
    let windowController = NSWindowController(window: window)
    addWindowController(windowController)
}

另一个例子(比如 Safari)

override func makeWindowControllers() {
    let window = NSWindow(contentRect: NSMakeRect(100, 100, 500, 500), styleMask: .titled, backing: .buffered, defer: true)
    let cascadingPoint = NSApp.mainWindow?.cascadeTopLeft(from: NSZeroPoint) ?? NSZeroPoint
    window.cascadeTopLeft(from: cascadingPoint)
    let windowController = NSWindowController(window: window)
    addWindowController(windowController)
}