Fixed the power sensor display in the desktop gadget.
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-2011
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 PersistentSettings settings;
50 private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
51 private bool mainIconEnabled = false;
52 private NotifyIcon mainIcon;
54 public SystemTray(IComputer computer, PersistentSettings settings) {
55 this.computer = computer;
56 this.settings = settings;
57 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
58 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
60 this.mainIcon = new NotifyIcon();
62 ContextMenu contextMenu = new ContextMenu();
63 MenuItem hideShowItem = new MenuItem("Hide/Show");
64 hideShowItem.Click += delegate(object obj, EventArgs args) {
65 SendHideShowCommand();
67 contextMenu.MenuItems.Add(hideShowItem);
68 contextMenu.MenuItems.Add(new MenuItem("-"));
69 MenuItem exitItem = new MenuItem("Exit");
70 exitItem.Click += delegate(object obj, EventArgs args) {
73 contextMenu.MenuItems.Add(exitItem);
74 this.mainIcon.ContextMenu = contextMenu;
75 this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
76 SendHideShowCommand();
78 this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
79 this.mainIcon.Text = "Open Hardware Monitor";
82 private void HardwareRemoved(IHardware hardware) {
83 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
84 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
85 foreach (ISensor sensor in hardware.Sensors)
86 SensorRemoved(sensor);
87 foreach (IHardware subHardware in hardware.SubHardware)
88 HardwareRemoved(subHardware);
91 private void HardwareAdded(IHardware hardware) {
92 foreach (ISensor sensor in hardware.Sensors)
94 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
95 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
96 foreach (IHardware subHardware in hardware.SubHardware)
97 HardwareAdded(subHardware);
100 private void SensorAdded(ISensor sensor) {
101 if (settings.GetValue(new Identifier(sensor.Identifier,
102 "tray").ToString(), false))
106 private void SensorRemoved(ISensor sensor) {
107 if (Contains(sensor))
108 Remove(sensor, false);
111 public void Dispose() {
112 foreach (SensorNotifyIcon icon in list)
117 public void Redraw() {
118 foreach (SensorNotifyIcon icon in list)
122 public bool Contains(ISensor sensor) {
123 foreach (SensorNotifyIcon icon in list)
124 if (icon.Sensor == sensor)
129 public void Add(ISensor sensor, bool balloonTip) {
130 if (Contains(sensor)) {
133 list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
134 UpdateMainIconVisibilty();
135 settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
139 public void Remove(ISensor sensor) {
140 Remove(sensor, true);
143 private void Remove(ISensor sensor, bool deleteConfig) {
146 new Identifier(sensor.Identifier, "tray").ToString());
148 new Identifier(sensor.Identifier, "traycolor").ToString());
150 SensorNotifyIcon instance = null;
151 foreach (SensorNotifyIcon icon in list)
152 if (icon.Sensor == sensor)
154 if (instance != null) {
155 list.Remove(instance);
156 UpdateMainIconVisibilty();
161 public event EventHandler HideShowCommand;
163 public void SendHideShowCommand() {
164 if (HideShowCommand != null)
165 HideShowCommand(this, null);
168 public event EventHandler ExitCommand;
170 public void SendExitCommand() {
171 if (ExitCommand != null)
172 ExitCommand(this, null);
175 private void UpdateMainIconVisibilty() {
176 if (mainIconEnabled) {
177 mainIcon.Visible = list.Count == 0;
179 mainIcon.Visible = false;
183 public bool IsMainIconEnabled {
184 get { return mainIconEnabled; }
186 if (mainIconEnabled != value) {
187 mainIconEnabled = value;
188 UpdateMainIconVisibilty();