Widow – Diff between revs 18 and 21

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