[英]Why does my NSWindow not float above full screen apps when created programmatically? (Swift 5)
[英]Allow an NSWindow (NSPanel) to float above full screen apps
我正在嘗試添加一個小窗口,從系統中的任何位置向主應用程序提供“快速輸入”。
用戶可以點擊熱鍵,窗口彈出,並漂浮在所有其他窗口之上。
在大多數情況下,這不是一個問題。 我可以將NSWindow配置為:
level = Int(CGWindowLevelKey.TornOffMenuWindowLevelKey.rawValue)
collectionBehavior = .CanJoinAllSpaces
我也可以使用NSNonactivatingPanelMask
選項設置為NSNonactivatingPanelMask
。
唯一的問題是:即使用戶位於包含全屏應用程序的空間,我怎樣才能使窗口彈出窗口?
我知道當應用程序是LSUIElement=true
(在Dock中沒有位置的應用程序)時,這是可能的,但我的不是。
好吧,我有正確的想法,棘手的部分是所有選項如何相互作用。 這是有效的:
[.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()
切換到無邊框將阻止用戶移動窗口。
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.