Skip to content

Instantly share code, notes, and snippets.

@maximbilan
Created October 21, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximbilan/88857197d8a46203c132 to your computer and use it in GitHub Desktop.
Save maximbilan/88857197d8a46203c132 to your computer and use it in GitHub Desktop.
Mac OS X Application Popup App Delegate
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2)
let popover = NSPopover()
var eventMonitor: EventMonitor?
func applicationDidFinishLaunching(aNotification: NSNotification) {
if let button = statusItem.button {
button.image = NSImage(named: "StatusBarButtonImage")
button.action = Selector("togglePopover:")
}
let mainViewController = NSStoryboard(name: "Main", bundle: nil).instantiateControllerWithIdentifier("ViewControllerId") as! ViewController
popover.contentViewController = mainViewController
eventMonitor = EventMonitor(mask: [.LeftMouseDownMask, .RightMouseDownMask]) { [unowned self] event in
if self.popover.shown {
self.closePopover(event)
}
}
eventMonitor?.start()
}
func applicationWillTerminate(aNotification: NSNotification) {
}
func togglePopover(sender: AnyObject?) {
if popover.shown {
closePopover(sender)
} else {
showPopover(sender)
}
}
func showPopover(sender: AnyObject?) {
if let button = statusItem.button {
popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: NSRectEdge.MinY)
}
eventMonitor?.start()
}
func closePopover(sender: AnyObject?) {
popover.performClose(sender)
eventMonitor?.stop()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment