Post

Replies

Boosts

Views

Activity

How to activate / deactivate menu items in SwiftUI
I am trying to create custom menu items in SwiftUI app for macOS (SwiftUI App life cycle). I don't quite understand the following behaviour: In the trivial code below: import SwiftUI @main struct MenuTestApp: App {     @State var active = false     var body: some Scene {         WindowGroup {             ContentView()         }         .commands(content: {             CommandMenu("Tools", content: {                 Button("Normally active", action: {active = !active}).disabled(active)                 Button("Normally inactive", action: {active = !active}).disabled(!active)             })             CommandGroup(after: .newItem, addition: {                 Button("Normally active", action: {active = !active}).disabled(active)                 Button("Normally inactive", action: {active = !active}).disabled(!active)             })         })     } } the buttons added via CommandMenu behave as expected (i.e. activate and deactivate according to the changing value of 'active'. The buttons added via CommandGroup get correctly configured according to the value of 'active' at launch, but ignore the changes of 'active' and don't change their state. What am I missing?
3
0
3.2k
Jan ’21