Widow – Blame information for rev 6

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.ComponentModel;
3 using System.Configuration;
4 using System.Threading.Tasks;
5 using System.Windows.Forms;
6 using Widow.Properties;
7 using Widow.Serialization;
8  
9 namespace Widow
10 {
6 office 11 public partial class MainForm : Form
1 office 12 {
13 #region Public Events & Delegates
14  
15 public event EventHandler<WindowCreatedEventArgs> WindowCreated;
16  
17 public event EventHandler<WindowDestroyedEventArgs> WindowDestroyed;
18  
19 #endregion
20  
21 #region Public Enums, Properties and Fields
22  
23 public Windows.Windows Windows { get; set; }
24  
25 public RuleEditForm RuleEditForm { get; set; }
26  
27 public Apply Apply { get; set; }
28  
5 office 29 public AboutForm AboutForm { get; set; }
30  
1 office 31 #endregion
32  
33 #region Constructors, Destructors and Finalizers
34  
6 office 35 public MainForm()
1 office 36 {
37 InitializeComponent();
6 office 38  
1 office 39 // Bind to settings changed event.
40 Settings.Default.SettingsLoaded += Default_SettingsLoaded;
41 Settings.Default.SettingsSaving += Default_SettingsSaving;
42 Settings.Default.PropertyChanged += Default_PropertyChanged;
43  
44 Natives.RegisterShellHookWindow(Handle);
45  
6 office 46 LoadWindows().ContinueWith(task =>
47 {
48 Apply = new Apply(this, Windows) {OnWindowCreate = Settings.Default.OnWindowCreate};
49 });
1 office 50 }
51  
52 /// <summary>
53 /// Clean up any resources being used.
54 /// </summary>
55 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
56 protected override void Dispose(bool disposing)
57 {
58 if (disposing && components != null)
59 {
60 Settings.Default.SettingsLoaded -= Default_SettingsLoaded;
61 Settings.Default.SettingsSaving -= Default_SettingsSaving;
62 Settings.Default.PropertyChanged -= Default_PropertyChanged;
63  
64 components.Dispose();
65 }
66  
67 base.Dispose(disposing);
68 }
69  
70 #endregion
71  
72 #region Private Overrides
73  
74 protected override void WndProc(ref Message m)
75 {
76 var handle = m.LParam;
77 var windowName = Helpers.GetWindowTitle(handle);
78  
79 switch (m.WParam.ToInt32())
80 {
81 case (int) Natives.WM.HSHELL_WINDOWCREATED:
82 WindowCreated?.Invoke(this, new WindowCreatedEventArgs(windowName, handle));
83 break;
84 case (int) Natives.WM.HSHELL_WINDOWDESTROYED:
85 WindowDestroyed?.Invoke(this, new WindowDestroyedEventArgs(windowName, handle));
86 break;
87 }
88  
89 base.WndProc(ref m);
90 }
91  
92 #endregion
93  
94 #region Event Handlers
95  
96 private void Default_PropertyChanged(object sender, PropertyChangedEventArgs e)
97 {
98 Settings.Default.Save();
99 }
100  
101 private void Default_SettingsSaving(object sender, CancelEventArgs e)
102 {
103 }
104  
105 private void Default_SettingsLoaded(object sender, SettingsLoadedEventArgs e)
106 {
107 }
108  
109 private void NewToolStripMenuItem_Click(object sender, EventArgs e)
110 {
111 RuleEditForm = new RuleEditForm(this, Windows);
112 RuleEditForm.Closed += RuleEditForm_Closed;
113 RuleEditForm.Show();
114 }
115  
116 private async void RuleEditForm_Closed(object sender, EventArgs e)
117 {
118 RuleEditForm.Closed -= RuleEditForm_Closed;
119 RuleEditForm.Dispose();
120 RuleEditForm = null;
121  
122 switch (await WindowsSerialization.Serialize(Windows, "Windows.xml"))
123 {
124 case SerializationSuccess serializationSuccess:
125 break;
126 case SerializationFailure serializationFailure:
127 break;
128 }
129 }
130  
131 private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
132 {
133 Application.Exit();
134 }
135  
136 private void LaunchOnBootToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
137 {
138 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
139  
140 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
141 }
142  
5 office 143 private void OnWindowCreateToolStripMenuItem_CheckedChanged(object sender, EventArgs e)
144 {
145 Settings.Default.OnWindowCreate = ((ToolStripMenuItem) sender).Checked;
146  
147 Apply.OnWindowCreate = Settings.Default.OnWindowCreate;
148 }
149  
150 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
151 {
152 // Show the about form.
153 AboutForm = new AboutForm();
154 AboutForm.Show();
155 }
156  
1 office 157 #endregion
158  
159 #region Private Methods
160  
161 private async Task LoadWindows()
162 {
163 switch (await WindowsSerialization.Deserialize("Windows.xml"))
164 {
165 case SerializationSuccess serializationSuccess:
166 Windows = serializationSuccess.Windows;
167 break;
168 case SerializationFailure serializationFailure:
169 Windows = new Windows.Windows();
170 break;
171 }
172 }
173  
174 #endregion
175 }
176 }