Added the remote web enhancement developed by Prince Samuel.
3 This Source Code Form is subject to the terms of the Mozilla Public
4 License, v. 2.0. If a copy of the MPL was not distributed with this
5 file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
15 using System.Windows.Forms;
16 using OpenHardwareMonitor.Hardware;
17 using OpenHardwareMonitor.Utilities;
19 namespace OpenHardwareMonitor.GUI {
20 public class SystemTray : IDisposable {
21 private IComputer computer;
22 private PersistentSettings settings;
23 private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
24 private bool mainIconEnabled = false;
25 private NotifyIcon mainIcon;
27 public SystemTray(IComputer computer, PersistentSettings settings) {
28 this.computer = computer;
29 this.settings = settings;
30 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
31 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
33 this.mainIcon = new NotifyIcon();
35 ContextMenu contextMenu = new ContextMenu();
36 MenuItem hideShowItem = new MenuItem("Hide/Show");
37 hideShowItem.Click += delegate(object obj, EventArgs args) {
38 SendHideShowCommand();
40 contextMenu.MenuItems.Add(hideShowItem);
41 contextMenu.MenuItems.Add(new MenuItem("-"));
42 MenuItem exitItem = new MenuItem("Exit");
43 exitItem.Click += delegate(object obj, EventArgs args) {
46 contextMenu.MenuItems.Add(exitItem);
47 this.mainIcon.ContextMenu = contextMenu;
48 this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
49 SendHideShowCommand();
51 this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
52 this.mainIcon.Text = "Open Hardware Monitor";
55 private void HardwareRemoved(IHardware hardware) {
56 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
57 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
58 foreach (ISensor sensor in hardware.Sensors)
59 SensorRemoved(sensor);
60 foreach (IHardware subHardware in hardware.SubHardware)
61 HardwareRemoved(subHardware);
64 private void HardwareAdded(IHardware hardware) {
65 foreach (ISensor sensor in hardware.Sensors)
67 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
68 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
69 foreach (IHardware subHardware in hardware.SubHardware)
70 HardwareAdded(subHardware);
73 private void SensorAdded(ISensor sensor) {
74 if (settings.GetValue(new Identifier(sensor.Identifier,
75 "tray").ToString(), false))
79 private void SensorRemoved(ISensor sensor) {
81 Remove(sensor, false);
84 public void Dispose() {
85 foreach (SensorNotifyIcon icon in list)
90 public void Redraw() {
91 foreach (SensorNotifyIcon icon in list)
95 public bool Contains(ISensor sensor) {
96 foreach (SensorNotifyIcon icon in list)
97 if (icon.Sensor == sensor)
102 public void Add(ISensor sensor, bool balloonTip) {
103 if (Contains(sensor)) {
106 list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
107 UpdateMainIconVisibilty();
108 settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
112 public void Remove(ISensor sensor) {
113 Remove(sensor, true);
116 private void Remove(ISensor sensor, bool deleteConfig) {
119 new Identifier(sensor.Identifier, "tray").ToString());
121 new Identifier(sensor.Identifier, "traycolor").ToString());
123 SensorNotifyIcon instance = null;
124 foreach (SensorNotifyIcon icon in list)
125 if (icon.Sensor == sensor)
127 if (instance != null) {
128 list.Remove(instance);
129 UpdateMainIconVisibilty();
134 public event EventHandler HideShowCommand;
136 public void SendHideShowCommand() {
137 if (HideShowCommand != null)
138 HideShowCommand(this, null);
141 public event EventHandler ExitCommand;
143 public void SendExitCommand() {
144 if (ExitCommand != null)
145 ExitCommand(this, null);
148 private void UpdateMainIconVisibilty() {
149 if (mainIconEnabled) {
150 mainIcon.Visible = list.Count == 0;
152 mainIcon.Visible = false;
156 public bool IsMainIconEnabled {
157 get { return mainIconEnabled; }
159 if (mainIconEnabled != value) {
160 mainIconEnabled = value;
161 UpdateMainIconVisibilty();