Skip to content

Commit

Permalink
- add lightning to battery when power cable connected (now the batter…
Browse files Browse the repository at this point in the history
…y widget have the same behavior as default macos)

- fix battery reader (app gets to know that AC is connected after the second read)
- updated selection in Network and Battery module
- update default color for mini widget in battery module
  • Loading branch information
exelban committed Jul 18, 2020
1 parent ad60478 commit 1d09a11
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 29 deletions.
84 changes: 67 additions & 17 deletions ModuleKit/Widgets/Battery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import StatsKit

public enum battery_additional_t: String {
case none = "None"
case separator_1 = "separator_1"
case percentage = "Percentage"
case time = "Time"
}
Expand All @@ -29,6 +30,7 @@ public class BatterykWidget: Widget {
private var percentage: Double = 1
private var time: Int = 0
private var charging: Bool = false
private var ACStatus: Bool = false

public init(preview: Bool, title: String, config: NSDictionary?, store: UnsafePointer<Store>?) {
let widgetTitle: String = title
Expand Down Expand Up @@ -61,7 +63,7 @@ public class BatterykWidget: Widget {
super.draw(dirtyRect)

var width: CGFloat = 30
var x: CGFloat = Constants.Widget.margin
var x: CGFloat = Constants.Widget.margin+1

let stringAttributes = [
NSAttributedString.Key.font: NSFont.systemFont(ofSize: 12, weight: .regular),
Expand Down Expand Up @@ -89,45 +91,93 @@ public class BatterykWidget: Widget {
x += stringWidth + Constants.Widget.margin
}

let w: CGFloat = 30 - (Constants.Widget.margin*2) - 4
let w: CGFloat = 28 - (Constants.Widget.margin*2) - 4
let h: CGFloat = 11
let y: CGFloat = (dirtyRect.size.height - h) / 2
let batteryFrame = NSBezierPath(roundedRect: NSRect(x: x+1, y: y, width: w, height: h), xRadius: 1, yRadius: 1)

if self.charging {
NSColor.systemGreen.set()
} else {
NSColor.textColor.set()
}
let batteryFrame = NSBezierPath(roundedRect: NSRect(x: x+1, y: y, width: w, height: h), xRadius: 1.5, yRadius: 1.5)

let bPX: CGFloat = x+w+1
let bPY: CGFloat = (dirtyRect.size.height / 2) - 2
let batteryPoint = NSBezierPath(roundedRect: NSRect(x: bPX, y: bPY, width: 2, height: 4), xRadius: 1, yRadius: 1)
NSColor.textColor.set()
batteryPoint.lineWidth = 1.1
batteryPoint.stroke()
batteryPoint.fill()

batteryFrame.lineWidth = 1
batteryFrame.stroke()

let maxWidth = w - 3
let inner = NSBezierPath(roundedRect: NSRect(x: x+2.5, y: y+1.5, width: maxWidth * CGFloat(self.percentage), height: h-3), xRadius: 0.5, yRadius: 0.5)
self.percentage.batteryColor(color: self.colorState).set()
inner.lineWidth = 0
inner.stroke()
inner.close()
inner.fill()
if !self.charging || !self.ACStatus {
let maxWidth = w - 3
let innerWidth: CGFloat = self.ACStatus ? maxWidth : maxWidth * CGFloat(self.percentage)
let inner = NSBezierPath(roundedRect: NSRect(x: x+2.5, y: y+1.5, width: innerWidth, height: h-3), xRadius: 0.5, yRadius: 0.5)
self.percentage.batteryColor(color: self.colorState).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 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),
] : [
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),
CGPoint(x: batteryCenter.x+2, y: maxY),
CGPoint(x: minX, y: batteryCenter.y-1.5),
CGPoint(x: batteryCenter.x-1, y: batteryCenter.y-1.5),
]

let linePath = NSBezierPath()
linePath.move(to: CGPoint(x: points[0].x, y: points[0].y))
for i in 1..<points.count {
linePath.line(to: CGPoint(x: points[i].x, y: points[i].y))
}
linePath.line(to: CGPoint(x: points[0].x, y: points[0].y))

NSColor.textColor.set()
linePath.fill()

let ctx = NSGraphicsContext.current!.cgContext
ctx.saveGState()
ctx.setBlendMode(.destinationOut)

NSColor.orange.set()
linePath.lineWidth = self.charging ? 2 : 1
linePath.stroke()

ctx.restoreGState()
}

self.setWidth(width)
}

