forked from kurumpa/dotSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SettingsForm.cs
315 lines (298 loc) · 9.48 KB
/
SettingsForm.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace dotSwitcher
{
public partial class SettingsForm : Form
{
public event EventHandler<EventArgs> Exit;
Settings settings;
Switcher engine;
public SettingsForm(Settings settings, Switcher engine)
{
this.settings = settings;
this.engine = engine;
engine.Error += OnEngineError;
InitializeComponent();
InitializeTrayIcon();
InitializeHotkeyBoxes();
UpdateUi();
}
/**
* SETTINGS FORM
*/
void ShowForm()
{
engine.Stop();
kbdHook.Start();
icon.Hide();
TopMost = true;
UpdateUi();
Show();
}
void HideForm()
{
kbdHook.Stop();
engine.Start();
if (settings.ShowTrayIcon == true)
{
icon.Show();
}
TopMost = false;
UpdateUi();
Hide();
}
// Update input values and icon state
void UpdateUi()
{
textBoxSwitchHotkey.Text = settings.SwitchHotkey.ToString();
textBoxConvertHotkey.Text = settings.ConvertSelectionHotkey.ToString();
checkBoxAutorun.Checked = settings.AutoStart == true;
checkBoxTrayIcon.Checked = settings.ShowTrayIcon == true;
DisplaySwitchDelay(settings.SwitchDelay);
icon.SetRunning(engine.IsStarted());
}
// also ESC
void buttonCancelSettings_Click(object sender, EventArgs e)
{
ResetSettings();
HideForm();
}
void buttonSaveSettings_Click(object sender, EventArgs e)
{
if (settings.SwitchHotkey.Alt || settings.SwitchHotkey.Win)
{
MessageBox.Show("Sorry, win+ and alt+ hotkeys are not supported yet");
return;
}
SaveSettings();
HideForm();
}
void buttonExit_Click(object sender, EventArgs e)
{
ResetSettings();
OnExit();
}
// initial hidden state
void SettingsForm_Shown(object sender, EventArgs e)
{
HideForm();
}
// prevent the form from user-initiated closing ([x] click, Alt+F4, closing from taskbar)
// (acts as Cancel)
void SettingsForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason != CloseReason.UserClosing)
{
return;
}
e.Cancel = true;
ResetSettings();
HideForm();
}
// receive window message from another instance
protected override void WndProc(ref Message m)
{
if (m.Msg == LowLevelAdapter.WM_SHOW_SETTINGS)
{
ShowForm();
}
base.WndProc(ref m);
}
/**
* TRAY ICON
*/
TrayIcon icon;
void InitializeTrayIcon()
{
icon = new TrayIcon(settings.ShowTrayIcon);
icon.DoubleClick += icon_SettingsClick;
icon.ExitClick += icon_ExitClick;
icon.SettingsClick += icon_SettingsClick;
icon.TogglePowerClick += icon_TogglePowerClick;
}
void icon_TogglePowerClick(object sender, EventArgs e)
{
if (engine.IsStarted())
{
engine.Stop();
UpdateUi();
}
else
{
engine.Start();
UpdateUi();
}
}
void icon_SettingsClick(object sender, EventArgs e)
{
ShowForm();
}
void icon_ExitClick(object sender, EventArgs e)
{
OnExit();
}
void OnEngineError(object sender, SwitcherErrorArgs e)
{
icon.ShowTooltip(e.Error.Message, ToolTipIcon.Error);
}
/**
* HOTKEY INPUTS
*/
KeyboardHook kbdHook;
KeyboardEventArgs currentHotkey;
HotKeyType currentHotkeyType;
enum HotKeyType { None, Switch, Convert }
void InitializeHotkeyBoxes()
{
textBoxSwitchHotkey.GotFocus += (s, e) => currentHotkeyType = HotKeyType.Switch;
textBoxSwitchHotkey.Enter += (s, e) => currentHotkeyType = HotKeyType.Switch;
textBoxSwitchHotkey.LostFocus += (s, e) => ApplyCurrentHotkey();
textBoxSwitchHotkey.Leave += (s, e) => ApplyCurrentHotkey();
textBoxConvertHotkey.GotFocus += (s, e) => currentHotkeyType = HotKeyType.Convert;
textBoxConvertHotkey.Enter += (s, e) => currentHotkeyType = HotKeyType.Convert;
textBoxConvertHotkey.LostFocus += (s, e) => ApplyCurrentHotkey();
textBoxConvertHotkey.Leave += (s, e) => ApplyCurrentHotkey();
currentHotkeyType = HotKeyType.None;
kbdHook = new KeyboardHook();
kbdHook.KeyboardEvent += kbdHook_KeyboardEvent;
}
void kbdHook_KeyboardEvent(object sender, KeyboardEventArgs e)
{
if (currentHotkeyType != HotKeyType.None)
{
var vk = e.KeyCode;
if (vk == Keys.Escape)
{
e.Handled = true;
ResetCurrentHotkey();
return;
}
if (vk != Keys.LMenu && vk != Keys.RMenu
&& vk != Keys.LWin && vk != Keys.RWin
&& vk != Keys.LShiftKey && vk != Keys.RShiftKey
&& vk != Keys.LControlKey && vk != Keys.RControlKey)
{
e.Handled = true;
}
SetCurrentHotkeyInputText(e.ToString());
currentHotkey = e;
}
}
// TODO: refactor this (make HotkeyInput : TextBox)
void SetCurrentHotkeyInputText(string text)
{
TextBox currentTextBox;
switch (currentHotkeyType)
{
case HotKeyType.Switch:
currentTextBox = textBoxSwitchHotkey;
break;
case HotKeyType.Convert:
currentTextBox = textBoxConvertHotkey;
break;
default:
currentTextBox = null;
break;
}
if (currentTextBox == null) { return; }
Invoke((MethodInvoker)delegate { currentTextBox.Text = text; });
}
void ResetCurrentHotkey()
{
switch (currentHotkeyType)
{
case HotKeyType.Switch:
currentHotkey = settings.SwitchHotkey;
break;
case HotKeyType.Convert:
currentHotkey = settings.ConvertSelectionHotkey;
break;
default:
currentHotkey = null;
break;
}
SetCurrentHotkeyInputText(currentHotkey == null ? "" : currentHotkey.ToString());
}
void ApplyCurrentHotkey()
{
if (!Visible || currentHotkey == null)
{
return;
}
switch (currentHotkeyType)
{
case HotKeyType.Switch:
settings.SwitchHotkey = currentHotkey;
break;
case HotKeyType.Convert:
settings.ConvertSelectionHotkey = currentHotkey;
break;
default:
break;
}
currentHotkeyType = HotKeyType.None;
}
/**
* SETTINGS
*/
void SaveSettings()
{
settings.Save();
if (settings.AutoStart == true) { LowLevelAdapter.CreateAutorunShortcut(); }
else { LowLevelAdapter.DeleteAutorunShortcut(); }
}
void ResetSettings()
{
settings.Reload();
}
/**
* OTHER INPUTS
*/
void checkBoxAutorun_CheckedChanged(object sender, EventArgs e)
{
settings.AutoStart = checkBoxAutorun.Checked;
}
private void checkBoxTrayIcon_CheckedChanged(object sender, EventArgs e)
{
settings.ShowTrayIcon = checkBoxTrayIcon.Checked;
}
void DisplaySwitchDelay(int delay)
{
textBoxDelay.Text = delay.ToString() + " ms";
}
private void textBoxDelay_TextChanged(object sender, EventArgs e)
{
short delay = 0;
if (!Int16.TryParse(Regex.Replace(textBoxDelay.Text, "[^0-9]", ""), out delay) || delay < 1)
{
delay = 1;
}
settings.SwitchDelay = delay;
DisplaySwitchDelay(delay);
}
/**
* MISC
*/
void OnExit()
{
engine.Stop();
if (Exit != null)
{
Exit(this, null);
}
}
private void buttonGithub_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/kurumpa/dotSwitcher/issues");
}
}
}