mirror of
https://github.com/morgan9e/macos-stats
synced 2026-04-15 00:34:08 +09:00
feat: hiding the low/high battery level notification when the battery is charged or discharged (#893)
This commit is contained in:
@@ -50,6 +50,7 @@ public class Battery: Module {
|
||||
|
||||
private var lowLevelNotificationState: Bool = false
|
||||
private var highLevelNotificationState: Bool = false
|
||||
private var notificationID: String? = nil
|
||||
|
||||
public init() {
|
||||
self.settingsView = Settings("Battery")
|
||||
@@ -97,6 +98,18 @@ public class Battery: Module {
|
||||
}
|
||||
}
|
||||
|
||||
public override func willTerminate() {
|
||||
guard self.isAvailable() else { return }
|
||||
|
||||
if let id = self.notificationID {
|
||||
if #available(macOS 10.14, *) {
|
||||
removeNotification(id)
|
||||
} else {
|
||||
removeNSNotification(id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override func isAvailable() -> Bool {
|
||||
let snapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
|
||||
let sources = IOPSCopyPowerSourcesList(snapshot).takeRetainedValue() as Array
|
||||
@@ -144,6 +157,14 @@ public class Battery: Module {
|
||||
|
||||
if (value.level > notificationLevel || value.powerSource != "Battery Power") && self.lowLevelNotificationState {
|
||||
if value.level > notificationLevel {
|
||||
if let id = self.notificationID {
|
||||
if #available(macOS 10.14, *) {
|
||||
removeNotification(id)
|
||||
} else {
|
||||
removeNSNotification(id)
|
||||
}
|
||||
self.notificationID = nil
|
||||
}
|
||||
self.lowLevelNotificationState = false
|
||||
}
|
||||
return
|
||||
@@ -161,12 +182,12 @@ public class Battery: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 10.14, *) {
|
||||
showNotification(
|
||||
self.notificationID = showNotification(
|
||||
title: title,
|
||||
subtitle: subtitle
|
||||
)
|
||||
} else {
|
||||
showNSNotification(
|
||||
self.notificationID = showNSNotification(
|
||||
title: title,
|
||||
subtitle: subtitle
|
||||
)
|
||||
@@ -188,6 +209,14 @@ public class Battery: Module {
|
||||
|
||||
if (value.level < notificationLevel || value.powerSource == "Battery Power") && self.highLevelNotificationState {
|
||||
if value.level < notificationLevel {
|
||||
if let id = self.notificationID {
|
||||
if #available(macOS 10.14, *) {
|
||||
removeNotification(id)
|
||||
} else {
|
||||
removeNSNotification(id)
|
||||
}
|
||||
self.notificationID = nil
|
||||
}
|
||||
self.highLevelNotificationState = false
|
||||
}
|
||||
return
|
||||
@@ -205,12 +234,12 @@ public class Battery: Module {
|
||||
}
|
||||
|
||||
if #available(macOS 10.14, *) {
|
||||
showNotification(
|
||||
self.notificationID = showNotification(
|
||||
title: title,
|
||||
subtitle: subtitle
|
||||
)
|
||||
} else {
|
||||
showNSNotification(
|
||||
self.notificationID = showNSNotification(
|
||||
title: title,
|
||||
subtitle: subtitle
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user