I want to make a data-driven WindowGroup so that my app can launch additional supporting windows. The problem is that on the iPad, the WindowGroup doesn't seem to know that a specific window for that ID is already open, so it opens another window for it. On a Mac, it works as expected and brings to the front the window with the same id.
Here is the sample code
@main
struct MultiWindowSimpleApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
// the additional window
WindowGroup(id: "AnItem", for: Item.ID.self) { $anID in
if let anID, let item = Items.main.item(withID: anID ) {
Text( "\(item.id)")
}
else{
Text( "item not found!")
}
}
}
}
//: ======================================================= ://
struct ContentView: View {
let content = Items()
var body: some View {
VStack {
ForEach(content.items) { anItem in
ItemView(item: anItem )
}
}
.padding()
}
}
// Supporting content
//: ======================================================= ://
/// the Item base class
struct Item:Identifiable, Hashable, Codable{
var id:UUID = UUID()
}
//: ======================================================= ://
/// creates and holds multiple items
struct Items{
static var main:Items!
var items:[Item]
init() {
var items:[Item] = []
for _ in 0..<5{
items.append(Item())
}
self.items = items
Self.main = self
}
func item( withID id:UUID )->Item?{
return items.first(where: { $0.id == id })
}
}
//: ===================================================================================== ://
/// displays the item showing its id, and when tapped it opens the item in new window
struct ItemView:View{
@Environment(\.openWindow) private var openWindow
@Environment(\.supportsMultipleWindows) private var supportsMultipleWindows
let item:Item
var body: some View{
Text( "\(item.id)" )
.font(.system(size: 13))
.frame( maxWidth: .infinity )
.padding()
.background(.yellow)
.onTapGesture {
if supportsMultipleWindows{
openWindow(value: item.id )
}
}
}
}
Here is a screen shot:
This seems to have been resolved in ios17.