Skip to content

Commit 06884c8

Browse files
committed
Show/Hide tray icon menu marked as Default (bold)
1 parent e9069ae commit 06884c8

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

GUI/SensorNotifyIcon.cs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
2+
33
This Source Code Form is subject to the terms of the Mozilla Public
44
License, v. 2.0. If a copy of the MPL was not distributed with this
55
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6-
6+
77
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
8-
8+
99
*/
1010

1111
using System;
@@ -36,7 +36,7 @@ public class SensorNotifyIcon : IDisposable {
3636
private Font smallFont;
3737

3838
public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
39-
bool balloonTip, PersistentSettings settings, UnitManager unitManager)
39+
bool balloonTip, PersistentSettings settings, UnitManager unitManager)
4040
{
4141
this.unitManager = unitManager;
4242
this.sensor = sensor;
@@ -45,17 +45,18 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
4545
//Color defaultColor = Color.Black;
4646
//if (sensor.SensorType == SensorType.Load ||
4747
// sensor.SensorType == SensorType.Control ||
48-
// sensor.SensorType == SensorType.Level)
48+
// sensor.SensorType == SensorType.Level)
4949
//{
5050
// defaultColor = Color.FromArgb(0xff, 0x70, 0x8c, 0xf1);
5151
//}
52-
Color = settings.GetValue(new Identifier(sensor.Identifier,
53-
"traycolor").ToString(), Color.FromArgb(0xff, 0x00, 0xff, 0xff));
54-
52+
Color = settings.GetValue(new Identifier(sensor.Identifier,
53+
"traycolor").ToString(), Color.FromArgb(0xff, 0x00, 0xff, 0xff));
54+
5555
this.pen = new Pen(Color.FromArgb(96, Color.Black));
5656

5757
ContextMenu contextMenu = new ContextMenu();
5858
MenuItem hideShowItem = new MenuItem("Hide/Show");
59+
hideShowItem.DefaultItem = true;
5960
hideShowItem.Click += delegate(object obj, EventArgs args) {
6061
sensorSystemTray.SendHideShowCommand();
6162
};
@@ -86,7 +87,7 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
8687
this.notifyIcon.ContextMenu = contextMenu;
8788
this.notifyIcon.DoubleClick += delegate(object obj, EventArgs args) {
8889
sensorSystemTray.SendHideShowCommand();
89-
};
90+
};
9091

9192
// get the default dpi to create an icon with the correct size
9293
float dpiX, dpiY;
@@ -95,7 +96,7 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
9596
dpiY = b.VerticalResolution;
9697
}
9798

98-
// adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
99+
// adjust the size of the icon to current dpi (default is 16x16 at 96 dpi)
99100
int width = (int)Math.Round(16 * dpiX / 96);
100101
int height = (int)Math.Round(16 * dpiY / 96);
101102

