Loading...

How to open and close windows programmatically in SwiftUI?

question swiftui macos
Ram Patra Published on September 3, 2024

To open or close a window programmatically from outside that window using environment variables, you need to leverage the new openWindow (macOS 13+) and dismissWindow (macOS 14+) environment variables. This environment variables allow you to programmatically open and close a window by its identifier.

Here’s how you can implement this:

Step-by-Step Example

  1. Define and use a WindowGroup with an identifier.
  2. Use the openWindow environment variable to open the window from another view.
  3. Use the dismissWindow environment variable to close the window from another view.

Example Implementation

import SwiftUI

@main
struct WindowExampleApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        
        WindowGroup("New Window") {
            NewWindowView()
        }
        .handlesExternalEvents(matching: ["newWindow"])
    }
}

struct ContentView: View {
    @Environment(\.openWindow) private var openWindow
    @Environment(\.dismissWindow) private var dismissWindow
    
    var body: some View {
        VStack {
            Button("Open New Window") {
                openWindow(id: "newWindow")
            }
            .padding()
            
            Button("Close New Window") {
                dismissWindow(id: "newWindow")
            }
            .padding()
        }
    }
}

struct NewWindowView: View {
    var body: some View {
        Text("This is a new window!")
            .frame(minWidth: 200, minHeight: 200)
            .padding()
    }
}

Explanation:

  1. @Environment(\.openWindow) and @Environment(\.dismissWindow):
    • These environment variables provide the ability to open and close windows programmatically.
  2. WindowGroup("New Window"):
    • This WindowGroup defines a window identified by "newWindow". The .handlesExternalEvents(matching: ["newWindow"]) allows this window group to be managed via external events like opening or closing.
  3. ContentView:
    • This main view contains two buttons:
      • The first button uses openWindow(id: "newWindow") to open a new window identified by "newWindow".
      • The second button uses dismissWindow(id: "newWindow") to close the window identified by "newWindow".
  4. NewWindowView:
    • This view is the content of the new window.

Running the App:

  • Opening the Window: Clicking the “Open New Window” button will open the new window with NewWindowView content.
  • Closing the Window: Clicking the “Close New Window” button will close the newly opened window using the dismissWindow environment variable.

This approach is more straightforward and leverages SwiftUI’s environment variables to manage windows without needing to manually track window references. It’s also more aligned with the declarative nature of SwiftUI.

If you’re curious to learn more ways to achieve the same, you can read this blog post.

Presentify

Take your presentation to the next level.

FaceScreen

Put your face and name on your screen.

ToDoBar

Your to-dos on your menu bar.

Ram Patra Published on September 3, 2024
Image placeholder

Keep reading

If this article was helpful, others might be too

question swiftui swift September 7, 2024 How to apply mirroring to any SwiftUI view?

You can apply mirroring to a SwiftUI view by using the scaleEffect(x:y:anchor:) modifier to flip the view horizontally or vertically. Specifically, you can set the x or y scale to -1.0 to mirror the view along that axis.

question swiftui iOS April 2, 2024 How to open a second view from first view in iOS using SwiftUI?

In SwiftUI, you can open another view (or navigate to another view) on the click of a button by utilizing navigation views and navigation links. Here’s a basic example of how to achieve this:

question swiftui iOS April 2, 2024 How to force an app or a view to open in landscape only mode in iOS using SwiftUI?

In SwiftUI, you can force an app to open in landscape mode by configuring the supported interface orientations in your app’s target settings. Here’s a step-by-step guide to configuring interface orientations in Xcode: