I have have edited this question to try to make it more clear and added an Xcode project
To simultate the problem I am trying to solve which is just press a button and open a new window in macOS with a model from SwiftData.
Here is the SwiftDataWindowApp file where I am trying to pass an ItemModel to WindowGroup. I am hoping just to pass the whole model becuase in other SwiftData applications I have never needed an Id.
import SwiftUI
import SwiftData
@main
struct SwiftDataWindowApp: App {
var body: some Scene {
WindowGroup {
RootView()
.modelContainer(for: [ItemModel.self])
}
// I can't figure out how to pass an ItemModel to WindowGroup this does not work
WindowGroup(for: [ItemModel.self]) { $item in
ItemView(item:$item)
.modelContainer(for:[ItemModel.self])
}
// Example for WindowGroup documentation
// A window group that displays messages.
//WindowGroup(for: Message.ID.self) { $messageID in
//MessageDetail(messageID: messageID)
// }
}
}
In ItemsButtonView file is where I am calling openWindow
import SwiftUI
struct ItemsButtonView: View {
@Environment(\.openWindow) private var openWindow
let item: ItemModel
var body: some View {
NavigationStack {
VStack(alignment: .leading) {
Text("\(item.name )")
Text("\(item.desc )")
Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
HStack {
Text("Open Item In New Window")
.onTapGesture {
// Open Item in new window
// openWindow(value: item)
print("Open Item Button Presed")
}
}
}
}
}
}
My model
import Foundation
import SwiftData
@Model
final class ItemModel {
var timestamp: Date
var name: String
var desc: String
init(timestamp: Date, name: String = "", desc: String = "") {
self.timestamp = timestamp
self.name = name
self.desc = desc
}
}
ItemView file that I want opened in new window.
import SwiftUI
struct ItemView: View {
let item: ItemModel
var body: some View {
VStack{
Text("Name: \(item.name)")
Text("Description: \(item.desc)")
Text("Date: \(item.timestamp)")
}
.frame(width:600, height:300)
}
}