From 2871e98fdafdc396f93467fdd4a75a4f5549b599 Mon Sep 17 00:00:00 2001 From: Serhiy Mytrovtsiy Date: Tue, 23 Aug 2022 11:27:43 +0200 Subject: [PATCH] feat: added a new additional information option to the battery widget - Percentage inside the icon (#1034) --- Kit/Widgets/Battery.swift | 29 +++++++++++++++++++++++++++-- Kit/types.swift | 2 ++ 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/Kit/Widgets/Battery.swift b/Kit/Widgets/Battery.swift index 8796b2da..a7308302 100644 --- a/Kit/Widgets/Battery.swift +++ b/Kit/Widgets/Battery.swift @@ -8,6 +8,7 @@ // // Copyright © 2020 Serhiy Mytrovtsiy. All rights reserved. // +// swiftlint:disable function_body_length type_body_length import Cocoa @@ -58,7 +59,6 @@ public class BatterykWidget: WidgetWrapper { fatalError("init(coder:) has not been implemented") } - // swiftlint:disable function_body_length public override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) @@ -147,7 +147,12 @@ public class BatterykWidget: WidgetWrapper { if let percentage = self.percentage { let maxWidth = batterySize.width - offset*2 - borderWidth*2 - 1 - let innerWidth: CGFloat = max(1, maxWidth * CGFloat(percentage)) + var innerWidth: CGFloat = max(1, maxWidth * CGFloat(percentage)) + + if self.additional == "innerPercentage" && !self.ACStatus { + innerWidth = maxWidth + } + let innerOffset: CGFloat = -offset + borderWidth + 1 let inner = NSBezierPath(roundedRect: NSRect( x: batteryFrame.bounds.origin.x + innerOffset, @@ -155,12 +160,32 @@ public class BatterykWidget: WidgetWrapper { width: innerWidth, height: batterySize.height - offset*2 - borderWidth*2 - 1 ), xRadius: 1, yRadius: 1) + if self.lowPowerModeState { percentage.batteryColor(color: self.colorState, lowPowerMode: self.lowPowerMode).set() } else { percentage.batteryColor(color: self.colorState).set() } inner.fill() + + if self.additional == "innerPercentage" && !self.ACStatus { + let style = NSMutableParagraphStyle() + style.alignment = .center + let attributes = [ + NSAttributedString.Key.font: NSFont.systemFont(ofSize: 8, weight: .bold), + NSAttributedString.Key.foregroundColor: NSColor.clear, + NSAttributedString.Key.paragraphStyle: style + ] + + let value = "\(Int((percentage.rounded(toPlaces: 2)) * 100))" + let rect = CGRect(x: inner.bounds.origin.x, y: (Constants.Widget.height-10)/2, width: innerWidth, height: 8) + let str = NSAttributedString.init(string: value, attributes: attributes) + + ctx.saveGState() + ctx.setBlendMode(.destinationIn) + str.draw(with: rect) + ctx.restoreGState() + } } else { let attributes = [ NSAttributedString.Key.font: NSFont.systemFont(ofSize: 11, weight: .regular), diff --git a/Kit/types.swift b/Kit/types.swift index c06ca969..74d9741f 100644 --- a/Kit/types.swift +++ b/Kit/types.swift @@ -81,6 +81,8 @@ public let SpeedPictogram: [KeyValue_t] = [ public let BatteryAdditionals: [KeyValue_t] = [ KeyValue_t(key: "none", value: "None"), KeyValue_t(key: "separator", value: "separator"), + KeyValue_t(key: "innerPercentage", value: "Percentage inside the icon"), + KeyValue_t(key: "separator", value: "separator"), KeyValue_t(key: "percentage", value: "Percentage"), KeyValue_t(key: "time", value: "Time"), KeyValue_t(key: "percentageAndTime", value: "Percentage and time"),