2010-02-12 00:36:56 +00:00
|
|
|
|
/*
|
|
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-02-12 00:36:56 +00:00
|
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
|
Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
|
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2010-02-12 22:46:31 +00:00
|
|
|
|
using System.Drawing;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using OpenHardwareMonitor.Hardware;
|
|
|
|
|
using OpenHardwareMonitor.Utilities;
|
|
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.GUI {
|
2010-06-05 18:59:54 +00:00
|
|
|
|
public class SystemTray : IDisposable {
|
2010-03-27 12:57:09 +00:00
|
|
|
|
private IComputer computer;
|
2010-08-08 13:57:26 +00:00
|
|
|
|
private PersistentSettings settings;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
private List<SensorNotifyIcon> list = new List<SensorNotifyIcon>();
|
2010-06-05 18:59:54 +00:00
|
|
|
|
private bool mainIconEnabled = false;
|
|
|
|
|
private NotifyIcon mainIcon;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
|
2010-08-08 13:57:26 +00:00
|
|
|
|
public SystemTray(IComputer computer, PersistentSettings settings) {
|
2010-02-12 00:36:56 +00:00
|
|
|
|
this.computer = computer;
|
2010-08-08 13:57:26 +00:00
|
|
|
|
this.settings = settings;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
|
|
|
|
|
computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
|
2010-06-05 18:59:54 +00:00
|
|
|
|
|
|
|
|
|
this.mainIcon = new NotifyIcon();
|
|
|
|
|
|
2010-07-18 12:38:01 +00:00
|
|
|
|
ContextMenu contextMenu = new ContextMenu();
|
|
|
|
|
MenuItem hideShowItem = new MenuItem("Hide/Show");
|
2010-06-05 18:59:54 +00:00
|
|
|
|
hideShowItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
|
SendHideShowCommand();
|
|
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
|
contextMenu.MenuItems.Add(hideShowItem);
|
|
|
|
|
contextMenu.MenuItems.Add(new MenuItem("-"));
|
|
|
|
|
MenuItem exitItem = new MenuItem("Exit");
|
2010-06-05 18:59:54 +00:00
|
|
|
|
exitItem.Click += delegate(object obj, EventArgs args) {
|
|
|
|
|
SendExitCommand();
|
|
|
|
|
};
|
2010-07-18 12:38:01 +00:00
|
|
|
|
contextMenu.MenuItems.Add(exitItem);
|
|
|
|
|
this.mainIcon.ContextMenu = contextMenu;
|
2010-06-05 18:59:54 +00:00
|
|
|
|
this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
|
|
|
|
|
SendHideShowCommand();
|
|
|
|
|
};
|
|
|
|
|
this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
|
2011-06-25 11:06:54 +00:00
|
|
|
|
this.mainIcon.Text = "Open Hardware Monitor";
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void HardwareRemoved(IHardware hardware) {
|
|
|
|
|
hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
|
|
|
|
|
hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
|
|
|
|
|
foreach (ISensor sensor in hardware.Sensors)
|
|
|
|
|
SensorRemoved(sensor);
|
2010-02-27 20:08:13 +00:00
|
|
|
|
foreach (IHardware subHardware in hardware.SubHardware)
|
|
|
|
|
HardwareRemoved(subHardware);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
2010-02-27 20:08:13 +00:00
|
|
|
|
|
2010-02-12 00:36:56 +00:00
|
|
|
|
private void HardwareAdded(IHardware hardware) {
|
|
|
|
|
foreach (ISensor sensor in hardware.Sensors)
|
|
|
|
|
SensorAdded(sensor);
|
|
|
|
|
hardware.SensorAdded += new SensorEventHandler(SensorAdded);
|
|
|
|
|
hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
|
2010-02-27 20:08:13 +00:00
|
|
|
|
foreach (IHardware subHardware in hardware.SubHardware)
|
|
|
|
|
HardwareAdded(subHardware);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SensorAdded(ISensor sensor) {
|
2010-08-12 20:53:27 +00:00
|
|
|
|
if (settings.GetValue(new Identifier(sensor.Identifier,
|
2010-05-06 19:20:38 +00:00
|
|
|
|
"tray").ToString(), false))
|
2010-02-12 22:46:31 +00:00
|
|
|
|
Add(sensor, false);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SensorRemoved(ISensor sensor) {
|
2010-02-12 22:46:31 +00:00
|
|
|
|
if (Contains(sensor))
|
|
|
|
|
Remove(sensor, false);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
foreach (SensorNotifyIcon icon in list)
|
|
|
|
|
icon.Dispose();
|
2010-06-05 18:59:54 +00:00
|
|
|
|
mainIcon.Dispose();
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Redraw() {
|
|
|
|
|
foreach (SensorNotifyIcon icon in list)
|
|
|
|
|
icon.Update();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Contains(ISensor sensor) {
|
|
|
|
|
foreach (SensorNotifyIcon icon in list)
|
|
|
|
|
if (icon.Sensor == sensor)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-12 22:46:31 +00:00
|
|
|
|
public void Add(ISensor sensor, bool balloonTip) {
|
2010-02-12 00:36:56 +00:00
|
|
|
|
if (Contains(sensor)) {
|
|
|
|
|
return;
|
2010-02-12 22:46:31 +00:00
|
|
|
|
} else {
|
2010-08-08 13:57:26 +00:00
|
|
|
|
list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings));
|
2010-06-05 18:59:54 +00:00
|
|
|
|
UpdateMainIconVisibilty();
|
2010-08-12 20:53:27 +00:00
|
|
|
|
settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Remove(ISensor sensor) {
|
2010-02-12 22:46:31 +00:00
|
|
|
|
Remove(sensor, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Remove(ISensor sensor, bool deleteConfig) {
|
|
|
|
|
if (deleteConfig) {
|
2010-08-08 13:57:26 +00:00
|
|
|
|
settings.Remove(
|
2010-05-06 19:20:38 +00:00
|
|
|
|
new Identifier(sensor.Identifier, "tray").ToString());
|
2010-08-08 13:57:26 +00:00
|
|
|
|
settings.Remove(
|
2010-05-06 19:20:38 +00:00
|
|
|
|
new Identifier(sensor.Identifier, "traycolor").ToString());
|
2010-02-12 22:46:31 +00:00
|
|
|
|
}
|
2010-02-12 00:36:56 +00:00
|
|
|
|
SensorNotifyIcon instance = null;
|
|
|
|
|
foreach (SensorNotifyIcon icon in list)
|
|
|
|
|
if (icon.Sensor == sensor)
|
|
|
|
|
instance = icon;
|
|
|
|
|
if (instance != null) {
|
|
|
|
|
list.Remove(instance);
|
2010-06-05 18:59:54 +00:00
|
|
|
|
UpdateMainIconVisibilty();
|
|
|
|
|
instance.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler HideShowCommand;
|
|
|
|
|
|
|
|
|
|
public void SendHideShowCommand() {
|
|
|
|
|
if (HideShowCommand != null)
|
|
|
|
|
HideShowCommand(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public event EventHandler ExitCommand;
|
|
|
|
|
|
|
|
|
|
public void SendExitCommand() {
|
|
|
|
|
if (ExitCommand != null)
|
|
|
|
|
ExitCommand(this, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateMainIconVisibilty() {
|
|
|
|
|
if (mainIconEnabled) {
|
|
|
|
|
mainIcon.Visible = list.Count == 0;
|
|
|
|
|
} else {
|
|
|
|
|
mainIcon.Visible = false;
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-06-05 18:59:54 +00:00
|
|
|
|
public bool IsMainIconEnabled {
|
|
|
|
|
get { return mainIconEnabled; }
|
|
|
|
|
set {
|
|
|
|
|
if (mainIconEnabled != value) {
|
|
|
|
|
mainIconEnabled = value;
|
|
|
|
|
UpdateMainIconVisibilty();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-12 00:36:56 +00:00
|
|
|
|
}
|
|
|
|
|
}
|