TT--
TT--

Reputation: 3205

How to open and display different files in WindowGroup windows (tabs) using fileImporter from CommandGroup on the WindowGroup?

In code below the @State var modelData initializes with empty structures, and in that case the ContentView shows nothing. (This is fine.)

The modelData can also initialize from a file obtained from fileImporter, in which case it processes that file and populates structures and ContentView displays this data.

When using menu File > Open File, the modelData is updated and the current window displays the new data. (This is desired.)

Problem

When using File > New Window, the new window displays the same content as the existing window. I see that this is expected since they share the same modelData.

I could init new modelData when it is passed to the environment of ContentView,

.environment(ModelData())

but then the @State var modelData has been disconnected from that ContentView and it can't be updated.

I also tried putting the @State private var modelData = ModelData() inside the WindowGroup but then it is out of scope for the .commands { }.

I also tried moving the @State private var modelData into the ContentView, but similarly, it is out of scope for the .commands { }.

I have tried some ideas from this answer: How to implement multi window with menu commands in SwiftUI for macOS?

and have referred to this documentation: Bring multiple windows to your SwiftUI app

I'm trying to do this on macOS.

import SwiftUI
import UniformTypeIdentifiers

@main
struct DataVisualizerApp: App {

    @State private var modelData = ModelData()
    @State private var showFileImporter = false
    @State private var openedFilePath = ""

    @AppStorage(\.fontSize) var fontSize

    var body: some Scene {
        WindowGroup {
            ContentView()
                .environment(modelData)
                .environment(\.font, Font.system(size: fontSize))
                .navigationTitle("""
                    Data Visualizer \
                    \(openedFilePath.isEmpty ? "" : " - " + openedFilePath)
                    """)
        }
        .commands {
            CommandGroup(before: CommandGroupPlacement.newItem) {
                Button("Open File") {
                    showFileImporter = true
                }
                .fileImporter(isPresented: $showFileImporter,
                              allowedContentTypes: [.data])
                { result in
                    switch result {
                    case .success(let file):
                        openedFilePath = file.path
                        modelData = ModelData(securityScopedURL: file)
                        break

                    case .failure(let error):
                        print(error)
                    }
                }
            }
        }

    }

}

Upvotes: 0

Views: 30

Answers (0)

Related Questions