Tzar
Tzar

Reputation: 4789

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

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.

Upvotes: 1

Views: 770

Answers (2)

dub
dub

Reputation: 71

It's been a little while since asked, but I came across this myself. My only way to get it to work is similar to workingdog support Ukraine's answer, but includes adding a Divider(). Curiously, an EmptyView() wouldn't work.

Bonus: doing this also allowed me to use the (cmd+n) keyboard shortcut for my own app's purposes (not creating a new window, but a new item).

Example:

@main
struct TestMacApp: App {
     @State var showView = false
     
     var body: some Scene {
         WindowGroup {
             ContentView()
         }
         .commandsRemoved()
         .commandsReplaced {
            CommandGroup(replacing: .newItem) {
                Divider()
            }
         }
     }
 }

Upvotes: 0

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: {}) }
             // ...
         }
     }
 }

Upvotes: 0

Related Questions