dotBeatBar/dotBeatBar_old/AppDelegate.swift

63 lines
1.9 KiB
Swift
Raw Normal View History

2019-07-26 20:38:19 -06:00
//
// AppDelegate.swift
// dotBeatBar_old
//
// Created by Aaron Bieber on 7/26/19.
// Copyright © 2019 Aaron Bieber. All rights reserved.
//
2019-07-26 21:44:43 -06:00
import Beat
2019-07-27 08:49:55 -06:00
import Cocoa
2019-07-26 21:44:43 -06:00
import Foundation
2019-07-26 20:38:19 -06:00
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let dnc = DistributedNotificationCenter.default()
2019-07-26 21:44:43 -06:00
var timer = Timer()
2019-07-26 21:44:43 -06:00
let si = NSStatusBar.system.statusItem(withLength:NSStatusItem.variableLength)
2019-07-26 21:44:43 -06:00
@objc func updateStatBar(_ sender: Any?) {
if let button = si.button {
button.title = "@\(Beat().text())"
2019-07-26 21:44:43 -06:00
}
}
2019-07-26 20:38:19 -06:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
2019-07-26 21:44:43 -06:00
updateStatBar(self)
timer = Timer.scheduledTimer(timeInterval: 1,
2019-07-27 08:49:55 -06:00
target: self,
selector: #selector(updateStatBar),
userInfo: nil,
repeats: true)
2019-07-26 21:44:43 -06:00
let menu = NSMenu()
2019-07-26 21:44:43 -06:00
menu.addItem(NSMenuItem(title: "Quit", action:
#selector(NSApplication.terminate(_:)), keyEquivalent: "q"))
si.menu = menu
_ = dnc.addObserver(forName: .init("com.apple.screenIsLocked"),
object: nil, queue: .main) { _ in
NSLog("Screen Locked. Removing ssh-agent keys.")
let task = Process()
let pipe = Pipe()
task.executableURL = URL(fileURLWithPath: "/usr/bin/ssh-add")
task.arguments = ["-D"]
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
NSLog(output! as String)
}
2019-07-26 20:38:19 -06:00
}
func applicationWillTerminate(_ aNotification: Notification) {
2019-07-26 21:44:43 -06:00
timer.invalidate()
2019-07-26 20:38:19 -06:00
}
}