B. Mohammad
B. Mohammad

Reputation: 2464

Add ContextMenu to MenuBarExtra SwiftUI macOS

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.

Upvotes: 4

Views: 495

Answers (1)

Maschina
Maschina

Reputation: 926

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
        }

Upvotes: 2

Related Questions