How to show context menu from NSButton in macOS

Issue #435

Use NSMenu and popUp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
func showQuitMenu() {
let menu = NSMenu()
let aboutItem = NSMenuItem(
title: "About",
action: #selector(onAboutTouched(_:)),
keyEquivalent: ""
)

let quitItem = NSMenuItem(
title: "Quit Hacker Pad",
action: #selector(onQuitTouched(_:)),
keyEquivalent: ""
)

aboutItem.target = self
quitItem.target = self

menu.addItem(aboutItem)
menu.addItem(quitItem)

menu.popUp(
positioning: aboutItem,
at: bottomView.quitButton.frame.origin,
in: bottomView
)
}

Use Omnia

1
2
3
4
5
6
7
8
9
10
let menuHandler = MenuHandler()
menuHandler.add(title: "About", action: {
NSWorkspace.shared.open(URL(string: "https://onmyway133.github.io/")!)
})

menuHandler.add(title: "Quit Hacker Pad", action: {
NSApp.terminate(nil)
})

menuHandler.show(from: self.bottomView.gearButton, in: self.bottomView)

Comments