Widow – Diff between revs 21 and 22

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