/
main.swift
253 lines (217 loc) · 7.98 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// main.swift
// Battery
//
// Created by Serhiy Mytrovtsiy on 06/06/2020.
// Using Swift 5.0.
// Running on macOS 10.15.
//
// Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved.
//
import Cocoa
import Kit
import IOKit.ps
struct Battery_Usage: value_t {
var powerSource: String = ""
var state: String? = nil
var isCharged: Bool = false
var isCharging: Bool = false
var isLowPowerMode: Bool? = false
var level: Double = 0
var cycles: Int = 0
var health: Int = 0
var designedCapacity: Int = 0
var maxCapacity: Int = 0
var amperage: Int = 0
var voltage: Double = 0
var temperature: Double = 0
var ACwatts: Int = 0
var timeToEmpty: Int = 0
var timeToCharge: Int = 0
var timeOnACPower: Date? = nil
public var widgetValue: Double {
get {
return self.level
}
}
}
public class Battery: Module {
private var usageReader: UsageReader? = nil
private var processReader: ProcessReader? = nil
private let popupView: Popup
private var settingsView: Settings
private var lowLevelNotificationState: Bool = false
private var highLevelNotificationState: Bool = false
private var notificationID: String? = nil
public init() {
self.settingsView = Settings("Battery")
self.popupView = Popup("Battery")
super.init(
popup: self.popupView,
settings: self.settingsView
)
guard self.available else { return }
self.usageReader = UsageReader()
self.processReader = ProcessReader()
self.settingsView.callback = {
DispatchQueue.global(qos: .background).async {
self.usageReader?.read()
}
}
self.settingsView.callbackWhenUpdateNumberOfProcesses = {
self.popupView.numberOfProcessesUpdated()
DispatchQueue.global(qos: .background).async {
self.processReader?.read()
}
}
self.usageReader?.callbackHandler = { [unowned self] value in
self.usageCallback(value)
}
self.usageReader?.readyCallback = { [unowned self] in
self.readyHandler()
}
self.processReader?.callbackHandler = { [unowned self] value in
if let list = value {
self.popupView.processCallback(list)
}
}
if let reader = self.usageReader {
self.addReader(reader)
}
if let reader = self.processReader {
self.addReader(reader)
}
}
public override func willTerminate() {
guard self.isAvailable() else { return }
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
}
}
public override func isAvailable() -> Bool {
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
return !sources.isEmpty
}
private func usageCallback(_ raw: Battery_Usage?) {
guard let value = raw, self.enabled else {
return
}
self.checkLowNotification(value: value)
self.checkHighNotification(value: value)
self.popupView.usageCallback(value)
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
switch w.item {
case let widget as Mini:
widget.setValue(abs(value.level))
widget.setColorZones((0.15, 0.3))
case let widget as BarChart:
widget.setValue([[ColorValue(value.level)]])
widget.setColorZones((0.15, 0.3))
case let widget as BatterykWidget:
widget.setValue(
percentage: value.level,
ACStatus: value.powerSource != "Battery Power",
isCharging: value.isCharging,
lowPowerMode: value.isLowPowerMode,
time: value.timeToEmpty == 0 && value.timeToCharge != 0 ? value.timeToCharge : value.timeToEmpty
)
default: break
}
}
}
private func checkLowNotification(value: Battery_Usage) {
let level = Store.shared.string(key: "\(self.config.name)_lowLevelNotification", defaultValue: "0.15")
if level == "Disabled" {
return
}
guard let notificationLevel = Double(level) else {
return
}
if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.lowLevelNotificationState {
if value.level > notificationLevel {
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
}
self.lowLevelNotificationState = false
}
return
}
if value.isCharging {
return
}
if value.level <= notificationLevel && !self.lowLevelNotificationState {
let title = localizedString("Low battery")
var subtitle = localizedString("Battery remaining", "\(Int(value.level*100))")
if value.timeToEmpty > 0 {
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
}
if #available(macOS 10.14, *) {
self.notificationID = showNotification(
title: title,
subtitle: subtitle
)
} else {
self.notificationID = showNSNotification(
title: title,
subtitle: subtitle
)
}
self.lowLevelNotificationState = true
}
}
private func checkHighNotification(value: Battery_Usage) {
let level = Store.shared.string(key: "\(self.config.name)_highLevelNotification", defaultValue: "Disabled")
if level == "Disabled" {
return
}
guard let notificationLevel = Double(level) else {
return
}
if (value.level < notificationLevel || value.powerSource == "Battery Power") && self.highLevelNotificationState {
if value.level < notificationLevel {
if let id = self.notificationID {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
}
self.highLevelNotificationState = false
}
return
}
if !value.isCharging {
return
}
if value.level >= notificationLevel && !self.highLevelNotificationState {
let title = localizedString("High battery")
var subtitle = localizedString("Battery remaining to full charge", "\(Int((1-value.level)*100))")
if value.timeToCharge > 0 {
subtitle += " (\(Double(value.timeToCharge*60).printSecondsToHoursMinutesSeconds()))"
}
if #available(macOS 10.14, *) {
self.notificationID = showNotification(
title: title,
subtitle: subtitle
)
} else {
self.notificationID = showNSNotification(
title: title,
subtitle: subtitle
)
}
self.highLevelNotificationState = true
}
}
}