@@ -114,10 +115,10 @@ public SensorNotifyIcon(SystemTray sensorSystemTray, ISensor sensor,
114115

115116
this.font = new Font(family,
116117
baseSize * width / 16.0f, GraphicsUnit.Pixel);
117-
this.smallFont = new Font(family,
118+
this.smallFont = new Font(family,
118119
0.75f * baseSize * width / 16.0f, GraphicsUnit.Pixel);
119120

120-
this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
121+
this.bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
121122
this.graphics = Graphics.FromImage(this.bitmap);
122123

123124
if (Environment.OSVersion.Version.Major > 5) {
@@ -132,7 +133,7 @@ public ISensor Sensor {
132133

133134
public Color Color {
134135
get { return color; }
135-
set {
136+
set {
136137
this.color = value;
137138
this.darkColor = Color.FromArgb(255,
138139
this.color.R / 3,
@@ -149,19 +150,19 @@ public Color Color {
149150
}
150151
}
151152

152-
public void Dispose() {
153+
public void Dispose() {
153154
Icon icon = notifyIcon.Icon;
154155
notifyIcon.Icon = null;
155156
if (icon != null)
156-
icon.Dispose();
157+
icon.Dispose();
157158
notifyIcon.Dispose();
158159

159160
if (brush != null)
160161
brush.Dispose();
161162
if (darkBrush != null)
162163
darkBrush.Dispose();
163164
pen.Dispose();
164-
graphics.Dispose();
165+
graphics.Dispose();
165166
bitmap.Dispose();
166167
font.Dispose();
167168
smallFont.Dispose();
@@ -176,15 +177,15 @@ private string GetString() {
176177
return string.Format("{0:F1}", sensor.Value);
177178
case SensorType.Clock:
178179
return string.Format("{0:F1}", 1e-3f * sensor.Value);
179-
case SensorType.Load:
180+
case SensorType.Load:
180181
return string.Format("{0:F0}", sensor.Value);
181182
case SensorType.Temperature:
182183
if (unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
183-
return string.Format("{0:F0}",
184+
return string.Format("{0:F0}",
184185
UnitManager.CelsiusToFahrenheit(sensor.Value));
185186
else
186187
return string.Format("{0:F0}", sensor.Value);
187-
case SensorType.Fan:
188+
case SensorType.Fan:
188189
return string.Format("{0:F1}", 1e-3f * sensor.Value);
189190
case SensorType.Flow:
190191
return string.Format("{0:F1}", 1e-3f * sensor.Value);
@@ -213,7 +214,7 @@ private Icon CreateTransparentIcon() {
213214

214215
graphics.Clear(Color.Black);
215216
TextRenderer.DrawText(graphics, text, small ? smallFont : font,
216-
new Point(-4, small ? 1 : -2), color, Color.Black);
217+
new Point(-4, small ? 1 : -2), color, Color.Black);
217218

218219
BitmapData data = bitmap.LockBits(
219220
new Rectangle(0, 0, bitmap.Width, bitmap.Height),
@@ -238,11 +239,11 @@ private Icon CreateTransparentIcon() {
238239
// bytes[i + 3] = (byte)(0.3 * red + 0.59 * green + 0.11 * blue);
239240
//}
240241

241-
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
242+
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
242243
PixelFormat.Format32bppArgb);
243244
}
244245

245-
private Icon CreatePercentageIcon() {
246+
private Icon CreatePercentageIcon() {
246247
try {
247248
graphics.Clear(Color.Transparent);
248249
} catch (ArgumentException) {
@@ -261,7 +262,7 @@ private Icon CreatePercentageIcon() {
261262
Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);
262263
bitmap.UnlockBits(data);
263264

264-
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
265+
return IconFactory.Create(bytes, bitmap.Width, bitmap.Height,
265266
PixelFormat.Format32bppArgb);
266267
}
267268

@@ -279,7 +280,7 @@ public void Update() {
279280
break;
280281
}
281282

282-
if (icon != null)
283+
if (icon != null)
283284
icon.Dispose();
284285

285286
string format = "";
@@ -299,22 +300,22 @@ public void Update() {
299300
string formattedValue = string.Format(format, sensor.Name, sensor.Value);
300301

301302
if (sensor.SensorType == SensorType.Temperature &&
302-
unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
303+
unitManager.TemperatureUnit == TemperatureUnit.Fahrenheit)
303304
{
304305
format = "\n{0}: {1:F1} °F";
305306
formattedValue = string.Format(format, sensor.Name,
306307
UnitManager.CelsiusToFahrenheit(sensor.Value));
307308
}
308309

309310
string hardwareName = sensor.Hardware.Name;
310-
hardwareName = hardwareName.Substring(0,
311+
hardwareName = hardwareName.Substring(0,
311312
Math.Min(63 - formattedValue.Length, hardwareName.Length));
312313
string text = hardwareName + formattedValue;
313314
if (text.Length > 63)
314315
text = null;
315316

316317
notifyIcon.Text = text;
317-
notifyIcon.Visible = true;
318+
notifyIcon.Visible = true;
318319
}
319320
}
320321
}

GUI/SystemTray.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/*
2-
2+
33
This Source Code Form is subject to the terms of the Mozilla Public
44
License, v. 2.0. If a copy of the MPL was not distributed with this
55
file, You can obtain one at http://mozilla.org/MPL/2.0/.
6-
6+
77
Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
8-
8+
99
*/
1010

1111
using System;
@@ -26,7 +26,7 @@ public class SystemTray : IDisposable {
2626
private NotifyIconAdv mainIcon;
2727

2828
public SystemTray(IComputer computer, PersistentSettings settings,
29-
UnitManager unitManager)
29+
UnitManager unitManager)
3030
{
3131
this.computer = computer;
3232
this.settings = settings;
@@ -38,11 +38,12 @@ public SystemTray(IComputer computer, PersistentSettings settings,
3838

3939
ContextMenu contextMenu = new ContextMenu();
4040
MenuItem hideShowItem = new MenuItem("Hide/Show");
41+
hideShowItem.DefaultItem = true;
4142
hideShowItem.Click += delegate(object obj, EventArgs args) {
4243
SendHideShowCommand();
4344
};
4445
contextMenu.MenuItems.Add(hideShowItem);
45-
contextMenu.MenuItems.Add(new MenuItem("-"));
46+
contextMenu.MenuItems.Add(new MenuItem("-"));
4647
MenuItem exitItem = new MenuItem("Exit");
4748
exitItem.Click += delegate(object obj, EventArgs args) {
4849
SendExitCommand();
@@ -59,7 +60,7 @@ public SystemTray(IComputer computer, PersistentSettings settings,
5960
private void HardwareRemoved(IHardware hardware) {
6061
hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
6162
hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
62-
foreach (ISensor sensor in hardware.Sensors)
63+
foreach (ISensor sensor in hardware.Sensors)
6364
SensorRemoved(sensor);
6465
foreach (IHardware subHardware in hardware.SubHardware)
6566
HardwareRemoved(subHardware);
@@ -75,13 +76,13 @@ private void HardwareAdded(IHardware hardware) {
7576
}
7677

7778
private void SensorAdded(ISensor sensor) {
78-
if (settings.GetValue(new Identifier(sensor.Identifier,
79-
"tray").ToString(), false))
80-
Add(sensor, false);
79+
if (settings.GetValue(new Identifier(sensor.Identifier,
80+
"tray").ToString(), false))
81+
Add(sensor, false);
8182
}
8283

8384
private void SensorRemoved(ISensor sensor) {
84-
if (Contains(sensor))
85+
if (Contains(sensor))
8586
Remove(sensor, false);
8687
}
8788

@@ -106,7 +107,7 @@ public bool Contains(ISensor sensor) {
106107
public void Add(ISensor sensor, bool balloonTip) {
107108
if (Contains(sensor)) {
108109
return;
109-
} else {
110+
} else {
110111
list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings, unitManager));
111112
UpdateMainIconVisibilty();
112113
settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
@@ -131,7 +132,7 @@ private void Remove(ISensor sensor, bool deleteConfig) {
131132
if (instance != null) {
132133
list.Remove(instance);
133134
UpdateMainIconVisibilty();
134-
instance.Dispose();
135+
instance.Dispose();
135136
}
136137
}
137138

0 commit comments

Comments
 (0)