You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.9 KiB

  1. //
  2. // AppDelegate.swift
  3. // Menubar RunCat
  4. //
  5. // Created by Takuto Nakamura on 2019/08/06.
  6. // Copyright © 2019 Takuto Nakamura. All rights reserved.
  7. //
  8. import Cocoa
  9. @NSApplicationMain
  10. class AppDelegate: NSObject, NSApplicationDelegate {
  11. @IBOutlet weak var menu: NSMenu!
  12. private let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
  13. private let nc = NSWorkspace.shared.notificationCenter
  14. private var frames = [NSImage]()
  15. private var cnt: Int = 0
  16. private var isRunning: Bool = false
  17. private var interval: Double = 1.0
  18. private let cpu = CPU()
  19. private var cpuTimer: Timer? = nil
  20. private var usage: (value: Double, description: String) = (0.0, "")
  21. private var isShowUsage: Bool = false
  22. func applicationDidFinishLaunching(_ aNotification: Notification) {
  23. for i in (0 ..< 5) {
  24. frames.append(NSImage(imageLiteralResourceName: "cat_page\(i)"))
  25. }
  26. statusItem.menu = menu
  27. statusItem.button?.imagePosition = .imageRight
  28. statusItem.button?.image = frames[cnt]
  29. cnt = (cnt + 1) % frames.count
  30. startRunning()
  31. }
  32. func applicationWillTerminate(_ aNotification: Notification) {
  33. stopRunning()
  34. }
  35. func setNotifications() {
  36. nc.addObserver(self, selector: #selector(AppDelegate.receiveSleepNote),
  37. name: NSWorkspace.willSleepNotification, object: nil)
  38. nc.addObserver(self, selector: #selector(AppDelegate.receiveWakeNote),
  39. name: NSWorkspace.didWakeNotification, object: nil)
  40. }
  41. @objc func receiveSleepNote() {
  42. stopRunning()
  43. }
  44. @objc func receiveWakeNote() {
  45. startRunning()
  46. }
  47. func startRunning() {
  48. cpuTimer = Timer.scheduledTimer(withTimeInterval: 5.0, repeats: true, block: { (t) in
  49. self.usage = self.cpu.usageCPU()
  50. self.interval = 0.02 * (100 - max(0.0, min(99.0, self.usage.value))) / 6
  51. self.statusItem.button?.title = self.isShowUsage ? self.usage.description : ""
  52. })
  53. cpuTimer?.fire()
  54. isRunning = true
  55. animate()
  56. }
  57. func stopRunning() {
  58. isRunning = false
  59. cpuTimer?.invalidate()
  60. }
  61. func animate() {
  62. statusItem.button?.image = frames[cnt]
  63. cnt = (cnt + 1) % frames.count
  64. if !isRunning { return }
  65. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + interval) {
  66. self.animate()
  67. }
  68. }
  69. @IBAction func toggleShowUsage(_ sender: NSMenuItem) {
  70. isShowUsage = sender.state == .off
  71. sender.state = isShowUsage ? .on : .off
  72. statusItem.button?.title = isShowUsage ? usage.description : ""
  73. }
  74. @IBAction func showAbout(_ sender: Any) {
  75. NSApp.activate(ignoringOtherApps: true)
  76. NSApp.orderFrontStandardAboutPanel(nil)
  77. }
  78. }