Created
January 29, 2024 14:41
-
-
Save daneden/063cfe56cd3444f2d6f4e0181e98b83c to your computer and use it in GitHub Desktop.
SwiftUI app showing how to prevent multiple `WindowGroup` windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContentView.swift | |
// WindowExample | |
// | |
// Created by Daniel Eden on 29/01/2024. | |
// | |
import SwiftUI | |
struct ContentView: View { | |
@Environment(\.openWindow) var openWindow | |
var body: some View { | |
Button("Open `WindowGroup` window") { | |
openWindow(id: "windowGroup", value: ScreenType.home) | |
} | |
Button("Open UUID window") { | |
openWindow(value: UUID()) | |
} | |
} | |
} | |
#Preview { | |
ContentView() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// WindowExampleApp.swift | |
// WindowExample | |
// | |
// Created by Daniel Eden on 29/01/2024. | |
// | |
import SwiftUI | |
enum ScreenType: Codable { | |
case home | |
} | |
@main | |
struct WindowExampleApp: App { | |
var body: some Scene { | |
WindowGroup { | |
ContentView() | |
} | |
WindowGroup("Home Screen", id: "windowGroup", for: ScreenType.self) { screenType in | |
Text("I'm part of a `WindowGroup`").foregroundStyle(.orange) | |
} defaultValue: { | |
.home | |
} | |
WindowGroup("uuid-example", for: UUID.self) { uuid in | |
Text("I'm a unique window with a value of \(uuid.wrappedValue?.uuidString ?? "nil")").foregroundStyle(.teal) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment