i would like to place a context menu onto a NSTableView
. this part is done. what i would liek to do is to show different menu entries based on the content of the ri
This is the easiest method for a custom/dynamic NSMenu
I found, that also preserves the system look (the blue selection border). Subclass NSTableView
and set your menu in menu(for:)
.
The important part is to set the menu on the table view, but return the menu from its super
call.
override func menu(for event: NSEvent) -> NSMenu? {
let point = convert(event.locationInWindow, from: nil)
let clickedRow = self.row(at: point)
var menuRows = selectedRowIndexes
// The blue selection box should always reflect the
// returned row indexes.
if menuRows.isEmpty || !menuRows.contains(clickedRow) {
menuRows = [clickedRow]
}
// Build your custom menu based on the menuRows indexes
self.menu = <#myMenu#>
return super.menu(for: event)
}