diff --git a/percentage/percentage/TrayIcon.cs b/percentage/percentage/TrayIcon.cs
index d3643c6..1f92680 100644
--- a/percentage/percentage/TrayIcon.cs
+++ b/percentage/percentage/TrayIcon.cs
@@ -7,11 +7,18 @@ namespace percentage
{
class TrayIcon
{
+ const string CHARGING = "Charging";
+ const string NOT_CHARGING = "Not Charging";
+ const string PLUGGED_IN = "Plugged In";
+ const string ON_BAT = "On Battery";
+ static Color TextColor = Color.White;
+ static Color BgColor = Color.Black;
+
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool DestroyIcon(IntPtr handle);
private const string iconFont = "Segoe UI";
- private const int iconFontSize = 14;
+ private const int iconFontSize = 16;
private string batteryPercentage;
private NotifyIcon notifyIcon;
@@ -39,16 +46,16 @@ public TrayIcon()
Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
- timer.Interval = 1000; // in miliseconds
+ timer.Interval = 5000; // in miliseconds
timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
- batteryPercentage = (powerStatus.BatteryLifePercent * 100).ToString();
+ batteryPercentage = FormatBatLevel(powerStatus);
- using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize), Color.White, Color.Black)))
+ using (Bitmap bitmap = new Bitmap(DrawText(batteryPercentage, new Font(iconFont, iconFontSize))))
{
System.IntPtr intPtr = bitmap.GetHicon();
try
@@ -56,7 +63,7 @@ private void timer_Tick(object sender, EventArgs e)
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
- notifyIcon.Text = batteryPercentage + "%";
+ notifyIcon.Text = FormatTooltip(powerStatus);
}
}
finally
@@ -73,20 +80,20 @@ private void menuItem_Click(object sender, EventArgs e)
Application.Exit();
}
- private Image DrawText(String text, Font font, Color textColor, Color backColor)
+ private Image DrawText(String text, Font font)
{
var textSize = GetImageSize(text, font);
Image image = new Bitmap((int) textSize.Width, (int) textSize.Height);
using (Graphics graphics = Graphics.FromImage(image))
{
- // paint the background
- graphics.Clear(backColor);
+ graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
+ graphics.Clear(BgColor);
// create a brush for the text
- using (Brush textBrush = new SolidBrush(textColor))
+ using (Brush textBrush = new SolidBrush(TextColor))
{
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
- graphics.DrawString(text, font, textBrush, 0, 0);
+ graphics.DrawString(text, font, textBrush, -2, 0);
graphics.Save();
}
}
@@ -100,5 +107,52 @@ private static SizeF GetImageSize(string text, Font font)
using (Graphics graphics = Graphics.FromImage(image))
return graphics.MeasureString(text, font);
}
+
+ private string FormatBatLevel(PowerStatus status)
+ {
+ return string.Format("{0:P0}", status.BatteryLifePercent);
+ }
+
+ private static string FormatTooltip(PowerStatus status)
+ {
+ return string.Format(
+ "{0:P0} - {1} remaining, {2}",
+ status.BatteryLifePercent,
+ HumanReadableRemainingTime(status.BatteryLifeRemaining),
+ PlugStatus(status)
+ );
+ }
+
+ private static string HumanReadableRemainingTime(int secondsRemaining)
+ {
+ if(secondsRemaining < 0)
+ {
+ return string.Format("∞");
+ }
+ int hours = 0;
+ int minutes = 0;
+ if(secondsRemaining >= 3600)
+ {
+ hours = secondsRemaining / 3600;
+ secondsRemaining = secondsRemaining % 3600;
+ }
+ if(secondsRemaining >= 60)
+ {
+ minutes = secondsRemaining / 60;
+ secondsRemaining = secondsRemaining % 60;
+ }
+ return string.Format("{0}:{1:D2}:{2:D2}", hours, minutes, secondsRemaining);
+ }
+
+ private static string PlugStatus(PowerStatus status)
+ {
+ string plugStatus = status.PowerLineStatus == PowerLineStatus.Online ? PLUGGED_IN : ON_BAT;
+ if(status.PowerLineStatus == PowerLineStatus.Offline)
+ {
+ return plugStatus;
+ }
+ string chargeStatus = status.BatteryChargeStatus == BatteryChargeStatus.Charging ? CHARGING : NOT_CHARGING;
+ return string.Format("{0}, {1}", plugStatus, chargeStatus);
+ }
}
}
diff --git a/percentage/percentage/percentage.csproj b/percentage/percentage/percentage.csproj
index 5711585..04f99f4 100644
--- a/percentage/percentage/percentage.csproj
+++ b/percentage/percentage/percentage.csproj
@@ -58,7 +58,7 @@
true
- true
+ false