Skip to content

Commit 1d9cb54

Browse files
committed
use sergiye.Common IconFactory
1 parent 77a4a86 commit 1d9cb54

File tree

3 files changed

+13
-460
lines changed

3 files changed

+13
-460
lines changed

OpenHardwareMonitor/UI/SensorNotifyIcon.cs

Lines changed: 13 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,23 @@
11
using System;
22
using System.Drawing;
3-
using System.Drawing.Drawing2D;
4-
using System.Drawing.Imaging;
5-
using System.Drawing.Text;
63
using System.Linq;
7-
using System.Runtime.InteropServices;
84
using System.Windows.Forms;
95
using OpenHardwareMonitor.Hardware;
10-
using OpenHardwareMonitor.Utilities;
116

127
namespace OpenHardwareMonitor.UI;
138

149
public class SensorNotifyIcon : IDisposable
1510
{
1611
private readonly NotifyIconAdv _notifyIcon;
17-
private readonly Bitmap _bitmap;
18-
private readonly Graphics _graphics;
19-
private Color _color;
20-
private Color _darkColor;
21-
private Brush _brush;
22-
private Brush _darkBrush;
23-
private Pen _pen;
24-
private readonly Font _font;
25-
private readonly Font _smallFont;
12+
private IconFactory _iconFactory;
2613

2714
public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor, PersistentSettings settings)
2815
{
2916
Sensor = sensor;
3017
_notifyIcon = new NotifyIconAdv();
18+
_iconFactory = new IconFactory();
19+
_iconFactory.Color = settings.GetValue(new Identifier(sensor.Identifier, "traycolor").ToString(), _iconFactory.Color);
3120

32-
//Color defaultColor = Color.White;
33-
//if (sensor.SensorType == SensorType.Load || sensor.SensorType == SensorType.Control || sensor.SensorType == SensorType.Level)
34-
// defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
35-
//todo: set defaultColor depending on the taskbar color
36-
var defaultColor = Color.FromArgb(0xff, 0x00, 0xff, 0xff);
37-
//if (sensor.SensorType == SensorType.Load ||
38-
// sensor.SensorType == SensorType.Control ||
39-
// sensor.SensorType == SensorType.Level)
40-
// defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
41-
Color = settings.GetValue(new Identifier(sensor.Identifier, "traycolor").ToString(), defaultColor);
42-
43-
_pen = new Pen(Color.FromArgb(96, Color.Black));
4421
var contextMenuStrip = new ContextMenuStrip();
4522
contextMenuStrip.Renderer = new ThemedToolStripRenderer();
4623
var hideShowItem = new ToolStripMenuItem("Hide/Show");
@@ -59,11 +36,12 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor, PersistentS
5936
var colorItem = new ToolStripMenuItem("Change Color...");
6037
colorItem.Click += delegate
6138
{
62-
ColorDialog dialog = new ColorDialog { Color = Color };
63-
if (dialog.ShowDialog() == DialogResult.OK)
39+
using (ColorDialog dialog = new ColorDialog { Color = _iconFactory.Color })
6440
{
65-
Color = dialog.Color;
66-
settings.SetValue(new Identifier(sensor.Identifier, "traycolor").ToString(), Color);
41+
if (dialog.ShowDialog() != DialogResult.OK)
42+
return;
43+
_iconFactory.Color = dialog.Color;
44+
settings.SetValue(new Identifier(sensor.Identifier, "traycolor").ToString(), _iconFactory.Color);
6745
}
6846
};
6947
contextMenuStrip.Items.Add(colorItem);
@@ -79,69 +57,17 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor, PersistentS
7957
{
8058
sensorSystemTray.SendHideShowCommand();
8159
};
82-
83-
// get the default dpi to create an icon with the correct size
84-
float dpiX, dpiY;
85-
using (Bitmap b = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
86-
{
87-
dpiX = b.HorizontalResolution;
88-
dpiY = b.VerticalResolution;
89-
}
90-
91-
// adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
92-
var width = Math.Max(16, (int)Math.Round(16 * dpiX / 96));
93-
var height = Math.Max(16, (int)Math.Round(16 * dpiY / 96));
94-
95-
// adjust the font size to the icon size
96-
_font = new Font("Arial", width == 16 ? 10.0F : 8.0F * dpiX / 96);
97-
_smallFont = new Font("Arial", width == 16 ? 8.0F : 6.0F * dpiX / 96);
98-
99-
_bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
100-
_graphics = Graphics.FromImage(_bitmap);
101-
if (Environment.OSVersion.Version.Major > 5)
102-
{
103-
_graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
104-
_graphics.SmoothingMode = SmoothingMode.AntiAlias;
105-
_graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
106-
_graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
107-
}
10860
}
10961

11062
public ISensor Sensor { get; }
11163

112-
public Color Color
113-
{
114-
get { return _color; }
115-
set
116-
{
117-
_color = value;
118-
_darkColor = Color.FromArgb(255, _color.R / 3, _color.G / 3, _color.B / 3);
119-
Brush brush = _brush;
120-
_brush = new SolidBrush(_color);
121-
brush?.Dispose();
122-
Pen pen = _pen;
123-
_pen = new Pen(Color.FromArgb(96, _color), 1);
124-
pen?.Dispose();
125-
Brush darkBrush = _darkBrush;
126-
_darkBrush = new SolidBrush(_darkColor);
127-
darkBrush?.Dispose();
128-
}
129-
}
130-
13164
public void Dispose()
13265
{
13366
Icon icon = _notifyIcon.Icon;
13467
_notifyIcon.Icon = null;
13568
icon?.Destroy();
13669
_notifyIcon.Dispose();
137-
138-
_brush?.Dispose();
139-
_darkBrush?.Dispose();
140-
_pen.Dispose();
141-
_graphics.Dispose();
142-
_bitmap.Dispose();
143-
_font.Dispose();
144-
_smallFont.Dispose();
70+
_iconFactory.Dispose();
14571
}
14672

14773
private string GetString()
@@ -203,110 +129,6 @@ private static string GetThroughputValue(float byteCount, byte preferredSufNum =
203129
return new string(result.Take(3).ToArray());
204130
}
205131

206-
private Icon CreateTransparentIcon()
207-
{
208-
string text = GetString();
209-
//int count = 0;
210-
//for (int i = 0; i < text.Length; i++)
211-
// if ((text[i] >= '0' && text[i] <= '9') || text[i] == '-')
212-
// count++;
213-
//bool small = count > 2;
214-
var small = text.Length > 2;
215-
216-
_graphics.Clear(Color.Black);
217-
//Rectangle bounds = new Rectangle(Point.Empty, _bitmap.Size);
218-
//TextRenderer.DrawText(_graphics, text, small ? _smallFont : _font,
219-
// bounds, Color.White, Color.Black, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
220-
var defaultBackColor = Color.Transparent;
221-
var transparentIcon = false;
222-
try
223-
{
224-
_graphics.Clear(defaultBackColor);
225-
}
226-
catch (Exception)
227-
{
228-
try
229-
{
230-
defaultBackColor = SystemTools.GetTaskbarColor();
231-
}
232-
catch (Exception)
233-
{
234-
defaultBackColor = Color.Black;
235-
transparentIcon = true;
236-
}
237-
_graphics.Clear(defaultBackColor);
238-
}
239-
240-
if (small)
241-
{
242-
if (text[1] == '.' || text[1] == ',')
243-
{
244-
var bigPart = text.Substring(0, 1);
245-
var smallPart = text.Substring(1);
246-
TextRenderer.DrawText(_graphics, bigPart, _font, new Point(-_bitmap.Width / 4, _bitmap.Height / 2), _color, defaultBackColor, TextFormatFlags.VerticalCenter);
247-
TextRenderer.DrawText(_graphics, smallPart, _smallFont, new Point(_bitmap.Width / 4, _bitmap.Height), _color, defaultBackColor, TextFormatFlags.Bottom);
248-
}
249-
else
250-
{
251-
var size = TextRenderer.MeasureText(text, _smallFont);
252-
TextRenderer.DrawText(_graphics, text, _smallFont, new Point((_bitmap.Width - size.Width) / 2, _bitmap.Height / 2), _color, defaultBackColor, TextFormatFlags.VerticalCenter);
253-
}
254-
}
255-
else
256-
{
257-
var size = TextRenderer.MeasureText(text, _font);
258-
TextRenderer.DrawText(_graphics, text, _font, new Point((_bitmap.Width - size.Width) / 2, _bitmap.Height / 2), _color, defaultBackColor, TextFormatFlags.VerticalCenter);
259-
}
260-
261-
BitmapData data = _bitmap.LockBits(
262-
new Rectangle(0, 0, _bitmap.Width, _bitmap.Height),
263-
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
264-
265-
IntPtr Scan0 = data.Scan0;
266-
267-
int numBytes = _bitmap.Width * _bitmap.Height * 4;
268-
byte[] bytes = new byte[numBytes];
269-
Marshal.Copy(Scan0, bytes, 0, numBytes);
270-
_bitmap.UnlockBits(data);
271-
272-
if (transparentIcon)
273-
{
274-
byte red, green, blue;
275-
for (int i = 0; i < bytes.Length; i += 4)
276-
{
277-
blue = bytes[i];
278-
green = bytes[i + 1];
279-
red = bytes[i + 2];
280-
281-
bytes[i] = _color.B;
282-
bytes[i + 1] = _color.G;
283-
bytes[i + 2] = _color.R;
284-
bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
285-
}
286-
}
287-
return IconFactory.Create(bytes, _bitmap.Width, _bitmap.Height,
288-
PixelFormat.Format32bppArgb);
289-
}
290-
291-
private Icon CreatePercentageIcon()
292-
{
293-
try
294-
{
295-
_graphics.Clear(Color.Transparent);
296-
}
297-
catch (ArgumentException)
298-
{
299-
_graphics.Clear(Color.Black);
300-
}
301-
_graphics.FillRectangle(_darkBrush, 0.5f, -0.5f, _bitmap.Width - 2, _bitmap.Height);
302-
float value = Sensor.Value.GetValueOrDefault();
303-
float y = (float)(_bitmap.Height * 0.01f) * (100 - value);
304-
_graphics.FillRectangle(_brush, 2, 2 + y, _bitmap.Width - 5, _bitmap.Height - 4 - y);
305-
_graphics.DrawRectangle(_pen, 1, 1, _bitmap.Width - 3, _bitmap.Height - 2);
306-
307-
return IconFactory.Create(_bitmap);
308-
}
309-
310132
public void Update(bool showPercentageIcons)
311133
{
312134
Icon icon = _notifyIcon.Icon;
@@ -315,10 +137,12 @@ public void Update(bool showPercentageIcons)
315137
case SensorType.Load:
316138
case SensorType.Control:
317139
case SensorType.Level:
318-
_notifyIcon.Icon = showPercentageIcons ? CreatePercentageIcon() : CreateTransparentIcon();
140+
_notifyIcon.Icon = showPercentageIcons
141+
? _iconFactory.CreatePercentageIcon(Sensor.Value.GetValueOrDefault())
142+
: _iconFactory.CreateTransparentIcon(GetString());
319143
break;
320144
default:
321-
_notifyIcon.Icon = CreateTransparentIcon();
145+
_notifyIcon.Icon = _iconFactory.CreateTransparentIcon(GetString());
322146
break;
323147
}
324148
icon?.Destroy();

0 commit comments

Comments
 (0)