Widow – Diff between revs 9 and 11

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