mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added a notification to the RAM usage (#913)
This commit is contained in:
@@ -54,11 +54,19 @@ public class RAM: Module {
|
||||
private var usageReader: UsageReader? = nil
|
||||
private var processReader: ProcessReader? = nil
|
||||
|
||||
private var notificationLevelState: Bool = false
|
||||
private var notificationID: String? = nil
|
||||
|
||||
private var splitValueState: Bool {
|
||||
get {
|
||||
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("RAM")
|
||||
@@ -118,6 +126,7 @@ public class RAM: Module {
|
||||
}
|
||||
|
||||
self.popupView.loadCallback(value)
|
||||
self.checkNotificationLevel(value.usage)
|
||||
|
||||
let total: Double = value.total == 0 ? 1 : value.total
|
||||
self.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
|
||||
@@ -160,4 +169,30 @@ public class RAM: 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("RAM utilization threshold")
|
||||
let subtitle = localizedString("RAM utilization 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user