Widow – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.Windows.Forms;
4 using Windows;
5  
6 namespace Widow
7 {
8 public partial class RuleEditForm : Form
9 {
10 #region Public Enums, Properties and Fields
11  
12 public Windows.Windows Windows { get; }
13  
14 public Form1 Form { get; set; }
15  
16 #endregion
17  
18 #region Constructors, Destructors and Finalizers
19  
20 public RuleEditForm(Form1 form1, Windows.Windows windows) : this(windows)
21 {
22 Form = form1;
23  
24 Form.WindowCreated += Form_WindowCreated;
25 Form.WindowDestroyed += Form_WindowDestroyed;
26 }
27  
28 public RuleEditForm(Windows.Windows windows) : this()
29 {
30 Windows = windows;
31  
32 foreach (var window in windows.Window)
33 {
34 windowRulesListBox.Items.Add(window);
35 }
36 }
37  
38 private RuleEditForm()
39 {
40 InitializeComponent();
41  
42 desktopWindowsListBox.DisplayMember = "Name";
43 windowRulesListBox.DisplayMember = "Name";
44  
45 RefreshWindows();
46 }
47  
48 /// <summary>
49 /// Clean up any resources being used.
50 /// </summary>
51 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
52 protected override void Dispose(bool disposing)
53 {
54 if (disposing && components != null)
55 {
56 Form.WindowCreated -= Form_WindowCreated;
57 Form.WindowDestroyed -= Form_WindowDestroyed;
58  
59 components.Dispose();
60 }
61  
62 base.Dispose(disposing);
63 }
64  
65 #endregion
66  
67 #region Event Handlers
68  
69 private void Form_WindowDestroyed(object sender, WindowDestroyedEventArgs e)
70 {
71 if (string.IsNullOrEmpty(e.Name))
72 {
73 return;
74 }
75  
76 var indices = new List<int>();
77 for (var i = 0; i < desktopWindowsListBox.Items.Count; ++i)
78 {
79 if (((Window) desktopWindowsListBox.Items[i]).Name == e.Name)
80 {
81 indices.Add(i);
82 }
83 }
84  
85 foreach (var index in indices)
86 {
87 desktopWindowsListBox.Items.RemoveAt(index);
88 }
89 }
90  
91 private void Form_WindowCreated(object sender, WindowCreatedEventArgs e)
92 {
93 if (string.IsNullOrEmpty(e.Name))
94 {
95 return;
96 }
97  
98 var found = false;
99 foreach (var window in desktopWindowsListBox.Items)
100 {
101 if (((Window) window).Name == e.Name)
102 {
103 found = true;
104 break;
105 }
106 }
107  
108 if (found)
109 {
110 return;
111 }
112  
113 if (Natives.GetWindowRect(e.Handle, out var rect))
114 {
115 var window = new Window(e.Name, rect.Top, rect.Left, rect.Right - rect.Left,
116 rect.Bottom - rect.Top);
117  
118 desktopWindowsListBox.Items.Add(window);
119 }
120 }
121  
122 private void WindowRulesListBox_SelectedIndexChanged(object sender, EventArgs e)
123 {
124 var listBox = (ListBox) sender;
125  
126 if (listBox.SelectedIndex == -1)
127 {
128 WindowName.Text = string.Empty;
129 WindowLeft.Text = string.Empty;
130 WindowTop.Text = string.Empty;
131 WindowWidth.Text = string.Empty;
132 WindowHeight.Text = string.Empty;
133 return;
134 }
135  
136 var window = (Window) listBox.SelectedItem;
137  
138 if (window == null)
139 {
140 return;
141 }
142  
143 WindowName.Text = window.Name;
144 WindowLeft.Text = window.Left.ToString();
145 WindowTop.Text = window.Top.ToString();
146 WindowWidth.Text = window.Width.ToString();
147 WindowHeight.Text = window.Height.ToString();
148 }
149  
150 private void RefreshButton_Click(object sender, EventArgs e)
151 {
152 RefreshWindows();
153 }
154  
155 private void RemoveButton_Click(object sender, EventArgs e)
156 {
157 var window = (Window) windowRulesListBox.SelectedItem;
158 if (window == null)
159 {
160 return;
161 }
162  
163 var index = -1;
164 for (var i = 0; i < windowRulesListBox.Items.Count; ++i)
165 {
166 if (((Window) windowRulesListBox.Items[i]).Name == window.Name)
167 {
168 index = i;
169 break;
170 }
171 }
172  
173 if (index == -1)
174 {
175 return;
176 }
177  
178 windowRulesListBox.Items.RemoveAt(index);
179 Windows.Window.Remove(window);
180 }
181  
182 private void AddButton_Click(object sender, EventArgs e)
183 {
184 var window = (Window) desktopWindowsListBox.SelectedItem;
185 if (window == null)
186 {
187 return;
188 }
189  
190 var found = false;
191 foreach (var windowRule in windowRulesListBox.Items)
192 {
193 if (((Window) windowRule).Name == window.Name)
194 {
195 found = true;
196 break;
197 }
198 }
199  
200 if (found)
201 {
202 return;
203 }
204  
205 windowRulesListBox.Items.Add(window);
206 Windows.Window.Add(window);
207 }
208  
209 private void OnWindowSettings_TextChanged(object sender, EventArgs e)
210 {
211 var selectedWindow = (Window) windowRulesListBox.SelectedItem;
212  
213 if (selectedWindow == null)
214 {
215 WindowName.Text = string.Empty;
216 WindowLeft.Text = string.Empty;
217 WindowTop.Text = string.Empty;
218 WindowWidth.Text = string.Empty;
219 WindowHeight.Text = string.Empty;
220  
221 return;
222 }
223  
224 var textBox = (TextBox) sender;
225 switch (textBox.Name)
226 {
227 case nameof(WindowName):
228 selectedWindow.Name = textBox.Text;
229 break;
230 case nameof(WindowLeft):
231 if (int.TryParse(textBox.Text, out var left))
232 {
233 selectedWindow.Left = left;
234 }
235  
236 break;
237 case nameof(WindowTop):
238 if (int.TryParse(textBox.Text, out var top))
239 {
240 selectedWindow.Top = top;
241 }
242  
243 break;
244 case nameof(WindowWidth):
245 if (int.TryParse(textBox.Text, out var width))
246 {
247 selectedWindow.Width = width;
248 }
249  
250 break;
251 case nameof(WindowHeight):
252 if (int.TryParse(textBox.Text, out var height))
253 {
254 selectedWindow.Height = height;
255 }
256  
257 break;
258 }
259 }
260  
261 #endregion
262  
263 #region Private Methods
264  
265 private void RefreshWindows()
266 {
267 foreach (var desktopWindow in Helpers.GetDesktopWindows())
268 {
269 if (string.IsNullOrEmpty(desktopWindow.Title))
270 {
271 continue;
272 }
273  
274 if (Natives.GetWindowRect(desktopWindow.Handle, out var rect))
275 {
276 var window = new Window(desktopWindow.Title, rect.Top, rect.Left, rect.Right - rect.Left,
277 rect.Bottom - rect.Top);
278  
279 var found = false;
280 foreach (var item in desktopWindowsListBox.Items)
281 {
282 if (((Window) item).Name == window.Name)
283 {
284 found = true;
285 break;
286 }
287 }
288  
289 if (!found)
290 {
291 desktopWindowsListBox.Items.Add(window);
292 }
293 }
294 }
295 }
296  
297 #endregion
298 }
299 }