Post

Replies

Boosts

Views

Activity

Select All in a NavigationListView on macOS?
There are a couple of questions in here. I want to do a basic master-detail split view, as so many SwiftUI apps do. But I want to let the user select an arbitrary subset of items in the list, select them all, and delete them (or do other operations). Select All, by default, is excruciatingly slow. I have a List view with a selection binding to a Set<PersistentIdentifier>. A ForEach inside renders the items from a SwiftData @Query. I have a couple hundred items, each with a String and Date property. The list item renders the String and nothing else. I click on an item, hit Command-A, and the app locks up for several seconds while it messes with the selection. It never highlights all the items, but sometimes highlights one (usually different from the one I clicked on).I have an .onChange(of: self.selection) in there to debug the selection. It is called many, many times (2-3 times per second, very slowly), and print the selection count, which starts at 135, and goes down by one, to about 103. If I scroll the list, the selection onChange gets called a bunch more times. Sometimes you see the selection highlights change. My SwiftUI view hierarchy looks like this: NavigationSplitView OrdersList struct OrdersList : View { var body: some View { List(selection: self.$selection) { ForEach(self.orders) { order in NavigationLink { Text("Order ID: \(order.vendorOrderID ?? "<none>")") } label: { Text("Order ID: \(order.vendorOrderID ?? "<none>")") } } .onDelete(perform: self.deleteOrders) .onChange(of: self.selection) { print("Selection changed: \(self.selection.count)") } } } func deleteOrders(offsets: IndexSet) { withAnimation { for index in offsets { self.modelContext.delete(self.orders[index]) } } } @State var selection = Set<PersistentIdentifier>() @Query var orders : [Order] @Environment(\.modelContext) var modelContext } If I use the mouse to select a single item, it behaves as expected. The item is selected, the onChange gets called, and if I choose Edit->Delete, the onDelete handler is called. If I use the mouse to shift-select multiple items, the behavior is similar to Select All above, but the selection Set count starts at the number of items I selected, and then is slowly whittled down to 0 or 1, with some random item being selected. I’d like for the Edit->Delete command to work with a keystroke (delete). How can I set the shortcut on an existing menu item without having to reimplement its behavior?
Topic: UI Frameworks SubTopic: SwiftUI
0
0
351
Sep ’24
Suddenly getting "Trailing closure passed to parameter of type 'Visibility' that does not accept a closure" for toolbars
Not sure exactly what’s going on here. I’ve been building out this little app for the past couple of weeks using Xcode 16 betas, and in a couple of places I have code like this: EditItemView(item: inItem) .toolbar { ToolbarItem { Button(action: { self.printLabel(item: inItem) }) { Label("Print Label", systemImage: "printer.filled.and.paper") } } } Now all of a sudden I'm getting an error on .toolbar: “Trailing closure passed to parameter of type 'Visibility' that does not accept a closure.” Looking at the comment for toolbar, it says this method "specifies the visibility," which is a weird thing for it to do with a name like that. But it also has a deprecation tag that I don't quite understand: @available(macOS, introduced: 13.0, deprecated: 100000.0, renamed: "toolbarVisibility(_:for:)"). The name change makes sense, given the description. But what is the huge deprecation version number? Just a hack? And why is this code no longer compiling? As I'm commenting out instances of this, all my toolbars are failing in this way. ETA: yeah, I finally found the right declaration of .toolbar: func toolbar<Content>(@ViewBuilder content: () -> Content) -> some View where Content : View. It's no longer choosing that one. I hate Swift sometimes. ETA2: I finally whittled it down to it not liking Label for some reason. Replacing that with Image works. But of course, the compiler won't tell me why. ETA3: It's worse than I thought: I tried to make a small test case for a bug, and Label works just fine. Why does my code not?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
0
1
722
Oct ’24
API to use for high-level audio playback to a specific audio device?
I'm working on a little light and sound controller in Swift, driving DMX lights and audio. For the audio portion, I need to play a bunch of looping sounds (long-duration MP3s), and occasionally play sound effects (short-duration sounds, varying formats). I want all of this mixed into selected channels on specific devices. That is, I might have one audio stream going to the left channel, and a completely different one going to the right channel. What's the right API to do this from Swift? Core Audio? AVPlayer stuff?
0
0
410
Oct ’24
Video playback performance suffers unless I wiggle the mouse
For years (since I got my M1 MacBook Pro) I've experienced this weird performance degradation. I've tried posting about it to numerous channels, always without a reply. I figured I'd try here. The symptom is: video playback (of visual frames, not audio; audio is unaffected) stutters if I just let the player play. I'll start a video playing, it will play normally, but if I leave the computer alone to play back, the frame rate will drop dramatically (one or two frames per second, even slower). If I wiggle the mouse (generating events), playback resumes normally. After a few seconds, it will stutter again. It's as if the computer were throttling back on system clocks or something because there’s no user input. As you can imagine, this is a frustrating user experience when trying to watch videos. It doesn't matter if it's a YouTube video playing in Safari, or a WWDC video playing in the WWDC.app or Developer.app, or any other video in QuickTime Player.app. And it doesn't always happen. I have my own video playback app that doesn't seem to suffer from this, even though it plays multiple videos simultaneously. I'm on power adapter, and settings are generally set to maximum performance when on power adapter. Any ideas?
0
0
383
Oct ’24
CloudKit sharing limits?
When you share records, they get put into a new zone. Creating a zone for the share makes sense to me, but I thought I read that there was a limit to the number of zones one could have (something like 1024). Does this mean a user can’t share more than 1024 separate items with 1024 different people? I assume any other items shared with the same group end up in an existing zone.
0
0
391
Oct ’24
Suppressing contextual menu on AVPlayerView in SwiftUI app
My app plays videos. At first I used SwiftUI’s VideoPlayer, but that didn't give me enough control over the underlying AVPlayerView, so I created my own NSViewRepresentable. That works well, and I can adjust the AVPlayerView as I see fit. But it seems to have a contextual menu that still appears instead of the one I try to apply using SwiftUI. If I put a non-opaque color over the AVPlayerView in a ZStack, I'm able to then add a .contextMenu that works, but only if the color is non-opaque (e.g. Color(red: 0, green: 0, blue: 0, opacity: 0.00001)). This feels like a pretty hacky solution, and doesn’t work if I set opacity: to 0. Is there a better way to keep AVPlayerView from handling events? It also gets scroll events (which scrubs through the movie) that I'd like to suppress.
Topic: UI Frameworks SubTopic: SwiftUI
0
0
272
Dec ’24