Inertia – Blame information for rev 6

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.ComponentModel;
2 office 3 using System.Configuration;
1 office 4 using System.Diagnostics;
5 using System.Threading;
6 using System.Threading.Tasks;
7 using System.Windows.Forms;
8 using WindowsInput;
9 using AutoUpdaterDotNET;
10 using Gma.System.MouseKeyHook;
2 office 11 using Inertia.Properties;
12 using Inertia.Utilities;
1 office 13  
14 namespace Inertia
15 {
16 public partial class Form1 : Form
17 {
18 #region Static Fields and Constants
19  
20 private static IKeyboardMouseEvents _globalMouseKeyHook;
21  
22 private static InputSimulator _inputSimulator;
23  
24 private static ScheduledContinuation _scrollUpContinuation;
25  
26 private static long _upDelta;
27  
28 private static long _downDelta;
29  
30 private static ScheduledContinuation _scrollDownContinuation;
31  
32 private static CancellationTokenSource _scrollCancellationTokenSource;
33  
34 #endregion
35  
36 #region Private Delegates, Events, Enums, Properties, Indexers and Fields
37  
38 private AboutForm _aboutForm;
39  
40 #endregion
41  
42 #region Constructors, Destructors and Finalizers
43  
44 public Form1()
45 {
46 InitializeComponent();
47 AutoUpdater.Start("http://inertia.grimore.org/update/update.xml");
48  
2 office 49 // Upgrade settings if required.
50 if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)
51 {
52 Settings.Default.Upgrade();
53 }
54  
55 // Bind to settings changed event.
56 Settings.Default.SettingsLoaded += DefaultOnSettingsLoaded;
57 Settings.Default.SettingsSaving += DefaultOnSettingsSaving;
58 Settings.Default.PropertyChanged += DefaultOnPropertyChanged;
59  
1 office 60 _inputSimulator = new InputSimulator();
61  
5 office 62 _scrollCancellationTokenSource = new CancellationTokenSource();
63  
1 office 64 _globalMouseKeyHook = Hook.GlobalEvents();
65 _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel;
66  
67 _scrollUpContinuation = new ScheduledContinuation();
68 _scrollDownContinuation = new ScheduledContinuation();
69 }
70  
71 /// <summary>
72 /// Clean up any resources being used.
73 /// </summary>
74 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
75 protected override void Dispose(bool disposing)
76 {
77 if (disposing && components != null)
78 {
79 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
80 _globalMouseKeyHook.Dispose();
81  
82 components.Dispose();
83 }
84  
85 base.Dispose(disposing);
86 }
87  
88 #endregion
89  
90 #region Event Handlers
91  
2 office 92 private void DefaultOnSettingsSaving(object sender, CancelEventArgs e)
93 {
94 }
95  
96 private void DefaultOnSettingsLoaded(object sender, SettingsLoadedEventArgs e)
97 {
98 }
99  
100 private void DefaultOnPropertyChanged(object sender, PropertyChangedEventArgs e)
101 {
102 Settings.Default.Save();
103 }
104  
1 office 105 private static void GlobalMouseKeyHookMouseWheel(object sender, MouseEventArgs e)
106 {
107 var delta = Math.Abs(e.Delta);
108  
109 switch (Math.Sign(e.Delta))
110 {
111 case 0:
112 break;
113 case 1: // up
114 Interlocked.Add(ref _upDelta, delta);
115 _scrollUpContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollUp);
116 Debug.WriteLine($"Mouse wheel moved up by {e.Delta}.");
117 break;
118 case -1: // down
119 Interlocked.Add(ref _downDelta, delta);
120 _scrollDownContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollDown);
121 Debug.WriteLine($"Mouse wheel moved down by {e.Delta}.");
122 break;
123 }
124 }
125  
126 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
127 {
128 if (_aboutForm != null)
129 {
130 return;
131 }
132  
133 _aboutForm = new AboutForm();
134 _aboutForm.Closing += AboutForm_Closing;
135 _aboutForm.Show();
136 }
137  
138 private void AboutForm_Closing(object sender, CancelEventArgs e)
139 {
140 if (_aboutForm == null)
141 {
142 return;
143 }
144  
145 _aboutForm.Closing -= AboutForm_Closing;
146 _aboutForm.Dispose();
147 _aboutForm = null;
148 }
149  
150 private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
151 {
6 office 152 Close();
153  
154 Environment.Exit(0);
1 office 155 }
156  
2 office 157 private void LaunchOnBootToolStripMenuItem_Click(object sender, EventArgs e)
158 {
159 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
160  
161 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
162 }
163  
1 office 164 #endregion
165  
166 #region Private Methods
167  
168 private static async void ScrollDown()
169 {
5 office 170 _scrollCancellationTokenSource.Cancel();
171 _scrollCancellationTokenSource = new CancellationTokenSource();
172 var cancellationToken = _scrollCancellationTokenSource.Token;
173  
1 office 174 var downDelta = Interlocked.Read(ref _downDelta);
175 var delta = Math.Abs(downDelta / SystemInformation.MouseWheelScrollDelta);
176 Interlocked.Exchange(ref _downDelta, 0);
177  
178 Debug.WriteLine($"Down event expired with delta {delta}");
179  
180 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
181  
182 try
5 office 183 {
1 office 184 await ScrollDown((int) delta, cancellationToken);
185 }
3 office 186 catch (TaskCanceledException)
187 {
188 // We do not care.
189 }
1 office 190 finally
191 {
192 _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel;
193 }
194 }
195  
196 private static async void ScrollUp()
197 {
5 office 198 _scrollCancellationTokenSource.Cancel();
199 _scrollCancellationTokenSource = new CancellationTokenSource();
200 var cancellationToken = _scrollCancellationTokenSource.Token;
201  
1 office 202 var upDelta = Interlocked.Read(ref _upDelta);
203 var delta = Math.Abs(upDelta / SystemInformation.MouseWheelScrollDelta);
204 Interlocked.Exchange(ref _upDelta, 0);
205  
206 Debug.WriteLine($"Up event expired with delta {delta}");
207  
208 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
209  
210 try
211 {
212 await ScrollUp((int) delta, cancellationToken);
213 }
3 office 214 catch (TaskCanceledException)
215 {
216 // We do not care.
217 }
1 office 218 finally
219 {
220 _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel;
221 }
222 }
223  
224 private static async Task ScrollUp(int delta, CancellationToken cancellationToken)
225 {
5 office 226 while (!cancellationToken.IsCancellationRequested && delta > 1)
1 office 227 {
228 _inputSimulator.Mouse.VerticalScroll(delta);
229 await Task.Delay(25 * delta, cancellationToken);
230 Debug.WriteLine($"Moving: {delta}");
231 delta = (int) Math.Log(delta, 2);
232 }
233 }
234  
235 private static async Task ScrollDown(int delta, CancellationToken cancellationToken)
236 {
5 office 237 while (!cancellationToken.IsCancellationRequested && delta > 1)
1 office 238 {
239 _inputSimulator.Mouse.VerticalScroll(-delta);
240 await Task.Delay(25 * delta, cancellationToken);
241 Debug.WriteLine($"Moving: {delta}");
242 delta = (int) Math.Log(delta, 2);
243 }
244 }
245  
246 #endregion
247 }
248 }