|
|
@ -15,7 +15,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { |
|
|
|
|
|
|
|
private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength) |
|
|
|
private let nc = NSWorkspace.shared.notificationCenter |
|
|
|
private var frames = [NSImage]() |
|
|
|
private var anim = [(Double, [NSImage])]() |
|
|
|
private var cnt: Int = 0 |
|
|
|
private var isRunning: Bool = false |
|
|
|
private var interval: Double = 1.0 |
|
|
@ -23,15 +23,26 @@ class AppDelegate: NSObject, NSApplicationDelegate { |
|
|
|
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 i in (0 ..< 5) { |
|
|
|
frames.append(NSImage(imageLiteralResourceName: "cat_page\(i)")) |
|
|
|
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 = frames[cnt] |
|
|
|
cnt = (cnt + 1) % frames.count |
|
|
|
statusItem.button?.image = anim[animId].1[cnt] |
|
|
|
|
|
|
|
startRunning() |
|
|
|
} |
|
|
@ -58,7 +69,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { |
|
|
|
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.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() |
|
|
@ -72,8 +83,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { |
|
|
|
} |
|
|
|
|
|
|
|
func animate() { |
|
|
|
statusItem.button?.image = frames[cnt] |
|
|
|
cnt = (cnt + 1) % frames.count |
|
|
|
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() |
|
|
@ -85,6 +96,20 @@ class AppDelegate: NSObject, NSApplicationDelegate { |
|
|
|
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) |
|
|
|