OpenWindowAction
An action that presents a window.
Declaration
type OpenWindowAction = { (options: { id: string; }): Promise<void>; (options: { id: string; value: unknown; }): Promise<void>; (options: { value: unknown; }): Promise<void>; };@MainActor @preconcurrency struct OpenWindowActionOverview
Use the EnvironmentValues.openWindow environment value to get the
instance of this structure for a given Environment. Then call the
instance to open a window. You call the instance directly because it
defines a OpenWindowAction.callAsFunction(id:) method that Swift calls
when you call the instance.
For example, you can define a button that opens a new mail viewer window:
@main
struct Mail: App {
var body: some Scene {
WindowGroup(id: "mail-viewer") {
MailViewer()
}
}
}
struct NewViewerButton: View {
@Environment(\.openWindow) private var openWindow
var body: some View {
Button("Open new mail viewer") {
openWindow(id: "mail-viewer")
}
}
}You indicate which scene to open by providing one of the following:
- A string identifier that you pass through the
idparameter, as in the above example. - A
valueparameter that has a type that matches the type that you specify in the scene's initializer. - Both an identifier and a value. This enables you to define multiple window groups that take input values of the same type, like a doc://com.apple.documentation/documentation/Foundation/UUID.
Use the first option to target either a WindowGroup or a
Window scene in your app that has a matching identifier. For a
WindowGroup, the system creates a new window for the group. If
the window group presents data, the system provides the default value
or nil to the window's root view. If the targeted scene is a
Window, the system orders it to the front.
Use the other two options to target a WindowGroup and provide
a value to present. If the interface already has a window from
the group that's presenting the specified value, the system brings the
window to the front. Otherwise, the system creates a new window and
passes a binding to the specified value.
From Apple’s SwiftUI documentation. Code samples there are Swift.
See also
More in Other values.
- AxisThe horizontal or vertical dimension in a 2D coordinate system.
- AxisSetAn efficient set of axes.
- ButtonRoleA value that describes the purpose of a button.
- CapsuleA capsule shape aligned inside the frame of the view containing it.
- CircleA circle centered on the frame of the view containing it.
- ConcentricRectangleA shape whose corners you configure, individually or uniformly, to be squared, rounded, or concentric relative to a container shape's corners.
- ContentMarginGuideA guide for resolving the content margins for a view.
- ContentMarginPlacementThe placement of margins.
- ContentModeConstants that define how a view's content fills the available space.
- DatePickerComponents
- DatePickerComponents2The date and time components that a date picker shows.
- DeviceHingeA type encapsulating the state of a single hinge.