From 31b68e439ea1d86d2287906793374091a4b5f14f Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Wed, 6 Jul 2022 20:26:12 +0200 Subject: [PATCH] feat: added a notification to the RAM usage (#913) --- Modules/RAM/main.swift | 35 +++++++++++++++++++++++++++++++++++ Modules/RAM/settings.swift | 19 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/Modules/RAM/main.swift b/Modules/RAM/main.swift index 32b28184..1051a956 100644 --- a/Modules/RAM/main.swift +++ b/Modules/RAM/main.swift @@ -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 + } + } } diff --git a/Modules/RAM/settings.swift b/Modules/RAM/settings.swift index add9daa7..59a748e6 100644 --- a/Modules/RAM/settings.swift +++ b/Modules/RAM/settings.swift @@ -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)") + } + } }