Skip to content

Instantly share code, notes, and snippets.

@comfortablejohn
Created December 27, 2017 02:52
Show Gist options
  • Save comfortablejohn/47b059895392f6bbdb798a64e399fa8d to your computer and use it in GitHub Desktop.
Save comfortablejohn/47b059895392f6bbdb798a64e399fa8d to your computer and use it in GitHub Desktop.
class MenuDelegate: NSObject, NSMenuDelegate {
func menuWillOpen(_ menu: NSMenu) {
print("Menu opened!")
// do things
}
}
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var statusItem: NSStatusItem?
var statusBar: NSStatusBar?
// our menu delegate
var menuDelegate: MenuDelegate?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// get a reference to the system wide status bar
self.statusBar = NSStatusBar.system
self.statusItem = self.statusBar?.statusItem(withLength: NSStatusItem.variableLength)
// setup the button which sits in the status bar (like your wifi icon)
if let button = self.statusItem?.button {
// use image from our asset named 'status-bar-icon'
button.image = NSImage(named: NSImage.Name("status-bar-icon"))
}
self.statusItem?.menu = NSMenu(title: "Menu")
// instantiate the menu delegate
self.menuDelegate = MenuDelegate()
if let menu = self.statusItem?.menu {
// add menu items
...
menu.delegate = self.menuDelegate
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment