1

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 calling openWindow in the App init function. This results in an error about needing to call openWindow from within the SwiftUI lifecycle (makes sense because body hasn't even been called yet).
  • Delaying the call to openWindow by putting it in a Task and sleeping for a bit. Nothing happens.
  • Using @Environment(\.scenePhase) to trigger the call to openWindow. This happens only after a document window is created (too late).
  • Using WindowGroup instead of Window and handlesExternalEvents. 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 use onAppear.

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.

Taylor
  • 5,871
  • 2
  • 30
  • 64

0 Answers0