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

前端 未结 3 1900
醉梦人生
醉梦人生 2021-02-10 00:09

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 float

3条回答
  •  灰色年华
    2021-02-10 00:46

    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
    • style mask: [.borderless, .nonactivatingPanel]

    And these properties:

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

    Swift 4.2 Code

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

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

提交回复
热议问题