VisionOS : defaultWindowPlacement unavailable?

I'm getting the following error in my swift build targeting VisionOS 2.0 :

" 'defaultDisplay' is unavailable in visionOS "

TLDR : how do I specify an initial window position in visionOS? The docs seem to be off? - see below.

The docs say it is available, but it is not, or at least my XCODE (Version 16.0 ) is throwing errors on it : https://developer.apple.com/documentation/swiftui/scene/defaultwindowplacement(_:)

I know apple is opinionated about window placement in visionOS, and maybe it will never be available, but the docs say it is in visionOS 2.0+ and it sure would be nice to be able to specify a default position toward the bottom of one's FOV, etc .

Side-note -- the example code in that doc also has the issue that "Window" is not available in visionOS ( WindowGroup is ).


example code -- barely modified from example code in doc :

 var body: some Scene {
        
        WindowGroup("MyLilWindow", id: "MyLilWindow") {
            TestView()
        }
        .windowResizability(.contentSize)
        .defaultWindowPlacement { content, context in
            let displayBounds = context.defaultDisplay.visibleRect
            let size = content.sizeThatFits(.unspecified)
            let position = CGPoint(
                x: displayBounds.midX - (size.width / 2),
                y: displayBounds.maxY - size.height - 140)
            return WindowPlacement(position)
        }

}
Answered by Vision Pro Engineer in 806311022

Hi @beechbot ,

The code snippet you referenced is for macOS. For visionOS, try something like:

  var body: some Scene {
        WindowGroup("content", id: "content") {
            ContentView()
        }
        
        WindowGroup("Status", id: "status") {
            StatusView()
        }
        .windowResizability(.contentSize)
        .defaultWindowPlacement { content, context in
       
            return WindowPlacement(.above(context.windows.first(where: { $0.id == "content" })!))
        }

where ContentView looks like this:

struct ContentView: View {
    @Environment(\.openWindow) var openWindow
    var body: some View {
        VStack {
            
            Button("open window") {
                openWindow(id: "status")
            }
        }
        .padding()
    }
}

You can use above, below, leading, or trailing, as well as utilityPanel.

Accepted Answer

Hi @beechbot ,

The code snippet you referenced is for macOS. For visionOS, try something like:

  var body: some Scene {
        WindowGroup("content", id: "content") {
            ContentView()
        }
        
        WindowGroup("Status", id: "status") {
            StatusView()
        }
        .windowResizability(.contentSize)
        .defaultWindowPlacement { content, context in
       
            return WindowPlacement(.above(context.windows.first(where: { $0.id == "content" })!))
        }

where ContentView looks like this:

struct ContentView: View {
    @Environment(\.openWindow) var openWindow
    var body: some View {
        VStack {
            
            Button("open window") {
                openWindow(id: "status")
            }
        }
        .padding()
    }
}

You can use above, below, leading, or trailing, as well as utilityPanel.

VisionOS : defaultWindowPlacement unavailable?
 
 
Q