It appears that Window
is not available on visionOS, only WindowGroup
. If I want to have a window with only one instance (say, a Menu or similar), how can I ensure that each time I call openWindow
, the same window appears, and a new one is not created?
How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here
Single Instance Windows on visionOS
You could either disable "Enable Multiple Windows" , use a State variable or check if the Window is already open programmatically.
Hi @benfromchicago ,
If I want to have a window with only one instance (say, a Menu or similar), how can I ensure that each time I call openWindow, the same window appears, and a new one is not created?
This is detailed in the WindowGroup documentation, so please take a look! I'll review the important parts below.
How openWindow works:
openWindow allows you to pass an id, value, or both to indicate which scene to open.
The id parameter always takes a string. For a WindowGroup
, if you pass just an id, the system creates a new window for the group. If the WindowGroup
presents data, the system provides the default value or nil to the window’s root view.
Ex:
WindowGroup(id: "firstWindow"){
ContentView()
}
In ContentView
, call openWindow(id: "firstWindow")
The value parameter takes in a variable of any type, so long as that type matches one specified in the WindowGroup
. This allows you to provide a value to present to the WindowGroup
. If the interface already has a window from the group that’s presenting the specified value, the system brings the window to the front. Otherwise, the system creates a new window and passes a binding to the specified value.
WindowGroup(for: String.self){ $num in
ContentView(number: num)
}
In ContentView
, call openWindow(value: "open")
(or use any variable of type String)
Lastly, you can also pass both an identifier and a value. This enables you to define multiple window groups that take input values of the same type like a UUID. This has the same behavior as above, a window presenting the value will be brought to the front, otherwise a new window will be created.