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

This commit is contained in:
Serhiy Mytrovtsiy
2022-07-06 20:26:12 +02:00
parent 1cb90efbb7
commit 31b68e439e
2 changed files with 54 additions and 0 deletions

View File

@@ -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
}
}
}

View File

@@ -17,6 +17,7 @@ internal class Settings: NSStackView, Settings_v {
private var updateTopIntervalValue: Int = 1
private var numberOfProcesses: Int = 8
private var splitValueState: Bool = false
private var notificationLevel: String = "Disabled"
private let title: String
@@ -31,6 +32,7 @@ internal class Settings: NSStackView, Settings_v {
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.splitValueState = Store.shared.bool(key: "\(self.title)_splitValue", defaultValue: self.splitValueState)
self.notificationLevel = Store.shared.string(key: "\(self.title)_notificationLevel", defaultValue: self.notificationLevel)
super.init(frame: NSRect(x: 0, y: 0, width: 0, height: 0))
@@ -80,6 +82,13 @@ internal class Settings: NSStackView, Settings_v {
state: self.splitValueState
))
}
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) {
@@ -118,4 +127,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)")
}
}
}