forked from kurumpa/dotSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrayIcon.cs
99 lines (86 loc) · 2.73 KB
/
TrayIcon.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dotSwitcher
{
public class TrayIcon
{
public event EventHandler<EventArgs> DoubleClick;
public event EventHandler<EventArgs> ExitClick;
public event EventHandler<EventArgs> SettingsClick;
public event EventHandler<EventArgs> TogglePowerClick;
private NotifyIcon trayIcon;
private ContextMenu trayMenu;
private MenuItem togglePowerItem;
private bool wasShownBeforeTooltip;
public TrayIcon (bool? visible = true)
{
trayMenu = new ContextMenu();
togglePowerItem = new MenuItem("", OnPowerClick);
trayMenu.MenuItems.Add(togglePowerItem);
trayMenu.MenuItems.Add("Settings", OnSettingsClick);
trayMenu.MenuItems.Add("-");
trayMenu.MenuItems.Add("Exit", OnExitClick);
trayIcon = new NotifyIcon();
trayIcon.Text = "dotSwitcher";
trayIcon.Icon = Properties.Resources.icon;
trayIcon.BalloonTipClosed += trayIcon_BalloonTipClosed;
trayIcon.MouseDoubleClick += trayIcon_Click;
trayIcon.ContextMenu = trayMenu;
trayIcon.Visible = visible == true;
}
public void SetRunning(bool isRunning)
{
togglePowerItem.Text = isRunning ? "Turn off" : "Turn on";
}
public void Show()
{
trayIcon.Visible = true;
}
public void Hide()
{
trayIcon.Visible = false;
}
public void ShowTooltip(string p, ToolTipIcon icon)
{
wasShownBeforeTooltip = trayIcon.Visible;
Show();
trayIcon.ShowBalloonTip(2000, "dotSwitcher error", p, icon);
}
private void OnExitClick(object sender, EventArgs e)
{
if (ExitClick != null)
{
ExitClick(this, null);
}
}
private void OnSettingsClick(object sender, EventArgs e)
{
if (SettingsClick != null)
{
SettingsClick(this, null);
}
}
private void OnPowerClick(object sender, EventArgs e)
{
if (TogglePowerClick != null)
{
TogglePowerClick(this, null);
}
}
void trayIcon_Click(object sender, MouseEventArgs e)
{
if (DoubleClick != null)
{
DoubleClick(this, null);
}
}
void trayIcon_BalloonTipClosed(object sender, EventArgs e)
{
trayIcon.Visible = wasShownBeforeTooltip;
}
}
}