Widow – Blame information for rev 11

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