Post

Replies

Boosts

Views

Activity

SwiftUI toolbar action with StateObject causes frame rate to drop
I have a fairly straight forward swift UI view, that allows you to drag a shape around. I've noticed that whenever I have a toolbar action that has an action that captures my observed object grid, the frame rate drops significantly. Here's the code below, with the state object currently in the action (grid.snapEnabled.toggle()) func makeItem() -> SGGridItem { SGGridItem(id: UUID(), rect: CGRect(origin: .init(x: 250, y: 250), size: .init(width: 50, height: 50))) } struct ContentView: View { @StateObject var grid = SGGridState(items: [ makeItem(), makeItem(), makeItem(), makeItem(), makeItem(), makeItem() ]) var body: some View { SGGridCanvas(grid:grid) { SGGridView(grid: grid) }.frame(maxWidth:.infinity, maxHeight: .infinity) .toolbar(content: { ToolbarItem(placement: .principal) { Button(action: { grid.snapEnabled.toggle() }) { Image(systemName: "square.grid.3x3.topleft.fill").opacity(grid.snapEnabled ? 1 : 0.5) } } }) .onAppear { grid.globalGridItemPadding = 5 grid.globalGridItemCornerRadius = 5 } } } In this GIF you can see a noticeable lag as the shape being dragged follows behind the mouse cursor. However, if I now change the toolbar action to be an empty closure like so ToolbarItem(placement: .principal) { Button(action: { }) { Image(systemName: "square.grid.3x3.topleft.fill").opacity(grid.snapEnabled ? 1 : 0.5) } } Then as you can see, the view now performs as expected (the shape follows the mouse exactly). Is there something special I'm meant to be doing here when capturing a state object in a closure? Is this a performance issue inherent in swift UI, or do I need to be changing my structure somehow? Edit: This can be reproduced even with simple state variables.
1
0
867
Jun ’21
DragGesture translation reporting incorrect values
I'm building a SwiftUI macOS app. I've got a basic Rectangle shape with a drag gesture on it. In the onEnded handler, I am wanting to determine if the user has effectively tapped on the object. I do this by checking that the width and height of the translation are both zero. (There are reasons I'm not using a tap gesture). Rectangle() .size(.init(width:50, height: 50)) .fill(Color.blue.opacity(0.01)) .gesture(DragGesture(minimumDistance:0) .onChanged { gesture in // Ommited } .onEnded { gesture in print("startLocation", gesture.startLocation) print("start", gesture.location) print("translation", gesture.translation) if gesture.translation == .zero { print("tap") } print() } ) I'm getting issues where translations are being reported with unexpected values. The values reported differ based on where I click in the rectangle. Here's a set of groups of individual clicks. The translation is derived from the startLocation and location fields. You can see variation between the startLocation and the location fields. If it was a very small variation I could debounce, however the fact that sometimes I get a value of 3 makes me wonder why such a variation could happen (I'm being over the top careful to execute the click without movement). Does anyone know why this variation is creeping in? startLocation (263.5149841308594, 144.3092803955078) start (263.51495361328125, 144.30926513671875) translation (-3.0517578125e-05, -1.52587890625e-05) startLocation (276.2882995605469, 144.43479919433594) start (276.288330078125, 144.434814453125) translation (3.0517578125e-05, 1.52587890625e-05) startLocation (274.3827209472656, 162.3402557373047) start (274.38275146484375, 162.34027099609375) translation (3.0517578125e-05, 1.52587890625e-05) startLocation (264.81805419921875, 167.47662353515625) start (264.81805419921875, 167.47662353515625) translation (0.0, 0.0) tap startLocation (254.5931396484375, 135.4690399169922) start (254.5931396484375, 135.46905517578125) translation (0.0, 1.52587890625e-05) startLocation (259.1647033691406, 140.26919555664062) start (259.16473388671875, 140.26919555664062) translation (3.0517578125e-05, 0.0)
2
0
741
Jun ’21
Why is sandbox not blocking this code?
I have a brand new AppKit App Delegate based application to showcase the issue I'm having. I've set "User Selected File" in the sandbox entitlements to none. I've changed the app delegate file to the following. import Cocoa import SwiftUI @main class AppDelegate: NSObject, NSApplicationDelegate { var window: NSWindow! func applicationDidFinishLaunching(_ aNotification: Notification) { let url = URL(fileURLWithPath: "/Library/LaunchDaemons") if let enumerator = FileManager.default.enumerator(at: url, includingPropertiesForKeys: [.isRegularFileKey, .isDirectoryKey], options: [.skipsHiddenFiles, .skipsPackageDescendants]) { for case let fileURL as URL in enumerator { print(fileURL); do { let contents = try String(contentsOf: fileURL, encoding: .utf8) print(contents) } catch { } } } } func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } } This code gets all files in the /Library/LaunchDaemons path, prints their url and also prints their content. At this point I'm confused. I was under the impression the sandbox is supposed to be blocking this code? Instead, the fileUrl and the contents are happily printed out. Is my understanding of sandbox incorrect? Why is this code able to run?
4
0
988
Feb ’21