Selectively Removing Default Menu Bar Menus (like File, Edit, and View) in macOS App Using SwiftUI

308 Views Asked by At

Is there a SwiftUI idiomatic way to selectively remove the defualt (File, Edit, and View) menus from the menu bar in a macOS app, while keeping other menus like the AppName menu intact? The app I’m building is a simple utility, so Edit and View menus are not relevant in this context.

I’m aware of the new .commandsRemoved(), available since macOS 13, but that apparently removes all menus.

I’m looking for a pure SwiftUI solution. Any guidance and help would be appreciated.

1

There are 1 best solutions below

1
workingdog support Ukraine On

You could try removing all commands using .commandsRemoved(), then adding back only the ones you want. You can also show/hide the particular command using a condition.

 @main
 struct TestMacApp: App {
     @State var showView = false
     
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .commandsRemoved()
         .commandsReplaced {
             CommandGroup(replacing: .help, addition: {})
             if showView { CommandGroup(replacing: .textEditing, addition: {}) }
             // ...
         }
     }
 }