Contents

DismissWindowAction

An action that dismisses a window associated to a particular scene.

Declaration

@MainActor @preconcurrency struct DismissWindowAction

Overview

Use the dismissWindow environment value to get the instance of this structure for a given Environment. Then call the instance to dismiss a window. You call the instance directly because it defines a callAsFunction(id:) method that Swift calls when you call the instance.

For example, you can define a button that closes an auxiliary window:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        #if os(macOS)
        Window("Auxiliary", id: "auxiliary") {
            AuxiliaryContentView()
        }
        #endif
    }
}

struct DismissWindowButton: View {
    @Environment(\.dismissWindow) private var dismissWindow

    var body: some View {
        Button("Close Auxiliary Window") {
            dismissWindow(id: "auxiliary")
        }
    }
}

If the window was opened with pushWindow, the original presenting will reappear when this action is performed.

Topics

Calling the action

See Also

Closing windows