//
|
|
// AppDelegate.swift
|
|
// Menubar RunCat
|
|
//
|
|
// Created by Takuto Nakamura on 2019/08/06.
|
|
// Copyright © 2019 Takuto Nakamura. All rights reserved.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
@NSApplicationMain
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
@IBOutlet weak var menu: NSMenu!
|
|
|
|
private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
private let nc = NSWorkspace.shared.notificationCenter
|
|
private var anim = [(Double, [NSImage])]()
|
|
private var cnt: Int = 0
|
|
private var isRunning: Bool = false
|
|
private var interval: Double = 1.0
|
|
private let cpu = CPU()
|
|
private var cpuTimer: Timer? = nil
|
|
private var usage: (value: Double, description: String) = (0.0, "")
|
|
private var isShowUsage: Bool = false
|
|
private var animId: Int = 0
|
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
for (name, len, rate) in [
|
|
("cat_page", 5, 1.0),
|
|
("dino", 2, 1.0),
|
|
("ablobcatbongo", 2, 0.5),
|
|
("ablobcatcoffee", 24, 0.2),
|
|
("ablobcatrave", 18, 0.2),
|
|
("ablobcatwave", 24, 0.4),
|
|
("ablobattention", 22, 0.2),
|
|
("ablobblewobble", 15, 0.2),
|
|
("annoyingdog", 2, 1.0),
|
|
] {
|
|
let animFrames = (0..<len).map { NSImage(imageLiteralResourceName: "\(name)\($0)") }
|
|
anim.append((rate, animFrames))
|
|
}
|
|
statusItem.menu = menu
|
|
statusItem.button?.imagePosition = .imageRight
|
|
statusItem.button?.image = anim[animId].1[cnt]
|
|
|
|
startRunning()
|
|
}
|
|
|
|
func applicationWillTerminate(_ aNotification: Notification) {
|
|
stopRunning()
|
|
}
|
|
|
|
func setNotifications() {
|
|
nc.addObserver(self, selector: #selector(AppDelegate.receiveSleepNote),
|
|
name: NSWorkspace.willSleepNotification, object: nil)
|
|
nc.addObserver(self, selector: #selector(AppDelegate.receiveWakeNote),
|
|
name: NSWorkspace.didWakeNotification, object: nil)
|
|
}
|
|
|
|
@objc func receiveSleepNote() {
|
|
stopRunning()
|
|
}
|
|
|
|
@objc func receiveWakeNote() {
|
|
startRunning()
|
|
}
|
|
|
|
func startRunning() {
|
|
cpuTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { (t) in
|
|
self.usage = self.cpu.usageCPU()
|
|
self.interval = 0.02 * (100 - max(0.0, min(99.0, self.usage.value))) / 6 * self.anim[self.animId].0
|
|
self.statusItem.button?.title = self.isShowUsage ? self.usage.description : ""
|
|
})
|
|
cpuTimer?.fire()
|
|
isRunning = true
|
|
animate()
|
|
}
|
|
|
|
func stopRunning() {
|
|
isRunning = false
|
|
cpuTimer?.invalidate()
|
|
}
|
|
|
|
func animate() {
|
|
statusItem.button?.image = anim[animId].1[cnt]
|
|
cnt = (cnt + 1) % anim[animId].1.count
|
|
if !isRunning { return }
|
|
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + interval) {
|
|
self.animate()
|
|
}
|
|
}
|
|
|
|
@IBAction func toggleShowUsage(_ sender: NSMenuItem) {
|
|
isShowUsage = sender.state == .off
|
|
sender.state = isShowUsage ? .on : .off
|
|
statusItem.button?.title = isShowUsage ? usage.description : ""
|
|
}
|
|
|
|
@IBAction func changeAnimation(_ sender: NSMenuItem) {
|
|
let animation = menu.item(withTag: 10)?.submenu
|
|
for (index, item) in (animation?.items ?? []).enumerated() {
|
|
if item == sender {
|
|
NSLog("%d", index)
|
|
animId = index
|
|
cnt = 0
|
|
item.state = NSControl.StateValue.on
|
|
} else {
|
|
item.state = NSControl.StateValue.off
|
|
}
|
|
}
|
|
}
|
|
|
|
@IBAction func showAbout(_ sender: Any) {
|
|
NSApp.activate(ignoringOtherApps: true)
|
|
NSApp.orderFrontStandardAboutPanel(nil)
|
|
}
|
|
|
|
}
|
|
|