I've got a document-based SwiftUI app and I'm trying to show a welcome window on startup (like the one in Xcode).
I've got:
Window("Welcome", id: "welcome") {
WelcomeView()
}
.windowStyle(.hiddenTitleBar)
.windowResizability(.contentSize)
This looks good and appears in the appropriate place in the Window menu (not among the document windows). The problem is that I can't figure out how to get it to show on startup. Things I've tried:
@Environment(\.openWindow) var openWindowand callingopenWindowin theAppinit function. This results in an error about needing to callopenWindowfrom within the SwiftUI lifecycle (makes sense becausebodyhasn't even been called yet).- Delaying the call to
openWindowby putting it in aTaskand sleeping for a bit. Nothing happens. - Using
@Environment(\.scenePhase)to trigger the call toopenWindow. This happens only after a document window is created (too late). - Using
WindowGroupinstead ofWindowandhandlesExternalEvents.WindowGroupdoesn't seem to be the right thing to use: File -> New becomes a submenu with both my normal document, and the Welcome window. - The technique here I think won't work because with a document-based app, I don't have a
Viewinstantiated on startup, and thus can't useonAppear.
I can most likely accomplish this by dropping down to AppKit, using an AppDelegate, explicitly creating a NSWindow and hosting the WelcomeView inside it. But I really shouldn't have to do that. Is there a way to do this in pure SwiftUI?
Update: I did accomplish this by dropping down to AppKit. Will leave this question here in case a nicer way becomes available.