SwiftUI Mac: Environment Object in secondary Window

Viewed 110

I have a Mac app developer in Xcode 12.4

I have found out how to setup and open a secondary Window (is there a better way?) with NSApp.sendAction(#selector(AppDelegate.openSecondaryWindow), to: nil, from: nil) and the code that follows.

The problem is that I want to access in SecondaryView the same environment object (global state) that ContentView has access to. How to do this?


class AppDelegate: NSObject, NSApplicationDelegate {
    var secondaryWindow: NSWindow!

    func applicationDidFinishLaunching(_ notification: Notification) {
       [...]
    }
    
    func applicationWillFinishLaunching(_ notification: Notification) {
        NSWindow.allowsAutomaticWindowTabbing = false
    }
    
    func applicationShouldTerminateAfterLastWindowClosed(_ app: NSApplication) -> Bool {
        return true
    }

    func applicationWillTerminate(_ notification: Notification) {
        [...]
    }
    
    @objc func openSecondaryWindow () {
        if self.secondaryWindow == nil {
            let secondaryView = SecondaryView()
            
            self.secondaryWindow = NSWindow(
                contentRect: NSRect(x: 20, y: 20, width: 480, height: 300),
                styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
                backing: .buffered,
                defer: false)
            self.spreadWindow.contentView = NSHostingView(rootView: secondaryView)
        }
        
        self.spreadWindow.makeKeyAndOrderFront(nil)
    }
}

@main
struct MyApp: App {
    @StateObject var globalState: GlobalState = GlobalState.shared
    
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
        
    var body: some Scene {
        WindowGroup {
            ContentView()
                .environmentObject(self.globalState)
        }
    }
}
1 Answers

Apply it directly to SecondaryView, like

    @objc func openSecondaryWindow () {
        if self.secondaryWindow == nil {
            let secondaryView = SecondaryView()
                .environmentObject(GlobalState.shared)      // << here !!

Related