How to use default placement to position volumes

We can use the defaultWindowPlacement scene modifier with Volumes, but there are a few things to keep in mind.

We already covered the basics of default placement when working with windows.

We can use these concepts with Volumes too. We start by defining our scenes in the app file. We set up a Volume and add the defaultWindowPlacement scene modifier. In this example, we ask the system to open this volume in the leading position relative to the main window.

// Initial scene as a Volume
WindowGroup {
    VolumeExample()
        .environment(appModel)
}
.windowStyle(.volumetric)


WindowGroup(id: "VolumeLeading") {
    Box()
}
.windowStyle(.volumetric)
.defaultWindowPlacement { _, context in
    if let mainWindow = context.windows.first {
        return WindowPlacement(.leading(mainWindow))
    }
    return WindowPlacement(.none)
}

//... and so on for the other placements

Caveats

  • The exact placement of the volume depends on the size and shape of the base scene. If we are starting from a window, then the bottom of the new volume will be aligned toward the center of the window. If we are starting from a volume, then the two volumes will be aligned on the bottom. (see the video demo)
  • Volumes often end up overlapping the scene they are opened near. The only workaround I know of is to scale down the content of the volume to leave a buffer.
  • When using the above and below placements with windows, the windows may be tilted slightly to face the user. This doesn’t happen with volumes.

With these caveats in mind, it seems to me that defaultWindowPlacement is mainly intended for Windows. It can still be useful to place a volume next to a window, but you should not expect a perfect placement.

Video Demo

visionOS simulator video showing a main window opening several volumes. The new volumes are positioned relative to the main window.

Sample code for this post is available in Garden09 in Step Into Examples on GitHub

Support our work so we can continue to bring you new examples and articles.

Questions or feedback?