Widow – Blame information for rev 13

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
11 office 3 using System.Threading.Tasks;
1 office 4 using System.Windows.Forms;
5 using Windows;
6  
7 namespace Widow
8 {
9 public partial class RuleEditForm : Form
10 {
11 #region Public Enums, Properties and Fields
12  
13 public Windows.Windows Windows { get; }
14  
6 office 15 public MainForm Form { get; set; }
1 office 16  
13 office 17 public DrawOverlayForm DrawOverlayForm { get; set; }
18  
1 office 19 #endregion
20  
21 #region Constructors, Destructors and Finalizers
22  
6 office 23 public RuleEditForm(MainForm mainForm, Windows.Windows windows) : this(windows)
1 office 24 {
6 office 25 Form = mainForm;
1 office 26  
27 Form.WindowCreated += Form_WindowCreated;
28 Form.WindowDestroyed += Form_WindowDestroyed;
29 }
30  
31 public RuleEditForm(Windows.Windows windows) : this()
32 {
33 Windows = windows;
34  
35 foreach (var window in windows.Window)
36 {
37 windowRulesListBox.Items.Add(window);
38 }
39 }
40  
41 private RuleEditForm()
42 {
43 InitializeComponent();
44  
11 office 45 desktopWindowsListBox.DisplayMember = nameof(Window.Title);
46 desktopWindowsListBox.SetDoubleBuffered();
47 windowRulesListBox.DisplayMember = nameof(Window.Title);
48 windowRulesListBox.SetDoubleBuffered();
1 office 49  
11 office 50 Task.Run(RefreshWindows);
1 office 51 }
52  
53 /// <summary>
54 /// Clean up any resources being used.
55 /// </summary>
56 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
57 protected override void Dispose(bool disposing)
58 {
59 if (disposing && components != null)
60 {
61 Form.WindowCreated -= Form_WindowCreated;
62 Form.WindowDestroyed -= Form_WindowDestroyed;
63  
64 components.Dispose();
65 }
66  
67 base.Dispose(disposing);
68 }
69  
70 #endregion
71  
72 #region Event Handlers
73  
74 private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e)
75 {
11 office 76 if (string.IsNullOrEmpty(e.Title))
1 office 77 {
78 return;
79 }
80  
81 var indices = new List<int>();
82 for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i)
83 {
11 office 84 if (((Window) desktopWindowsListBox.Items[i]).Title == e.Title)
1 office 85 {
86 indices.Add(i);
87 }
88 }
89  
90 foreach (var index in indices)
91 {
92 desktopWindowsListBox.Items.RemoveAt(index);
93 }
94 }
95  
96 private void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
97 {
11 office 98 if (string.IsNullOrEmpty(e.Title))
1 office 99 {
100 return;
101 }
102  
103 var found = false;
104 foreach (var window in desktopWindowsListBox.Items)
105 {
11 office 106 if (((Window) window).Title == e.Title)
1 office 107 {
108 found = true;
109 break;
110 }
111 }
112  
113 if (found)
114 {
115 return;
116 }
117  
11 office 118 if (!Natives.GetWindowRect(e.Handle, out var rect))
1 office 119 {
11 office 120 return;
121 }
1 office 122  
11 office 123 var process = Helpers.GetProcessName(e.Handle);
124 if (string.IsNullOrEmpty(process))
125 {
126 return;
1 office 127 }
11 office 128  
129 var newWindow = new Window(process, e.Title, rect.Top, rect.Left, rect.Right - rect.Left,
130 rect.Bottom - rect.Top);
131  
132 desktopWindowsListBox.Items.Add(newWindow);
1 office 133 }
134  
135 private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e)
136 {
137 var listBox = (ListBox) sender;
138  
139 if (listBox.SelectedIndex == -1)
140 {
11 office 141 WindowTitle.Text = string.Empty;
9 office 142 ignoreLeftCheckBox.Checked = false;
1 office 143 WindowLeft.Text = string.Empty;
9 office 144 ignoreTopCheckBox.Checked = false;
1 office 145 WindowTop.Text = string.Empty;
9 office 146 ignoreWidthCheckBox.Checked = false;
1 office 147 WindowWidth.Text = string.Empty;
9 office 148 ignoreHeightCheckBox.Checked = false;
1 office 149 WindowHeight.Text = string.Empty;
150 return;
151 }
152  
153 var window = (Window) listBox.SelectedItem;
154  
155 if (window == null)
156 {
157 return;
158 }
159  
11 office 160 WindowTitle.Text = window.Title;
9 office 161 ignoreLeftCheckBox.Checked = window.IgnoreLeft;
1 office 162 WindowLeft.Text = window.Left.ToString();
9 office 163 ignoreTopCheckBox.Checked = window.IgnoreTop;
1 office 164 WindowTop.Text = window.Top.ToString();
9 office 165 ignoreWidthCheckBox.Checked = window.IgnoreWidth;
1 office 166 WindowWidth.Text = window.Width.ToString();
9 office 167 ignoreHeightCheckBox.Checked = window.IgnoreHeight;
1 office 168 WindowHeight.Text = window.Height.ToString();
169 }
170  
171 private void RefreshButton_Click(object sender, EventArgs e)
172 {
11 office 173 Task.Run(RefreshWindows);
1 office 174 }
175  
176 private void RemoveButton_Click(object sender, EventArgs e)
177 {
178 var window = (Window) windowRulesListBox.SelectedItem;
179 if (window == null)
180 {
181 return;
182 }
183  
184 var index = -1;
185 for (var i = 0; i < windowRulesListBox.Items.Count; ++i)
186 {
11 office 187 if (((Window) windowRulesListBox.Items[i]).Title != window.Title)
1 office 188 {
11 office 189 continue;
1 office 190 }
11 office 191  
192 index = i;
193 break;
1 office 194 }
195  
196 if (index == -1)
197 {
198 return;
199 }
200  
201 windowRulesListBox.Items.RemoveAt(index);
202 Windows.Window.Remove(window);
203 }
204  
205 private void AddButton_Click(object sender, EventArgs e)
206 {
207 var window = (Window) desktopWindowsListBox.SelectedItem;
208 if (window == null)
209 {
210 return;
211 }
212  
213 var found = false;
214 foreach (var windowRule in windowRulesListBox.Items)
215 {
11 office 216 if (((Window) windowRule).Title != window.Title)
1 office 217 {
11 office 218 continue;
1 office 219 }
11 office 220  
221 found = true;
222 break;
1 office 223 }
224  
225 if (found)
226 {
227 return;
228 }
229  
230 windowRulesListBox.Items.Add(window);
231 Windows.Window.Add(window);
232 }
233  
234 private void OnWindowSettings_TextChanged(object sender, EventArgs e)
235 {
236 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
237  
238 if (selectedWindow == null)
239 {
11 office 240 WindowTitle.Text = string.Empty;
1 office 241 WindowLeft.Text = string.Empty;
242 WindowTop.Text = string.Empty;
243 WindowWidth.Text = string.Empty;
244 WindowHeight.Text = string.Empty;
245  
246 return;
247 }
248  
249 var textBox = (TextBox) sender;
250 switch (textBox.Name)
251 {
11 office 252 case nameof(WindowTitle):
253 selectedWindow.Title = textBox.Text;
1 office 254 break;
255 case nameof(WindowLeft):
256 if (int.TryParse(textBox.Text, out var left))
257 {
258 selectedWindow.Left = left;
259 }
260  
261 break;
262 case nameof(WindowTop):
263 if (int.TryParse(textBox.Text, out var top))
264 {
265 selectedWindow.Top = top;
266 }
267  
268 break;
269 case nameof(WindowWidth):
270 if (int.TryParse(textBox.Text, out var width))
271 {
272 selectedWindow.Width = width;
273 }
274  
275 break;
276 case nameof(WindowHeight):
277 if (int.TryParse(textBox.Text, out var height))
278 {
279 selectedWindow.Height = height;
280 }
281  
282 break;
283 }
284 }
285  
9 office 286 private void OnIgnoreWindowSettings_CheckedChanged(object sender, EventArgs e)
287 {
288 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
289  
290 if (selectedWindow == null)
291 {
292 ignoreLeftCheckBox.Checked = false;
293 ignoreTopCheckBox.Checked = false;
294 ignoreWidthCheckBox.Checked = false;
295 ignoreHeightCheckBox.Checked = false;
296  
297 return;
298 }
299  
300 var checkBox = (CheckBox) sender;
301 switch (checkBox.Name)
302 {
303 case nameof(ignoreHeightCheckBox):
304 selectedWindow.IgnoreHeight = checkBox.Checked;
305 break;
306 case nameof(ignoreWidthCheckBox):
307 selectedWindow.IgnoreWidth = checkBox.Checked;
308 break;
309 case nameof(ignoreTopCheckBox):
310 selectedWindow.IgnoreTop = checkBox.Checked;
311 break;
312 case nameof(ignoreLeftCheckBox):
313 selectedWindow.IgnoreLeft = checkBox.Checked;
314 break;
315 }
316 }
317  
1 office 318 #endregion
319  
320 #region Private Methods
321  
322 private void RefreshWindows()
323 {
11 office 324 foreach (var handle in Helpers.FindWindows((wnd, param) => true))
1 office 325 {
11 office 326 var title = Helpers.GetWindowTitle(handle);
327 if (string.IsNullOrEmpty(title))
1 office 328 {
329 continue;
330 }
331  
11 office 332 var process = Helpers.GetProcessName(handle);
333 if (string.IsNullOrEmpty(process))
1 office 334 {
11 office 335 continue;
336 }
1 office 337  
11 office 338 if (!Natives.GetWindowRect(handle, out var rect))
339 {
340 continue;
341 }
342  
343 var window = new Window(process, title, rect.Top, rect.Left, rect.Right - rect.Left,
344 rect.Bottom - rect.Top);
345  
346 this.Execute(() =>
347 {
1 office 348 var found = false;
349 foreach (var item in desktopWindowsListBox.Items)
350 {
11 office 351 if (((Window) item).Title != window.Title)
1 office 352 {
11 office 353 continue;
1 office 354 }
11 office 355  
356 found = true;
357 break;
1 office 358 }
359  
360 if (!found)
361 {
362 desktopWindowsListBox.Items.Add(window);
363 }
11 office 364 });
1 office 365 }
366 }
367  
368 #endregion
13 office 369  
370 private void DrawButton_Click(object sender, EventArgs e)
371 {
372 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
373 if (selectedWindow == null)
374 {
375 return;
376 }
377  
378 if (DrawOverlayForm != null)
379 {
380 return;
381 }
382  
383 DrawOverlayForm = new DrawOverlayForm();
384 DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn;
385 DrawOverlayForm.Closed += DrawOverlayForm_Closed;
386 DrawOverlayForm.Show();
387  
388 }
389  
390 private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e)
391 {
392 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
393 if (selectedWindow == null)
394 {
395 return;
396 }
397  
398 WindowLeft.Text = e.Left.ToString();
399 WindowTop.Text = e.Top.ToString();
400 WindowWidth.Text = e.Width.ToString();
401 WindowHeight.Text = e.Height.ToString();
402  
403 selectedWindow.Left = e.Left;
404 selectedWindow.Top = e.Top;
405 selectedWindow.Width = e.Width;
406 selectedWindow.Height = e.Height;
407 }
408  
409 private void DrawOverlayForm_Closed(object sender, EventArgs e)
410 {
411 DrawOverlayForm.Closed -= DrawOverlayForm_Closed;
412 DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn;
413 DrawOverlayForm.Dispose();
414 DrawOverlayForm = null;
415 }
1 office 416 }
417 }