My VisionOS App (Travel Immersive) has two interface windows: a main 2D interface window and a 3D Earth window. If the user first closes the main interface window and then the Earth window, clicking the app icon again will only launch the Earth window while failing to display the main interface window. However, if the user closes the Earth window first and then the main interface window, the app restarts normally.
Below is the code of
import SwiftUI
@main struct Travel_ImmersiveApp: App { @StateObject private var appModel = AppModel()
var body: some Scene {
WindowGroup(id: "MainWindow") {
ContentView()
.environmentObject(appModel)
.onDisappear {
appModel.closeEarthWindow = true
}
}
.windowStyle(.automatic)
.defaultSize(width: 1280, height: 825)
WindowGroup(id: "Earth") {
if !appModel.closeEarthWindow {
Globe3DView()
.environmentObject(appModel)
.onDisappear {
appModel.isGlobeWindowOpen = false
}
} else {
EmptyView() // 关闭时渲染空视图
}
}
.windowStyle(.volumetric)
.defaultSize(width: 0.8, height: 0.8, depth: 0.8, in: .meters)
ImmersiveSpace(id: "ImmersiveView") {
ImmersiveView()
.environmentObject(appModel)
}
}
}
Hello @Travel_Immersive,
If the user first closes the main interface window and then the Earth window, clicking the app icon again will only launch the Earth window while failing to display the main interface window. However, if the user closes the Earth window first and then the main interface window, the app restarts normally.
I understand that this might be due to a current limitation in visionOS where the system cannot distinguish between: User actively closing the main window (which should exit the app) User pressing the Home button (which should only background the app)
To clarify, when you close a window scene on visionOS, that scene will be disconnected, unless is is your app's last window scene.
If you close your app's last window scene, it will be backgrounded.
Given this, and what you have described so far about your app, here is my recommendation:
-
Do not allow your users to become "stranded" on your "Earth" scene with no way back to the "main" scene of your app. Provide a UI affordance in your "Earth" scene to get back to your "main" scene. (Instead of attempting to use events/states from other scenes to create this behavior)
-
File an enhancement request for the window management APIs that you would like to see (i.e. "I would like an API that will enable my app to tie the lifecycle of different window scenes together in a particular way.")
--Greg