-
Notifications
You must be signed in to change notification settings - Fork 5
/
PingBarAppDelegate.swift
124 lines (102 loc) · 3.54 KB
/
PingBarAppDelegate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// PingBarAppDelegate.swift
// PingBar
//
// Created by Peter Kristensen on 20/11/2021.
//
import Foundation
import MenuBuilder
import SwiftUI
class PingBarAppDelegate: NSObject, NSApplicationDelegate {
private var barItem: NSStatusItem?
private var pingQueue = OperationQueue()
private var lastReply = Date()
@AppStorage("doPing") var enabled = false
@AppStorage("pingHost") var host = "localhost"
@AppStorage("pingDelay") var delay: TimeInterval = 1.0
@AppStorage("maxPings") var maxPings = 10
@AppStorage("timePrecision") var precision = 2
private var pinger: Pinger?
private var pingTimer: Timer?
func stopPinging() {
guard let timer = pingTimer else {
return
}
timer.invalidate()
}
func updateTime() {
let secs = Date().timeIntervalSince(lastReply)
let str = String(format: "%%.%df", precision)
barItem?.button?.title = String(format: str, secs)
}
func startPinging() {
if !enabled {
return
}
pinger = Pinger(withHost: host)
lastReply = Date()
updateTime()
pingTimer = Timer.scheduledTimer(withTimeInterval: delay, repeats: true, block: { _ in
guard let pinger = self.pinger else {
print("missing pinger")
return
}
if self.pingQueue.operationCount < self.maxPings {
self.pingQueue.addOperation {
let delay = pinger.pingOnce()
OperationQueue.main.addOperation {
if delay >= 0.0 {
self.lastReply = Date().addingTimeInterval(-delay)
}
self.updateTime()
}
}
} else {
self.updateTime()
}
})
}
func applicationDidFinishLaunching(_: Notification) {
UserDefaults.standard.register(defaults: [
"pingDelay": 1.0,
"pingHost": "localhost",
"doPing": false,
"timePrecision": 0,
"maxPings": 10, // TODO: add to ui!
])
NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification,
object: UserDefaults.standard,
queue: nil) { _ in
self.stopPinging()
self.startPinging()
}
let bar = NSStatusBar.system
let item = bar.statusItem(withLength: NSStatusItem.variableLength)
item.button?.title = "Test"
var prefName = "Preferences…"
if #available(macOS 13.0, *) {
prefName = "Settings…"
}
let menu = NSMenu {
MenuItem(prefName).onSelect {
if #available(macOS 13.0, *) {
NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil)
} else {
NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil)
}
NSApp.activate(ignoringOtherApps: true)
}
MenuItem("About").onSelect {
NSApp.orderFrontStandardAboutPanel(nil)
NSApp.activate(ignoringOtherApps: true)
}
SeparatorItem()
MenuItem("Quit").onSelect {
NSApp.terminate(nil)
}
}
item.menu = menu
barItem = item
startPinging()
}
}