Widow – Blame information for rev 14

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  
14 office 318 private void DrawButton_Click(object sender, EventArgs e)
319 {
320 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
321 if (selectedWindow == null)
322 {
323 return;
324 }
325  
326 if (DrawOverlayForm != null)
327 {
328 return;
329 }
330  
331 DrawOverlayForm = new DrawOverlayForm();
332 DrawOverlayForm.WindowDrawn += DrawOverlayForm_WindowDrawn;
333 DrawOverlayForm.Closed += DrawOverlayForm_Closed;
334 DrawOverlayForm.Show();
335 }
336  
337 private void DrawOverlayForm_WindowDrawn(object sender, WindowDrawnEventArgs e)
338 {
339 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
340 if (selectedWindow == null)
341 {
342 return;
343 }
344  
345 WindowLeft.Text = e.Left.ToString();
346 WindowTop.Text = e.Top.ToString();
347 WindowWidth.Text = e.Width.ToString();
348 WindowHeight.Text = e.Height.ToString();
349  
350 selectedWindow.Left = e.Left;
351 selectedWindow.Top = e.Top;
352 selectedWindow.Width = e.Width;
353 selectedWindow.Height = e.Height;
354 }
355  
356 private void DrawOverlayForm_Closed(object sender, EventArgs e)
357 {
358 DrawOverlayForm.Closed -= DrawOverlayForm_Closed;
359 DrawOverlayForm.WindowDrawn -= DrawOverlayForm_WindowDrawn;
360 DrawOverlayForm.Dispose();
361 DrawOverlayForm = null;
362 }
363  
1 office 364 #endregion
365  
366 #region Private Methods
367  
368 private void RefreshWindows()
369 {
11 office 370 foreach (var handle in Helpers.FindWindows((wnd, param) => true))
1 office 371 {
14 office 372 var windowTitle = Helpers.GetWindowTitle(handle);
373 if (string.IsNullOrEmpty(windowTitle))
1 office 374 {
375 continue;
376 }
377  
14 office 378 var windowClass = Helpers.GetWindowClass(handle);
379 if (string.IsNullOrEmpty(windowClass))
1 office 380 {
11 office 381 continue;
382 }
1 office 383  
11 office 384 if (!Natives.GetWindowRect(handle, out var rect))
385 {
386 continue;
387 }
388  
14 office 389 var window = new Window(windowClass, windowTitle, rect.Top, rect.Left, rect.Right - rect.Left,
11 office 390 rect.Bottom - rect.Top);
391  
392 this.Execute(() =>
393 {
1 office 394 var found = false;
395 foreach (var item in desktopWindowsListBox.Items)
396 {
11 office 397 if (((Window) item).Title != window.Title)
1 office 398 {
11 office 399 continue;
1 office 400 }
11 office 401  
402 found = true;
403 break;
1 office 404 }
405  
406 if (!found)
407 {
408 desktopWindowsListBox.Items.Add(window);
409 }
11 office 410 });
1 office 411 }
412 }
413  
414 #endregion
415 }
416 }