TechnologiesSwiftUI

PushWindowAction struct

iOSmacOStvOSwatchOSvisionOS✓ renders

An action that opens the requested window in place of the window the action

How it works

PushWindowAction is a callable action value that opens a new instance of a window or window group scene that your app declares, without dismissing the window the request originates from. It addresses the case where a single window isn't enough — a document needs an auxiliary inspector, a tool palette, or a detached panel — by letting one window programmatically bring another onto screen. Reach for it whenever a button or other interaction in a multi-window app should spawn an additional window alongside the current one rather than replacing it. You don't construct PushWindowAction yourself; SwiftUI provides a ready-made instance through the environment.

  1. Read the action from the environment with @Environment(\.pushWindow)

    SwiftUI vends a configured PushWindowAction through the environment, so you obtain it by declaring an environment property keyed to the pushWindow key path rather than instantiating it. Here @Environment(\.pushWindow) private var pushWindow binds the action into a property named pushWindow that the view body can invoke.

  2. Call the action by its identifier

    PushWindowAction is callable: invoking the value with an id argument requests that SwiftUI open the scene declared with that identifier. In the demo, pushWindow(id: "inspector") asks SwiftUI to push the scene whose identifier is "inspector" onto screen as a new window.

  3. Trigger it from an interaction

    Because the action is just a value you call, you place the call inside whatever event handler should open the window — most commonly a Button's action closure. The Button("Open Inspector") here runs pushWindow(id: "inspector") when tapped, turning a user gesture into a new-window request.

  4. Match the id to a declared scene

    The identifier passed to PushWindowAction must correspond to a Window or WindowGroup that your app declares with the same id in its scene body; otherwise there is nothing for the action to open. The literal "inspector" used in pushWindow(id: "inspector") is the contract between this call site and the app-level scene declaration.

Try it — Change the identifier in pushWindow(id: "inspector") to one your app's scene body doesn't declare, and observe that the button no longer opens a window — confirming the action only resolves ids that match a declared scene.

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.

PushWindowAction.swift
struct PushWindowActionDemo: View {
    @Environment(\.pushWindow) private var pushWindow

    var body: some View {
        VStack(spacing: 16) {
            Text("Document Viewer")
                .font(.headline)
            Button("Open Inspector") {
                pushWindow(id: "inspector")
            }
            .buttonStyle(.borderedProminent)
        }
        .padding()
    }
}
Live preview
Document Viewer Open Inspector
Document Viewer Open Inspector
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →