How to specify which WindowGroup should appear on visionOS app launch?

278 Views Asked by At

I'm writing a visionOS application using SwiftUI. The app utilises multiple WindowGroups.
I'm struggling with window management, especially in terms of window dismissal and app relaunch.

Here's a demo code presenting the problem:

import SwiftUI

@main
struct demoApp: App {
    var body: some Scene {
        WindowGroup {
            FirstView()
        }

        WindowGroup(id: "second") {
            SecondView()
        }
    }
}
import SwiftUI

struct FirstView: View {
    @Environment(\.openWindow) var openWindow

    var body: some View {
        VStack {
            Text("First window group")
            Button("Open second window group") {
                openWindow(id: "second")
            }
        }
    }
}
import SwiftUI

struct SecondView: View {
    var body: some View {
        Text("Second window group")
    }
}

I'm proceeding with the following actions:

  1. The app launches with the first WindowGroup.

  2. Click the button calling \.openWindow and opening the second WindowGroup.

  3. Click the native 'close' button at the bottom of the first window.

  4. Click the native 'close' button at the bottom of the second window. Now all of the groups are closed.

  5. Relaunch the app by clicking the app icon from the Home menu.

The app launches showing the second WindowGroup as it was the last one closed. I want the app to open the first WindowGroup (the root one in my case) instead.

Is there any way of specifying which window group of the scene should be opened in such situation?

I haven't found any information in the documentation about detecting which WindowGroup is opened at the moment, also haven't managed to come up with a solution using \.scenePhase.

0

There are 0 best solutions below