mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-14 00:04:15 +09:00
feat: added a notification to the GPU usage (#913)
This commit is contained in:
@@ -70,12 +70,19 @@ public class GPU: Module {
|
||||
private var popupView: Popup = Popup()
|
||||
|
||||
private var selectedGPU: String = ""
|
||||
private var notificationLevelState: Bool = false
|
||||
private var notificationID: String? = nil
|
||||
|
||||
private var showType: Bool {
|
||||
get {
|
||||
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.settingsView = Settings("GPU")
|
||||
@@ -131,6 +138,8 @@ public class GPU: Module {
|
||||
return
|
||||
}
|
||||
|
||||
self.checkNotificationLevel(utilization)
|
||||
|
||||
self.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
|
||||
switch w.item {
|
||||
case let widget as Mini:
|
||||
@@ -146,4 +155,30 @@ 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 {
|
||||
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("GPU usage threshold")
|
||||
let subtitle = localizedString("GPU 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ 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
|
||||
|
||||
@@ -31,6 +32,7 @@ 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))
|
||||
|
||||
@@ -69,6 +71,13 @@ 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() {
|
||||
@@ -164,4 +173,14 @@ 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)")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user