pistacchio
pistacchio

Reputation: 58883

Remove / replace / add menus in SwiftUI

I have a simple SwfitUI (XCode 12.4, MacOS 11.1) Mac application with an AppDelegate.

How do I remove the default File / Edit / View / Window / Help menus and replace them with custom menus?

class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ notification: Notification) {
        let _ = NSApplication.shared.windows.map { $0.tabbingMode = .disallowed }
        let mainWindow = NSApplication.shared.windows.first!
        
        mainWindow.backgroundColor = NSColor(red: 1, green: 1, blue: 1, alpha: 0.3)
        mainWindow.titlebarAppearsTransparent = true
        mainWindow.titleVisibility = .hidden        
        mainWindow.backgroundColor = NSColor(red: 1, green: 1, blue: 1, alpha: 1)
    }
    
    func applicationWillFinishLaunching(_ notification: Notification) {
        NSWindow.allowsAutomaticWindowTabbing = false
    }
}

@main
struct VeyBoardApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Upvotes: 3

Views: 1772

Answers (1)

pistacchio
pistacchio

Reputation: 58883

It can be done like this:

@main
struct VeyBoardApp: App {
    @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

    var body: some Scene {
        WindowGroup {
            ContentView()
        }
        .commands {
            CommandGroup(replacing: CommandGroupPlacement.newItem) {}
            CommandGroup(replacing: CommandGroupPlacement.sidebar) {
                Button("New menu item in View") {
                    print("clicked")
                }
            }
        }
    }
}

Upvotes: 4

Related Questions