feat: added a new additional information option to the battery widget - Percentage inside the icon (#1034)

This commit is contained in:
Serhiy Mytrovtsiy
2022-08-23 11:27:43 +02:00
parent ad078dee4a
commit 2871e98fda
2 changed files with 29 additions and 2 deletions

View File

@@ -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),

View File

@@ -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"),