mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: added a notification to the CPU usage (#913)
This commit is contained in:
@@ -219,3 +219,25 @@ public var isM1: Bool {
|
||||
return SystemKit.shared.device.info.cpu?.name == "Apple M1" ? true : false
|
||||
}
|
||||
}
|
||||
|
||||
public let notificationLevels: [KeyValue_t] = [
|
||||
KeyValue_t(key: "Disabled", value: "Disabled"),
|
||||
KeyValue_t(key: "10%", value: "10%"),
|
||||
KeyValue_t(key: "15%", value: "15%"),
|
||||
KeyValue_t(key: "20%", value: "20%"),
|
||||
KeyValue_t(key: "25%", value: "25%"),
|
||||
KeyValue_t(key: "30%", value: "30%"),
|
||||
KeyValue_t(key: "40%", value: "40%"),
|
||||
KeyValue_t(key: "50%", value: "50%"),
|
||||
KeyValue_t(key: "55%", value: "55%"),
|
||||
KeyValue_t(key: "60%", value: "60%"),
|
||||
KeyValue_t(key: "65%", value: "65%"),
|
||||
KeyValue_t(key: "70%", value: "70%"),
|
||||
KeyValue_t(key: "75%", value: "75%"),
|
||||
KeyValue_t(key: "80%", value: "80%"),
|
||||
KeyValue_t(key: "85%", value: "85%"),
|
||||
KeyValue_t(key: "90%", value: "90%"),
|
||||
KeyValue_t(key: "95%", value: "95%"),
|
||||
KeyValue_t(key: "97%", value: "97%"),
|
||||
KeyValue_t(key: "100%", value: "100%")
|
||||
]
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ internal class Settings: NSStackView, Settings_v {
|
||||
private var updateIntervalValue: Int = 1
|
||||
private var updateTopIntervalValue: Int = 1
|
||||
private var numberOfProcesses: Int = 8
|
||||
private var notificationLevel: String = "Disabled"
|
||||
|
||||
private let title: String
|
||||
private var hasHyperthreadingCores = false
|
||||
@@ -42,6 +43,7 @@ internal class Settings: NSStackView, Settings_v {
|
||||
self.updateIntervalValue = Store.shared.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue)
|
||||
self.updateTopIntervalValue = Store.shared.int(key: "\(self.title)_updateTopInterval", defaultValue: self.updateTopIntervalValue)
|
||||
self.numberOfProcesses = Store.shared.int(key: "\(self.title)_processes", defaultValue: self.numberOfProcesses)
|
||||
self.notificationLevel = Store.shared.string(key: "\(self.title)_notificationLevel", defaultValue: self.notificationLevel)
|
||||
if !self.usagePerCoreState {
|
||||
self.hyperthreadState = false
|
||||
}
|
||||
@@ -135,6 +137,13 @@ internal class Settings: NSStackView, Settings_v {
|
||||
items: NumbersOfProcesses.map{ "\($0)" },
|
||||
selected: "\(self.numberOfProcesses)"
|
||||
))
|
||||
|
||||
self.addArrangedSubview(selectSettingsRow(
|
||||
title: localizedString("Notification level"),
|
||||
action: #selector(changeNotificationLevel),
|
||||
items: notificationLevels,
|
||||
selected: self.notificationLevel == "disabled" ? self.notificationLevel : "\(Int((Double(self.notificationLevel) ?? 0)*100))%"
|
||||
))
|
||||
}
|
||||
|
||||
@objc private func changeUpdateInterval(_ sender: NSMenuItem) {
|
||||
@@ -225,4 +234,14 @@ internal class Settings: NSStackView, Settings_v {
|
||||
Store.shared.set(key: "\(self.title)_splitValue", value: self.splitValueState)
|
||||
self.callback()
|
||||
}
|
||||
|
||||
@objc func changeNotificationLevel(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else { return }
|
||||
|
||||
if key == "Disabled" {
|
||||
Store.shared.set(key: "\(self.title)_notificationLevel", value: key)
|
||||
} else if let value = Double(key.replacingOccurrences(of: "%", with: "")) {
|
||||
Store.shared.set(key: "\(self.title)_notificationLevel", value: "\(value/100)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user