Skip to content

Instantly share code, notes, and snippets.

@daneden
Created January 29, 2024 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daneden/063cfe56cd3444f2d6f4e0181e98b83c to your computer and use it in GitHub Desktop.
Save daneden/063cfe56cd3444f2d6f4e0181e98b83c to your computer and use it in GitHub Desktop.
SwiftUI app showing how to prevent multiple `WindowGroup` windows
//
// 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()
}
//
// 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