-
Notifications
You must be signed in to change notification settings - Fork 2
/
MainForm.cs
371 lines (319 loc) · 12.3 KB
/
MainForm.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
using DmLib.Window;
using OnTopper.Properties;
using System;
using System.Diagnostics;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using OnTopper.Util;
using Action = OnTopper.Util.Action;
namespace OnTopper
{
public partial class MainForm : Form
{
private readonly SettingsForm settingsForm = new SettingsForm();
private readonly AboutForm aboutForm = new AboutForm();
private readonly TransparencyForm transparencyForm = new TransparencyForm();
private readonly SizeForm sizeForm = new SizeForm();
private readonly LogForm logForm = new LogForm();
private bool balloonShowed;
private State.WINDOW_STATE newState;
private readonly Settings settings = Settings.Default;
public MainForm()
{
InitializeComponent();
Thread.CurrentThread.CurrentUICulture = new CultureInfo(settings.LanguageAbbreviation.ToLower());
Controls.Clear();
InitializeComponent();
listBoxProcesses.DisplayMember = "ProcessName";
SetNotifyIcon();
UpdateProcesses();
var updateService = UpdateService.GetInstance();
if (!updateService.UpdateAvailable()) return;
var res = MessageBox.Show(
$"New version {updateService.GetWebVersion()} is available, want to install it now?",
"Update", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res != DialogResult.Yes) return;
updateService.UpdateDownloaded += OnUpdateDownloaded;
updateService.DownloadUpdate();
}
private static void OnUpdateDownloaded(object sender, EventArgs e)
{
MessageBox.Show("Update downloaded, want to restart now?", "Update", MessageBoxButtons.YesNo,
MessageBoxIcon.Question);
}
private void AddActionToLogForm(Action action)
{
logForm.AddAction(action);
}
private void SetWindowState(State.WINDOW_STATE state)
{
try
{
State.SetWindowState((Process) listBoxProcesses.SelectedItem, state);
}
catch (ProcessNotExistsException)
{
MessageBox.Show(LocalizedMessageProvider.GetMessage("PROCESS_DOESNT_EXISTS"),
LocalizedMessageProvider.GetMessage("ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Error);
UpdateProcesses();
}
}
private string GetProcessNameByIndex(int index)
{
try
{
return ((Process) listBoxProcesses.Items[index]).ProcessName;
}
catch (Exception)
{
return null;
}
}
private bool InAutoRunAndMinimized()
{
return settings.AutoHide && settings.AutoStart;
}
private void UpdateProcesses()
{
var selected = listBoxProcesses.SelectedIndex;
var selectedName = GetProcessNameByIndex(selected);
listBoxProcesses.BeginUpdate();
listBoxProcesses.Items.Clear();
foreach (var process in Process.GetProcesses())
{
if (settingsForm.HideNonInteractive)
{
if (!State.HasMainWindow(process)) continue;
listBoxProcesses.Items.Add(process);
continue;
}
listBoxProcesses.Items.Add(process);
}
listBoxProcesses.EndUpdate();
if (selected == -1) return;
if (selectedName.Equals(GetProcessNameByIndex(selected)))
{
listBoxProcesses.SelectedIndex = selected;
}
}
private static void ShowSelectProcessMessageBox()
{
MessageBox.Show(LocalizedMessageProvider.GetMessage("SELECT_PROCESS"),
LocalizedMessageProvider.GetMessage("ERROR"), MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
private void SetNotifyIcon()
{
var iconMenu = new ContextMenu();
var openItem = new MenuItem(LocalizedMessageProvider.GetMessage("OPEN"));
var hideItem = new MenuItem(LocalizedMessageProvider.GetMessage("HIDE_IN_TASKBAR"));
if (InAutoRunAndMinimized())
{
hideItem.Checked = true;
}
var closeItem = new MenuItem(LocalizedMessageProvider.GetMessage("CLOSE"));
var handler = new EventHandler(OnClickIconMenuItem);
openItem.Click += handler;
hideItem.Click += handler;
closeItem.Click += handler;
iconMenu.MenuItems.Add(openItem);
iconMenu.MenuItems.Add(hideItem);
iconMenu.MenuItems.Add(closeItem);
notifyIcon.Text = "OnTopper";
notifyIcon.BalloonTipTitle = LocalizedMessageProvider.GetMessage("MINIMIZED");
notifyIcon.BalloonTipText = LocalizedMessageProvider.GetMessage("MINIMIZED_RIGHT_CLICK");
notifyIcon.ContextMenu = iconMenu;
}
private bool ToggleTaskbarVisibility()
{
ShowInTaskbar = !ShowInTaskbar;
return !ShowInTaskbar;
}
private void OnClickIconMenuItem(object sender, EventArgs e)
{
if (!(sender is MenuItem item)) return;
var text = item.Text;
if (text.Equals(LocalizedMessageProvider.GetMessage("OPEN")))
{
WindowState = FormWindowState.Normal;
}
else if (text.Equals(LocalizedMessageProvider.GetMessage("HIDE_IN_TASKBAR")))
{
item.Checked = ToggleTaskbarVisibility();
}
else if (text.Equals(LocalizedMessageProvider.GetMessage("CLOSE")))
{
Close();
}
}
private void ButtonUpdate_Click(object sender, EventArgs e)
{
UpdateProcesses();
}
private void ButtonSetTop_Click(object sender, EventArgs e)
{
if (listBoxProcesses.SelectedItem == null)
{
ShowSelectProcessMessageBox();
return;
}
SetWindowState(newState);
logForm.AddAction(newState == State.WINDOW_STATE.TOP
? new TopStateAction(listBoxProcesses.SelectedItem as Process, State.WINDOW_STATE.TOP)
: new TopStateAction(listBoxProcesses.SelectedItem as Process, State.WINDOW_STATE.UNTOP));
newState = newState == State.WINDOW_STATE.TOP ? State.WINDOW_STATE.UNTOP : State.WINDOW_STATE.TOP;
RefreshTopButton();
}
private void ButtonThisOnTop_Click(object sender, EventArgs e)
{
if (TopMost)
{
buttonThisOnTop.Text = LocalizedMessageProvider.GetMessage("SET_THIS");
TopMost = false;
}
else
{
buttonThisOnTop.Text = LocalizedMessageProvider.GetMessage("UNSET_THIS");
TopMost = true;
}
}
private void ButtonAbout_Click(object sender, EventArgs e)
{
aboutForm.ShowDialogWithTopMostState(TopMost);
}
private void ButtonSettings_Click(object sender, EventArgs e)
{
settingsForm.ShowDialogWithTopMostState(TopMost);
listBoxProcesses.DisplayMember = settingsForm.ShowWindowTitles ? "MainWindowTitle" : "ProcessName";
if (settingsForm.TimerEnabled)
{
timer.Interval = settingsForm.Interval;
timer.Start();
}
else
{
timer.Stop();
}
UpdateProcesses();
}
private void TextBoxSearch_TextChanged(object sender, EventArgs e)
{
var index = listBoxProcesses.FindString(textBoxSearch.Text);
if (index != -1)
{
listBoxProcesses.SelectedIndex = index;
}
}
private void MainForm_SizeChanged(object sender, EventArgs e)
{
switch (WindowState)
{
case FormWindowState.Minimized:
{
notifyIcon.Visible = true;
if (!balloonShowed)
{
notifyIcon.ShowBalloonTip(5000);
balloonShowed = true;
}
break;
}
case FormWindowState.Maximized:
case FormWindowState.Normal:
notifyIcon.Visible = false;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private void Timer_Tick(object sender, EventArgs e)
{
UpdateProcesses();
}
private void ListBoxProcesses_MouseDoubleClick(object sender, MouseEventArgs e)
{
var index = listBoxProcesses.SelectedIndex;
if (index == -1) return;
try
{
var proc = (Process) listBoxProcesses.Items[index];
MessageBox.Show(
$"Priority: {proc.BasePriority}\n" +
$"Id: {proc.Id}\nName: {proc.ProcessName}\n" +
$"Responding: {proc.Responding}\n" +
$"Started: {proc.StartTime}\n" +
$"Top most: {State.IsTopMost(proc)}",
"Process info");
}
catch (Exception)
{
UpdateProcesses();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
if (InAutoRunAndMinimized())
{
WindowState = FormWindowState.Minimized;
ShowInTaskbar = false;
}
else
{
var updater = new Updater();
if (!updater.UpdateAvailable() || !settings.UpdateVersion) return;
var result = MessageBox.Show(LocalizedMessageProvider.GetMessage("NEW_VERSION_AVAILABLE_QUESTION"),
LocalizedMessageProvider.GetMessage("UPDATE"), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
Updater.InstallUpdate();
return;
}
settings.UpdateVersion = false;
settings.Save();
}
}
private void ButtonTransparency_Click(object sender, EventArgs e)
{
if (listBoxProcesses.SelectedItem == null)
{
ShowSelectProcessMessageBox();
return;
}
transparencyForm.ShowDialogWithTopMostState(TopMost);
var p = listBoxProcesses.SelectedItem as Process;
Transparency.SetWindowTransparency(p, transparencyForm.Current);
AddActionToLogForm(new OpacityAction(transparencyForm.Previous, transparencyForm.Current, p));
}
private void ButtonProperties_Click(object sender, EventArgs e)
{
if (listBoxProcesses.SelectedItem == null)
{
ShowSelectProcessMessageBox();
return;
}
sizeForm.ShowDialogAndSetWindowSize((Process) listBoxProcesses.SelectedItem, this.TopMost);
}
private void ButtonLog_Click(object sender, EventArgs e)
{
logForm.ShowDialogWithTopMostState(State.IsTopMost(Process.GetCurrentProcess()));
UpdateProcesses();
RefreshTopButton();
}
private void ListBoxProcesses_Click(object sender, EventArgs e)
{
if (listBoxProcesses.SelectedIndex == -1)
{
ShowSelectProcessMessageBox();
return;
}
var proc = listBoxProcesses.SelectedItem as Process;
newState = !State.IsTopMost(proc) ? State.WINDOW_STATE.TOP : State.WINDOW_STATE.UNTOP;
RefreshTopButton();
}
private void RefreshTopButton()
{
buttonSetTop.Text =
LocalizedMessageProvider.GetMessage(newState == State.WINDOW_STATE.TOP ? "SET_TOP" : "UNSET_TOP");
}
}
}