Hello, I'm having trouble understanding Focus in SwiftUI on macOS for my application. I have distilled it down to a small reproducible case here.
struct ContentView: View {
struct Item : Identifiable, Equatable, Hashable {
let id : Int
let url : URL
@ViewBuilder var view : some View {
if id % 2 != 0 {
AsyncImage(url: url)
} else {
Image(systemName: "questionmark.diamond")
}
}
}
struct DetailView : View {
@Binding var item: Item?
var items : [Item] {
if let item {
return [item, item]
}
return []
}
var body: some View {
List(items) { item in
item.view
}
}
}
var url : URL {
return URL(string: "https://is1-ssl.mzstatic.com/image/thumb/Music/4c/aa/6f/mzi.cnwthgxu.jpg/900x900bb.jpg")!
}
var items : [Item] {
var a : [Item] = []
for i in 0..<10 {
a.append(Item(id: i, url: url))
}
return a
}
@State private var selectedItem : Item?
var body: some View {
NavigationSplitView {
List(items, selection:$selectedItem) { item in
NavigationLink(value: item) {
Text("\(item.id)")
}
}
} detail: {
DetailView(item: $selectedItem)
}
}
}
If you click on an even row, the behavior is as expected. The side List
highlights. Press the Tab key. Nothing highlights, but I assume focus is in the DetailView.
Press the Tab key a second time. It will now highlight the focus on the side bar show/hide button in the toolbar. Press the Tab key again, and it will go back to the side view list’s selection.
If you click on an odd row, the behavior is not as expected. The side List
highlights. Press the Tab key. Nothing highlights, but I still assume focus is in the DetailView.
Press the Tab key a second time. This time, instead of focusing the sidebar show/hide button, nothing highlights again. I still assume focus is in the DetailView,
but it is not clear what. Now press the Tab key again, and the highlight focuses on the sidebar show/hide button.
I’m confused why two Images (one from Image(systemName:)
and the other from an URL via AsyncImage)
have different Tab key behavior. Why does AsyncImage
seem to capture the two images, while the SF Symbols Images do not? Why does anything in the DetailView
capture focus at all?
Thanks for any tips!
FB12044843