Widow – Blame information for rev 5

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