In SwiftUI on macOS, you can add items to the main menu by using things like CommandGroup
, CommandMenu
.
But the default menus already have items. For example, the Edit menu has Undo, Redo, Cut, Copy, Paste, Delete, Select All. Those menu items are grayed out and don't do anything. Is there a way to hook some Swift code to these? Or do you just use CommandGroup(replacing: ...)
to replace them? That doesn't seem right; why put them there if you just have to replace them?
Besides hooking up a function to run when they get clicked, I'd like to know how to enable and disable them depending on app state.
WindowGroup {
MyContentView()
}.commands {
SidebarCommands()
CommandGroup(after: .newItem) {
Button { newFolder() } label: { Text("New Folder") }
.keyboardShortcut("n", modifiers: [.command, .option])
}
}