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.
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 pushWindowbinds the action into a property namedpushWindowthat the view body can invoke.Call the action by its identifier
PushWindowAction is callable: invoking the value with an
idargument 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.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 runspushWindow(id: "inspector")when tapped, turning a user gesture into a new-window request.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.
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.
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()
}
}