UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

invalid display identifier error while trying to make app full screen.

Forums > SwiftUI

@ian  

Hello all,

I am working on a SwiftUI MacOS app that is simple note pad type app.

The app is a single window with a single TextEditor view, the unique feature of my app is that when my computer goes to sleep, the app window is made front and full screen so that when I return or wake my computer my notes are the first thing I see.

I am running into a couple bugs that I could use some guidance with.

  1. When I have an external monitor plugged into my laptop I get the following error and the app window will not go full screen or come to front: invalid display identifier 904CC364-346D-40EA-B050-8A3632616A77

  2. When I am running a browser (tested with Firefox and Safari) the app will also not go fullscreen but I get no errors.

Below is the relevant code from my App.swift file:

//
//  FullScreenApp.swift
//  FullScreen
//
//  Created by user on 6/18/23.
//
//import Foundation
import SwiftUI

@main
struct FullScreenApp: App {

    var body: some Scene {
        Window("theWindow", id: "myWindow") {
           ContentView()
               .onAppear {
                   DispatchQueue.main.asyncAfter(deadline: .now()) {
                       if let window = NSApplication.shared.windows.first {
                           window.backgroundColor = .white
                       }
                   }
               }
        }

        .windowStyle(HiddenTitleBarWindowStyle())

        }

        class AppDelegate: NSObject, NSApplicationDelegate, NSWindowDelegate {
                func applicationDidFinishLaunching(_ notification: Notification) {
                    let mainWindow = NSApp.windows[0]
                    mainWindow.delegate = self

                    mainWindow.standardWindowButton(.zoomButton)?.isHidden = true

                    //Lets add a listener thing here.

                    NSWorkspace.shared.notificationCenter.addObserver(self, selector: #selector(sleepListener(_:)), name: NSWorkspace.didWakeNotification, object: nil)
                }

                func windowShouldClose(_ sender: NSWindow) -> Bool {
                    NSApp.hide(nil)
                    return false
                }

            @objc private func sleepListener(_ aNotification: Notification) {
                    print("Listening for sleep")

                    if aNotification.name == NSWorkspace.didWakeNotification {
                        print("Did wake detected from App.swift file!")

                        NSApp.unhide(self)

                        if let wnd = NSApp.windows.first {

                            let isWindowFullscreen = wnd.styleMask.contains(.fullScreen)
                            print("is window full screen: \(isWindowFullscreen)")

                            //This brings the app to the most very front of everything.
                            wnd.orderFrontRegardless()
                            wnd.setIsVisible(true)
                            wnd.toggleFullScreen(nil)

                            print("Should be full screen")

                        }

                    } else if aNotification.name == NSWorkspace.didWakeNotification {
                        print("Woke up")
                    } else {
                        print("Some other event other than the first two")
                    }
                }
            }

        @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

}

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.