Widow – Diff between revs 9 and 11

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 9 Rev 11
Line 1... Line 1...
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;
Line 5... Line 6...
5   6  
6 namespace Widow 7 namespace Widow
Line 37... Line 38...
37   38  
38 private RuleEditForm() 39 private RuleEditForm()
39 { 40 {
Line 40... Line 41...
40 InitializeComponent(); 41 InitializeComponent();
-   42  
41   43 desktopWindowsListBox.DisplayMember = nameof(Window.Title);
-   44 desktopWindowsListBox.SetDoubleBuffered();
Line 42... Line 45...
42 desktopWindowsListBox.DisplayMember = "Name"; 45 windowRulesListBox.DisplayMember = nameof(Window.Title);
43 windowRulesListBox.DisplayMember = "Name"; 46 windowRulesListBox.SetDoubleBuffered();
Line 44... Line 47...
44   47  
45 RefreshWindows(); 48 Task.Run(RefreshWindows);
46 } 49 }
Line 66... Line 69...
66   69  
Line 67... Line 70...
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 {
Line 73... Line 76...
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 {
Line 88... Line 91...
88 } 91 }
89 } 92 }
Line 90... Line 93...
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;
Line 96... Line 99...
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;
Line 108... Line 111...
108 if (found) 111 if (found)
109 { 112 {
110 return; 113 return;
111 } 114 }
Line 112... Line 115...
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;
Line 116... Line 119...
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  
Line 119... Line 130...
119 } 130 desktopWindowsListBox.Items.Add(newWindow);
120 } 131 }
121   132  
Line 122... Line 133...
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;
Line 142... Line 153...
142 if (window == null) 153 if (window == null)
143 { 154 {
144 return; 155 return;
145 } 156 }
Line 146... Line 157...
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();
Line 155... Line 166...
155 WindowHeight.Text = window.Height.ToString(); 166 WindowHeight.Text = window.Height.ToString();
156 } 167 }
Line 157... Line 168...
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);
Line 161... Line 172...
161 } 172 }
162   173  
163 private void RemoveButton_Click(object sender, EventArgs e) 174 private void RemoveButton_Click(object sender, EventArgs e)
Line 169... Line 180...
169 } 180 }
Line 170... Line 181...
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 { -  
176 index = i; 186 {
177 break; 187 continue;
-   188 }
-   189  
-   190 index = i;
178 } 191 break;
Line 179... Line 192...
179 } 192 }
180   193  
181 if (index == -1) 194 if (index == -1)
Line 196... Line 209...
196 } 209 }
Line 197... Line 210...
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; -  
204 break; 216 continue;
-   217 }
-   218  
-   219 found = true;
205 } 220 break;
Line 206... Line 221...
206 } 221 }
207   222  
208 if (found) 223 if (found)
Line 218... Line 233...
218 { 233 {
219 var selectedWindow = (Window) windowRulesListBox.SelectedItem; 234 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
Line 220... Line 235...
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;
Line 230... Line 245...
230 } 245 }
Line 231... Line 246...
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 {
Line 302... Line 317...
302   317  
Line 303... Line 318...
303 #region Private Methods 318 #region Private Methods
304   319  
305 private void RefreshWindows() 320 private void RefreshWindows()
306 { 321 {
-   322 foreach (var handle in Helpers.FindWindows((wnd, param) => true))
307 foreach (var desktopWindow in Helpers.GetDesktopWindows()) 323 {
-   324 var title = Helpers.GetWindowTitle(handle);
-   325 if (string.IsNullOrEmpty(title))
-   326 {
-   327 continue;
-   328 }
-   329  
308 { 330 var process = Helpers.GetProcessName(handle);
309 if (string.IsNullOrEmpty(desktopWindow.Title)) 331 if (string.IsNullOrEmpty(process))
310 { 332 {
Line 311... Line 333...
311 continue; 333 continue;
312 } 334 }
-   335  
-   336 if (!Natives.GetWindowRect(handle, out var rect))
-   337 {
313   338 continue;
314 if (Natives.GetWindowRect(desktopWindow.Handle, out var rect)) 339 }
Line -... Line 340...
-   340  
-   341 var window = new Window(process, title, rect.Top, rect.Left, rect.Right - rect.Left,
315 { 342 rect.Bottom - rect.Top);
316 var window = new Window(desktopWindow.Title, rect.Top, rect.Left, rect.Right - rect.Left, 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 { -  
322 if (((Window) item).Name == window.Name) 348 {
-   349 if (((Window) item).Title != window.Title)
-   350 {
-   351 continue;
323 { 352 }
Line 324... Line 353...
324 found = true; 353  
325 break; 354 found = true;
326 } 355 break;
327 } 356 }
328   357  
329 if (!found) 358 if (!found)
330 { 359 {
Line 331... Line 360...
331 desktopWindowsListBox.Items.Add(window); 360 desktopWindowsListBox.Items.Add(window);
332 } 361 }