sindresorhus/KeyboardShortcuts

@State is not updated when used in App

DorianRudolph opened this issue · 1 comments

Perhaps I'm using this wrong, but why does the following code not work?

import SwiftUI
import KeyboardShortcuts

@main
struct TestAppApp: App {
    @State var isUnicornMode: Bool = false;
    init() {
        KeyboardShortcuts.onKeyDown(for: .toggleUnicornMode) { [self] in
            isUnicornMode.toggle();
            print("Unicorn \(isUnicornMode)")
        }
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

The print statement is always false.
When I use the @MainActor AppState: ObservableObject it works. I'm new to swift programming, but why does the variable in my example not change? Setting = true directly also does not work.

I recommend asking on Stack Overflow. Your question is not directly about this package.