Skip to content

Commit

Permalink
Whoops I did it again
Browse files Browse the repository at this point in the history
I redid it again
  • Loading branch information
noblereign authored Dec 2, 2018
1 parent f8abfc9 commit b880126
Showing 1 changed file with 2 additions and 161 deletions.
163 changes: 2 additions & 161 deletions percentage/percentage/TrayIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,6 @@
using System.Runtime.InteropServices;
using System.Windows.Forms;

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.Transparent;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool DestroyIcon(IntPtr handle);

private const string iconFont = "Segoe UI";
private const int iconFontSize = 14;

private string batteryPercentage;
private NotifyIcon notifyIcon;

public TrayIcon()
{
ContextMenu contextMenu = new ContextMenu();
MenuItem menuItem = new MenuItem();

notifyIcon = new NotifyIcon();
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace percentage
{
class TrayIcon
Expand All @@ -44,7 +13,7 @@ class TrayIcon
private const string iconFont = "Segoe UI";
private const int iconFontSize = 14;
const string CHARGING = "Charging";
const string NOT_CHARGING = "Not charging";
const string NOT_CHARGING = "Low charging power";
const string PLUGGED_IN = "Plugged in";
const string ON_BAT = "On battery";

Expand Down Expand Up @@ -120,7 +89,7 @@ private Image DrawText(String text, Font font, Color textColor, Color backColor)
// create a brush for the text
using (Brush textBrush = new SolidBrush(textColor))
{
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
graphics.DrawString(text, font, textBrush, 0, 0);
graphics.Save();
}
Expand All @@ -136,7 +105,6 @@ private static SizeF GetImageSize(string text, Font font)
return graphics.MeasureString(text, font);
}


private string FormatBatLevel(PowerStatus status)
{
return string.Format("{0:P0}", status.BatteryLifePercent);
Expand Down Expand Up @@ -185,130 +153,3 @@ private static string PlugStatus(PowerStatus status)
}
}
}

// initialize contextMenu
contextMenu.MenuItems.AddRange(new MenuItem[] { menuItem });

// initialize menuItem
menuItem.Index = 0;
menuItem.Text = "Exit";
menuItem.Click += new System.EventHandler(menuItem_Click);

notifyIcon.ContextMenu = contextMenu;

batteryPercentage = "?";

notifyIcon.Visible = true;

Timer timer = new Timer();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = 1500; // in miliseconds
timer.Start();
}

private void timer_Tick(object sender, EventArgs e)
{
PowerStatus powerStatus = SystemInformation.PowerStatus;
batteryPercentage = FormatBatLevel(powerStatus);

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 = FormatTooltip(powerStatus);
}
}
finally
{
DestroyIcon(intPtr);
}
}
}

private void menuItem_Click(object sender, EventArgs e)
{
notifyIcon.Visible = false;
notifyIcon.Dispose();
Application.Exit();
}

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(BgColor);

// create a brush for the text
using (Brush textBrush = new SolidBrush(TextColor))
{
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
graphics.DrawString(text, font, textBrush, -2, 0);
graphics.Save();
}
}

return image;
}

private static SizeF GetImageSize(string text, Font font)
{
using (Image image = new Bitmap(1, 1))
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);
}
}
}

0 comments on commit b880126

Please sign in to comment.