Skip to content

SwiftUI scenes, windows, navigation, and commands

A scene is a system-managed presentation of part of the app. One process can own several windows, each with different navigation, selection, size, and lifecycle state.

Separate shared data from window state

@main
struct FieldNotesApp: App {
@State private var library = NoteLibraryModel()
var body: some Scene {
WindowGroup {
NotesScene(library: library)
}
WindowGroup("Note", for: NoteID.self) { $noteID in
NoteWindow(library: library, noteID: noteID)
}
}
}

The library is shared application data. Each NotesScene owns its own selection, route path, search query, and draft presentation. Putting all navigation in one global object makes two windows fight over one path.

Open a focused window by value

@Environment(\.openWindow) private var openWindow
Button("Open in New Window") {
openWindow(value: note.id)
}

The system decides window placement and restoration. The destination still handles a missing or deleted note.

Adapt navigation from available presentation

NavigationSplitView can show sidebar, content, and detail where space permits, then collapse for compact presentation. Keep the selected note and route intent independent from the number of visible columns.

compact: list -> detail
regular: sidebar | list | detail

Do not branch on device model. iPad windows resize, external displays differ, and platform idioms evolve.

Put commands on application actions

.commands {
CommandGroup(after: .newItem) {
Button("New Note") { library.beginNewNote() }
.keyboardShortcut("n", modifiers: .command)
}
}

Menu and keyboard actions call the same application command as visible controls. Disabled state and focused-window context need to match the current scene.

Respond to lifecycle without assuming termination

Scene phase can prompt bounded work such as committing an already-valid draft or refreshing stale data. It is not a promise of background execution. Save durable work before suspension and make repeated callbacks idempotent.

Validation boundary

Multiwindow APIs, commands, split-view collapse, scene restoration, lifecycle transitions, and platform-specific behavior were not compiled or run. They remain Not verified until tested with supported Xcode destinations.

Series navigation

References