-
Notifications
You must be signed in to change notification settings - Fork 100
/
BatteryWidget.cs
51 lines (45 loc) · 1.78 KB
/
BatteryWidget.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Windows.Forms;
namespace workspacer.Bar.Widgets
{
public class BatteryWidget : BarWidgetBase
{
public Color LowChargeColor { get; set; } = Color.Red;
public Color MedChargeColor { get; set; } = Color.Yellow;
public Color HighChargeColor { get; set; } = Color.Green;
public bool HasBatteryWarning { get; set; } = true;
public double LowChargeThreshold { get; set; } = 0.10;
public double MedChargeThreshold { get; set; } = 0.50;
public int Interval { get; set; } = 5000;
private System.Timers.Timer _timer;
public override IBarWidgetPart[] GetParts()
{
PowerStatus pwr = SystemInformation.PowerStatus;
float currentBatteryCharge = pwr.BatteryLifePercent;
if (HasBatteryWarning)
{
if (currentBatteryCharge <= LowChargeThreshold)
{
return Parts(Part(currentBatteryCharge.ToString("#0%"), LowChargeColor, fontname: FontName));
}
else if (currentBatteryCharge <= MedChargeThreshold)
{
return Parts(Part(currentBatteryCharge.ToString("#0%"), MedChargeColor, fontname: FontName));
}
else
{
return Parts(Part(currentBatteryCharge.ToString("#0%"), HighChargeColor, fontname: FontName));
}
}
else
{
return Parts(Part(currentBatteryCharge.ToString("#0%"), fontname: FontName));
}
}
public override void Initialize()
{
_timer = new System.Timers.Timer(Interval);
_timer.Elapsed += (s, e) => MarkDirty();
_timer.Enabled = true;
}
}
}