Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tune performance with ButtonBehavior in SwiftUI #779

Open
onmyway133 opened this issue Feb 24, 2021 · 0 comments
Open

How to tune performance with ButtonBehavior in SwiftUI #779

onmyway133 opened this issue Feb 24, 2021 · 0 comments

Comments

@onmyway133
Copy link
Owner

onmyway133 commented Feb 24, 2021

With Xcode 12.4, macOS 11.0 app. Every time we switch the system dark and light mode, the CPU goes up to 100%. Instruments show that there's an increasing number of ButtonBehavior

Screenshot 2021-02-24 at 10 12 05

Suspect State in a row in LazyVStack

Every cell has its own toggle state

struct Cell: View {
    enum ToggleState {
        case general
        case request
        case response
    }

    let item: Item
    @State
    private var toggleState: ToggleState = .general

    func toggleButton(text: String, state: ToggleState) -> some View {
        Button(action: { self.toggleState = state }) {
            Text(text)
                .foregroundColor(state == toggleState ? Color.label : Color.secondary)
                .fontWeight(state == toggleState ? .bold : .regular)
        }
        .buttonStyle(BorderlessButtonStyle())
    }
}

Removing the buttons fix the problem. The workaround is to use Text with onTapGesture

Text(text)
    .foregroundColor(state == toggleState ? Color.label : Color.secondary)
    .fontWeight(state == toggleState ? .bold : .regular)
    .onTapGesture {
        self.toggleState = state
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant