SwiftUI how to control the display of a view in a window on MacOS

I need to display different WindowGroup depending on the condition, how is this possible?

    var body: some Scene
    {
        WindowGroup(id: "startView")
        {
            StartView(isLogined: $isLogined)
                .onAppear
                {
                    useMiniWindowStyle(status: true)
                }
        }// startView window
        .windowResizability(.contentSize)

        WindowGroup(id: "loginView")
        {
            AuthView(currentLogin: $isLogined)
                .frame(minWidth: screen!.width / 1.8, minHeight: screen!.height - 200)
        } // loginView window
        .windowResizability(.contentSize)
        .windowStyle(HiddenTitleBarWindowStyle())
    }
Answered by DTS Engineer in 774473022

Currently, control flow statement cannot be used within SceneBuilder. You would need to use the openWindow action environment value to create a new window that matches the unique strings identifier for the WindowGroup you intend to open. Keep in mind that SwiftUI lunches the first scene that appears in the SceneBuilder and on platforms like macOS and iPadOS, people can open more than one window from the group simultaneously.

You might consider using navigationDestination(for:destination:) to associate a destination view with a presented data type instead. The pseudocode below is an example.

struct ContentView: View {
    @State private var appState: AppState?

    var body: some View {
        NavigationStack {
            List {
                NavigationLink("SignIn", value: AppState.signIn)
                NavigationLink("SignUp", value: AppState.signUp)
            }
            .navigationDestination(for: AppState.self) { state in
                switch state {
                case .signIn: Text("Signed In")
                case .signUp: Text("SignUp")
                }
            }
        }
    }
}

Do you want to select between "startView" and "loginView" ?

I do not find in this code any State var that would control which to display, nor any toggle of such a State var.

Currently, control flow statement cannot be used within SceneBuilder. You would need to use the openWindow action environment value to create a new window that matches the unique strings identifier for the WindowGroup you intend to open. Keep in mind that SwiftUI lunches the first scene that appears in the SceneBuilder and on platforms like macOS and iPadOS, people can open more than one window from the group simultaneously.

You might consider using navigationDestination(for:destination:) to associate a destination view with a presented data type instead. The pseudocode below is an example.

struct ContentView: View {
    @State private var appState: AppState?

    var body: some View {
        NavigationStack {
            List {
                NavigationLink("SignIn", value: AppState.signIn)
                NavigationLink("SignUp", value: AppState.signUp)
            }
            .navigationDestination(for: AppState.self) { state in
                switch state {
                case .signIn: Text("Signed In")
                case .signUp: Text("SignUp")
                }
            }
        }
    }
}
SwiftUI how to control the display of a view in a window on MacOS
 
 
Q