SwiftUI Scene with a single window on macOS

Is it possible to only allow a single window instance on macOS?

WindowGroup/DocumentGroup allow the user to create multiple instances of a window. I'd like to only allow one, for an Onboarding sequence.

I've checked the Scene documentation, and it appears the only types conforming to the Scene protocol are WindowGroup, DocumentGroup and Settings. How can I create a single Window in a SwiftUI App?

An example use case:
Code Block swift
struct TutorialScene: Scene {
  var body: some Scene {
// I don't want to allow multiple windows of this Scene!
WindowGroup {
TutorialView()
}
}


I did not try, so that's just a suggestion. Could you disable the menu command that allows to create more windows ?
Claude31, thanks for that idea. It does seem that I can use the following code to hide the New Window menu item:
Code Block swift
    .commands {
        CommandGroup(replacing: CommandGroupPlacement.newItem) {
      }
    }


Unfortunately this affects my other scenes/windows, one of which is using DocumentGroups, and needs that menu option. I've also noticed that I can't seem to customize commands per-scene/window. The last .commands block I supply is used regardless of what Scene I apply it to.
The best would be to have a WindowGroup property to set the max number of windows.

Maybe you could file an enhancement request for it.
In Xcode Beta 6 there's a new API added to Scene:

Code Block swift
@available(iOS 14.0, macOS 11.0, *)
public func handlesExternalEvents(matching conditions: Set<String>) -> some Scene

Based on the doc comments I don't think this will let me limit the window count, but I'm not sure. I'll report back after macOS Beta 6 is released and I play around with it.
You can replace the "New Window" command with an empty CommandGroup like so:

Code Block
CommandGroup(replacing: .newItem, addition: {
})

This works on macOS:

Window("SingleWindow", id: "main") {

I found your question while searching for how to do it on iPadOS.

SwiftUI Scene with a single window on macOS
 
 
Q