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