- add an option to hide additional information when the battery is full (#138)

This commit is contained in:
Serhiy Mytrovtsiy
2020-11-14 16:21:27 +01:00
parent 133eff398f
commit 6069ea23e8
13 changed files with 76 additions and 36 deletions

View File

@@ -17,6 +17,7 @@ public class BatterykWidget: Widget {
private var timeFormat: String = "short"
private var iconState: Bool = true
private var colorState: Bool = false
private var hideAdditionalWhenFull: Bool = true
private let store: UnsafePointer<Store>?
@@ -32,13 +33,14 @@ public class BatterykWidget: Widget {
self.title = widgetTitle
self.type = .battery
self.preview = preview
self.canDrawConcurrently = true
self.wantsLayer = true
if self.store != nil {
self.additional = store!.pointee.string(key: "\(self.title)_\(self.type.rawValue)_additional", defaultValue: self.additional)
self.timeFormat = store!.pointee.string(key: "\(self.title)_timeFormat", defaultValue: self.timeFormat)
self.iconState = store!.pointee.bool(key: "\(self.title)_\(self.type.rawValue)_icon", defaultValue: self.iconState)
self.colorState = store!.pointee.bool(key: "\(self.title)_\(self.type.rawValue)_color", defaultValue: self.colorState)
self.hideAdditionalWhenFull = store!.pointee.bool(key: "\(self.title)_\(self.type.rawValue)_hideAdditionalWhenFull", defaultValue: self.hideAdditionalWhenFull)
}
if self.preview {
@@ -60,38 +62,40 @@ public class BatterykWidget: Widget {
var x: CGFloat = Constants.Widget.margin+1
let isShortTimeFormat: Bool = self.timeFormat == "short"
switch self.additional {
case "percentage":
let rowWidth = self.drawOneRow(
value: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "time":
let rowWidth = self.drawOneRow(
value: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "percentageAndTime":
let rowWidth = self.drawTwoRows(
first: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
second: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "timeAndPercentage":
let rowWidth = self.drawTwoRows(
first: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
second: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
default: break
if !self.hideAdditionalWhenFull || (self.hideAdditionalWhenFull && self.percentage != 1) {
switch self.additional {
case "percentage":
let rowWidth = self.drawOneRow(
value: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "time":
let rowWidth = self.drawOneRow(
value: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "percentageAndTime":
let rowWidth = self.drawTwoRows(
first: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
second: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
case "timeAndPercentage":
let rowWidth = self.drawTwoRows(
first: Double(self.time*60).printSecondsToHoursMinutesSeconds(short: isShortTimeFormat),
second: "\(Int((self.percentage.rounded(toPlaces: 2)) * 100))%",
x: x
).rounded(.up)
width += rowWidth + Constants.Widget.margin
x += rowWidth + Constants.Widget.margin
default: break
}
}
let w: CGFloat = 28 - (Constants.Widget.margin*2) - 4
@@ -245,19 +249,31 @@ public class BatterykWidget: Widget {
public override func settings(superview: NSView) {
let rowHeight: CGFloat = 30
let height: CGFloat = ((rowHeight + Constants.Settings.margin) * 2) + Constants.Settings.margin
let height: CGFloat = ((rowHeight + Constants.Settings.margin) * 3) + Constants.Settings.margin
superview.setFrameSize(NSSize(width: superview.frame.width, height: height))
let view: NSView = NSView(frame: NSRect(x: Constants.Settings.margin, y: Constants.Settings.margin, width: superview.frame.width - (Constants.Settings.margin*2), height: superview.frame.height - (Constants.Settings.margin*2)))
let view: NSView = NSView(frame: NSRect(
x: Constants.Settings.margin,
y: Constants.Settings.margin,
width: superview.frame.width - (Constants.Settings.margin*2),
height: superview.frame.height - (Constants.Settings.margin*2)
))
view.addSubview(SelectRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 1, width: view.frame.width, height: rowHeight),
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 2, width: view.frame.width, height: rowHeight),
title: LocalizedString("Additional information"),
action: #selector(toggleAdditional),
items: BatteryAdditionals,
selected: self.additional
))
view.addSubview(ToggleTitleRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 1, width: view.frame.width, height: rowHeight),
title: LocalizedString("Hide additional information when full"),
action: #selector(toggleHideAdditionalWhenFull),
state: self.hideAdditionalWhenFull
))
view.addSubview(ToggleTitleRow(
frame: NSRect(x: 0, y: (rowHeight + Constants.Settings.margin) * 0, width: view.frame.width, height: rowHeight),
title: LocalizedString("Colorize"),
@@ -277,6 +293,18 @@ public class BatterykWidget: Widget {
self.display()
}
@objc private func toggleHideAdditionalWhenFull(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {
state = sender is NSSwitch ? (sender as! NSSwitch).state: nil
} else {
state = sender is NSButton ? (sender as! NSButton).state: nil
}
self.hideAdditionalWhenFull = state! == .on ? true : false
self.store?.pointee.set(key: "\(self.title)_\(self.type.rawValue)_hideAdditionalWhenFull", value: self.hideAdditionalWhenFull)
self.display()
}
@objc private func toggleColor(_ sender: NSControl) {
var state: NSControl.StateValue? = nil
if #available(OSX 10.15, *) {

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "Prozentsatz und Zeit";
"Time and percentage" = "Zeit und Prozentsatz";
"Time format" = "Zeitformat";
"Hide additional information when full" = "Verstecken Zusätzliche Informationen, wenn der Akku voll ist";

View File

@@ -161,3 +161,4 @@
"Percentage and time" = "Percentage and time";
"Time and percentage" = "Time and percentage";
"Time format" = "Time format";
"Hide additional information when full" = "Hide additional information when full";

View File

@@ -157,3 +157,4 @@
"Percentage and time" = "Porcentaje y tiempo";
"Time and percentage" = "Tiempo y porcentaje";
"Time format" = "Formato de tiempo";
"Hide additional information when full" = "Ocultar información adicional cuando la batería está llena";

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "Pourcentage et temps";
"Time and percentage" = "Temps et pourcentage";
"Time format" = "Format de l'heure";
"Hide additional information when full" = "Masquer les informations supplémentaires lorsque la batterie est pleine";

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "비율과 시간";
"Time and percentage" = "시간과 비율";
"Time format" = "시간 형식";
"Hide additional information when full" = "배터리가 가득 차면 추가 정보 숨기기";

View File

@@ -161,3 +161,4 @@
"Percentage and time" = "Procenty i czas";
"Time and percentage" = "Czas i procenty";
"Time format" = "Format czasu";
"Hide additional information when full" = "Ukryj dodatkowe informacje, gdy bateria jest naładowana";

View File

@@ -161,3 +161,4 @@
"Percentage and time" = "Проценты и время";
"Time and percentage" = "Время и Проценты";
"Time format" = "Формат времени";
"Hide additional information when full" = "Скрыть дополнительную информацию при полном заряде";

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "Yüzde ve zaman";
"Time and percentage" = "Zaman ve yüzde";
"Time format" = "Zaman formatı";
"Hide additional information when full" = "Pil dolduğunda ek bilgileri gizle";

View File

@@ -161,3 +161,4 @@
"Percentage and time" = "Проценти і час";
"Time and percentage" = "Час і проценти";
"Time format" = "Формат часу";
"Hide additional information when full" = "Приховати додаткову інформацію, якщо акумулятор заряджений";

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "Phần trăm và thời gian";
"Time and percentage" = "Thời gian và tỷ lệ phần trăm";
"Time format" = "Định dạng thời gian";
"Hide additional information when full" = "Ẩn thông tin bổ sung khi pin đầy";

View File

@@ -160,3 +160,4 @@
"Percentage and time" = "百分比和时间";
"Time and percentage" = "时间和百分比";
"Time format" = "时间格式";
"Hide additional information when full" = "电池充满后隐藏其他信息";

View File

@@ -156,3 +156,4 @@
"Percentage and time" = "百分比和時間";
"Time and percentage" = "時間和百分比";
"Time format" = "時間格式";
"Hide additional information when full" = "電池充滿後隱藏其他信息";