Refresh @CommandsBuilder to update menu bar items

I'm trying to update the menu items in the main menu for a SwiftUI macOS app (not Catalyst). I haven't been able to find any way to do it.

The menu items are always as they are when first loaded. :(

Is there a way to "validate" commands in SwiftUI?

Examples for why you change menu items: "show" or "hide" title change, disabled item, or toggle state.

Code Block
CommandGroup(replacing: CommandGroupPlacement.toolbar) {
Button(action: {
NotificationCenter.default.post(name: .SortByName, object: nil) }) { Text(NSLocalizedString("Sort by \(sortType.rawValue)", comment: "key command for sorting all dates by name"))}
.keyboardShortcut("1", modifiers: .command)
.disabled(sortType == .name)

The ".disabled" modifier needs to update in this example.
I'm seeing a similar problem. I'm trying to change the CommandGroup Buttons based to some state in an ObservableObject, and it's not happening. Did you ever figure this out? As you said, uses include:
  1. Showing a checkmark for boolean states, like "✓ Foo enabled"

  2. Items that show or hide parts of view. Eg, "Show Sidebar" ↔︎ "Hide Sidebar"

  3. Enabling or disabling items



I don't know if this is a bug or what, but here is something that works. Add a level of indirection. Example:

Code Block
struct MyApp: App {
@StateObject var appModel: AppModel = .init()
...
    var body: some Scene {
      WindowGroup {
...
}.commands {
CommandGroup(after: CommandGroupPlacement.toolbar) {
    ItemsThatUpdate().environmentObject(appModel)
}
}
...
struct ItemsThatUpdate: View {
    @EnvironmentObject var appModel: AppModel
    var body: some View {
        Group {
            Button(action: toggleFoo) {
// Now this text will update as desired when model changes
                Text(verbatim: "Toggle Foo - \(appModel.foo)")
            }
            Button ...
Button ...
        }
    }
    private func toggleFoo() {
        appModel.foo.toggle()
    }
}

Did you ever find a solution?

Refresh @CommandsBuilder to update menu bar items
 
 
Q