Inertia – Blame information for rev 1

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