I am wondering how to validate menu items in SwiftUI (2.0, macOS).
Given the following:
Suppose menu item 1 is conditional and should be shown or hidden depending on the app state.
In AppKit we would use NSMenuItemValidation
to determine if a menu item should be shown/disable/enabled.
Question: how can we do menu validation in SwiftUI?
From what I have found so far CommandMenu is build *once* and cannot be dynamically adapted to reflect the current AppState. Or am I missing something?
Given the following:
Code Block swift @main struct MyApp: App { var body: some Scene { WindowGroup { ContentView() } .commands { CommandMenu("DEBUG") { Button(action: { }) { Text("menu item 1") } Button(action: { }) { Text("menu item 2") } } } } }
Suppose menu item 1 is conditional and should be shown or hidden depending on the app state.
In AppKit we would use NSMenuItemValidation
Code Block swift func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { // validate the menuItem to be shown }
to determine if a menu item should be shown/disable/enabled.
Question: how can we do menu validation in SwiftUI?
From what I have found so far CommandMenu is build *once* and cannot be dynamically adapted to reflect the current AppState. Or am I missing something?