public func setValue(percentage: Double, isCharging: Bool, time: Int) {
public func setValue(percentage: Double, ACStatus: Bool, isCharging: Bool, time: Int) {
var updated: Bool = false

if self.percentage != percentage {
self.percentage = abs(percentage)
updated = true
}
if self.ACStatus != ACStatus {
self.ACStatus = ACStatus
updated = true
}
if self.charging != isCharging {
self.charging = isCharging
updated = true
Expand Down
3 changes: 2 additions & 1 deletion ModuleKit/Widgets/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import Cocoa
import StatsKit

public enum network_icon_t: String {
case none = ""
case none = "None"
case separator = "separator"
case dot = "Dots"
case arrow = "Arrows"
case char = "Character"
Expand Down
6 changes: 6 additions & 0 deletions Modules/Battery/config.plist
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
<false/>
<key>Title</key>
<string>BAT</string>
<key>Color</key>
<string>monochrome</string>
<key>Preview</key>
<dict>
<key>Label</key>
Expand All @@ -25,6 +27,10 @@
<key>Value</key>
<string>0.72</string>
</dict>
<key>Unsupported colors</key>
<array>
<string>pressure</string>
</array>
</dict>
<key>battery</key>
<dict>
Expand Down
16 changes: 9 additions & 7 deletions Modules/Battery/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct Battery_Usage: value_t {
var powerSource: String = ""
var state: String = ""
var isCharged: Bool = false
var isCharging: Bool = false
var level: Double = 0
var cycles: Int = 0
var health: Int = 0
Expand All @@ -27,7 +28,6 @@ struct Battery_Usage: value_t {
var temperature: Double = 0

var ACwatts: Int = 0
var ACstatus: Bool = false

var timeToEmpty: Int = 0
var timeToCharge: Int = 0
Expand Down Expand Up @@ -90,7 +90,8 @@ public class Battery: Module {
if let widget = self.widget as? BatterykWidget {
widget.setValue(
percentage: value?.level ?? 0,
isCharging: value?.level == 100 ? true : value!.level > 0,
ACStatus: value?.powerSource != "Battery Power",
isCharging: value?.isCharging ?? false,
time: (value?.timeToEmpty == 0 && value?.timeToCharge != 0 ? value?.timeToCharge : value?.timeToEmpty) ?? 0
)
}
Expand All @@ -102,11 +103,12 @@ public class Battery: Module {
return
}

var subtitle = "\((Int(value.level*100)))% remaining"
if value.timeToEmpty != 0 {
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
}
if let notificationLevel = Double(level), value.level <= notificationLevel {
if let notificationLevel = Double(level), abs(value.level) <= notificationLevel {
var subtitle = "\((Int(abs(value.level)*100)))% remaining"
if value.timeToEmpty != 0 {
subtitle += " (\(Double(value.timeToEmpty*60).printSecondsToHoursMinutesSeconds()))"
}

showNotification(
title: "Low battery",
subtitle: subtitle,
Expand Down
2 changes: 1 addition & 1 deletion Modules/Battery/readers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ internal class UsageReader: Reader<Battery_Usage> {
}
}
self.usage.ACwatts = ACwatts
self.usage.ACstatus = self.getBoolValue("IsCharging" as CFString) ?? false

if self.usage.powerSource == "Battery Power" {
cap = 0 - cap
}
self.usage.level = cap
self.usage.isCharging = self.getBoolValue("IsCharging" as CFString) ?? false

DispatchQueue.main.async(execute: {
self.callback(self.usage)
Expand Down
17 changes: 14 additions & 3 deletions StatsKit/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,10 +354,21 @@ public extension NSView {
let select: NSPopUpButton = NSPopUpButton(frame: NSRect(x: row.frame.width - 50, y: (row.frame.height-26)/2, width: 50, height: 26))
select.target = self
select.action = action
items.forEach { (item: String) in
select.addItem(withTitle: item == "" ? "None" : item)

let menu = NSMenu()
items.forEach { (color: String) in
if color.contains("separator") {
menu.addItem(NSMenuItem.separator())
} else {
let interfaceMenu = NSMenuItem(title: color, action: nil, keyEquivalent: "")
menu.addItem(interfaceMenu)
if selected == color {
interfaceMenu.state = .on
}
}
}
select.selectItem(withTitle: selected)

select.menu = menu
select.sizeToFit()

rowTitle.setFrameSize(NSSize(width: row.frame.width - select.frame.width, height: rowTitle.frame.height))
Expand Down

0 comments on commit 1d09a11

Please sign in to comment.