简体   繁体   English

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

[英]Allow an NSWindow (NSPanel) to float above full screen apps

I'm trying to add a little window that provides "quick input" from any place in the system to the main app. 我正在尝试添加一个小窗口,从系统中的任何位置向主应用程序提供“快速输入”。

The user could hit a hotkey, the window pops up, and floats above all other windows. 用户可以点击热键,窗口弹出,并漂浮在所有其他窗口之上。

For the most part, this isn't much of a problem. 在大多数情况下,这不是一个问题。 I can configure an NSWindow to be: 我可以将NSWindow配置为:

level = Int(CGWindowLevelKey.TornOffMenuWindowLevelKey.rawValue)
collectionBehavior = .CanJoinAllSpaces

I can also make it an NSPanel with NSNonactivatingPanelMask option set. 我也可以使用NSNonactivatingPanelMask选项设置为NSNonactivatingPanelMask

The only problem is: how can I make it so that the window can pop up on the screen even if the user is on a space containing a full screen app? 唯一的问题是:即使用户位于包含全屏应用程序的空间,我怎样才能使窗口弹出窗口?

I know this is possible when the app is LSUIElement=true (an app without a position in the Dock), but mine isn't. 我知道当应用程序是LSUIElement=true (在Dock中没有位置的应用程序)时,这是可能的,但我的不是。

Okay, I had the right idea, the tricky part is how all the options interact with each other. 好吧,我有正确的想法,棘手的部分是所有选项如何相互作用。 Here's what works: 这是有效的:

  • NSPanel, not NSWindow NSPanel, 而不是 NSWindow
  • style mask: [.borderless, .nonactivatingPanel] 样式面具: [.borderless, .nonactivatingPanel]

And these properties: 而这些属性:

panel.level = .mainMenu
panel.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]

Swift 4.2 Code Swift 4.2代码

Create and show a panel using these settings. 使用这些设置创建并显示面板。 Then you can drag the panel onto a fullscreen app (dual monitor setup). 然后,您可以将面板拖动到全屏应用程序(双显示器设置)。

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()

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

Switching to borderless will prevent the user from moving your window. 切换到无边框将阻止用户移动窗口。

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()

And the Swift 4.0 translation is this.. I am still testing this, but it seems to be working. Swift 4.0的翻译是这样的......我还在测试它,但它似乎正在工作。

self.view.window?.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.mainMenuWindow)))
self.view.window?.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]

The Swift 3.0 version of your self-answer is 您自我回答的Swift 3.0版本是

window.level = Int(CGWindowLevelForKey(.mainMenuWindow))
window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM