Skip to content

Commit

Permalink
Whenever the device directory changes, reload the menu.
Browse files Browse the repository at this point in the history
  • Loading branch information
luosheng committed Nov 11, 2015
1 parent 1601b7c commit b7ccb03
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 10 additions & 7 deletions OpenSim/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ class AppDelegate: NSObject, NSApplicationDelegate {
var fileInfoList: [FileInfo?]!

func applicationDidFinishLaunching(aNotification: NSNotification) {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
statusItem.image = NSImage(named: "menubar")
statusItem.image!.template = true
statusItem.menu = NSMenu()

buildMenu()

fileInfoList = buildFileInfoList()
let timer = NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "checkFileInfoList:", userInfo: nil, repeats: true)
NSRunLoop.currentRunLoop().addTimer(timer, forMode: NSRunLoopCommonModes)
buildMenu()
}

private func buildFileInfoList() -> [FileInfo?] {
Expand All @@ -30,18 +36,15 @@ class AppDelegate: NSObject, NSApplicationDelegate {
func checkFileInfoList(timer: NSTimer) {
let newFileInfoList = buildFileInfoList()
if fileInfoList != newFileInfoList {
print("yes")
buildMenu()
}
fileInfoList = newFileInfoList
}

func buildMenu() {
statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(NSVariableStatusItemLength)
statusItem.image = NSImage(named: "menubar")
statusItem.image!.template = true

statusItem.menu = NSMenu()
statusItem.menu!.removeAllItems()

DeviceManager.defaultManager.reload()
let iOSDevices = DeviceManager.defaultManager.deviceMapping.filter { $0.0.containsString("iOS") }.flatMap { $0.1 }
iOSDevices.forEach { device in
let deviceMenuItem = statusItem.menu?.addItemWithTitle("\(device.name) (\(device.state))", action: nil, keyEquivalent: "")
Expand Down
4 changes: 0 additions & 4 deletions OpenSim/DeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ final class DeviceManager {
static let defaultManager = DeviceManager()
var deviceMapping = [String: [Device]]()

init() {
reload()
}

func reload() {
guard let devicesPlist = NSDictionary(contentsOfURL: URLHelper.deviceSetURL)?[DeviceManager.devicesKey] as? [String: AnyObject] else {
return
Expand Down

0 comments on commit b7ccb03

Please sign in to comment.