2

I am trying to create an NSWindow and make the window float above full screen apps. I have tried this using two different approaches which are outlined below.

Method 1 - storyboard (working)

I have a project with a storyboard that includes a window controller. This window controller has the following class assigned to it in the identity inspector.

class WindowController: NSWindowController {

    override func windowDidLoad() {
        super.windowDidLoad()
        guard let window = self.window else { return }

        // Enable full screen
        window.backgroundColor = .red
        window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.mainMenuWindow)))
        window.styleMask = [.fullScreen]
        window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
    }

}

This method works without any issues as seen in this screenshot. However, I would prefer to create the NSWindow programmatically.

Method 2 - programmatically (not working)

In this approach I have the following WindowController class:

class WindowController: NSWindowController {

    init() {
        super.init(window: NSWindow())
        guard let window = self.window else { return }
        setup(window: window)
    }

    func setup(window: NSWindow) {
        window.backgroundColor = .red
        window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.mainMenuWindow)))
        window.styleMask = [.fullScreen]
        window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary]
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
}

Along with the following in my appDelegate:

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    let window = WindowController()

    func applicationDidFinishLaunching(_ aNotification: Notification) {
        window.showWindow(self)
    }

}

Despite this successfully creating a NSWindow that floats above windowed apps, as seen here, the NSWindow refuses to float above full screen screen apps.

From the time I have spent trying to debug the issue it seems as though window.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary] does not do anything when creating the NSWindow programmatically in the second approach.

I am fairly new to swift so any help regarding why the NSWindow is not floating above fullscreen apps in the second approach would be greatly appreciated!

Versions:

  • Swift 5

  • Xcode 11.2.1

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hish
  • 21
  • 4
  • https://stackoverflow.com/a/27397096/2303865 – Leo Dabus Nov 19 '19 at 18:08
  • 1
    @LeoDabus I have already tried setting up my window levels in windowDidAppear, windowDidLoad, and even in an IBAction which made no difference at all for fullscreen. I have also used window?.level = .floating too. Evidently the question you linked is not related especially since my issue only applies to when the NSWindow is created programmatically as opposed to using a storyboard. I am definitely missing something but I am confident it is not what you have linked. – Hish Nov 19 '19 at 22:37
  • This is just for you to know how is the proper syntax – Leo Dabus Nov 19 '19 at 22:46
  • I think you can’t have a window above full screen since High Sierra (above screen savers included) – Leo Dabus Nov 19 '19 at 22:47
  • 1
    @LeoDabus It would probably be wise to add some context before simply commenting a link then... And the assumption about high sierra is incorrect as I already have a window floating above full screen working in my current app on Catalina, just not completely programmatically which is what I wish to do. – Hish Nov 25 '19 at 01:58

1 Answers1

0

7 months later and I managed to get it working by changing the window's styleMask to borderless

window.styleMask = [.borderless]

Better late then never I suppose...

Hish
  • 21
  • 4
  • This wasn't working to allow a window to show over top of another application's full-screen window that participates in Spaces (ie: green titlebar button, makes a window occupy an entire Space all to itself). I had to add the LSUIElement key to my app's info.plist to allow this to work. It has the effect of making the app sidestep the window manager so floating windows aren't forced to non-full-screen Spaces. (Fortunately my app is a system utility app that runs dockless in the system menu since this key removes the app's dock icon and removes it from participating in Cmd+Tab app switching.) – stef Feb 07 '22 at 05:20