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:
Code Block
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?
Answered by AP1399 in 657867022
Thanks!
It is working indeed.
Whatever the root cause of the problem is, it is not just the @State variables. Originally I ran into this issue when I was using either @ObservedObject or @StateObject and the boolean controlling the button state would be @Published in the corresponding class. I wrote the example posted here just to simplify the case and focus on the problem.

What am I missing?

Maybe it is caused by some implementation details of the current SwiftUI in the similar way where .sheet cannot handle @State vars.

A simple workaround:
Code Block
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: {
MyCommandGroup(active: $active)
})
})
}
}
struct MyCommandGroup: View {
@Binding var active: Bool
var body: some View {
Button("Normally active", action: {active.toggle()}).disabled(active)
Button("Normally inactive", action: {active.toggle()}).disabled(!active)
}
}


Accepted Answer
Thanks!
It is working indeed.
Whatever the root cause of the problem is, it is not just the @State variables. Originally I ran into this issue when I was using either @ObservedObject or @StateObject and the boolean controlling the button state would be @Published in the corresponding class. I wrote the example posted here just to simplify the case and focus on the problem.

I ran into this issue when I was using either @ObservedObject or @StateObject and the boolean controlling the button state would be @Published in the corresponding class.

I guess such things may cause issue when used with .sheet as well.
How to activate / deactivate menu items in SwiftUI
 
 
Q