Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
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-2012 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 UnitManager unitManager;
24 private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
25 private bool mainIconEnabled = false;
26 private NotifyIconAdv mainIcon;
28 public SystemTray(IComputer computer, PersistentSettings settings,
29 UnitManager unitManager)
31 this.computer = computer;
32 this.settings = settings;
33 this.unitManager = unitManager;
34 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
35 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
37 this.mainIcon = new NotifyIconAdv();
39 ContextMenu contextMenu = new ContextMenu();
40 MenuItem hideShowItem = new MenuItem("Hide/Show");
41 hideShowItem.Click += delegate(object obj, EventArgs args) {
42 SendHideShowCommand();
44 contextMenu.MenuItems.Add(hideShowItem);
45 contextMenu.MenuItems.Add(new MenuItem("-"));
46 MenuItem exitItem = new MenuItem("Exit");
47 exitItem.Click += delegate(object obj, EventArgs args) {
50 contextMenu.MenuItems.Add(exitItem);
51 this.mainIcon.ContextMenu = contextMenu;
52 this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
53 SendHideShowCommand();
55 this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
56 this.mainIcon.Text = "Open Hardware Monitor";
59 private void HardwareRemoved(IHardware hardware) {
60 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
61 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
62 foreach (ISensor sensor in hardware.Sensors)
63 SensorRemoved(sensor);
64 foreach (IHardware subHardware in hardware.SubHardware)
65 HardwareRemoved(subHardware);
68 private void HardwareAdded(IHardware hardware) {
69 foreach (ISensor sensor in hardware.Sensors)
71 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
72 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
73 foreach (IHardware subHardware in hardware.SubHardware)
74 HardwareAdded(subHardware);
77 private void SensorAdded(ISensor sensor) {
78 if (settings.GetValue(new Identifier(sensor.Identifier,
79 "tray").ToString(), false))
83 private void SensorRemoved(ISensor sensor) {
85 Remove(sensor, false);
88 public void Dispose() {
89 foreach (SensorNotifyIcon icon in list)
94 public void Redraw() {
95 foreach (SensorNotifyIcon icon in list)
99 public bool Contains(ISensor sensor) {
100 foreach (SensorNotifyIcon icon in list)
101 if (icon.Sensor == sensor)
106 public void Add(ISensor sensor, bool balloonTip) {
107 if (Contains(sensor)) {
110 list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings, unitManager));
111 UpdateMainIconVisibilty();
112 settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
116 public void Remove(ISensor sensor) {
117 Remove(sensor, true);
120 private void Remove(ISensor sensor, bool deleteConfig) {
123 new Identifier(sensor.Identifier, "tray").ToString());
125 new Identifier(sensor.Identifier, "traycolor").ToString());
127 SensorNotifyIcon instance = null;
128 foreach (SensorNotifyIcon icon in list)
129 if (icon.Sensor == sensor)
131 if (instance != null) {
132 list.Remove(instance);
133 UpdateMainIconVisibilty();
138 public event EventHandler HideShowCommand;
140 public void SendHideShowCommand() {
141 if (HideShowCommand != null)
142 HideShowCommand(this, null);
145 public event EventHandler ExitCommand;
147 public void SendExitCommand() {
148 if (ExitCommand != null)
149 ExitCommand(this, null);
152 private void UpdateMainIconVisibilty() {
153 if (mainIconEnabled) {
154 mainIcon.Visible = list.Count == 0;
156 mainIcon.Visible = false;
160 public bool IsMainIconEnabled {
161 get { return mainIconEnabled; }
163 if (mainIconEnabled != value) {
164 mainIconEnabled = value;
165 UpdateMainIconVisibilty();