From 383f4195bb129c363b74b24f641e9da3ce66549b Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Fri, 24 Jul 2020 10:28:14 +0200 Subject: [PATCH] - fix battery charging state in a popup view - add a visualization of battery level when charging (vertical bar) --- ModuleKit/Widgets/Battery.swift | 30 ++++++++++++++++-------------- Modules/Battery/popup.swift | 2 +- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/ModuleKit/Widgets/Battery.swift b/ModuleKit/Widgets/Battery.swift index 9824f5a6..3eeca4c9 100644 --- a/ModuleKit/Widgets/Battery.swift +++ b/ModuleKit/Widgets/Battery.swift @@ -106,7 +106,7 @@ public class BatterykWidget: Widget { batteryFrame.lineWidth = 1 batteryFrame.stroke() - + if !self.charging || !self.ACStatus { let maxWidth = w - 3 let innerWidth: CGFloat = self.ACStatus ? maxWidth : maxWidth * CGFloat(self.percentage) @@ -116,25 +116,27 @@ public class BatterykWidget: Widget { inner.stroke() inner.close() inner.fill() + } else if self.charging { + let maxHeight = h - 3 + let height: CGFloat = maxHeight * CGFloat(self.percentage) + let inner = NSBezierPath(roundedRect: NSRect(x: x+2.5, y: y+1.5, width: w-3, height: height), xRadius: 0.5, yRadius: 0.5) + (self.percentage == 1 ? NSColor.textColor : NSColor.systemGreen).set() + inner.lineWidth = 0 + inner.stroke() + inner.close() + inner.fill() } if self.ACStatus { let batteryCenter: CGPoint = CGPoint(x: x+1+(w/2), y: y+(h/2)) let boltSize: CGSize = CGSize(width: 8, height: h+3+4) - let minX = batteryCenter.x - (boltSize.width/2) - (self.charging ? 1 : 0) - let maxX = batteryCenter.x + (boltSize.width/2) + (self.charging ? 1 : 0) - let minY = batteryCenter.y - (boltSize.height/2) - (self.charging ? 4 : 0) - let maxY = batteryCenter.y + (boltSize.height/2) + (self.charging ? 4 : 0) + let minX = batteryCenter.x - (boltSize.width/2) + let maxX = batteryCenter.x + (boltSize.width/2) + let minY = batteryCenter.y - (boltSize.height/2) + let maxY = batteryCenter.y + (boltSize.height/2) - let points: [CGPoint] = self.charging ? [ - CGPoint(x: batteryCenter.x-3, y: minY), - CGPoint(x: maxX, y: batteryCenter.y+2), - CGPoint(x: batteryCenter.x+1, y: batteryCenter.y+2), - CGPoint(x: batteryCenter.x+3, y: maxY), - CGPoint(x: minX, y: batteryCenter.y-2), - CGPoint(x: batteryCenter.x-1, y: batteryCenter.y-2), - ] : [ + let points: [CGPoint] = [ CGPoint(x: batteryCenter.x-2, y: minY), CGPoint(x: maxX, y: batteryCenter.y+1.5), CGPoint(x: batteryCenter.x+1, y: batteryCenter.y+1.5), @@ -158,7 +160,7 @@ public class BatterykWidget: Widget { ctx.setBlendMode(.destinationOut) NSColor.orange.set() - linePath.lineWidth = self.charging ? 2 : 1 + linePath.lineWidth = 1 linePath.stroke() ctx.restoreGState() diff --git a/Modules/Battery/popup.swift b/Modules/Battery/popup.swift index 24f59cb2..06bc5540 100644 --- a/Modules/Battery/popup.swift +++ b/Modules/Battery/popup.swift @@ -162,7 +162,7 @@ internal class Popup: NSView { self.temperatureField?.stringValue = "\(value.temperature) °C" self.powerField?.stringValue = value.powerSource == "Battery Power" ? "Not connected" : "\(value.ACwatts) W" - self.chargingStateField?.stringValue = value.level > 0 ? "Yes" : "No" + self.chargingStateField?.stringValue = value.isCharging ? "Yes" : "No" }) } }