fix battery widget; fix widget change; fix battery reader;

This commit is contained in:
Serhiy Mytrovtsiy
2020-01-15 16:13:31 +01:00
parent ef9c59a6d4
commit 66969e1766
5 changed files with 24 additions and 11 deletions

View File

@@ -97,6 +97,10 @@ extension Module {
widget.shortName = String(self.name.prefix(3)).uppercased()
widget.Init()
self.readers.forEach { reader in
reader.read()
}
self.widget.view = widget as! NSView
}
}

View File

@@ -26,7 +26,7 @@ class NetworkReader: Reader {
self.read()
}
}
public func read() {
if !self.enabled && self.initialized { return }
self.initialized = true
@@ -44,13 +44,14 @@ class NetworkReader: Reader {
continue
}
pointer = pointer!.pointee.ifa_next
upload = info[0]
download = info[1]
upload += info[0]
download += info[1]
}
freeifaddrs(interfaceAddresses)
let lastUpload = self.uploadValue
let lastDownload = self.downloadValue
if lastUpload != 0 && lastDownload != 0 {
DispatchQueue.main.async(execute: {
self.callback([Double(download - lastDownload), Double(upload - lastUpload)])

View File

@@ -47,8 +47,8 @@ class BatteryPercentageWidget: BatteryWidget {
override func update() {
if self.value == 0 { return }
if percentageValue.isHidden {
percentageValue.isHidden = false
if self.percentageValue.isHidden {
self.percentageValue.isHidden = false
}
self.percentageValue.stringValue = "\(Int(self.value * 100))%"

View File

@@ -34,17 +34,25 @@ class BatteryTimeWidget: BatteryWidget {
timeValue.alignment = .right
timeValue.font = NSFont.systemFont(ofSize: 11, weight: .regular)
timeValue.stringValue = (self.time*60).printSecondsToHoursMinutesSeconds()
if self.time == 0 {
timeValue.isHidden = true
}
self.addSubview(timeValue)
}
override func update() {
if self.time <= 0 && self.size == self.batterySize + timeWidth {
if self.value == 0 { return }
if self.timeValue.isHidden {
self.timeValue.isHidden = false
}
self.timeValue.stringValue = (self.time*60).printSecondsToHoursMinutesSeconds()
if self.time == 0 && self.size == self.batterySize + timeWidth {
self.changeWidth(width: 0)
} else if self.time >= 0 || self.size != self.batterySize + timeWidth {
} else if self.time > 0 && self.size != self.batterySize + timeWidth {
self.changeWidth(width: timeWidth)
}
self.timeValue.stringValue = (self.time*60).printSecondsToHoursMinutesSeconds()
}
}

View File

@@ -107,7 +107,7 @@ public struct Units {
public func getReadableSpeed() -> String {
switch bytes {
case 0..<1_024:
return "0 KB"
return "0 KB/s"
case 1_024..<(1_024 * 1_024):
return String(format: "%.0f KB/s", kilobytes)
case 1_024..<(1_024 * 1_024 * 100):
@@ -124,7 +124,7 @@ public struct Units {
public func getReadableMemory() -> String {
switch bytes {
case 0..<1_024:
return "0 KB"
return "0 KB/s"
case 1_024..<(1_024 * 1_024):
return String(format: "%.0f KB", kilobytes)
case 1_024..<(1_024 * 1_024 * 1_024):