Widow – Diff between revs 28 and 29

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 28 Rev 29
1 using System; 1 using System;
2 using System.ComponentModel; 2 using System.ComponentModel;
3 using System.Configuration; 3 using System.Configuration;
4 using System.Globalization; 4 using System.Globalization;
5 using System.IO; 5 using System.IO;
6 using System.Threading.Tasks; 6 using System.Threading.Tasks;
7 using System.Windows.Forms; 7 using System.Windows.Forms;
8 using AutoUpdaterDotNET; 8 using AutoUpdaterDotNET;
9 using Widow.Properties; 9 using Widow.Properties;
10 using Widow.Serialization; 10 using Widow.Serialization;
11   11  
12 namespace Widow 12 namespace Widow
13 { 13 {
14 public partial class MainForm : Form 14 public partial class MainForm : Form
15 { 15 {
16 #region Public Events & Delegates 16 #region Public Events & Delegates
17   17  
18 public event EventHandler<WindowCreatedEventArgs> WindowCreated; 18 public event EventHandler<WindowCreatedEventArgs> WindowCreated;
19   19  
20 public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed; 20 public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed;
21   21  
22 #endregion 22 #endregion
23   23  
24 #region Public Enums, Properties and Fields 24 #region Public Enums, Properties and Fields
25   25  
26 public Windows.Windows Windows { get; set; } 26 public Windows.Windows Windows { get; set; }
27   27  
28 public RuleEditForm RuleEditForm { get; set; } 28 public RuleEditForm RuleEditForm { get; set; }
29   29  
30 public WindowManipulation WindowManipulation { get; set; } 30 public WindowManipulation WindowManipulation { get; set; }
31   31  
32 public AboutForm AboutForm { get; set; } 32 public AboutForm AboutForm { get; set; }
33   33  
34 public LogForm LogForm { get; private set; } 34 public LogForm LogForm { get; private set; }
35   35  
36 #endregion 36 #endregion
37   37  
38 #region Constructors, Destructors and Finalizers 38 #region Constructors, Destructors and Finalizers
39   39  
40 public MainForm() 40 public MainForm()
41 { 41 {
42 InitializeComponent(); 42 InitializeComponent();
43 AutoUpdater.Start("http://widow.grimore.org/update/update.xml"); 43 AutoUpdater.Start("http://widow.grimore.org/update/update.xml");
44   44  
45 // Upgrade settings if required. 45 // Upgrade settings if required.
46 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile) 46 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
47 { 47 {
48 Settings.Default.Upgrade(); 48 Settings.Default.Upgrade();
49 } 49 }
50   50  
51 // Bind to settings changed event. 51 // Bind to settings changed event.
52 Settings.Default.SettingsLoaded += Default_SettingsLoaded; 52 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
53 Settings.Default.SettingsSaving += Default_SettingsSaving; 53 Settings.Default.SettingsSaving += Default_SettingsSaving;
54 Settings.Default.PropertyChanged += Default_PropertyChanged; 54 Settings.Default.PropertyChanged += Default_PropertyChanged;
55   55  
56 Natives.RegisterShellHookWindow(Handle); 56 Natives.RegisterShellHookWindow(Handle);
57   57  
58 WindowManipulation = new WindowManipulation(Settings.Default, this); 58 WindowManipulation = new WindowManipulation(Settings.Default, this);
59   59  
60 LoadWindows().ContinueWith(task => { WindowManipulation.AddWindows(Windows); }); 60 LoadWindows().ContinueWith(task => { WindowManipulation.AddWindows(Windows); });
61 } 61 }
62   62  
63 /// <summary> 63 /// <summary>
64 /// Clean up any resources being used. 64 /// Clean up any resources being used.
65 /// </summary> 65 /// </summary>
66 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 66 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
67 protected override void Dispose(bool disposing) 67 protected override void Dispose(bool disposing)
68 { 68 {
69 if (disposing && components != null) 69 if (disposing && components != null)
70 { 70 {
71 Settings.Default.SettingsLoaded -= Default_SettingsLoaded; 71 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
72 Settings.Default.SettingsSaving -= Default_SettingsSaving; 72 Settings.Default.SettingsSaving -= Default_SettingsSaving;
73 Settings.Default.PropertyChanged -= Default_PropertyChanged; 73 Settings.Default.PropertyChanged -= Default_PropertyChanged;
74   74  
75 components.Dispose(); 75 components.Dispose();
76 } 76 }
77   77  
78 base.Dispose(disposing); 78 base.Dispose(disposing);
79 } 79 }
80   80  
81 #endregion 81 #endregion
82   82  
83 #region Private Overrides 83 #region Private Overrides
84   84  
85 protected override void WndProc(ref Message m) 85 protected override void WndProc(ref Message m)
86 { 86 {
87 var handle = m.LParam; 87 var handle = m.LParam;
88 var windowName = Helpers.GetWindowTitle(handle); 88 var windowName = Helpers.GetWindowTitle(handle);
89   89  
90 switch (m.WParam.ToInt32()) 90 switch (m.WParam.ToInt32())
91 { 91 {
92 case (int) Natives.WM.HSHELL_WINDOWCREATED: 92 case (int) Natives.WM.HSHELL_WINDOWCREATED:
93 WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle)); 93 WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle));
94 break; 94 break;
95 case (int) Natives.WM.HSHELL_WINDOWDESTROYED: 95 case (int) Natives.WM.HSHELL_WINDOWDESTROYED:
96 WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle)); 96 WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle));
97 break; 97 break;
98 } 98 }
99   99  
100 base.WndProc(ref m); 100 base.WndProc(ref m);
101 } 101 }
102   102  
103 #endregion 103 #endregion
104   104  
105 #region Event Handlers 105 #region Event Handlers
106   106  
107 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e) 107 private static void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
108 { 108 {
109 Settings.Default.Save(); 109 Settings.Default.Save();
110 } 110 }
111   111  
112 private static void Default_SettingsSaving(object sender, CancelEventArgs e) 112 private static void Default_SettingsSaving(object sender, CancelEventArgs e)
113 { 113 {
114 } 114 }
115   115  
116 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e) 116 private static void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
117 { 117 {
118 } 118 }
119   119  
120 private void NewToolStripMenuItem_Click(object sender, EventArgs e) 120 private void NewToolStripMenuItem_Click(object sender, EventArgs e)
121 { 121 {
122 if (RuleEditForm != null) 122 if (RuleEditForm != null)
123 { 123 {
124 return; 124 return;
125 } 125 }
126   126  
127 RuleEditForm = new RuleEditForm(this, Windows); 127 RuleEditForm = new RuleEditForm(this, Windows);
128 RuleEditForm.Closed += RuleEditForm_Closed; 128 RuleEditForm.Closed += RuleEditForm_Closed;
129 RuleEditForm.Show(); 129 RuleEditForm.Show();
130 } 130 }
131   131  
132 private async void RuleEditForm_Closed(object sender, EventArgs e) 132 private async void RuleEditForm_Closed(object sender, EventArgs e)
133 { 133 {
134 RuleEditForm.Closed -= RuleEditForm_Closed; 134 RuleEditForm.Closed -= RuleEditForm_Closed;
135 RuleEditForm.Dispose(); 135 RuleEditForm.Dispose();
136 RuleEditForm = null; 136 RuleEditForm = null;
137   137  
138 if (!Directory.Exists(Constants.UserApplicationDirectory)) 138 if (!Directory.Exists(Constants.UserApplicationDirectory))
139 { 139 {
140 Directory.CreateDirectory(Constants.UserApplicationDirectory); 140 Directory.CreateDirectory(Constants.UserApplicationDirectory);
141 } 141 }
142   142  
143 switch (await WindowsSerialization.Serialize(Windows, Constants.WindowsSettingsFile)) 143 switch (await WindowsSerialization.Serialize(Windows, Constants.WindowsSettingsFile))
144 { 144 {
145 case SerializationSuccess serializationSuccess: 145 case SerializationSuccess serializationSuccess:
146 if (LogForm != null) 146 if (LogForm != null)
147 { 147 {
148 LogForm.InvokeIfRequired(form => 148 LogForm.InvokeIfRequired(form =>
149 { 149 {
150 form.logTextBox.Text += "Windows saved." + Environment.NewLine; 150 form.logTextBox.Text += "Windows saved." + Environment.NewLine;
151 }); 151 });
152 } 152 }
153   153  
154 break; 154 break;
155 case SerializationFailure serializationFailure: 155 case SerializationFailure serializationFailure:
156 if (LogForm != null) 156 if (LogForm != null)
157 { 157 {
158 LogForm.InvokeIfRequired(form => 158 LogForm.InvokeIfRequired(form =>
159 { 159 {
160 form.logTextBox.Text += "Failed to save windows: " + 160 form.logTextBox.Text += "Failed to save windows: " +
161 serializationFailure.Exception.Message + Environment.NewLine; 161 serializationFailure.Exception.Message + Environment.NewLine;
162 }); 162 });
163 } 163 }
164   164  
165 break; 165 break;
166 } 166 }
167 } 167 }
168   168  
169 private void ExitToolStripMenuItem_Click(object sender, EventArgs e) 169 private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
170 { 170 {
-   171 Close();
-   172  
171 Application.Exit(); 173 Environment.Exit(0);
172 } 174 }
173   175  
174 private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 176 private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
175 { 177 {
176 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked; 178 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
177   179  
178 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot); 180 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
179 } 181 }
180   182  
181 private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 183 private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
182 { 184 {
183 Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked; 185 Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;
184   186  
185 WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate; 187 WindowManipulation.OnWindowCreate = Settings.Default.OnWindowCreate;
186 } 188 }
187   189  
188 private void AboutToolStripMenuItem_Click(object sender, EventArgs e) 190 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
189 { 191 {
190 // Show the about form. 192 // Show the about form.
191 AboutForm = new AboutForm(); 193 AboutForm = new AboutForm();
192 AboutForm.Show(); 194 AboutForm.Show();
193 } 195 }
194   196  
195 private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e) 197 private async void ApplyNowToolStripMenuItem_Click(object sender, EventArgs e)
196 { 198 {
197 await WindowManipulation.Apply(); 199 await WindowManipulation.Apply();
198 } 200 }
199   201  
200 private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e) 202 private void OnEveryToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
201 { 203 {
202 var toolStripMenuItem = (ToolStripMenuItem) sender; 204 var toolStripMenuItem = (ToolStripMenuItem) sender;
203   205  
204 if (!int.TryParse(toolStripTextBox1.Text, out var time)) 206 if (!int.TryParse(toolStripTextBox1.Text, out var time))
205 { 207 {
206 return; 208 return;
207 } 209 }
208   210  
209 Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; 211 Settings.Default.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
210 Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture); 212 Settings.Default.ApplyEveryTime = time.ToString(CultureInfo.InvariantCulture);
211   213  
212 WindowManipulation.ApplyEveryTime = time; 214 WindowManipulation.ApplyEveryTime = time;
213 WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked; 215 WindowManipulation.ApplyEveryTimeEnabled = toolStripMenuItem.Checked;
214 } 216 }
215   217  
216 private void NotifyIcon1_DoubleClick(object sender, EventArgs e) 218 private void NotifyIcon1_DoubleClick(object sender, EventArgs e)
217 { 219 {
218 if (RuleEditForm != null) 220 if (RuleEditForm != null)
219 { 221 {
220 return; 222 return;
221 } 223 }
222   224  
223 RuleEditForm = new RuleEditForm(this, Windows); 225 RuleEditForm = new RuleEditForm(this, Windows);
224 RuleEditForm.Closed += RuleEditForm_Closed; 226 RuleEditForm.Closed += RuleEditForm_Closed;
225 RuleEditForm.Show(); 227 RuleEditForm.Show();
226 } 228 }
227   229  
228 private async void NotifyIcon1_Click(object sender, EventArgs e) 230 private async void NotifyIcon1_Click(object sender, EventArgs e)
229 { 231 {
230 await WindowManipulation.Apply(); 232 await WindowManipulation.Apply();
231 } 233 }
232   234  
233 private void ShowLogToolStripMenuItem_Click(object sender, EventArgs e) 235 private void ShowLogToolStripMenuItem_Click(object sender, EventArgs e)
234 { 236 {
235 if (LogForm != null) 237 if (LogForm != null)
236 { 238 {
237 return; 239 return;
238 } 240 }
239   241  
240 LogForm = new LogForm(); 242 LogForm = new LogForm();
241 LogForm.Closed += LogForm_Closed; 243 LogForm.Closed += LogForm_Closed;
242 LogForm.Show(); 244 LogForm.Show();
243 } 245 }
244   246  
245 private void LogForm_Closed(object sender, EventArgs e) 247 private void LogForm_Closed(object sender, EventArgs e)
246 { 248 {
247 LogForm.Closed -= LogForm_Closed; 249 LogForm.Closed -= LogForm_Closed;
248 LogForm.Dispose(); 250 LogForm.Dispose();
249 LogForm = null; 251 LogForm = null;
250 } 252 }
251   253  
252 #endregion 254 #endregion
253   255  
254 #region Private Methods 256 #region Private Methods
255   257  
256 private async Task LoadWindows() 258 private async Task LoadWindows()
257 { 259 {
258 if (!Directory.Exists(Constants.UserApplicationDirectory)) 260 if (!Directory.Exists(Constants.UserApplicationDirectory))
259 { 261 {
260 Directory.CreateDirectory(Constants.UserApplicationDirectory); 262 Directory.CreateDirectory(Constants.UserApplicationDirectory);
261 } 263 }
262   264  
263 switch (await WindowsSerialization.Deserialize(Constants.WindowsSettingsFile)) 265 switch (await WindowsSerialization.Deserialize(Constants.WindowsSettingsFile))
264 { 266 {
265 case SerializationSuccess serializationSuccess: 267 case SerializationSuccess serializationSuccess:
266 Windows = serializationSuccess.Windows; 268 Windows = serializationSuccess.Windows;
267 if (LogForm != null) 269 if (LogForm != null)
268 { 270 {
269 LogForm.InvokeIfRequired(form => 271 LogForm.InvokeIfRequired(form =>
270 { 272 {
271 form.logTextBox.Text += "Windows loaded." + Environment.NewLine; 273 form.logTextBox.Text += "Windows loaded." + Environment.NewLine;
272 }); 274 });
273 } 275 }
274   276  
275 break; 277 break;
276 case SerializationFailure serializationFailure: 278 case SerializationFailure serializationFailure:
277 Windows = new Windows.Windows(); 279 Windows = new Windows.Windows();
278 if (LogForm != null) 280 if (LogForm != null)
279 { 281 {
280 LogForm.InvokeIfRequired(form => 282 LogForm.InvokeIfRequired(form =>
281 { 283 {
282 form.logTextBox.Text += "Failed to load windows: " + 284 form.logTextBox.Text += "Failed to load windows: " +
283 serializationFailure.Exception.Message + Environment.NewLine; 285 serializationFailure.Exception.Message + Environment.NewLine;
284 }); 286 });
285 } 287 }
286   288  
287 break; 289 break;
288 } 290 }
289 } 291 }
290   292  
291 #endregion 293 #endregion
292 } 294 }
293 } 295 }
294   296  
295
Generated by GNU Enscript 1.6.5.90.
297
Generated by GNU Enscript 1.6.5.90.
296   298  
297   299  
298   300