Handling Multiple Windows on visionOS

Handling Multiple Windows on visionOS

Handling Multiple Windows on visionOS

April 2, 2024

image

Sometimes in visionOS you use multiple windows. Maybe you have a 3D object you want to show off in a Volume, as opposed to a boring 2D window.

Spatial Computing
Spatial Computing

But with two windows open, you users can close your main window and.... dangnabbit there's no way to reopen it. The trick is to use ScenePhase to track your main window's lifecycle, and then use that state to update UI in secondary windows (e.g. add a button to reopen the main window if its closed)Here's how to solve your visionOS "my users are trapped in a useless second window" issue:

image
image
image
image

A few notes:

1. Don't use onAppear/Disappear as the _exclusive_ lifecycle tracker. You get bad results as it doesn't cover all lifecycle events.

2. Yes I use onAppear to set the initial active state. This is due to how onChange operates, given the setup outlined here.

3. This setup opens a new window for each model you open. You can have a single "ongoing" window if you want (left as an exercise for the reader)

4. The ScenePhase case inactive is *not* background. Background is "closed", inactive can be just "the user hasn't looked my way in while"

5. Force Quitting (hold both hardware buttons down for a bit) or restarting the device are alternative "solutions"

6. This same ScenePhase state juggle works for ImmersiveSpaces (any scene type really)

7. Complex apps can often have many scene types. Abstracting these state/scene properties in some fashion can be worth investigating, depending on your needs.

Happy Coding from Polycam 🚀