From af010230ed3c78696df4d06c0a11d1555165f98c Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 12 Dec 2023 20:43:37 +0100 Subject: [PATCH] feat: added notifications for the GPU module --- Modules/GPU/main.swift | 28 +++-------------- Modules/GPU/notifications.swift | 56 +++++++++++++++++++++++++++++++++ Modules/GPU/settings.swift | 19 ----------- 3 files changed, 61 insertions(+), 42 deletions(-) create mode 100644 Modules/GPU/notifications.swift diff --git a/Modules/GPU/main.swift b/Modules/GPU/main.swift index fc9ea540ee1..157dbacc788 100644 --- a/Modules/GPU/main.swift +++ b/Modules/GPU/main.swift @@ -68,6 +68,7 @@ public class GPU: Module { private let popupView: Popup private let settingsView: Settings private let portalView: Portal + private let notificationsView: Notifications private var infoReader: InfoReader? = nil @@ -80,21 +81,18 @@ public class GPU: Module { return Store.shared.bool(key: "\(self.config.name)_showType", defaultValue: false) } } - private var notificationLevel: String { - get { - return Store.shared.string(key: "\(self.config.name)_notificationLevel", defaultValue: "Disabled") - } - } public init() { self.popupView = Popup() self.settingsView = Settings("GPU") self.portalView = Portal("GPU") + self.notificationsView = Notifications(.GPU) super.init( popup: self.popupView, settings: self.settingsView, - portal: self.portalView + portal: self.portalView, + notifications: self.notificationsView ) guard self.available else { return } @@ -142,7 +140,7 @@ public class GPU: Module { } self.portalView.loadCallback(selectedGPU) - self.checkNotificationLevel(utilization) + self.notificationsView.usageCallback(utilization) self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in switch w.item { @@ -159,20 +157,4 @@ public class GPU: 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 { - removeNotification(id) - self.notificationID = nil - self.notificationLevelState = false - } else if value >= level && !self.notificationLevelState { - self.notificationID = showNotification( - title: localizedString("GPU usage threshold"), - subtitle: localizedString("GPU usage is", "\(Int((value)*100))%") - ) - self.notificationLevelState = true - } - } } diff --git a/Modules/GPU/notifications.swift b/Modules/GPU/notifications.swift new file mode 100644 index 00000000000..326c389657c --- /dev/null +++ b/Modules/GPU/notifications.swift @@ -0,0 +1,56 @@ +// +// notifications.swift +// GPU +// +// Created by Serhiy Mytrovtsiy on 05/12/2023 +// Using Swift 5.0 +// Running on macOS 14.1 +// +// Copyright © 2023 Serhiy Mytrovtsiy. All rights reserved. +// + +import Cocoa +import Kit + +class Notifications: NotificationsWrapper { + private let usageID: String = "usage" + private var usageLevel: String = "" + + public init(_ module: ModuleType) { + super.init(module, [self.usageID]) + + if Store.shared.exist(key: "\(self.module)_notificationLevel") { + let value = Store.shared.string(key: "\(self.module)_notificationLevel", defaultValue: self.usageLevel) + Store.shared.set(key: "\(self.module)_notifications_usage", value: value) + Store.shared.remove("\(self.module)_notificationLevel") + } + + self.usageLevel = Store.shared.string(key: "\(self.module)_notifications_usage", defaultValue: self.usageLevel) + + self.addArrangedSubview(selectSettingsRow( + title: localizedString("Usage"), + action: #selector(self.changeUsage), + items: notificationLevels, + selected: self.usageLevel + )) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + internal func usageCallback(_ value: Double) { + let title = localizedString("GPU usage threshold") + + if let threshold = Double(self.usageLevel) { + let subtitle = localizedString("GPU usage is", "\(Int((value)*100))%") + self.checkDouble(id: self.usageID, value: value, threshold: threshold, title: title, subtitle: subtitle) + } + } + + @objc private func changeUsage(_ sender: NSMenuItem) { + guard let key = sender.representedObject as? String else { return } + self.usageLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)" + Store.shared.set(key: "\(self.module)_notifications_usage", value: self.usageLevel) + } +} diff --git a/Modules/GPU/settings.swift b/Modules/GPU/settings.swift index 557c2a3ab3d..b7335a7da3b 100644 --- a/Modules/GPU/settings.swift +++ b/Modules/GPU/settings.swift @@ -16,7 +16,6 @@ internal class Settings: NSStackView, Settings_v { private var updateIntervalValue: Int = 1 private var selectedGPU: String private var showTypeValue: Bool = false - private var notificationLevel: String = "Disabled" private let title: String @@ -32,7 +31,6 @@ internal class Settings: NSStackView, Settings_v { self.selectedGPU = Store.shared.string(key: "\(self.title)_gpu", defaultValue: "") self.updateIntervalValue = Store.shared.int(key: "\(self.title)_updateInterval", defaultValue: self.updateIntervalValue) self.showTypeValue = Store.shared.bool(key: "\(self.title)_showType", defaultValue: self.showTypeValue) - self.notificationLevel = Store.shared.string(key: "\(self.title)_notificationLevel", defaultValue: self.notificationLevel) super.init(frame: NSRect(x: 0, y: 0, width: 0, height: 0)) @@ -71,13 +69,6 @@ internal class Settings: NSStackView, Settings_v { } self.addGPUSelector() - - self.addArrangedSubview(selectSettingsRow( - title: localizedString("Notification level"), - action: #selector(changeNotificationLevel), - items: notificationLevels, - selected: self.notificationLevel == "disabled" ? self.notificationLevel : "\(Int((Double(self.notificationLevel) ?? 0)*100))%" - )) } private func addGPUSelector() { @@ -173,14 +164,4 @@ internal class Settings: NSStackView, Settings_v { Store.shared.set(key: "\(self.title)_showType", value: self.showTypeValue) 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)") - } - } }