Post

Replies

Boosts

Views

Activity

Help with SwiftUI macOS focus (keyboard navigation)
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
0
0
802
Mar ’23
NavigationSplitView does not run .task for each detail view instance
NavigationSplitView does not run .task for each detail view instance. I'm trying to update my code from NavigationView to NavigationSplitView. I was having trouble, since I rely upon .task to update the @State of my detail view. It does not update this state in the detail view when using NavigationSplitView. Please see my comments in the code snippet below. Does anyone have any suggestions? FWIW I have also filed FB11932889. Thanks for any help. import SwiftUI enum Category: CaseIterable, Hashable {   case first   case second } struct CategoryRow: View {   let category: Category   var body: some View {     Text("Row: \(String(describing: category))")   } } struct CategoryDetail: View {   enum LoadingState {     case none     case loading     case loaded     case error   }   let category: Category   @State private var loadingState: LoadingState = .none   var body: some View {     VStack {       Text("Category: \(String(describing: category))")       Text("LoadingState: \(String(describing: loadingState))")     }     .task {       // If this .task does not run ever, the view will show a .none Category.       // If this .task runs, the view will start by showing .loading, and then .loaded       // The bug found when this is in CategoryNavigationSplitView that: this .task does not run for the .second category,       //  but the @State is not reset, it will show .loaded for the second category chosen!       //  I expect this .task to run each time, and not carry over the @State from       //  previous CategoryDetail. It must be a new CategoryDetail View, since the       //  let Category constant is changing.       // When this is in CategoryNavigationView, the .task runs as expected.       loadingState = .loading       do {         try await Task.sleep(for: .seconds(2))         loadingState = .loaded       } catch {         loadingState = .error       }     }   } } struct CategoryNavigationSplitView: View {   @State var selectedCategory: Category?   var body: some View {     NavigationSplitView {       List(Category.allCases, id: \.self, selection: $selectedCategory) { category in         NavigationLink(value: category) { CategoryRow(category: category) }       }     } detail: {       if let selectedCategory {         CategoryDetail(category: selectedCategory)       } else {         Text("Select a Category")       }     }   } } struct CategoryNavigationView: View {   @State var selectedCategory: Category?   var body: some View {     NavigationView {       List(Category.allCases, id: \.self, selection: $selectedCategory) { category in         NavigationLink {           CategoryDetail(category: category)         } label: {           CategoryRow(category: category)         }       }     }     Text("Select a Category")   } }
4
1
1.5k
Jan ’23
xcodebuild and swift package dependencies
Hello, I have an Xcode project (not a workspace), that depends upon a swift package found on GitHub. You can find the PR here that demonstrates the problem. https://github.com/bolsinga/MissingArt/pull/2 My project has a Swift Package dependency. It's a macOS target. If I Cmd-B in Xcode it will build and run just fine (finding the package and building it too). If I xcodebuild -verbose from the command line in CI, it will fail. The problem is that when the application files compile and import the module found in the Swift Package, the module is not found. If this were ObjC, I'd know that the search path was not found. I do not see anything about my module on the link line. I have followed the instructions about adding a Swift Package Mgr dependency, and it works locally. I'm sure that I'm missing one small thing; I'm not sure if xcodebuild with no options (thus getting the default behavior) is the proper thing to do. Any ideas? Thank you, -- Greg
4
0
4.1k
Dec ’22