From c23375309b90fd16c9e3ce31da10c1f37c0d6b9e Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Sat, 20 Aug 2022 13:27:44 +0200 Subject: [PATCH] feat: added a low power mode colorizing to the Battery widget (#1024) --- Kit/Widgets/Battery.swift | 9 +++++++-- Kit/extensions.swift | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/Kit/Widgets/Battery.swift b/Kit/Widgets/Battery.swift index 00f27f58..091e1cc7 100644 --- a/Kit/Widgets/Battery.swift +++ b/Kit/Widgets/Battery.swift @@ -22,6 +22,7 @@ public class BatterykWidget: WidgetWrapper { private var time: Int = 0 private var charging: Bool = false private var ACStatus: Bool = false + private var lowPowerMode: Bool = false public init(title: String, config: NSDictionary?, preview: Bool = false) { let widgetTitle: String = title @@ -152,7 +153,7 @@ public class BatterykWidget: WidgetWrapper { width: innerWidth, height: batterySize.height - offset*2 - borderWidth*2 - 1 ), xRadius: 1, yRadius: 1) - percentage.batteryColor(color: self.colorState).set() + percentage.batteryColor(color: self.colorState, lowPowerMode: self.lowPowerMode).set() inner.fill() } else { let attributes = [ @@ -292,7 +293,7 @@ public class BatterykWidget: WidgetWrapper { return rowWidth } - public func setValue(percentage: Double? = nil, ACStatus: Bool? = nil, isCharging: Bool? = nil, time: Int? = nil) { + public func setValue(percentage: Double? = nil, ACStatus: Bool? = nil, isCharging: Bool? = nil, lowPowerMode: Bool? = nil, time: Int? = nil) { var updated: Bool = false let timeFormat: String = Store.shared.string(key: "\(self.title)_timeFormat", defaultValue: self.timeFormat) @@ -316,6 +317,10 @@ public class BatterykWidget: WidgetWrapper { self.timeFormat = timeFormat updated = true } + if let state = lowPowerMode, self.lowPowerMode != state { + self.lowPowerMode = state + updated = true + } if updated { DispatchQueue.main.async(execute: { diff --git a/Kit/extensions.swift b/Kit/extensions.swift index 04072ae3..7bf0c7c2 100644 --- a/Kit/extensions.swift +++ b/Kit/extensions.swift @@ -173,7 +173,11 @@ public extension Double { } } - func batteryColor(color: Bool = false) -> NSColor { + func batteryColor(color: Bool = false, lowPowerMode: Bool = false) -> NSColor { + if lowPowerMode { + return NSColor.systemOrange + } + switch self { case 0.2...0.4: if !color {