swiftmacosnswindowexternal-display

Opening an NSWIndow / NSView on a connected display


Environment: macOS Big Sur 11.5.2 / Swift 5.4
Target Platform: macOS

I came across this discussion while searching for how to open a new NSWindow / NSView on a connected display.

I converted the obj-c code to Swift, but I cannot get it to work. I have the method set up inside an IBAction and I linked the action to a button I placed in the view titled "Show External Display."

What am I doing wrong?

import Cocoa

class ViewController: NSViewController {

    var externalDisplay: NSScreen?
    var fullScreenWindow: NSWindow?
    var fullScreenView: NSView?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }

    @IBAction func showExternalDisplayButtonClicked(sender: Any?) {
        // Open a window on the external display if present
        if NSScreen.screens.count > 1 {
            externalDisplay = NSScreen.screens.last
            let externalDisplayRect = externalDisplay!.frame
            fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: .borderless, backing: .buffered, defer: true, screen: externalDisplay)
            fullScreenWindow!.level = .normal
            fullScreenWindow!.isOpaque = false
            fullScreenWindow!.hidesOnDeactivate = false
            fullScreenWindow!.backgroundColor = .red
            let viewRect = NSRect(x: 0, y: 0, width: externalDisplay!.frame.width, height: externalDisplay!.frame.height)
            fullScreenView = NSView(frame: viewRect)
            fullScreenWindow!.contentView = fullScreenView
            fullScreenWindow!.makeKeyAndOrderFront(self)
        }
    }

}

Solution

  • Why do you make styleMask: .borderless? Docs say a borderless window can't become key or main so makeKeyAndOrderFront fails.

    If you make a normal window like this:

    let mask: NSWindow.StyleMask = [.titled, .closable, .miniaturizable, .resizable]
    fullScreenWindow = NSWindow(contentRect: externalDisplayRect, styleMask: mask, backing: .buffered, defer: true, screen: externalDisplay)
    

    it opens on the last screen.