-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShowTouchesWindow.cs
410 lines (283 loc) · 9.55 KB
/
ShowTouchesWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using System;
using UIKit;
using CoreGraphics;
using Foundation;
using System.Timers;
namespace XamarinShowTouches
{
class MBFingerTipView : UIImageView{
public TimeSpan timestamp;
public bool shouldAutomaticallyRemoveAfterTimeout;
public bool isFadingOut;
public MBFingerTipView(UIImage image):base(image){
}
}
class MBFingerTipOverlayWindow : UIWindow{
public MBFingerTipOverlayWindow(CGRect frame) : base(frame){}
// UIKit tries to get the rootViewController from the overlay window.
// Instead, try to find the rootViewController on some other application window.
// Fixes problems with status bar hiding, because it considers the overlay window a candidate for controlling the status bar.
public override UIViewController RootViewController {
get {
foreach (UIWindow window in UIApplication.SharedApplication.Windows) {
if (this == window)
continue;
UIViewController realRootViewController = window.RootViewController;
if (realRootViewController != null)
return realRootViewController;
}
return base.RootViewController;
}
set {
base.RootViewController = value;
}
}
}
public class ShowTouchesWindow : UIWindow
{
/** A custom image to use to show touches on screen. If unset, defaults to a partially-transparent stroked circle. */
UIImage touchImage;
/** The alpha transparency value to use for the touch image. Defaults to 0.5. */
nfloat touchAlpha;
/** The time over which to fade out touch images. Defaults to 0.3. */
nfloat fadeDuration;
/** If using the default touchImage, the color with which to stroke the shape. Defaults to black. */
UIColor strokeColor;
/** If using the default touchImage, the color with which to fill the shape. Defaults to white. */
UIColor fillColor;
/** Sets whether touches should always show regardless of whether the display is mirroring. Defaults to NO. */
bool alwaysShowTouches;
public bool AlwaysShowTouches {
get { return alwaysShowTouches; }
set {
if (alwaysShowTouches != value) {
alwaysShowTouches = value;
updateFingerTipsAreActive ();
}
}
}
UIWindow overlayWindow;
bool active;
bool fingerTipRemovalScheduled;
Timer removalTimer;
public ShowTouchesWindow(IntPtr p) : base(p){
GeneralSetUp ();
}
public ShowTouchesWindow(CGRect frame):base(frame){
GeneralSetUp ();
}
void GeneralSetUp(){
strokeColor = UIColor.Black;
fillColor = UIColor.White;
touchAlpha = 0.5f;
fadeDuration = 0.3f;
//TODO add IDisposable and remove notification observer!
NSNotificationCenter.DefaultCenter.AddObserver (UIScreen.DidConnectNotification, (obj) => screenConnect ());
NSNotificationCenter.DefaultCenter.AddObserver (UIScreen.DidDisconnectNotification, (obj) => screenDisconnect ());
// Set up active now, in case the screen was present before the window was created (or application launched).
//
updateFingerTipsAreActive();
}
protected override void Dispose (bool disposing)
{
base.Dispose (disposing);
}
public UIWindow OverlayWindow(){
if (overlayWindow == null) {
overlayWindow = new MBFingerTipOverlayWindow (Frame);
overlayWindow.UserInteractionEnabled = false;
overlayWindow.WindowLevel = UIWindowLevel.StatusBar;
overlayWindow.BackgroundColor = UIColor.Clear;
overlayWindow.Hidden = false;
}
return overlayWindow;
}
public UIImage TouchImage(){
if (touchImage == null) {
UIBezierPath clipPath = UIBezierPath.FromRect (new CGRect (0, 0, 50, 50));
UIGraphics.BeginImageContextWithOptions (clipPath.Bounds.Size, false, 0);
UIBezierPath drawPath = UIBezierPath.FromArc (new CGPoint (25, 25), 22, 0, 2 * (nfloat)Math.PI, true);
drawPath.LineWidth = 2;
strokeColor.SetStroke ();
fillColor.SetFill ();
drawPath.Stroke ();
drawPath.Fill ();
clipPath.AddClip ();
touchImage = UIGraphics.GetImageFromCurrentImageContext ();
UIGraphics.EndImageContext ();
}
return touchImage;
}
void screenConnect(){
updateFingerTipsAreActive ();
}
void screenDisconnect(){
updateFingerTipsAreActive ();
}
//TODO check this actually works right
public bool anyScreenIsMirrored(){
//bool response = false;
foreach (UIScreen screen in UIScreen.Screens) {
//Is this right?!?!?!
if (!screen.RespondsToSelector (new ObjCRuntime.Selector ("mirroredScreen"))) {
return false;
}
if (screen.MirroredScreen != null) {
return true;
}
}
return false;
}
void updateFingerTipsAreActive(){
if (alwaysShowTouches) {
active = true;
} else {
active = anyScreenIsMirrored ();
}
}
#region UIWindow Overides
public override void SendEvent (UIEvent evt)
{
if (evt.Type == UIEventType.Touches) {
if (active) {
NSSet allTouches = evt.AllTouches;
foreach (UITouch touch in allTouches) {
switch (touch.Phase) {
case UITouchPhase.Began:
case UITouchPhase.Moved:
case UITouchPhase.Stationary:
MBFingerTipView touchView = OverlayWindow ().ViewWithTag (touch.GetHashCode ()) as MBFingerTipView;
if (touch.Phase != UITouchPhase.Stationary && touchView != null && touchView.isFadingOut) {
touchView.RemoveFromSuperview ();
touchView = null;
}
if (touchView == null && touch.Phase != UITouchPhase.Stationary) {
touchView = new MBFingerTipView (TouchImage ());
OverlayWindow ().AddSubview (touchView);
}
if (!touchView.isFadingOut) {
touchView.Alpha = touchAlpha;
touchView.Center = touch.LocationInView (OverlayWindow ());
touchView.Tag = touch.GetHashCode ();
touchView.timestamp = TimeSpan.FromSeconds (touch.Timestamp);
touchView.shouldAutomaticallyRemoveAfterTimeout = shouldAutomaticallyRemoveFingerTipForTouch (touch);
}
break;
case UITouchPhase.Ended:
case UITouchPhase.Cancelled:
removeFingerTipWithHash (touch.GetHashCode (), true);
break;
}
}
}
scheduleFingerTipRemoval ();
}
base.SendEvent (evt);
}
#endregion
#region Private
void scheduleFingerTipRemoval(){
if (fingerTipRemovalScheduled) {
return;
}
fingerTipRemovalScheduled = true;
removalTimer = new Timer(100);
removalTimer.AutoReset = false;
removalTimer.Elapsed += (sender, e) =>
{
InvokeOnMainThread(() =>
{
removeInactiveFingerTips();
});
};
removalTimer.Enabled = true;
}
void cancelScheduledFingerTipRemoval(){
fingerTipRemovalScheduled = true;
removalTimer.Enabled = false;
}
void removeInactiveFingerTips(){
fingerTipRemovalScheduled = false;
var now = NSProcessInfo.ProcessInfo.SystemUptime;
nfloat REMOVAL_DELAY = 0.2f;
foreach (MBFingerTipView touchView in OverlayWindow().Subviews) {
if (!(touchView is MBFingerTipView)) {
continue;
}
if (touchView.shouldAutomaticallyRemoveAfterTimeout && now > touchView.timestamp.TotalSeconds + REMOVAL_DELAY) {
removeFingerTipWithHash (touchView.Tag, true);
}
}
if (OverlayWindow ().Subviews.Length > 0) {
scheduleFingerTipRemoval ();
}
}
void removeFingerTipWithHash(nint hash, bool animated){
MBFingerTipView touchView = OverlayWindow ().ViewWithTag (hash) as MBFingerTipView;
if (!(touchView is MBFingerTipView)) {
return;
}
if (touchView.isFadingOut) {
return;
}
bool animationsWereEnabled = UIView.AnimationsEnabled;
if (animated) {
UIView.AnimationsEnabled = true;
UIView.BeginAnimations (null, IntPtr.Zero);
UIView.SetAnimationDuration (fadeDuration);
}
touchView.Frame = new CGRect (touchView.Center.X - touchView.Frame.Size.Width,
touchView.Center.Y - touchView.Frame.Size.Height,
touchView.Frame.Size.Width * 2,
touchView.Frame.Size.Height * 2);
touchView.Alpha = 0;
if (animated) {
UIView.CommitAnimations ();
UIView.AnimationsEnabled = animationsWereEnabled;
}
touchView.isFadingOut = true;
var aTimer = new Timer (fadeDuration * 1000);
aTimer.AutoReset = false;
aTimer.Elapsed += ((sender, e) => {
InvokeOnMainThread(() => {
touchView.RemoveFromSuperview();
});
});
aTimer.Enabled = true;
}
bool shouldAutomaticallyRemoveFingerTipForTouch(UITouch touch){
// We don't reliably get UITouchPhaseEnded or UITouchPhaseCancelled
// events via -sendEvent: for certain touch events. Known cases
// include swipe-to-delete on a table view row, and tap-to-cancel
// swipe to delete. We automatically remove their associated
// fingertips after a suitable timeout.
//
// It would be much nicer if we could remove all touch events after
// a suitable time out, but then we'll prematurely remove touch and
// hold events that are picked up by gesture recognizers (since we
// don't use UITouchPhaseStationary touches for those. *sigh*). So we
// end up with this more complicated setup.
var view = touch.View;
if (view != null) {
view = view.HitTest (touch.LocationInView (view), null);
while (view != null) {
if (view is UITableViewCell) {
foreach (UIGestureRecognizer recognizer in touch.GestureRecognizers) {
if (recognizer is UISwipeGestureRecognizer) {
return true;
}
}
}
if (view is UITableView) {
if (touch.GestureRecognizers.Length == 0) {
return true;
}
}
view = view.Superview;
}
}
return false;
}
#endregion
}
}