Change code style to C# coding convention

This commit is contained in:
Victor 2013-10-20 13:53:56 +03:00
parent 79681415ee
commit f077a2498d
4 changed files with 130 additions and 122 deletions

View File

@ -4,11 +4,11 @@ namespace BatteryWidget {
public class Battery { public class Battery {
public static int getBatteryPercentage() { public static int GetBatteryPercentage() {
return (int) (SystemInformation.PowerStatus.BatteryLifePercent * 100); return (int) (SystemInformation.PowerStatus.BatteryLifePercent * 100);
} }
public static bool isBatteryPowerOn() { public static bool IsBatteryPowerOn() {
return (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online); return (SystemInformation.PowerStatus.PowerLineStatus == PowerLineStatus.Online);
} }
} }

View File

@ -5,44 +5,44 @@ namespace BatteryWidget {
public class BatteryGraph { public class BatteryGraph {
private const int MAX_GRAPH_POINTS = 50; private const int MaxGraphPoints = 50;
private bool isShowing; private bool _isShowing;
private List<int> percentage; private readonly List<int> _percentage;
public BatteryGraph() { public BatteryGraph() {
isShowing = false; _isShowing = false;
percentage = new List<int>(); _percentage = new List<int>();
} }
public bool graphIsShowing() { public bool GraphIsShowing() {
return isShowing; return _isShowing;
} }
public bool revertShowState() { public bool RevertShowState() {
isShowing = !isShowing; _isShowing = !_isShowing;
return isShowing; return _isShowing;
} }
public void addPercent(int currentPercentage) { public void AddPercent(int currentPercentage) {
percentage.Add(currentPercentage); _percentage.Add(currentPercentage);
while (percentage.Count >= MAX_GRAPH_POINTS) { while (_percentage.Count >= MaxGraphPoints) {
percentage.RemoveAt(0); _percentage.RemoveAt(0);
} }
} }
public void drawGraph(Graphics g) { public void DrawGraph(Graphics g) {
float width = g.VisibleClipBounds.Width; float width = g.VisibleClipBounds.Width;
float height = g.VisibleClipBounds.Height; float height = g.VisibleClipBounds.Height;
float deltaX = width / MAX_GRAPH_POINTS; float deltaX = width / MaxGraphPoints;
float x1 = 0; float x1 = 0;
float y1 = (100 - percentage[0]) * height / 100; float y1 = (100 - _percentage[0]) * height / 100;
for (int i = 1; i < percentage.Count; i++) { for (int i = 1; i < _percentage.Count; i++) {
float x2 = x1 + deltaX; 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); g.DrawLine(Pens.Silver, x1, y1, x2, y2);

View File

@ -8,8 +8,8 @@ namespace BatteryWidget {
public static class Config { public static class Config {
private const string PATH = "res"; private const string Path = "res";
private const string CONFIG = "config"; private const string ConfigFile = "config";
/// <summary> /// <summary>
/// Image state names. /// Image state names.
@ -22,59 +22,59 @@ namespace BatteryWidget {
Power Power
} }
public static string title; public static string Title;
public static Size windowSize; public static Size WindowSize;
public static Color warningStateColor, normalStateColor, powerStateColor; 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];
/// <summary> /// <summary>
/// Get image from cache or filesystem (with store to cache). /// Get image from cache or filesystem (with store to cache).
/// </summary> /// </summary>
/// <param name="name">image state name</param> /// <param name="name">image state name</param>
/// <returns>Image object</returns> /// <returns>Image object</returns>
public static Image get(Name name) { public static Image Get(Name name) {
int index = (int) name; int index = (int) name;
// Check cache availability // Check cache availability
if (cache[index] != null) return cache[index]; if (Cache[index] != null) return Cache[index];
// Try to load image from file // Try to load image from file
string resName = name.ToString().ToLower(); string resName = name.ToString().ToLower();
Image img = null; Image img;
try { try {
img = loadImage(resName); img = LoadImage(resName);
} catch (Exception) { } catch (Exception) {
ResourceManager rm = Resources.ResourceManager; ResourceManager rm = Resources.ResourceManager;
img = (Image) rm.GetObject(resName); img = (Image) rm.GetObject(resName);
} }
cache[index] = img; Cache[index] = img;
return img; return img;
} }
/// <summary> /// <summary>
/// Read config with colors, names etc. /// Read config with colors, names etc.
/// </summary> /// </summary>
public static void readConfig() { public static void ReadConfig() {
try { try {
using (var reader = new StreamReader(PATH + Path.DirectorySeparatorChar + CONFIG)) { using (var reader = new StreamReader(Path + System.IO.Path.DirectorySeparatorChar + ConfigFile)) {
// Title // Title
title = readLine(reader).Trim(); Title = ReadLine(reader).Trim();
// Size // Size
windowSize = readSize(reader); WindowSize = ReadSize(reader);
// Colors // Colors
warningStateColor = readColor(reader); WarningStateColor = ReadColor(reader);
normalStateColor = readColor(reader); NormalStateColor = ReadColor(reader);
powerStateColor = readColor(reader); PowerStateColor = ReadColor(reader);
} }
} catch (Exception) { } catch (Exception) {
// Default configuration // Default configuration
title = "Android Battery Widget"; Title = "Android Battery Widget";
windowSize = new Size(120, 69); WindowSize = new Size(120, 69);
warningStateColor = Color.Red; WarningStateColor = Color.Red;
normalStateColor = Color.DarkGreen; NormalStateColor = Color.DarkGreen;
powerStateColor = Color.Black; PowerStateColor = Color.Black;
} }
} }
@ -82,9 +82,9 @@ namespace BatteryWidget {
/// <summary> /// <summary>
/// Read image from filesystem. /// Read image from filesystem.
/// </summary> /// </summary>
private static Image loadImage(string name) { private static Image LoadImage(string name) {
string filename = PATH + Path.DirectorySeparatorChar + name + ".png"; string filename = Path + System.IO.Path.DirectorySeparatorChar + name + ".png";
using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) { using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.Read)) {
return new Bitmap(fs); return new Bitmap(fs);
} }
} }
@ -92,16 +92,16 @@ namespace BatteryWidget {
/// <summary> /// <summary>
/// Read color from stream. /// Read color from stream.
/// </summary> /// </summary>
private static Color readColor(StreamReader reader) { private static Color ReadColor(StreamReader reader) {
string line = readLine(reader); string line = ReadLine(reader);
return ColorTranslator.FromHtml(line); return ColorTranslator.FromHtml(line);
} }
/// <summary> /// <summary>
/// Read size value from stream. /// Read size value from stream.
/// </summary> /// </summary>
private static Size readSize(StreamReader reader) { private static Size ReadSize(StreamReader reader) {
string line = readLine(reader); string line = ReadLine(reader);
string[] arr = line.Split(';'); string[] arr = line.Split(';');
int width = Convert.ToInt16(arr[0]); int width = Convert.ToInt16(arr[0]);
int height = Convert.ToInt16(arr[1]); int height = Convert.ToInt16(arr[1]);
@ -111,11 +111,11 @@ namespace BatteryWidget {
/// <summary> /// <summary>
/// Read line missing comments. /// Read line missing comments.
/// </summary> /// </summary>
private static string readLine(StreamReader reader) { private static string ReadLine(StreamReader reader) {
string line; string line;
do { do {
line = reader.ReadLine(); line = reader.ReadLine();
} while ( (line.Trim().Length == 0) || (line.StartsWith("//")) ); } while ((line != null) && ( (line.Trim().Length == 0) || (line.StartsWith("//")) ));
return line; return line;
} }
} }

View File

@ -6,60 +6,60 @@ namespace BatteryWidget {
public partial class WidgetForm : Form { public partial class WidgetForm : Form {
private const int HIDE_CONTROLS_INTERVAL = 1000; private const int HideControlsInterval = 1000;
private const int UPDATE_INFO_INTERVAL = 60 * 1000; private const int UpdateInfoInterval = 60 * 1000;
/** Contol moving window */ /** Contol moving window */
private bool windowDragStatus; private bool _windowDragStatus;
private Point clickPoint; private Point _clickPoint;
private Timer updateInfoTimer; private Timer _updateInfoTimer;
private Timer hideControlsTimer; private Timer _hideControlsTimer;
private Image grayImage; private readonly Image _grayImage;
private BatteryGraph graph; private readonly BatteryGraph _graph;
#region Initialize #region Initialize
public WidgetForm() { public WidgetForm() {
InitializeComponent(); InitializeComponent();
Config.readConfig(); Config.ReadConfig();
titleLabel.Text = Config.title; titleLabel.Text = Config.Title;
this.Text = Config.title; this.Text = Config.Title;
this.Size = Config.windowSize; this.Size = Config.WindowSize;
imageLabel.Image = Config.get(Config.Name.Green); imageLabel.Image = Config.Get(Config.Name.Green);
grayImage = Config.get(Config.Name.Gray); _grayImage = Config.Get(Config.Name.Gray);
graph = new BatteryGraph(); _graph = new BatteryGraph();
initTimers(); InitTimers();
initWindow(); InitWindow();
initMouseEventHandler(); initMouseEventHandler();
updateInfo(); UpdateInfo();
} }
private void initTimers() { private void InitTimers() {
// Timer for hide window's title // Timer for hide window's title
hideControlsTimer = new Timer(); _hideControlsTimer = new Timer();
hideControlsTimer.Interval = HIDE_CONTROLS_INTERVAL; _hideControlsTimer.Interval = HideControlsInterval;
hideControlsTimer.Tick += (s1, e1) => { _hideControlsTimer.Tick += (s1, e1) => {
hideControlsTimer.Stop(); _hideControlsTimer.Stop();
titleLabel.Hide(); titleLabel.Hide();
exitLabel.Hide(); exitLabel.Hide();
}; };
// Timer for update info // Timer for update info
updateInfoTimer = new Timer(); _updateInfoTimer = new Timer();
updateInfoTimer.Interval = UPDATE_INFO_INTERVAL; _updateInfoTimer.Interval = UpdateInfoInterval;
updateInfoTimer.Tick += (s, e) => updateInfo(); _updateInfoTimer.Tick += (s, e) => UpdateInfo();
updateInfoTimer.Start(); _updateInfoTimer.Start();
} }
private void initWindow() { private void InitWindow() {
this.TransparencyKey = this.BackColor; this.TransparencyKey = this.BackColor;
this.ShowInTaskbar = false; this.ShowInTaskbar = false;
@ -76,21 +76,21 @@ namespace BatteryWidget {
titleLabel.MouseDown += (s, e) => { titleLabel.MouseDown += (s, e) => {
if (e.Button == MouseButtons.Left) { if (e.Button == MouseButtons.Left) {
// Moving mode // Moving mode
windowDragStatus = true; _windowDragStatus = true;
clickPoint = new Point(e.X, e.Y); _clickPoint = new Point(e.X, e.Y);
} else { } else {
windowDragStatus = false; _windowDragStatus = false;
} }
}; };
titleLabel.MouseMove += (s, e) => { titleLabel.MouseMove += (s, e) => {
if (windowDragStatus) { if (_windowDragStatus) {
Point pointMoveTo = this.PointToScreen(new Point(e.X, e.Y)); 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; this.Location = pointMoveTo;
} }
}; };
titleLabel.MouseUp += (s, e) => { titleLabel.MouseUp += (s, e) => {
windowDragStatus = false; _windowDragStatus = false;
Settings.Default.windowPosition = this.Location; Settings.Default.windowPosition = this.Location;
Settings.Default.Save(); Settings.Default.Save();
}; };
@ -103,29 +103,29 @@ namespace BatteryWidget {
// Show controls // Show controls
titleLabel.Show(); titleLabel.Show();
exitLabel.Show(); exitLabel.Show();
hideControlsTimer.Stop(); _hideControlsTimer.Stop();
}; };
imageLabel.MouseLeave += (s, e) => { imageLabel.MouseLeave += (s, e) => {
// Hide controls // Hide controls
hideControlsTimer.Start(); _hideControlsTimer.Start();
}; };
imageLabel.MouseClick += (s, e) => { imageLabel.MouseClick += (s, e) => {
// Change view by click: graph/image // Change view by click: graph/image
graph.revertShowState(); _graph.RevertShowState();
updateInfo(); UpdateInfo();
}; };
} }
#endregion #endregion
private void updateInfo() { private void UpdateInfo() {
int percent = Battery.getBatteryPercentage(); int percent = Battery.GetBatteryPercentage();
graph.addPercent(percent); _graph.AddPercent(percent);
// Display graph // Display graph
if (graph.graphIsShowing()) { if (_graph.GraphIsShowing()) {
Bitmap bmp = new Bitmap(Size.Width, Size.Height); Bitmap bmp = new Bitmap(Size.Width, Size.Height);
Graphics g = Graphics.FromImage(bmp); Graphics g = Graphics.FromImage(bmp);
graph.drawGraph(g); _graph.DrawGraph(g);
g.Dispose(); g.Dispose();
imageLabel.Text = ""; imageLabel.Text = "";
@ -134,7 +134,7 @@ namespace BatteryWidget {
} }
imageLabel.Text = percent + "%"; imageLabel.Text = percent + "%";
imageLabel.Image = makeImage(percent); imageLabel.Image = MakeImage(percent);
} }
protected override CreateParams CreateParams { protected override CreateParams CreateParams {
@ -146,56 +146,64 @@ namespace BatteryWidget {
} }
#region Images #region Images
private Image makeImage(int percent) { /// <summary>
/// Gets an image by battery percentage.
/// </summary>
/// <param name="percent"></param>
/// <returns></returns>
private Image MakeImage(int percent) {
if (percent <= 8) { if (percent <= 8) {
return warningStateImage(); return WarningStateImage();
} }
updateInfoTimer.Interval = UPDATE_INFO_INTERVAL; _updateInfoTimer.Interval = UpdateInfoInterval;
if (Battery.isBatteryPowerOn()) { if (Battery.IsBatteryPowerOn()) {
return powerStateImage(); return PowerStateImage();
} }
return normalStateImage(percent); return NormalStateImage(percent);
} }
private Image powerStateImage() { private Image PowerStateImage() {
imageLabel.BackColor = Config.powerStateColor; imageLabel.BackColor = Config.PowerStateColor;
return Config.get(Config.Name.Power); return Config.Get(Config.Name.Power);
} }
private Image normalStateImage(int percent) { private Image NormalStateImage(int percent) {
imageLabel.BackColor = Config.normalStateColor; imageLabel.BackColor = Config.NormalStateColor;
Bitmap bmp = new Bitmap(selectImage(percent)); Bitmap bmp = new Bitmap(SelectImage(percent));
int y = (100 - percent) * bmp.Height / 100; int y = (100 - percent) * bmp.Height / 100;
Rectangle destRect = new Rectangle(0, 0, bmp.Width, y); Rectangle destRect = new Rectangle(0, 0, bmp.Width, y);
Graphics g = Graphics.FromImage(bmp); Graphics g = Graphics.FromImage(bmp);
g.DrawImageUnscaledAndClipped(grayImage, destRect); g.DrawImageUnscaledAndClipped(_grayImage, destRect);
g.Dispose(); g.Dispose();
return bmp; return bmp;
} }
private Image warningStateImage() { private Image WarningStateImage() {
updateInfoTimer.Interval = 800; _updateInfoTimer.Interval = 800;
if (imageLabel.Image.Equals(grayImage)) { if (imageLabel.Image.Equals(_grayImage)) {
imageLabel.BackColor = Config.normalStateColor; imageLabel.BackColor = Config.NormalStateColor;
return Config.get(Config.Name.Red); return Config.Get(Config.Name.Red);
} }
imageLabel.BackColor = Config.warningStateColor; imageLabel.BackColor = Config.WarningStateColor;
return grayImage; return _grayImage;
} }
private Image selectImage(int percent) { /// <summary>
/// Select an image by battery percentage in normal state.
/// </summary>
private static Image SelectImage(int percent) {
if (percent < 30) if (percent < 30)
return Config.get(Config.Name.Red); return Config.Get(Config.Name.Red);
else if (percent < 70) if (percent < 70)
return Config.get(Config.Name.Yellow); return Config.Get(Config.Name.Yellow);
return Config.get(Config.Name.Green); return Config.Get(Config.Name.Green);
} }
#endregion #endregion
} }