Fixed Issue 91.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 The contents of this file are subject to the Mozilla Public License Version
6 1.1 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.mozilla.org/MPL/
11 Software distributed under the License is distributed on an "AS IS" basis,
12 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 for the specific language governing rights and limitations under the License.
15 The Original Code is the Open Hardware Monitor code.
17 The Initial Developer of the Original Code is
18 Michael Möller <m.moeller@gmx.ch>.
19 Portions created by the Initial Developer are Copyright (C) 2009-2010
20 the Initial Developer. All Rights Reserved.
24 Alternatively, the contents of this file may be used under the terms of
25 either the GNU General Public License Version 2 or later (the "GPL"), or
26 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 in which case the provisions of the GPL or the LGPL are applicable instead
28 of those above. If you wish to allow use of your version of this file only
29 under the terms of either the GPL or the LGPL, and not to allow others to
30 use your version of this file under the terms of the MPL, indicate your
31 decision by deleting the provisions above and replace them with the notice
32 and other provisions required by the GPL or the LGPL. If you do not delete
33 the provisions above, a recipient may use your version of this file under
34 the terms of any one of the MPL, the GPL or the LGPL.
39 using System.Collections.Generic;
42 using System.Windows.Forms;
43 using OpenHardwareMonitor.Hardware;
44 using OpenHardwareMonitor.Utilities;
46 namespace OpenHardwareMonitor.GUI {
47 public class SystemTray : IDisposable {
48 private IComputer computer;
49 private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
50 private bool mainIconEnabled = false;
51 private NotifyIcon mainIcon;
53 public SystemTray(IComputer computer) {
54 this.computer = computer;
55 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
56 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
58 this.mainIcon = new NotifyIcon();
60 ContextMenuStrip contextMenuStrip = new ContextMenuStrip();
61 ToolStripMenuItem hideShowItem = new ToolStripMenuItem("Hide/Show");
62 hideShowItem.Click += delegate(object obj, EventArgs args) {
63 SendHideShowCommand();
65 contextMenuStrip.Items.Add(hideShowItem);
66 contextMenuStrip.Items.Add(new ToolStripSeparator());
67 ToolStripMenuItem exitItem = new ToolStripMenuItem("Exit");
68 exitItem.Click += delegate(object obj, EventArgs args) {
71 contextMenuStrip.Items.Add(exitItem);
72 this.mainIcon.ContextMenuStrip = contextMenuStrip;
73 this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
74 SendHideShowCommand();
76 this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
79 private void HardwareRemoved(IHardware hardware) {
80 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
81 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
82 foreach (ISensor sensor in hardware.Sensors)
83 SensorRemoved(sensor);
84 foreach (IHardware subHardware in hardware.SubHardware)
85 HardwareRemoved(subHardware);
88 private void HardwareAdded(IHardware hardware) {
89 foreach (ISensor sensor in hardware.Sensors)
91 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
92 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
93 foreach (IHardware subHardware in hardware.SubHardware)
94 HardwareAdded(subHardware);
97 private void SensorAdded(ISensor sensor) {
98 if (Config.Get(new Identifier(sensor.Identifier,
99 "tray").ToString(), false))
103 private void SensorRemoved(ISensor sensor) {
104 if (Contains(sensor))
105 Remove(sensor, false);
108 public void Dispose() {
109 foreach (SensorNotifyIcon icon in list)
114 public void Redraw() {
115 foreach (SensorNotifyIcon icon in list)
119 public bool Contains(ISensor sensor) {
120 foreach (SensorNotifyIcon icon in list)
121 if (icon.Sensor == sensor)
126 public void Add(ISensor sensor, bool balloonTip) {
127 if (Contains(sensor)) {
130 list.Add(new SensorNotifyIcon(this, sensor, balloonTip));
131 UpdateMainIconVisibilty();
132 Config.Set(new Identifier(sensor.Identifier, "tray").ToString(), true);
136 public void Remove(ISensor sensor) {
137 Remove(sensor, true);
140 private void Remove(ISensor sensor, bool deleteConfig) {
143 new Identifier(sensor.Identifier, "tray").ToString());
145 new Identifier(sensor.Identifier, "traycolor").ToString());
147 SensorNotifyIcon instance = null;
148 foreach (SensorNotifyIcon icon in list)
149 if (icon.Sensor == sensor)
151 if (instance != null) {
152 list.Remove(instance);
153 UpdateMainIconVisibilty();
158 public event EventHandler HideShowCommand;
160 public void SendHideShowCommand() {
161 if (HideShowCommand != null)
162 HideShowCommand(this, null);
165 public event EventHandler ExitCommand;
167 public void SendExitCommand() {
168 if (ExitCommand != null)
169 ExitCommand(this, null);
172 private void UpdateMainIconVisibilty() {
173 if (mainIconEnabled) {
174 mainIcon.Visible = list.Count == 0;
176 mainIcon.Visible = false;
180 public bool IsMainIconEnabled {
181 get { return mainIconEnabled; }
183 if (mainIconEnabled != value) {
184 mainIconEnabled = value;
185 UpdateMainIconVisibilty();