Post

Replies

Boosts

Views

Activity

Reply to App Window Closure Sequence Impacts Main Interface Reload Behavior
Hello Greg @DTS Engineer , Thank you for your prompt response. I've implemented your suggestion and tried several approaches to solve my window management issue, but I'm still encountering problems that I'd like to clarify. Current Issue: When clicking on a country flag in the Earth window, multiple instances of both the main window and Earth window are being created, despite using the same window IDs. What I've Tried: I've implemented a window management system with a centralized WindowManager singleton that queues window open/close operations and prevents operations that are too frequent: // WindowManager singleton to manage window operations class WindowManager { static let shared = WindowManager() // Serial queue to ensure window operations happen in order private let windowOperationQueue = DispatchQueue(label: "com.mxsing.travelimmersive.windowqueue") // Track last operation time by window ID private var lastOperationTime: [String: Date] = [:] // Track pending open requests private var pendingOpenRequests: [String: Bool] = [:] private init() {} // Request to open a window func requestOpenWindow(id: String, openWindowAction: @escaping (String) -> Void, updateStateAction: @escaping () -> Void) { windowOperationQueue.async { [weak self] in guard let self = self else { return } // Check for pending requests if self.pendingOpenRequests[id] == true { print("Window Manager: Already have pending request for \(id), ignoring") return } // Check last operation time to prevent frequent operations let now = Date() if let lastTime = self.lastOperationTime[id], now.timeIntervalSince(lastTime) < 1.0 { print("Window Manager: Operation for \(id) too frequent, ignoring") return } // Update timestamp and request status self.lastOperationTime[id] = now self.pendingOpenRequests[id] = true // Execute UI operations on main thread DispatchQueue.main.async { print("Window Manager: Opening window \(id)") updateStateAction() // Update state first openWindowAction(id) // Then open window // Clear pending status after 300ms DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) { self.pendingOpenRequests[id] = false } } } } } In AppModel, I've added methods to safely open and close windows: // Safe window opening method that uses the WindowManager func safeOpenWindow(id: String, openWindowAction: @escaping (String) -> Void) { WindowManager.shared.requestOpenWindow( id: id, openWindowAction: openWindowAction, updateStateAction: { [weak self] in // Update state if id == "MainWindow" { self?.mainWindowOpen = true self?.updateWindowExistence(id: id, exists: true) } else if id == "Earth" { self?.isGlobeWindowOpen = true self?.updateWindowExistence(id: id, exists: true) } } ) } // Safe window opening method that uses the WindowManager func safeOpenWindow(id: String, openWindowAction: @escaping (String) -> Void) { WindowManager.shared.requestOpenWindow( id: id, openWindowAction: openWindowAction, updateStateAction: { [weak self] in // Update state if id == "MainWindow" { self?.mainWindowOpen = true self?.updateWindowExistence(id: id, exists: true) } else if id == "Earth" { self?.isGlobeWindowOpen = true self?.updateWindowExistence(id: id, exists: true) } } ) } When a flag is clicked in the Earth view, we call this method: // In CountryButtonView Button { // Process country selection appModel.handleGlobeFlagTap(country: country.name) // Use safe window manager to open main window print("Flag clicked: Opening main window through safe window manager") appModel.safeOpenWindow(id: "MainWindow") { windowId in openWindow(id: windowId) } } label: { // Button content } Key Questions: Window Closure Detection: Is it possible to directly capture the event when a user manually closes a window in visionOS? If so, how can we detect this event? Duplicate Windows: According to the documentation, calling openWindow(id:) should focus an existing window with the same ID rather than creating a new one. However, in our testing, we still see multiple instances of windows with the same ID being created. How can we prevent duplicate windows from being created? Is there a recommended way to implement reliable window state tracking in visionOS that avoids these issues? Are there any undocumented limitations or best practices we should be aware of? I've included the relevant code snippets above, but I'm happy to provide more implementation details if needed. Any guidance would be greatly appreciated. Thank you
Topic: Spatial Computing SubTopic: General Tags:
Apr ’25
Reply to App Window Closure Sequence Impacts Main Interface Reload Behavior
I have another question regarding window management in visionOS, specifically about distinguishing between window closure and app backgrounding. Following the solution provided by radicalappdev in this thread, I implemented window state management using ScenePhase and shared state in AppModel: // In AppModel var mainWindowOpen: Bool = true // In ContentView @Environment(\.scenePhase) private var scenePhase .onChange(of: scenePhase, initial: true) { switch scenePhase { case .inactive, .background: appModel.mainWindowOpen = false case .active: appModel.mainWindowOpen = true @unknown default: appModel.mainWindowOpen = false } } This solution successfully handles the window closure sequence issue, but introduces a new problem: when users press the Home button on Vision Pro, the app exits because the main window's ScenePhase changes to .inactive or .background, which triggers the same state change as when the window is closed. 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) My questions are: Is this indeed a current limitation in visionOS, or is there a way to differentiate between these two events? If it is a limitation, are there any workarounds or alternative approaches to handle this scenario? Is this something that might be addressed in future visionOS updates? Any guidance would be greatly appreciated. Thank you.
Topic: Spatial Computing SubTopic: General Tags:
Apr ’25
Reply to Unable to Create a Fully Immersive Experience That Hides Other Windows in visionOS App
Hello Greg, @DTS Engineer Thank you for your previous guidance on using the dismissWindow action. Following your advice, I've successfully implemented window dismissal when entering the immersive panorama viewing experience, which has greatly improved the immersive feeling for users. Here's my current implementation: // When entering immersive panorama mode, I dismiss other windows func getPanoImageAndOpenImmersiveSpace() async { // ...existing code... if !appModel.immersiveSpaceOpened { // Dismiss main window and Earth window before opening immersive space dismissWindow(id: "MainWindow") dismissWindow(id: "Earth") try await openImmersiveSpace(id: "ImmersiveView") await MainActor.run { appModel.immersiveSpaceOpened = true cardState = .normal } } // ...remaining code... This works well for entering the immersive mode - all other windows are properly hidden. However, I'm facing a new challenge: when exiting the immersive panorama view, I can't properly restore the previous window state. My current exit implementation: func exitPanoramaView() { Task { // Close immersive space await dismissImmersiveSpace() // Reset immersive state appModel.resetImmersiveState() // Close panorama menu appModel.updateWindowExistence(id: "PanoramaMenu", exists: false) dismissWindow(id: "PanoramaMenu") // Try to reopen main window and Earth window openWindow(id: "MainWindow") if appModel.isGlobeWindowOpen { openWindow(id: "Earth") } } } While this reopens the windows, they appear in their initial state rather than preserving the state they had before entering the immersive mode (such as scroll position in the main window, or the specific view of the globe in the Earth window). Is there a recommended approach to: Preserve window state when dismissing windows before entering immersive mode? Restore windows with their previous state when exiting immersive mode? Handle the transition between regular windows and immersive space more smoothly? Any further guidance or code examples would be greatly appreciated. Thank you for your assistance.
Topic: Spatial Computing SubTopic: General Tags:
Apr ’25
Reply to App Window Closure Sequence Impacts Main Interface Reload Behavior
Thanks, radicalappdev. I have tried your solution, it works well, if user close the main interface window first, the app will exit, but it seems that this solution can trigger a new problem, when I press home button, the App will also exit, can not run in the background. I asked AI, was told that currently the VisionOS can't differentiate the closure of main interface window and pressing of home button. Look forward to your further help. Thanks~
Topic: Spatial Computing SubTopic: General Tags:
Apr ’25