Inertia – Blame information for rev 3

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  
62 _globalMouseKeyHook = Hook.GlobalEvents();
63 _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel;
64  
65 _scrollUpContinuation = new ScheduledContinuation();
66 _scrollDownContinuation = new ScheduledContinuation();
67 }
68  
69 /// <summary>
70 /// Clean up any resources being used.
71 /// </summary>
72 /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
73 protected override void Dispose(bool disposing)
74 {
75 if (disposing && components != null)
76 {
77 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
78 _globalMouseKeyHook.Dispose();
79  
80 components.Dispose();
81 }
82  
83 base.Dispose(disposing);
84 }
85  
86 #endregion
87  
88 #region Event Handlers
89  
2 office 90 private void DefaultOnSettingsSaving(object sender, CancelEventArgs e)
91 {
92 }
93  
94 private void DefaultOnSettingsLoaded(object sender, SettingsLoadedEventArgs e)
95 {
96 }
97  
98 private void DefaultOnPropertyChanged(object sender, PropertyChangedEventArgs e)
99 {
100 Settings.Default.Save();
101 }
102  
1 office 103 private static void GlobalMouseKeyHookMouseWheel(object sender, MouseEventArgs e)
104 {
105 var delta = Math.Abs(e.Delta);
106  
107 switch (Math.Sign(e.Delta))
108 {
109 case 0:
110 break;
111 case 1: // up
112 Interlocked.Add(ref _upDelta, delta);
113 _scrollUpContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollUp);
114 Debug.WriteLine($"Mouse wheel moved up by {e.Delta}.");
115 break;
116 case -1: // down
117 Interlocked.Add(ref _downDelta, delta);
118 _scrollDownContinuation.Schedule(TimeSpan.FromMilliseconds(50), ScrollDown);
119 Debug.WriteLine($"Mouse wheel moved down by {e.Delta}.");
120 break;
121 }
122 }
123  
124 private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
125 {
126 if (_aboutForm != null)
127 {
128 return;
129 }
130  
131 _aboutForm = new AboutForm();
132 _aboutForm.Closing += AboutForm_Closing;
133 _aboutForm.Show();
134 }
135  
136 private void AboutForm_Closing(object sender, CancelEventArgs e)
137 {
138 if (_aboutForm == null)
139 {
140 return;
141 }
142  
143 _aboutForm.Closing -= AboutForm_Closing;
144 _aboutForm.Dispose();
145 _aboutForm = null;
146 }
147  
148 private void QuitToolStripMenuItem_Click(object sender, EventArgs e)
149 {
150 Application.Exit();
151 }
152  
2 office 153 private void LaunchOnBootToolStripMenuItem_Click(object sender, EventArgs e)
154 {
155 Settings.Default.LaunchOnBoot = ((ToolStripMenuItem) sender).Checked;
156  
157 LaunchOnBoot.Set(Settings.Default.LaunchOnBoot);
158 }
159  
1 office 160 #endregion
161  
162 #region Private Methods
163  
164 private static async void ScrollDown()
165 {
166 var downDelta = Interlocked.Read(ref _downDelta);
167 var delta = Math.Abs(downDelta / SystemInformation.MouseWheelScrollDelta);
168 Interlocked.Exchange(ref _downDelta, 0);
169  
170 Debug.WriteLine($"Down event expired with delta {delta}");
171  
172 if (_scrollCancellationTokenSource != null)
173 {
174 _scrollCancellationTokenSource.Cancel();
175 }
176  
177 _scrollCancellationTokenSource = new CancellationTokenSource();
178 var cancellationToken = _scrollCancellationTokenSource.Token;
179  
180 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
181  
182 try
183 {
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 {
198 var upDelta = Interlocked.Read(ref _upDelta);
199 var delta = Math.Abs(upDelta / SystemInformation.MouseWheelScrollDelta);
200 Interlocked.Exchange(ref _upDelta, 0);
201  
202 Debug.WriteLine($"Up event expired with delta {delta}");
203  
204 if (_scrollCancellationTokenSource != null)
205 {
206 _scrollCancellationTokenSource.Cancel();
207 }
208  
209 _scrollCancellationTokenSource = new CancellationTokenSource();
210 var cancellationToken = _scrollCancellationTokenSource.Token;
211  
212 _globalMouseKeyHook.MouseWheel -= GlobalMouseKeyHookMouseWheel;
213  
214 try
215 {
216 await ScrollUp((int) delta, cancellationToken);
217 }
3 office 218 catch (TaskCanceledException)
219 {
220 // We do not care.
221 }
1 office 222 finally
223 {
224 _globalMouseKeyHook.MouseWheel += GlobalMouseKeyHookMouseWheel;
225 }
226 }
227  
228 private static async Task ScrollUp(int delta, CancellationToken cancellationToken)
229 {
230 while (!cancellationToken.IsCancellationRequested && delta > 0)
231 {
232 _inputSimulator.Mouse.VerticalScroll(delta);
233 await Task.Delay(25 * delta, cancellationToken);
234 Debug.WriteLine($"Moving: {delta}");
235 delta = (int) Math.Log(delta, 2);
236 }
237 }
238  
239 private static async Task ScrollDown(int delta, CancellationToken cancellationToken)
240 {
241 while (!cancellationToken.IsCancellationRequested && delta > 0)
242 {
243 _inputSimulator.Mouse.VerticalScroll(-delta);
244 await Task.Delay(25 * delta, cancellationToken);
245 Debug.WriteLine($"Moving: {delta}");
246 delta = (int) Math.Log(delta, 2);
247 }
248 }
249  
250 #endregion
251 }
252 }