Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add ContextMenu to MenuBarExtra SwiftUI macOS

Tags:

macos

swiftui

I want to be able to show a context menu when I right click my MenubarExtra app, similar to the Figma app behaviour for macOS:

enter image description here

Note that the left clic is showing the actual MenubarExtra window.

like image 721
B. Mohammad Avatar asked Sep 06 '25 21:09

B. Mohammad


1 Answers

The closest solution would be using MenuBarExtraAccess.

You can directly access the NSStatusItem via

.menuBarExtraAccess(isPresented: $isMenuPresented) { statusItem in // <-- the magic ✨
    // access status item or store it in a @State var
}

and implement a right-click listener with:

// MARK: Right-mouse icon click
        NSEvent.addLocalMonitorForEvents(matching: .rightMouseDown) { [weak self] event in
            if event.window == self?.statusItem.button?.window {
                WLLNotificationCenter.Keys.statusbarDidClick.send(.rightMouseClicked)
                let menu = StatusBarContextMenu()
                menu.popUp()
            }
            return event
        }
like image 176
Maschina Avatar answered Sep 09 '25 12:09

Maschina