feat: added a notification to the CPU usage (#913)

This commit is contained in:
Serhiy Mytrovtsiy
2022-07-04 18:11:52 +02:00
parent 576bfdd5e1
commit bd2b25bf52
3 changed files with 76 additions and 0 deletions

View File

@@ -41,6 +41,9 @@ public class CPU: Module {
private var limitReader: LimitReader? = nil
private var averageReader: AverageReader? = nil
private var notificationLevelState: Bool = false
private var notificationID: String? = nil
private var usagePerCoreState: Bool {
get {
return Store.shared.bool(key: "\(self.config.name)_usagePerCore", defaultValue: false)
@@ -51,6 +54,11 @@ public class CPU: Module {
return Store.shared.bool(key: "\(self.config.name)_splitValue", defaultValue: false)
}
}
private var notificationLevel: String {
get {
return Store.shared.string(key: "\(self.config.name)_notificationLevel", defaultValue: "Disabled")
}
}
public init() {
self.settingsView = Settings("CPU")
@@ -154,6 +162,7 @@ public class CPU: Module {
}
self.popupView.loadCallback(value)
self.checkNotificationLevel(value.totalUsage)
self.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
switch w.item {
@@ -184,4 +193,30 @@ public class CPU: Module {
}
}
}
private func checkNotificationLevel(_ value: Double) {
guard self.notificationLevel != "Disabled", let level = Double(self.notificationLevel) else { return }
if let id = self.notificationID, value < level && self.notificationLevelState {
if #available(macOS 10.14, *) {
removeNotification(id)
} else {
removeNSNotification(id)
}
self.notificationID = nil
self.notificationLevelState = false
} else if value >= level && !self.notificationLevelState {
let title = localizedString("CPU usage threshold")
let subtitle = localizedString("CPU usage is", "\(Int((value)*100))%")
if #available(macOS 10.14, *) {
self.notificationID = showNotification(title: title, subtitle: subtitle)
} else {
self.notificationID = showNSNotification(title: title, subtitle: subtitle)
}
self.notificationLevelState = true
}
}
}