Drag degradation when SwiftUI macOS [Toolbar -> ToolbarItem -> Button -> Action] contains @State variable reference

Viewed 152

2021/10/28 Update:
Fixed when building with:

  • macOS 12.0.1 (21A559)
  • Xcode 13.1 (13A1030d)

2021/10/19 Update:


Could anyone shed some light on why referencing a @State variable inside the following introduces drag degradation/choppiness, or a workaround?

I have tried the following:

  • Different variable types
  • Using the variable within the action and not using it
  • Running same code on iOS - no lag at all.
  • Xcode Running Xcode Version 13.0 (13A233)

Code

struct ContentView: View {
    
    @State private var iAmAnUnusedBool: Bool = true
    @State private var dragOffset = CGSize.zero
    @State private var width: CGFloat = 200
    
    var body: some View {
        HStack(alignment: .top, spacing: 0) {
            TextEditor(text: .constant("Left Panel"))
            
            Rectangle()
                .frame(width: 6)
                .onHover { $0 ? NSCursor.resizeLeftRight.push() : NSCursor.pop() }
                .gesture(
                    DragGesture(minimumDistance: 0, coordinateSpace: .named("contentView"))
                        .onChanged { gesture in
                            dragOffset = gesture.translation
                        }
                        .onEnded { gesture in
                            let dragOffsetOriginal = dragOffset.width
                            dragOffset = .zero
                            width = width - dragOffsetOriginal
                        }
                )
            TextEditor(text: .constant("Right panel"))
                .frame(width: width - dragOffset.width)
        }
        .toolbar {
            ToolbarItem {
                Button("Button") {
                    iAmAnUnusedBool // THIS INTRODUCES DEGRADATION
                }
            }
        }
        .coordinateSpace(name: "contentView")
    }
}

No Lag

Given iAmAnUnusedBool is commented out
When I drag the Rectangle() back and forth
Then there is no lag
Dragging not laggy

Lag

Given iAmAnUnusedBool is uncommented
When I drag the Rectangle() back and forth
Then there is lag (appears 1 minute after rapid dragging)
Dragging is laggy

0 Answers
Related