diff --git a/BatteryWidget/Battery.cs b/BatteryWidget/Battery.cs index d2e92b9..38a0999 100644 --- a/BatteryWidget/Battery.cs +++ b/BatteryWidget/Battery.cs @@ -4,11 +4,11 @@ namespace BatteryWidget { public class Battery { - public static int getBatteryPercentage() { + public static int GetBatteryPercentage() { return (int) (SystemInformation.PowerStatus.BatteryLifePercent * 100); } - public static bool isBatteryPowerOn() { + public static bool IsBatteryPowerOn() { return (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online); } } diff --git a/BatteryWidget/BatteryGraph.cs b/BatteryWidget/BatteryGraph.cs index a3e4a94..bad0d36 100644 --- a/BatteryWidget/BatteryGraph.cs +++ b/BatteryWidget/BatteryGraph.cs @@ -5,44 +5,44 @@ namespace BatteryWidget { public class BatteryGraph { - private const int MAX_GRAPH_POINTS = 50; + private const int MaxGraphPoints = 50; - private bool isShowing; - private List percentage; + private bool _isShowing; + private readonly List _percentage; public BatteryGraph() { - isShowing = false; + _isShowing = false; - percentage = new List(); + _percentage = new List(); } - public bool graphIsShowing() { - return isShowing; + public bool GraphIsShowing() { + return _isShowing; } - public bool revertShowState() { - isShowing = !isShowing; - return isShowing; + public bool RevertShowState() { + _isShowing = !_isShowing; + return _isShowing; } - public void addPercent(int currentPercentage) { - percentage.Add(currentPercentage); + public void AddPercent(int currentPercentage) { + _percentage.Add(currentPercentage); - while (percentage.Count >= MAX_GRAPH_POINTS) { - percentage.RemoveAt(0); + while (_percentage.Count >= MaxGraphPoints) { + _percentage.RemoveAt(0); } } - public void drawGraph(Graphics g) { + public void DrawGraph(Graphics g) { float width = g.VisibleClipBounds.Width; float height = g.VisibleClipBounds.Height; - float deltaX = width / MAX_GRAPH_POINTS; + float deltaX = width / MaxGraphPoints; float x1 = 0; - float y1 = (100 - percentage[0]) * height / 100; - for (int i = 1; i < percentage.Count; i++) { + float y1 = (100 - _percentage[0]) * height / 100; + for (int i = 1; i < _percentage.Count; i++) { float x2 = x1 + deltaX; - float y2 = (100 - percentage[i]) * height / 100; + float y2 = (100 - _percentage[i]) * height / 100; g.DrawLine(Pens.Silver, x1, y1, x2, y2); diff --git a/BatteryWidget/Config.cs b/BatteryWidget/Config.cs index 56c4f29..d865b17 100644 --- a/BatteryWidget/Config.cs +++ b/BatteryWidget/Config.cs @@ -8,8 +8,8 @@ namespace BatteryWidget { public static class Config { - private const string PATH = "res"; - private const string CONFIG = "config"; + private const string Path = "res"; + private const string ConfigFile = "config"; /// /// Image state names. @@ -22,59 +22,59 @@ namespace BatteryWidget { Power } - public static string title; - public static Size windowSize; - public static Color warningStateColor, normalStateColor, powerStateColor; + public static string Title; + public static Size WindowSize; + public static Color WarningStateColor, NormalStateColor, PowerStateColor; - private static Image[] cache = new Image[(int) Name.Power + 1]; + private static readonly Image[] Cache = new Image[(int) Name.Power + 1]; /// /// Get image from cache or filesystem (with store to cache). /// /// image state name /// Image object - public static Image get(Name name) { + public static Image Get(Name name) { int index = (int) name; // Check cache availability - if (cache[index] != null) return cache[index]; + if (Cache[index] != null) return Cache[index]; // Try to load image from file string resName = name.ToString().ToLower(); - Image img = null; + Image img; try { - img = loadImage(resName); + img = LoadImage(resName); } catch (Exception) { ResourceManager rm = Resources.ResourceManager; img = (Image) rm.GetObject(resName); } - cache[index] = img; + Cache[index] = img; return img; } /// /// Read config with colors, names etc. /// - public static void readConfig() { + public static void ReadConfig() { try { - using (var reader = new StreamReader(PATH + Path.DirectorySeparatorChar + CONFIG)) { + using (var reader = new StreamReader(Path + System.IO.Path.DirectorySeparatorChar + ConfigFile)) { // Title - title = readLine(reader).Trim(); + Title = ReadLine(reader).Trim(); // Size - windowSize = readSize(reader); + WindowSize = ReadSize(reader); // Colors - warningStateColor = readColor(reader); - normalStateColor = readColor(reader); - powerStateColor = readColor(reader); + WarningStateColor = ReadColor(reader); + NormalStateColor = ReadColor(reader); + PowerStateColor = ReadColor(reader); } } catch (Exception) { // Default configuration - title = "Android Battery Widget"; - windowSize = new Size(120, 69); - warningStateColor = Color.Red; - normalStateColor = Color.DarkGreen; - powerStateColor = Color.Black; + Title = "Android Battery Widget"; + WindowSize = new Size(120, 69); + WarningStateColor = Color.Red; + NormalStateColor = Color.DarkGreen; + PowerStateColor = Color.Black; } } @@ -82,9 +82,9 @@ namespace BatteryWidget { /// /// Read image from filesystem. /// - private static Image loadImage(string name) { - string filename = PATH + Path.DirectorySeparatorChar + name + ".png"; - using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { + private static Image LoadImage(string name) { + string filename = Path + System.IO.Path.DirectorySeparatorChar + name + ".png"; + using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { return new Bitmap(fs); } } @@ -92,16 +92,16 @@ namespace BatteryWidget { /// /// Read color from stream. /// - private static Color readColor(StreamReader reader) { - string line = readLine(reader); + private static Color ReadColor(StreamReader reader) { + string line = ReadLine(reader); return ColorTranslator.FromHtml(line); } /// /// Read size value from stream. /// - private static Size readSize(StreamReader reader) { - string line = readLine(reader); + private static Size ReadSize(StreamReader reader) { + string line = ReadLine(reader); string[] arr = line.Split(';'); int width = Convert.ToInt16(arr[0]); int height = Convert.ToInt16(arr[1]); @@ -111,11 +111,11 @@ namespace BatteryWidget { /// /// Read line missing comments. /// - private static string readLine(StreamReader reader) { + private static string ReadLine(StreamReader reader) { string line; do { line = reader.ReadLine(); - } while ( (line.Trim().Length == 0) || (line.StartsWith("//")) ); + } while ((line != null) && ( (line.Trim().Length == 0) || (line.StartsWith("//")) )); return line; } } diff --git a/BatteryWidget/WidgetForm.cs b/BatteryWidget/WidgetForm.cs index a66d550..9935fef 100644 --- a/BatteryWidget/WidgetForm.cs +++ b/BatteryWidget/WidgetForm.cs @@ -6,60 +6,60 @@ namespace BatteryWidget { public partial class WidgetForm : Form { - private const int HIDE_CONTROLS_INTERVAL = 1000; - private const int UPDATE_INFO_INTERVAL = 60 * 1000; + private const int HideControlsInterval = 1000; + private const int UpdateInfoInterval = 60 * 1000; /** Contol moving window */ - private bool windowDragStatus; - private Point clickPoint; + private bool _windowDragStatus; + private Point _clickPoint; - private Timer updateInfoTimer; - private Timer hideControlsTimer; + private Timer _updateInfoTimer; + private Timer _hideControlsTimer; - private Image grayImage; + private readonly Image _grayImage; - private BatteryGraph graph; + private readonly BatteryGraph _graph; #region Initialize public WidgetForm() { InitializeComponent(); - Config.readConfig(); - titleLabel.Text = Config.title; - this.Text = Config.title; - this.Size = Config.windowSize; + Config.ReadConfig(); + titleLabel.Text = Config.Title; + this.Text = Config.Title; + this.Size = Config.WindowSize; - imageLabel.Image = Config.get(Config.Name.Green); - grayImage = Config.get(Config.Name.Gray); + imageLabel.Image = Config.Get(Config.Name.Green); + _grayImage = Config.Get(Config.Name.Gray); - graph = new BatteryGraph(); + _graph = new BatteryGraph(); - initTimers(); - initWindow(); + InitTimers(); + InitWindow(); initMouseEventHandler(); - updateInfo(); + UpdateInfo(); } - private void initTimers() { + private void InitTimers() { // Timer for hide window's title - hideControlsTimer = new Timer(); - hideControlsTimer.Interval = HIDE_CONTROLS_INTERVAL; - hideControlsTimer.Tick += (s1, e1) => { - hideControlsTimer.Stop(); + _hideControlsTimer = new Timer(); + _hideControlsTimer.Interval = HideControlsInterval; + _hideControlsTimer.Tick += (s1, e1) => { + _hideControlsTimer.Stop(); titleLabel.Hide(); exitLabel.Hide(); }; // Timer for update info - updateInfoTimer = new Timer(); - updateInfoTimer.Interval = UPDATE_INFO_INTERVAL; - updateInfoTimer.Tick += (s, e) => updateInfo(); - updateInfoTimer.Start(); + _updateInfoTimer = new Timer(); + _updateInfoTimer.Interval = UpdateInfoInterval; + _updateInfoTimer.Tick += (s, e) => UpdateInfo(); + _updateInfoTimer.Start(); } - private void initWindow() { + private void InitWindow() { this.TransparencyKey = this.BackColor; this.ShowInTaskbar = false; @@ -76,21 +76,21 @@ namespace BatteryWidget { titleLabel.MouseDown += (s, e) => { if (e.Button == MouseButtons.Left) { // Moving mode - windowDragStatus = true; - clickPoint = new Point(e.X, e.Y); + _windowDragStatus = true; + _clickPoint = new Point(e.X, e.Y); } else { - windowDragStatus = false; + _windowDragStatus = false; } }; titleLabel.MouseMove += (s, e) => { - if (windowDragStatus) { + if (_windowDragStatus) { Point pointMoveTo = this.PointToScreen(new Point(e.X, e.Y)); - pointMoveTo.Offset(-clickPoint.X, -clickPoint.Y); + pointMoveTo.Offset(-_clickPoint.X, -_clickPoint.Y); this.Location = pointMoveTo; } }; titleLabel.MouseUp += (s, e) => { - windowDragStatus = false; + _windowDragStatus = false; Settings.Default.windowPosition = this.Location; Settings.Default.Save(); }; @@ -103,29 +103,29 @@ namespace BatteryWidget { // Show controls titleLabel.Show(); exitLabel.Show(); - hideControlsTimer.Stop(); + _hideControlsTimer.Stop(); }; imageLabel.MouseLeave += (s, e) => { // Hide controls - hideControlsTimer.Start(); + _hideControlsTimer.Start(); }; imageLabel.MouseClick += (s, e) => { // Change view by click: graph/image - graph.revertShowState(); - updateInfo(); + _graph.RevertShowState(); + UpdateInfo(); }; } #endregion - private void updateInfo() { - int percent = Battery.getBatteryPercentage(); - graph.addPercent(percent); + private void UpdateInfo() { + int percent = Battery.GetBatteryPercentage(); + _graph.AddPercent(percent); // Display graph - if (graph.graphIsShowing()) { + if (_graph.GraphIsShowing()) { Bitmap bmp = new Bitmap(Size.Width, Size.Height); Graphics g = Graphics.FromImage(bmp); - graph.drawGraph(g); + _graph.DrawGraph(g); g.Dispose(); imageLabel.Text = ""; @@ -134,7 +134,7 @@ namespace BatteryWidget { } imageLabel.Text = percent + "%"; - imageLabel.Image = makeImage(percent); + imageLabel.Image = MakeImage(percent); } protected override CreateParams CreateParams { @@ -146,56 +146,64 @@ namespace BatteryWidget { } #region Images - private Image makeImage(int percent) { + /// + /// Gets an image by battery percentage. + /// + /// + /// + private Image MakeImage(int percent) { if (percent <= 8) { - return warningStateImage(); + return WarningStateImage(); } - updateInfoTimer.Interval = UPDATE_INFO_INTERVAL; + _updateInfoTimer.Interval = UpdateInfoInterval; - if (Battery.isBatteryPowerOn()) { - return powerStateImage(); + if (Battery.IsBatteryPowerOn()) { + return PowerStateImage(); } - return normalStateImage(percent); + return NormalStateImage(percent); } - private Image powerStateImage() { - imageLabel.BackColor = Config.powerStateColor; - return Config.get(Config.Name.Power); + private Image PowerStateImage() { + imageLabel.BackColor = Config.PowerStateColor; + return Config.Get(Config.Name.Power); } - private Image normalStateImage(int percent) { - imageLabel.BackColor = Config.normalStateColor; + private Image NormalStateImage(int percent) { + imageLabel.BackColor = Config.NormalStateColor; - Bitmap bmp = new Bitmap(selectImage(percent)); + Bitmap bmp = new Bitmap(SelectImage(percent)); int y = (100 - percent) * bmp.Height / 100; Rectangle destRect = new Rectangle(0, 0, bmp.Width, y); Graphics g = Graphics.FromImage(bmp); - g.DrawImageUnscaledAndClipped(grayImage, destRect); + g.DrawImageUnscaledAndClipped(_grayImage, destRect); g.Dispose(); return bmp; } - private Image warningStateImage() { - updateInfoTimer.Interval = 800; - if (imageLabel.Image.Equals(grayImage)) { - imageLabel.BackColor = Config.normalStateColor; - return Config.get(Config.Name.Red); + private Image WarningStateImage() { + _updateInfoTimer.Interval = 800; + if (imageLabel.Image.Equals(_grayImage)) { + imageLabel.BackColor = Config.NormalStateColor; + return Config.Get(Config.Name.Red); } - imageLabel.BackColor = Config.warningStateColor; - return grayImage; + imageLabel.BackColor = Config.WarningStateColor; + return _grayImage; } - private Image selectImage(int percent) { + /// + /// Select an image by battery percentage in normal state. + /// + private static Image SelectImage(int percent) { if (percent < 30) - return Config.get(Config.Name.Red); - else if (percent < 70) - return Config.get(Config.Name.Yellow); - return Config.get(Config.Name.Green); + return Config.Get(Config.Name.Red); + if (percent < 70) + return Config.Get(Config.Name.Yellow); + return Config.Get(Config.Name.Green); } #endregion }