Skip to content

Window Management (SwiftUI)

A SwiftUI guide for coding agents. Also covers openwindow environment action, menubarextra macos menu bar app, window scene macos, windowgroup for value type, supportsmultiplewindows check.

The macOS 13 and iOS 16 scene APIs make windows declarative: a Window scene for singletons, value-typed WindowGroups for per-item windows, openWindow to open them, and MenuBarExtra for menu bar apps.

Check Support, Then Open by Id

\.supportsMultipleWindows tells you whether the platform can open extra windows. The Window scene declares one unique window with an id, and \.openWindow opens it.

@main struct MyApp: App {
  var body: some Scene {
    #if os(macOS)
    Window("Statistics", id: "stats") {
      StatisticsView()
    }
    #endif

    WindowGroup {
      ContentView()
    }
  }
}

struct ContentView: View {
  @Environment(\.openWindow) private var openWindow

  var body: some View {
    Button("Open statistics") {
      openWindow(id: "stats")
    }
  }
}

Value-Typed Window Groups

WindowGroup(for:) registers a group keyed by a value type. Opening a window with a value routes it to that group, one window per item.

WindowGroup(for: Item.ID.self) { $itemId in
  ItemView(itemId: itemId ?? UUID())
}

WindowGroup(id: "editor") {
  EditorView()
}

// call sites
openWindow(id: "item", value: item.id)
openWindow(id: "editor")

The binding's value can be nil (a window restored without its value), so provide a fallback.

Styling

windowStyle on the scene toggles titleBar versus hiddenTitleBar. presentedWindowStyle on a view hierarchy styles the windows that hierarchy opens.

MenuBarExtra is a scene that lives in the macOS menu bar. The isInserted binding shows or hides the icon, and menuBarExtraStyle(.window) renders the content in a floating window instead of a pull-down menu.

@main struct MyApp: App {
  @State private var menuBarExtraShown = true

  var body: some Scene {
    #if os(macOS)
    MenuBarExtra(isInserted: $menuBarExtraShown) {
      MenuBarView()
    } label: {
      Label("MyApp", systemImage: "star")
    }
    .menuBarExtraStyle(.window)
    #endif

    WindowGroup {
      ContentView()
    }
  }
}

Use this guidance in your coding agent

Install the Better Design MCP once. Your agent then loads this page with one call.

get-swiftui-guide({ topic: "swiftui-window-management" })
claude mcp add --scope user better-design --transport http https://better-design.com/api/mcp --header "Authorization: Bearer <YOUR_API_KEY>"
Browse related design systems