Created
October 21, 2015 17:34
-
-
Save maximbilan/88857197d8a46203c132 to your computer and use it in GitHub Desktop.
Mac OS X Application Popup App Delegate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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