允许NSWindow(NSPanel)浮动在全屏应用之上

rad*_*dex 4 macos cocoa nswindow nspanel

我正在尝试添加一个小窗口,从系统中的任何位置向主应用程序提供"快速输入".

用户可以点击热键,窗口弹出,并漂浮在所有其他窗口之上.

在大多数情况下,这不是一个问题.我可以将NSWindow配置为:

level = Int(CGWindowLevelKey.TornOffMenuWindowLevelKey.rawValue)
collectionBehavior = .CanJoinAllSpaces
Run Code Online (Sandbox Code Playgroud)

我也可以使用NSNonactivatingPanelMask选项集作为NSPanel .

唯一的问题是:即使用户位于包含全屏应用程序的空间,我怎样才能使窗口弹出窗口?

我知道当应用程序LSUIElement=true(在Dock中没有位置的应用程序)时这是可能的,但我的不是.

rad*_*dex 6

好吧,我有正确的想法,棘手的部分是所有选项如何相互作用.这是有效的:

  • NSPanel,而不是 NSWindow
  • 风格面具: [.borderless, .nonactivatingPanel]

而这些属性:

panel.level = .mainMenu
panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
Run Code Online (Sandbox Code Playgroud)

Swift 4.2代码

使用这些设置创建并显示面板.然后,您可以将面板拖动到全屏应用程序(双显示器设置).

let panel2 = NSPanel(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: [.titled, .nonactivatingPanel], backing: .buffered, defer: true)
panel2.level = .mainMenu
panel2.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
panel2.orderFrontRegardless()
Run Code Online (Sandbox Code Playgroud)

将NSPanel浮动到全屏macOS应用程序之上

切换到无边框将阻止用户移动窗口.

let panel2 = NSPanel(contentRect: NSRect(x: 0, y: 0, width: 200, height: 200), styleMask: [.borderless, .nonactivatingPanel], backing: .buffered, defer: true)
panel2.level = .mainMenu
panel2.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
panel2.orderFrontRegardless()
Run Code Online (Sandbox Code Playgroud)