UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Confused about Window and WindowGroup

Forums > macOS

I'm being a bit thick. I know I can create a Mac app and, in the top level scene, I can create a WindowGroup for my ContentView. That works fine. However, I can replace the WindowGroup with a plain Window and that works just as well. So why do I need two components?

I've read the description of WindowGroup several times but it doesnt help. It's technically accurate and tells me what it is, but not why its different from a Window or why I should use it in preferance to a Window.

What is it that WindowGroup gives me that Window doesn't?

Sorry if this is a bit basic. When I Google information on WindowGroup I tend to get the Apple docs regurgitated as though it is blindingly obvious what it means. That either means it's blindingly obvious or that other people can't define the difference either!

Thanks in advance. Steve

3      

However, I can replace the WindowGroup with a plain Window and that works just as well. So why do I need two components?

Can you open several windows of the app at the same time just using Window?

As far as I understand with WindowGroup you can open several windows of the same app. Logically, just using Window you can open just one window of the app...

“SwiftUI includes several structures to create standard Scenes for every system. The currently available are WindowGroup, Window, DocumentGroup, Settings, and MenuBarExtra. Some of these structures are designed to create Scenes for Mac computers, as we will see later, and others are more generic. For instance, the WindowGroup structure can manage multiple windows for iPhones, iPads and Macs, and therefore it is the one recommended for most projects.”

Excerpt From SwiftUI for Masterminds 3rd Edition 2022 J.D. Gauchat

3      

If I use Window, I get one main window for my app. From there I can open other windows, but I still have only one main window. I'll checkout SwiftUI for Masterminds to see if they go into more detail.

Thank you.

3      

Ah, I see, when I specify Window as the top level component, I will only ever get one top level window. If I use WindowGroup, a menu item will be generated for me (File -> New Window) that creates another instance of the top level window. I also have a window-specific close menu item.

I suspect it's doing more for me than that, but it makes sense that it's managing multiple top level windows.

Steve

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.