GUI/SystemTray.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
branchMiniDisplay
changeset 444 9b09e2ee0968
parent 362 1dfe9dac1651
permissions -rw-r--r--
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.
     1 /*
     2  
     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/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Windows.Forms;
    16 using OpenHardwareMonitor.Hardware;
    17 using OpenHardwareMonitor.Utilities;
    18 
    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;
    27 
    28     public SystemTray(IComputer computer, PersistentSettings settings,
    29       UnitManager unitManager) 
    30     {
    31       this.computer = computer;
    32       this.settings = settings;
    33       this.unitManager = unitManager;
    34       computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    35       computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    36 
    37       this.mainIcon = new NotifyIconAdv();
    38 
    39       ContextMenu contextMenu = new ContextMenu();
    40       MenuItem hideShowItem = new MenuItem("Hide/Show");
    41       hideShowItem.Click += delegate(object obj, EventArgs args) {
    42         SendHideShowCommand();
    43       };
    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) {
    48         SendExitCommand();
    49       };
    50       contextMenu.MenuItems.Add(exitItem);
    51       this.mainIcon.ContextMenu = contextMenu;
    52       this.mainIcon.DoubleClick += delegate(object obj, EventArgs args) {
    53         SendHideShowCommand();
    54       };
    55       this.mainIcon.Icon = EmbeddedResources.GetIcon("smallicon.ico");
    56       this.mainIcon.Text = "Open Hardware Monitor";
    57     }
    58 
    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);
    66     }
    67 
    68     private void HardwareAdded(IHardware hardware) {
    69       foreach (ISensor sensor in hardware.Sensors)
    70         SensorAdded(sensor);
    71       hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    72       hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    73       foreach (IHardware subHardware in hardware.SubHardware)
    74         HardwareAdded(subHardware);
    75     }
    76 
    77     private void SensorAdded(ISensor sensor) {
    78       if (settings.GetValue(new Identifier(sensor.Identifier, 
    79         "tray").ToString(), false)) 
    80         Add(sensor, false);   
    81     }
    82 
    83     private void SensorRemoved(ISensor sensor) {
    84       if (Contains(sensor)) 
    85         Remove(sensor, false);
    86     }
    87 
    88     public void Dispose() {
    89       foreach (SensorNotifyIcon icon in list)
    90         icon.Dispose();
    91       mainIcon.Dispose();
    92     }
    93 
    94     public void Redraw() {
    95       foreach (SensorNotifyIcon icon in list)
    96         icon.Update();
    97     }
    98 
    99     public bool Contains(ISensor sensor) {
   100       foreach (SensorNotifyIcon icon in list)
   101         if (icon.Sensor == sensor)
   102           return true;
   103       return false;
   104     }
   105 
   106     public void Add(ISensor sensor, bool balloonTip) {
   107       if (Contains(sensor)) {
   108         return;
   109       } else {        
   110         list.Add(new SensorNotifyIcon(this, sensor, balloonTip, settings, unitManager));
   111         UpdateMainIconVisibilty();
   112         settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
   113       }
   114     }
   115 
   116     public void Remove(ISensor sensor) {
   117       Remove(sensor, true);
   118     }
   119 
   120     private void Remove(ISensor sensor, bool deleteConfig) {
   121       if (deleteConfig) {
   122         settings.Remove(
   123           new Identifier(sensor.Identifier, "tray").ToString());
   124         settings.Remove(
   125           new Identifier(sensor.Identifier, "traycolor").ToString());
   126       }
   127       SensorNotifyIcon instance = null;
   128       foreach (SensorNotifyIcon icon in list)
   129         if (icon.Sensor == sensor)
   130           instance = icon;
   131       if (instance != null) {
   132         list.Remove(instance);
   133         UpdateMainIconVisibilty();
   134         instance.Dispose();        
   135       }
   136     }
   137 
   138     public event EventHandler HideShowCommand;
   139 
   140     public void SendHideShowCommand() {
   141       if (HideShowCommand != null)
   142         HideShowCommand(this, null);
   143     }
   144 
   145     public event EventHandler ExitCommand;
   146 
   147     public void SendExitCommand() {
   148       if (ExitCommand != null)
   149         ExitCommand(this, null);
   150     }
   151 
   152     private void UpdateMainIconVisibilty() {
   153       if (mainIconEnabled) {
   154         mainIcon.Visible = list.Count == 0;
   155       } else {
   156         mainIcon.Visible = false;
   157       }
   158     }
   159 
   160     public bool IsMainIconEnabled {
   161       get { return mainIconEnabled; }
   162       set {
   163         if (mainIconEnabled != value) {
   164           mainIconEnabled = value;
   165           UpdateMainIconVisibilty();
   166         }
   167       }
   168     }
   169   }
   170 }