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