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 openWindow
and callingopenWindow
in theApp
init function. This results in an error about needing to callopenWindow
from within the SwiftUI lifecycle (makes sense becausebody
hasn't even been called yet).- Delaying the call to
openWindow
by putting it in aTask
and 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
WindowGroup
instead ofWindow
andhandlesExternalEvents
.WindowGroup
doesn'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
View
instantiated 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.