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