2
votes

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

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
This is just for you to know how is the proper syntaxLeo Dabus
I think you can’t have a window above full screen since High Sierra (above screen savers included)Leo Dabus
@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

1 Answers

0
votes

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...