Skip to content

Commit

Permalink
Add percentage symbol to icon
Browse files Browse the repository at this point in the history
Change tooltip to display full power info
Disable clickonce signing
  • Loading branch information
nicka101 committed Sep 9, 2018
1 parent 20cc010 commit cf7f694
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
74 changes: 64 additions & 10 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,24 +46,24 @@ 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
{
using (Icon icon = Icon.FromHandle(intPtr))
{
notifyIcon.Icon = icon;
notifyIcon.Text = batteryPercentage + "%";
notifyIcon.Text = FormatTooltip(powerStatus);
}
}
finally
Expand All @@ -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();
}
}
Expand All @@ -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);
}
}
}
2 changes: 1 addition & 1 deletion percentage/percentage/percentage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
<SignManifests>false</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
Expand Down

0 comments on commit cf7f694

Please sign in to comment.