mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: added notifications view to the Battery module
This commit is contained in:
@@ -49,6 +49,7 @@ public class Battery: Module {
|
||||
private let popupView: Popup
|
||||
private let settingsView: Settings
|
||||
private let portalView: Portal
|
||||
private let notificationsView: Notifications
|
||||
|
||||
private var usageReader: UsageReader? = nil
|
||||
private var processReader: ProcessReader? = nil
|
||||
@@ -61,11 +62,13 @@ public class Battery: Module {
|
||||
self.settingsView = Settings("Battery")
|
||||
self.popupView = Popup("Battery")
|
||||
self.portalView = Portal("Battery")
|
||||
self.notificationsView = Notifications(.battery)
|
||||
|
||||
super.init(
|
||||
popup: self.popupView,
|
||||
settings: self.settingsView,
|
||||
portal: self.portalView
|
||||
portal: self.portalView,
|
||||
notifications: self.notificationsView
|
||||
)
|
||||
guard self.available else { return }
|
||||
|
||||
@@ -107,10 +110,7 @@ public class Battery: Module {
|
||||
|
||||
public override func willTerminate() {
|
||||
guard self.isAvailable() else { return }
|
||||
|
||||
if let id = self.notificationID {
|
||||
removeNotification(id)
|
||||
}
|
||||
self.notificationsView.willTerminate()
|
||||
}
|
||||
|
||||
public override func isAvailable() -> Bool {
|
||||
@@ -122,10 +122,9 @@ public class Battery: Module {
|
||||
private func usageCallback(_ raw: Battery_Usage?) {
|
||||
guard let value = raw, self.enabled else { return }
|
||||
|
||||
self.checkLowNotification(value: value)
|
||||
self.checkHighNotification(value: value)
|
||||
self.popupView.usageCallback(value)
|
||||
self.portalView.loadCallback(value)
|
||||
self.notificationsView.usageCallback(value)
|
||||
|
||||
self.menuBar.widgets.filter{ $0.isActive }.forEach { (w: Widget) in
|
||||
switch w.item {
|
||||
@@ -152,74 +151,4 @@ public class Battery: Module {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func checkLowNotification(value: Battery_Usage) {
|
||||
let level = Store.shared.string(key: "\(self.config.name)_lowLevelNotification", defaultValue: "0.15")
|
||||
if level == "Disabled" {
|
||||
return
|
||||
}
|
||||
|
||||
guard let notificationLevel = Double(level) else {
|
||||
return
|
||||
}
|
||||
|
||||
if (value.level > notificationLevel || !value.isBatteryPowered) && self.lowLevelNotificationState {
|
||||
if value.level > notificationLevel {
|
||||
if let id = self.notificationID {
|
||||
removeNotification(id)
|
||||
self.notificationID = nil
|
||||
}
|
||||
self.lowLevelNotificationState = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if value.isCharging {
|
||||
return
|
||||
}
|
||||
|
||||
if value.level <= notificationLevel && !self.lowLevelNotificationState {
|
||||
var subtitle = localizedString("Battery remaining", "\(Int(value.level*100))")
|
||||
if value.timeToEmpty > 0 {
|
||||
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
|
||||
}
|
||||
self.notificationID = showNotification(title: localizedString("Low battery"), subtitle: subtitle)
|
||||
self.lowLevelNotificationState = true
|
||||
}
|
||||
}
|
||||
|
||||
private func checkHighNotification(value: Battery_Usage) {
|
||||
let level = Store.shared.string(key: "\(self.config.name)_highLevelNotification", defaultValue: "Disabled")
|
||||
if level == "Disabled" {
|
||||
return
|
||||
}
|
||||
|
||||
guard let notificationLevel = Double(level) else {
|
||||
return
|
||||
}
|
||||
|
||||
if (value.level < notificationLevel || value.isBatteryPowered) && self.highLevelNotificationState {
|
||||
if value.level < notificationLevel {
|
||||
if let id = self.notificationID {
|
||||
removeNotification(id)
|
||||
self.notificationID = nil
|
||||
}
|
||||
self.highLevelNotificationState = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if !value.isCharging {
|
||||
return
|
||||
}
|
||||
|
||||
if value.level >= notificationLevel && !self.highLevelNotificationState {
|
||||
var subtitle = localizedString("Battery remaining to full charge", "\(Int((1-value.level)*100))")
|
||||
if value.timeToCharge > 0 {
|
||||
subtitle += " (\(Double(value.timeToCharge*60).printSecondsToHoursMinutesSeconds()))"
|
||||
}
|
||||
self.notificationID = showNotification(title: localizedString("High battery"), subtitle: subtitle)
|
||||
self.highLevelNotificationState = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
87
Modules/Battery/notifications.swift
Normal file
87
Modules/Battery/notifications.swift
Normal file
@@ -0,0 +1,87 @@
|
||||
//
|
||||
// notifications.swift
|
||||
// Battery
|
||||
//
|
||||
// Created by Serhiy Mytrovtsiy on 17/12/2023
|
||||
// Using Swift 5.0
|
||||
// Running on macOS 14.2
|
||||
//
|
||||
// Copyright © 2023 Serhiy Mytrovtsiy. All rights reserved.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
import Kit
|
||||
|
||||
class Notifications: NotificationsWrapper {
|
||||
private let lowID: String = "low"
|
||||
private let highID: String = "high"
|
||||
private var lowLevel: String = ""
|
||||
private var highLevel: String = ""
|
||||
|
||||
public init(_ module: ModuleType) {
|
||||
super.init(module, [self.lowID, self.highID])
|
||||
|
||||
if Store.shared.exist(key: "\(self.module)_lowLevelNotification") {
|
||||
let value = Store.shared.string(key: "\(self.module)_lowLevelNotification", defaultValue: self.lowID)
|
||||
Store.shared.set(key: "\(self.module)_notifications_low", value: value)
|
||||
Store.shared.remove("\(self.module)_lowLevelNotification")
|
||||
}
|
||||
if Store.shared.exist(key: "\(self.module)_highLevelNotification") {
|
||||
let value = Store.shared.string(key: "\(self.module)_highLevelNotification", defaultValue: self.highLevel)
|
||||
Store.shared.set(key: "\(self.module)_notifications_high", value: value)
|
||||
Store.shared.remove("\(self.module)_highLevelNotification")
|
||||
}
|
||||
|
||||
self.lowLevel = Store.shared.string(key: "\(self.module)_notifications_low", defaultValue: self.lowLevel)
|
||||
self.highLevel = Store.shared.string(key: "\(self.module)_notifications_high", defaultValue: self.highLevel)
|
||||
|
||||
self.addArrangedSubview(selectSettingsRow(
|
||||
title: localizedString("Low level notification"),
|
||||
action: #selector(self.changeLowLevel),
|
||||
items: notificationLevels,
|
||||
selected: self.lowLevel
|
||||
))
|
||||
self.addArrangedSubview(selectSettingsRow(
|
||||
title: localizedString("High level notification"),
|
||||
action: #selector(self.changeHighLevel),
|
||||
items: notificationLevels,
|
||||
selected: self.highLevel
|
||||
))
|
||||
}
|
||||
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
internal func usageCallback(_ value: Battery_Usage) {
|
||||
if let threshold = Double(self.lowLevel) {
|
||||
let title = localizedString("Low battery")
|
||||
var subtitle = localizedString("Battery remaining", "\(Int(value.level*100))")
|
||||
if value.timeToEmpty > 0 {
|
||||
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
|
||||
}
|
||||
self.checkDouble(id: self.lowID, value: value.level, threshold: threshold, title: title, subtitle: subtitle, less: true)
|
||||
}
|
||||
|
||||
if let threshold = Double(self.highLevel) {
|
||||
let title = localizedString("High battery")
|
||||
var subtitle = localizedString("Battery remaining to full charge", "\(Int((1-value.level)*100))")
|
||||
if value.timeToCharge > 0 {
|
||||
subtitle += " (\(Double(value.timeToCharge*60).printSecondsToHoursMinutesSeconds()))"
|
||||
}
|
||||
self.checkDouble(id: self.lowID, value: value.level, threshold: threshold, title: title, subtitle: subtitle)
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeLowLevel(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else { return }
|
||||
self.lowLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)"
|
||||
Store.shared.set(key: "\(self.module)_notifications_low", value: self.lowLevel)
|
||||
}
|
||||
|
||||
@objc private func changeHighLevel(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else { return }
|
||||
self.highLevel = key.isEmpty ? "" : "\(Double(key) ?? 0)"
|
||||
Store.shared.set(key: "\(self.module)_notifications_high", value: self.highLevel)
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,6 @@ public class ProcessReader: Reader<[TopProcess]> {
|
||||
}
|
||||
|
||||
let task = Process()
|
||||
task.launchPath = "/bin/ps"
|
||||
task.launchPath = "/usr/bin/top"
|
||||
task.arguments = ["-o", "power", "-l", "2", "-n", "\(self.numberOfProcesses)", "-stats", "pid,command,power"]
|
||||
|
||||
|
||||
@@ -21,42 +21,6 @@ internal class Settings: NSStackView, Settings_v {
|
||||
private var button: NSPopUpButton?
|
||||
|
||||
private var numberOfProcesses: Int = 8
|
||||
private let lowLevelsList: [KeyValue_t] = [
|
||||
KeyValue_t(key: "Disabled", value: "Disabled"),
|
||||
KeyValue_t(key: "3%", value: "3%"),
|
||||
KeyValue_t(key: "5%", value: "5%"),
|
||||
KeyValue_t(key: "10%", value: "10%"),
|
||||
KeyValue_t(key: "15%", value: "15%"),
|
||||
KeyValue_t(key: "20%", value: "20%"),
|
||||
KeyValue_t(key: "25%", value: "25%"),
|
||||
KeyValue_t(key: "30%", value: "30%"),
|
||||
KeyValue_t(key: "40%", value: "40%"),
|
||||
KeyValue_t(key: "50%", value: "50%"),
|
||||
KeyValue_t(key: "60%", value: "60%")
|
||||
]
|
||||
private let highLevelsList: [KeyValue_t] = [
|
||||
KeyValue_t(key: "Disabled", value: "Disabled"),
|
||||
KeyValue_t(key: "50%", value: "50%"),
|
||||
KeyValue_t(key: "60%", value: "60%"),
|
||||
KeyValue_t(key: "70%", value: "70%"),
|
||||
KeyValue_t(key: "75%", value: "75%"),
|
||||
KeyValue_t(key: "80%", value: "80%"),
|
||||
KeyValue_t(key: "85%", value: "85%"),
|
||||
KeyValue_t(key: "90%", value: "90%"),
|
||||
KeyValue_t(key: "95%", value: "95%"),
|
||||
KeyValue_t(key: "97%", value: "97%"),
|
||||
KeyValue_t(key: "100%", value: "100%")
|
||||
]
|
||||
private var lowLevelNotification: String {
|
||||
get {
|
||||
return Store.shared.string(key: "\(self.title)_lowLevelNotification", defaultValue: "0.15")
|
||||
}
|
||||
}
|
||||
private var highLevelNotification: String {
|
||||
get {
|
||||
return Store.shared.string(key: "\(self.title)_highLevelNotification", defaultValue: "Disabled")
|
||||
}
|
||||
}
|
||||
private var timeFormat: String = "short"
|
||||
|
||||
public init(_ title: String) {
|
||||
@@ -84,20 +48,6 @@ internal class Settings: NSStackView, Settings_v {
|
||||
public func load(widgets: [widget_t]) {
|
||||
self.subviews.forEach{ $0.removeFromSuperview() }
|
||||
|
||||
self.addArrangedSubview(selectSettingsRow(
|
||||
title: localizedString("Low level notification"),
|
||||
action: #selector(changeUpdateIntervalLow),
|
||||
items: self.lowLevelsList,
|
||||
selected: self.lowLevelNotification == "Disabled" ? self.lowLevelNotification : "\(Int((Double(self.lowLevelNotification) ?? 0)*100))%"
|
||||
))
|
||||
|
||||
self.addArrangedSubview(selectSettingsRow(
|
||||
title: localizedString("High level notification"),
|
||||
action: #selector(changeUpdateIntervalHigh),
|
||||
items: self.highLevelsList,
|
||||
selected: self.highLevelNotification == "Disabled" ? self.highLevelNotification : "\(Int((Double(self.highLevelNotification) ?? 0)*100))%"
|
||||
))
|
||||
|
||||
self.addArrangedSubview(selectSettingsRowV1(
|
||||
title: localizedString("Number of top processes"),
|
||||
action: #selector(changeNumberOfProcesses),
|
||||
@@ -115,30 +65,6 @@ internal class Settings: NSStackView, Settings_v {
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeUpdateIntervalLow(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else {
|
||||
return
|
||||
}
|
||||
|
||||
if key == "Disabled" {
|
||||
Store.shared.set(key: "\(self.title)_lowLevelNotification", value: key)
|
||||
} else if let value = Double(key.replacingOccurrences(of: "%", with: "")) {
|
||||
Store.shared.set(key: "\(self.title)_lowLevelNotification", value: "\(value/100)")
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeUpdateIntervalHigh(_ sender: NSMenuItem) {
|
||||
guard let key = sender.representedObject as? String else {
|
||||
return
|
||||
}
|
||||
|
||||
if key == "Disabled" {
|
||||
Store.shared.set(key: "\(self.title)_highLevelNotification", value: key)
|
||||
} else if let value = Double(key.replacingOccurrences(of: "%", with: "")) {
|
||||
Store.shared.set(key: "\(self.title)_highLevelNotification", value: "\(value/100)")
|
||||
}
|
||||
}
|
||||
|
||||
@objc private func changeNumberOfProcesses(_ sender: NSMenuItem) {
|
||||
if let value = Int(sender.title) {
|
||||
self.numberOfProcesses = value
|
||||
|
||||
Reference in New Issue
Block a user