簡體   English   中英

允許NSWindow(NSPanel)浮動在全屏應用之上

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

我正在嘗試添加一個小窗口,從系統中的任何位置向主應用程序提供“快速輸入”。

用戶可以點擊熱鍵,窗口彈出,並漂浮在所有其他窗口之上。

在大多數情況下,這不是一個問題。 我可以將NSWindow配置為:

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

我也可以使用NSNonactivatingPanelMask選項設置為NSNonactivatingPanelMask

唯一的問題是:即使用戶位於包含全屏應用程序的空間,我怎樣才能使窗口彈出窗口?

我知道當應用程序是LSUIElement=true (在Dock中沒有位置的應用程序)時,這是可能的,但我的不是。

好吧,我有正確的想法,棘手的部分是所有選項如何相互作用。 這是有效的:

  • NSPanel, 而不是 NSWindow
  • 樣式面具: [.borderless, .nonactivatingPanel]

而這些屬性:

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

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

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

Swift 4.0的翻譯是這樣的......我還在測試它,但它似乎正在工作。

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

您自我回答的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