TechnologiesSwiftUI

WindowPlacementContext struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A type which represents contextual information used for sizing and

How it works

WindowPlacementContext is the read-only environment that SwiftUI hands to a window-placement closure so you can decide where a window opens. It describes the world the window is about to appear in — the displays attached to the system and the windows already on screen — letting you compute a position or size relative to that current state rather than a fixed coordinate. Reach for it whenever a window's placement should depend on the user's setup, such as centering on the active display, offsetting from an existing window, or sizing to fit the available screen. You don't construct it yourself; you receive an instance and read its members.

  1. Receive the context from a WindowPlacement closure

    WindowPlacementContext arrives as the parameter to a placement-computing closure attached to a scene (for example through defaultWindowPlacement or a WindowPlacement initializer). Its job is to supply the facts you need to return a placement; everything you read off it is a snapshot of the current window environment. In the example the signature is shown literally as Position(context: WindowPlacementContext), naming the value you work with as context.

  2. Pick a screen with defaultDisplay

    The defaultDisplay property gives you the system's preferred display — the sensible default to target when you have no stronger preference. You typically anchor a window's frame against this display's bounds so it opens where the user is most likely looking. The example surfaces this member as context.defaultDisplay.

  3. Enumerate all screens with displays

    The displays collection lets you inspect every display currently attached, so your placement logic can adapt to multi-monitor setups — choosing the largest screen, the one under the cursor, or a specific arrangement. You iterate it to find the display you actually want before computing a frame. It appears in the example as context.displays.

  4. Account for existing windows with windows

    The windows collection reports the windows already on screen, which is what makes relative placement possible — cascading a new window off the last one, avoiding overlap, or aligning to a sibling. By reading these positions you can offset your result instead of stacking windows on top of each other. The example exposes this as context.windows.

  5. Return a placement built from the context

    Having read the displays and windows, you combine them into the WindowPlacement value the closure returns — a position and optional size expressed against the context you just inspected. Because the context is recomputed each time the window is placed, the same logic responds to whatever monitors and windows are present at that moment. The example frames this purpose with Position(context: WindowPlacementContext).

Try it — Change the screen you anchor to from context.defaultDisplay to a display chosen out of context.displays (for instance the last one) and the window will open on a different monitor.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

WindowPlacementContext.swift
struct WindowPlacementContextDemo: View {
    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            Text("WindowPlacement")
                .font(.headline)
            Text("Position(context: WindowPlacementContext)")
                .font(.caption.monospaced())
                .foregroundStyle(.secondary)
            Divider()
            Label("context.defaultDisplay", systemImage: "display")
            Label("context.displays", systemImage: "rectangle.on.rectangle")
            Label("context.windows", systemImage: "macwindow")
        }
        .font(.callout)
        .padding()
    }
}
Live preview
WindowPlacement Position(context: WindowPlacementContext) context.defaultDisplay context.displays context.windows
WindowPlacement Position(context: WindowPlacementContext) context.defaultDisplay context.displays context.windows
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →