How it works
Window is a Scene you declare in your App to present a single, unique window for a particular purpose — an inspector, a tool palette, a game board, or any surface that should never have duplicates. Unlike WindowGroup, which lets people open many interchangeable copies of the same interface, a Window is created at most once; opening it again brings the existing instance forward rather than spawning a new one. Reach for Window when a piece of your app's UI is inherently singular and you want the system to manage its uniqueness, ordering, and lifetime for you.
Declare the scene in your App's body
A
Windowis aScene, so it lives in thebodyof a type conforming toAppalongside your other scenes. You construct it with a title and a unique identifier and hand it a view builder for its content, as inWindow("Inspector", id: "inspector") { ContentView() }. The system guarantees only one instance of that scene exists at a time.Give it a stable identity with the id parameter
The
idargument ("inspector"in the example) is the string you use elsewhere to refer to this exact window. Because the identity is fixed, it is what enforces the single-instance behavior and what other parts of your app target when they need to show or hide the window.Provide the content view
The trailing view builder supplies what the window hosts — here that role is filled by
ContentView(). The view you return (theVStackwith themacwindowImageand the"Inspector"Textin the runnable sample) is ordinary SwiftUI: theWindowscene simply gives it a place to live and a frame to fill.Open and dismiss it programmatically
Because the window has a known
id, you drive its visibility from anywhere using theopenWindowanddismissWindowenvironment actions, passing the same identifier — for exampleopenWindow(id: "inspector"). CallingopenWindowwhen the window is already open activates the existing instance instead of creating a second one.Attach scene modifiers to shape its behavior
As a
Scene, aWindowaccepts scene-level modifiers such asdefaultPosition(_:),defaultSize(_:),keyboardShortcut(_:), andcommands(content:), letting you set where it first appears, how large it opens, and which menu items toggle it. These refine the single window without changing the content view it hosts.
ContentView whose action calls openWindow(id: "inspector") twice in a row, then confirm only one Inspector window appears — the second call reactivates the existing instance instead of opening a duplicate.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 WindowDemo: View {
var body: some View {
// `Window` is a single-instance Scene declared in an App:
// Window("Inspector", id: "inspector") { ContentView() }
// This view is the content such a Window would host.
VStack(spacing: 12) {
Image(systemName: "macwindow")
.font(.system(size: 44))
.foregroundStyle(.tint)
Text("Inspector")
.font(.headline)
Text("Hosted in a single-instance Window scene")
.font(.caption)
.foregroundStyle(.secondary)
.multilineTextAlignment(.center)
}
.padding()
}
}