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