2016-05-14 20:54:49 +00:00
|
|
|
//
|
|
|
|
// AppDelegate.swift
|
|
|
|
// ZeroTier One
|
|
|
|
//
|
|
|
|
// Created by Grant Limberg on 5/14/16.
|
|
|
|
// Copyright © 2016 ZeroTier, Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
@NSApplicationMain
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
|
|
|
|
@IBOutlet weak var window: NSWindow!
|
|
|
|
|
|
|
|
|
2016-05-14 22:42:45 +00:00
|
|
|
let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(-2.0)
|
|
|
|
|
|
|
|
let networkListPopover = NSPopover()
|
|
|
|
let joinNetworkPopover = NSPopover()
|
|
|
|
|
|
|
|
var transientMonitor: AnyObject? = nil
|
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
let monitor = NetworkMonitor()
|
2016-05-14 22:42:45 +00:00
|
|
|
|
2016-06-17 03:53:55 +00:00
|
|
|
func applicationDidFinishLaunching(aNotification: NSNotification) {
|
|
|
|
let nc = NSNotificationCenter.defaultCenter()
|
|
|
|
nc.addObserver(self, selector: #selector(onNetworkListUpdated(_:)), name: networkUpdateKey, object: nil)
|
2016-05-14 22:42:45 +00:00
|
|
|
|
|
|
|
statusItem.image = NSImage(named: "MenuBarIconMac")
|
|
|
|
|
|
|
|
let menu = NSMenu()
|
|
|
|
|
|
|
|
menu.addItem(NSMenuItem(title: "Show Networks", action: #selector(AppDelegate.showNetworks), keyEquivalent: "n"))
|
|
|
|
menu.addItem(NSMenuItem(title: "Join Network", action: #selector(AppDelegate.joinNetwork), keyEquivalent: "j"))
|
|
|
|
menu.addItem(NSMenuItem.separatorItem())
|
|
|
|
menu.addItem(NSMenuItem(title: "Quit ZeroTier One", action: #selector(AppDelegate.quit), keyEquivalent: "q"))
|
|
|
|
|
|
|
|
statusItem.menu = menu
|
|
|
|
|
|
|
|
joinNetworkPopover.contentViewController = JoinNetworkViewController(
|
|
|
|
nibName: "JoinNetworkViewController", bundle: nil)
|
|
|
|
joinNetworkPopover.behavior = .Transient
|
|
|
|
|
2016-05-27 22:07:20 +00:00
|
|
|
joinNetworkPopover.appearance = NSAppearance(named: NSAppearanceNameAqua)
|
|
|
|
|
2016-05-14 22:42:45 +00:00
|
|
|
networkListPopover.contentViewController = ShowNetworksViewController(
|
|
|
|
nibName: "ShowNetworksViewController", bundle: nil)
|
|
|
|
networkListPopover.behavior = .Transient
|
2016-05-27 22:07:20 +00:00
|
|
|
|
|
|
|
networkListPopover.appearance = NSAppearance(named: NSAppearanceNameAqua)
|
2016-05-14 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func applicationWillTerminate(aNotification: NSNotification) {
|
|
|
|
// Insert code here to tear down your application
|
2016-06-17 03:53:55 +00:00
|
|
|
let nc = NSNotificationCenter.defaultCenter()
|
|
|
|
nc.removeObserver(self)
|
2016-05-14 20:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-14 22:42:45 +00:00
|
|
|
func showNetworks() {
|
|
|
|
if let button = statusItem.button {
|
|
|
|
networkListPopover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: .MinY)
|
|
|
|
|
|
|
|
if transientMonitor == nil {
|
|
|
|
transientMonitor = NSEvent.addGlobalMonitorForEventsMatchingMask(
|
|
|
|
[.LeftMouseDownMask, .RightMouseDownMask, .OtherMouseDownMask]) { (event: NSEvent) -> Void in
|
|
|
|
|
|
|
|
NSEvent.removeMonitor(self.transientMonitor!)
|
|
|
|
self.transientMonitor = nil
|
|
|
|
self.networkListPopover.close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinNetwork() {
|
|
|
|
if let button = statusItem.button {
|
|
|
|
joinNetworkPopover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: .MinY)
|
|
|
|
|
|
|
|
if transientMonitor == nil {
|
|
|
|
transientMonitor = NSEvent.addGlobalMonitorForEventsMatchingMask(
|
|
|
|
[.LeftMouseDownMask, .RightMouseDownMask, .OtherMouseDownMask]) { (event: NSEvent) -> Void in
|
|
|
|
|
|
|
|
NSEvent.removeMonitor(self.transientMonitor!)
|
|
|
|
self.transientMonitor = nil
|
|
|
|
self.joinNetworkPopover.close()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func quit() {
|
|
|
|
NSApp.performSelector(#selector(NSApp.terminate(_:)), withObject: nil, afterDelay: 0.0)
|
|
|
|
}
|
2016-06-17 03:53:55 +00:00
|
|
|
|
|
|
|
func onNetworkListUpdated(note: NSNotification) {
|
|
|
|
let netList = note.userInfo!["networks"] as! [Network]
|
|
|
|
(networkListPopover.contentViewController as! ShowNetworksViewController).setNetworks(netList)
|
|
|
|
}
|
2016-05-14 20:54:49 +00:00
|
|
|
}
|
|
|
|
|