-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSystemTrayProcess.cs
113 lines (88 loc) · 2.44 KB
/
SystemTrayProcess.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
namespace Iksokodo;
using System;
using System.Timers;
using System.Windows.Forms;
using Timer = System.Timers.Timer;
internal class SystemTrayProcess : ApplicationContext
{
private static readonly Icon _normal = new("iksokodo.ico"), _suspended = new("iksokodo_suspend.ico");
private readonly NotifyIcon _trayIcon;
private readonly ToolStripMenuItem _toggleButton, _changeHotkeyButton, _exitButton;
private readonly Converter _converter;
private readonly Timer _timer;
private const string PAUSE_MESSAGE = "Pause", RESUME_MESSAGE = "Resume", CHANGE_HOTKEY_MESSAGE = "Change Hotkey", EXIT_MESSAGE = "Exit";
internal SystemTrayProcess()
{
ContextMenuStrip menuStrip = new();
_toggleButton = new()
{
Name = PAUSE_MESSAGE,
Text = PAUSE_MESSAGE,
};
_changeHotkeyButton = new()
{
Name = CHANGE_HOTKEY_MESSAGE,
Text = CHANGE_HOTKEY_MESSAGE,
};
_exitButton = new()
{
Name = EXIT_MESSAGE,
Text = EXIT_MESSAGE,
};
_toggleButton.Click += new EventHandler(ToggleLoop);
_changeHotkeyButton.Click += new EventHandler(ChangeHotkey);
_exitButton.Click += new EventHandler(Exit);
menuStrip.Items.Add(_toggleButton);
menuStrip.Items.Add(_changeHotkeyButton);
menuStrip.Items.Add(_exitButton);
_trayIcon = new()
{
Visible = true,
Icon = _normal,
Text = "Iksokodo",
ContextMenuStrip = menuStrip,
};
_trayIcon.MouseClick += TrayRightClick;
_converter = new();
HotkeyManager.RegisterHotkey(ToggleLoop);
_timer = new() { Interval = 10 };
_timer.Elapsed += (object _, ElapsedEventArgs _) => _converter.Convert();
_timer.Start();
}
private void TrayRightClick(object sender, MouseEventArgs e)
{
switch (e.Button)
{
case MouseButtons.Left:
{
ToggleLoop(null, null);
break;
}
case MouseButtons.Right:
{
_trayIcon.ContextMenuStrip.Show(Cursor.Position);
break;
}
}
}
private void ToggleLoop(object sender, EventArgs e)
{
bool wasPaused = _toggleButton.Name == RESUME_MESSAGE;
_toggleButton.Name = wasPaused ? PAUSE_MESSAGE : RESUME_MESSAGE;
_toggleButton.Text = _toggleButton.Name;
_trayIcon.Icon = wasPaused ? _normal : _suspended;
_converter.Paused = !wasPaused;
}
private void ChangeHotkey(object sender, EventArgs e)
{
EnterHotkeyForm form = new();
HotkeyManager.EnableSelector(form.GetHotkeyTextbox());
form.Show();
}
internal void Exit(object sender, EventArgs e)
{
_trayIcon.Visible = false;
_timer.Stop();
Application.Exit();
}
}