-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMouse.js
1272 lines (1062 loc) · 38.3 KB
/
Mouse.js
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
var Guacamole = Guacamole || {};
/**
* Provides cross-browser mouse events for a given element. The events of
* the given element are automatically populated with handlers that translate
* mouse events into a non-browser-specific event provided by the
* Guacamole.Mouse instance.
*
* @example
* var mouse = new Guacamole.Mouse(client.getDisplay().getElement());
*
* // Forward all mouse interaction over Guacamole connection
* mouse.onEach(['mousedown', 'mousemove', 'mouseup'], function sendMouseEvent(e) {
* client.sendMouseState(e.state, true);
* });
*
* @example
* // Hide software cursor when mouse leaves display
* mouse.on('mouseout', function hideCursor() {
* client.getDisplay().showCursor(false);
* });
*
* @constructor
* @augments Guacamole.Mouse.Event.Target
* @param {!Element} element
* The Element to use to provide mouse events.
*/
Guacamole.Mouse = function Mouse(element) {
Guacamole.Mouse.Event.Target.call(this);
/**
* Reference to this Guacamole.Mouse.
*
* @private
* @type {!Guacamole.Mouse}
*/
var guac_mouse = this;
/**
* The number of mousemove events to require before re-enabling mouse
* event handling after receiving a touch event.
*
* @type {!number}
*/
this.touchMouseThreshold = 3;
/**
* The minimum amount of pixels scrolled required for a single scroll button
* click.
*
* @type {!number}
*/
this.scrollThreshold = 53;
/**
* The number of pixels to scroll per line.
*
* @type {!number}
*/
this.PIXELS_PER_LINE = 18;
/**
* The number of pixels to scroll per page.
*
* @type {!number}
*/
this.PIXELS_PER_PAGE = this.PIXELS_PER_LINE * 16;
/**
* Array of {@link Guacamole.Mouse.State} button names corresponding to the
* mouse button indices used by DOM mouse events.
*
* @private
* @type {!string[]}
*/
var MOUSE_BUTTONS = [
Guacamole.Mouse.State.Buttons.LEFT,
Guacamole.Mouse.State.Buttons.MIDDLE,
Guacamole.Mouse.State.Buttons.RIGHT
];
/**
* Counter of mouse events to ignore. This decremented by mousemove, and
* while non-zero, mouse events will have no effect.
*
* @private
* @type {!number}
*/
var ignore_mouse = 0;
/**
* Cumulative scroll delta amount. This value is accumulated through scroll
* events and results in scroll button clicks if it exceeds a certain
* threshold.
*
* @private
* @type {!number}
*/
var scroll_delta = 0;
// Block context menu so right-click gets sent properly
element.addEventListener("contextmenu", function(e) {
Guacamole.Event.DOMEvent.cancelEvent(e);
}, false);
element.addEventListener("mousemove", function(e) {
// If ignoring events, decrement counter
if (ignore_mouse) {
Guacamole.Event.DOMEvent.cancelEvent(e);
ignore_mouse--;
return;
}
guac_mouse.move(Guacamole.Position.fromClientPosition(element, e.clientX, e.clientY), e);
}, false);
element.addEventListener("mousedown", function(e) {
// Do not handle if ignoring events
if (ignore_mouse) {
Guacamole.Event.DOMEvent.cancelEvent(e);
return;
}
var button = MOUSE_BUTTONS[e.button];
if (button)
guac_mouse.press(button, e);
}, false);
element.addEventListener("mouseup", function(e) {
// Do not handle if ignoring events
if (ignore_mouse) {
Guacamole.Event.DOMEvent.cancelEvent(e);
return;
}
var button = MOUSE_BUTTONS[e.button];
if (button)
guac_mouse.release(button, e);
}, false);
element.addEventListener("mouseout", function(e) {
// Get parent of the element the mouse pointer is leaving
if (!e) e = window.event;
// Check that mouseout is due to actually LEAVING the element
var target = e.relatedTarget || e.toElement;
while (target) {
if (target === element)
return;
target = target.parentNode;
}
// Release all buttons and fire mouseout
guac_mouse.reset(e);
guac_mouse.out(e);
}, false);
// Override selection on mouse event element.
element.addEventListener("selectstart", function(e) {
Guacamole.Event.DOMEvent.cancelEvent(e);
}, false);
// Ignore all pending mouse events when touch events are the apparent source
function ignorePendingMouseEvents() { ignore_mouse = guac_mouse.touchMouseThreshold; }
element.addEventListener("touchmove", ignorePendingMouseEvents, false);
element.addEventListener("touchstart", ignorePendingMouseEvents, false);
element.addEventListener("touchend", ignorePendingMouseEvents, false);
// Scroll wheel support
function mousewheel_handler(e) {
// Determine approximate scroll amount (in pixels)
var delta = e.deltaY || -e.wheelDeltaY || -e.wheelDelta;
// If successfully retrieved scroll amount, convert to pixels if not
// already in pixels
if (delta) {
// Convert to pixels if delta was lines
if (e.deltaMode === 1)
delta = e.deltaY * guac_mouse.PIXELS_PER_LINE;
// Convert to pixels if delta was pages
else if (e.deltaMode === 2)
delta = e.deltaY * guac_mouse.PIXELS_PER_PAGE;
}
// Otherwise, assume legacy mousewheel event and line scrolling
else
delta = e.detail * guac_mouse.PIXELS_PER_LINE;
// Update overall delta
scroll_delta += delta;
// Up
if (scroll_delta <= -guac_mouse.scrollThreshold) {
// Repeatedly click the up button until insufficient delta remains
do {
guac_mouse.click(Guacamole.Mouse.State.Buttons.UP);
scroll_delta += guac_mouse.scrollThreshold;
} while (scroll_delta <= -guac_mouse.scrollThreshold);
// Reset delta
scroll_delta = 0;
}
// Down
if (scroll_delta >= guac_mouse.scrollThreshold) {
// Repeatedly click the down button until insufficient delta remains
do {
guac_mouse.click(Guacamole.Mouse.State.Buttons.DOWN);
scroll_delta -= guac_mouse.scrollThreshold;
} while (scroll_delta >= guac_mouse.scrollThreshold);
// Reset delta
scroll_delta = 0;
}
// All scroll/wheel events must currently be cancelled regardless of
// whether the dispatched event is cancelled, as there is no Guacamole
// scroll event and thus no way to cancel scroll events that are
// smaller than required to produce an up/down click
Guacamole.Event.DOMEvent.cancelEvent(e);
}
if (window.WheelEvent) {
// All modern browsers support wheel events.
element.addEventListener('wheel', mousewheel_handler, false);
}
else {
// Legacy FireFox wheel events.
element.addEventListener('DOMMouseScroll', mousewheel_handler, false);
// Legacy Chrome/IE/other wheel events.
element.addEventListener('mousewheel', mousewheel_handler, false);
}
/**
* Whether the browser supports CSS3 cursor styling, including hotspot
* coordinates.
*
* @private
* @type {!boolean}
*/
var CSS3_CURSOR_SUPPORTED = (function() {
var div = document.createElement("div");
// If no cursor property at all, then no support
if (!("cursor" in div.style))
return false;
try {
// Apply simple 1x1 PNG
div.style.cursor = "url(data:image/png;base64,"
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB"
+ "AQMAAAAl21bKAAAAA1BMVEX///+nxBvI"
+ "AAAACklEQVQI12NgAAAAAgAB4iG8MwAA"
+ "AABJRU5ErkJggg==) 0 0, auto";
}
catch (e) {
return false;
}
// Verify cursor property is set to URL with hotspot
return /\burl\([^()]*\)\s+0\s+0\b/.test(div.style.cursor || "");
})();
/**
* Changes the local mouse cursor to the given canvas, having the given
* hotspot coordinates. This affects styling of the element backing this
* Guacamole.Mouse only, and may fail depending on browser support for
* setting the mouse cursor.
*
* If setting the local cursor is desired, it is up to the implementation
* to do something else, such as use the software cursor built into
* Guacamole.Display, if the local cursor cannot be set.
*
* @param {!HTMLCanvasElement} canvas
* The cursor image.
*
* @param {!number} x
* The X-coordinate of the cursor hotspot.
*
* @param {!number} y
* The Y-coordinate of the cursor hotspot.
*
* @return {!boolean}
* true if the cursor was successfully set, false if the cursor could
* not be set for any reason.
*/
this.setCursor = function(canvas, x, y) {
// Attempt to set via CSS3 cursor styling
if (CSS3_CURSOR_SUPPORTED) {
var dataURL = canvas.toDataURL('image/png');
element.style.cursor = "url(" + dataURL + ") " + x + " " + y + ", auto";
return true;
}
// Otherwise, setting cursor failed
return false;
};
};
/**
* The current state of a mouse, including position and buttons.
*
* @constructor
* @augments Guacamole.Position
* @param {Guacamole.Mouse.State|object} [template={}]
* The object whose properties should be copied within the new
* Guacamole.Mouse.State.
*/
Guacamole.Mouse.State = function State(template) {
/**
* Returns the template object that would be provided to the
* Guacamole.Mouse.State constructor to produce a new Guacamole.Mouse.State
* object with the properties specified. The order and type of arguments
* used by this function are identical to those accepted by the
* Guacamole.Mouse.State constructor of Apache Guacamole 1.3.0 and older.
*
* @private
* @param {!number} x
* The X position of the mouse pointer in pixels.
*
* @param {!number} y
* The Y position of the mouse pointer in pixels.
*
* @param {!boolean} left
* Whether the left mouse button is pressed.
*
* @param {!boolean} middle
* Whether the middle mouse button is pressed.
*
* @param {!boolean} right
* Whether the right mouse button is pressed.
*
* @param {!boolean} up
* Whether the up mouse button is pressed (the fourth button, usually
* part of a scroll wheel).
*
* @param {!boolean} down
* Whether the down mouse button is pressed (the fifth button, usually
* part of a scroll wheel).
*
* @return {!object}
* The equivalent template object that would be passed to the new
* Guacamole.Mouse.State constructor.
*/
var legacyConstructor = function legacyConstructor(x, y, left, middle, right, up, down) {
return {
x : x,
y : y,
left : left,
middle : middle,
right : right,
up : up,
down : down
};
};
// Accept old-style constructor, as well
if (arguments.length > 1)
template = legacyConstructor.apply(this, arguments);
else
template = template || {};
Guacamole.Position.call(this, template);
/**
* Whether the left mouse button is currently pressed.
*
* @type {!boolean}
* @default false
*/
this.left = template.left || false;
/**
* Whether the middle mouse button is currently pressed.
*
* @type {!boolean}
* @default false
*/
this.middle = template.middle || false;
/**
* Whether the right mouse button is currently pressed.
*
* @type {!boolean}
* @default false
*/
this.right = template.right || false;
/**
* Whether the up mouse button is currently pressed. This is the fourth
* mouse button, associated with upward scrolling of the mouse scroll
* wheel.
*
* @type {!boolean}
* @default false
*/
this.up = template.up || false;
/**
* Whether the down mouse button is currently pressed. This is the fifth
* mouse button, associated with downward scrolling of the mouse scroll
* wheel.
*
* @type {!boolean}
* @default false
*/
this.down = template.down || false;
};
/**
* All mouse buttons that may be represented by a
* {@link Guacamole.Mouse.State}.
*
* @readonly
* @enum
*/
Guacamole.Mouse.State.Buttons = {
/**
* The name of the {@link Guacamole.Mouse.State} property representing the
* left mouse button.
*
* @constant
* @type {!string}
*/
LEFT : 'left',
/**
* The name of the {@link Guacamole.Mouse.State} property representing the
* middle mouse button.
*
* @constant
* @type {!string}
*/
MIDDLE : 'middle',
/**
* The name of the {@link Guacamole.Mouse.State} property representing the
* right mouse button.
*
* @constant
* @type {!string}
*/
RIGHT : 'right',
/**
* The name of the {@link Guacamole.Mouse.State} property representing the
* up mouse button (the fourth mouse button, clicked when the mouse scroll
* wheel is scrolled up).
*
* @constant
* @type {!string}
*/
UP : 'up',
/**
* The name of the {@link Guacamole.Mouse.State} property representing the
* down mouse button (the fifth mouse button, clicked when the mouse scroll
* wheel is scrolled up).
*
* @constant
* @type {!string}
*/
DOWN : 'down'
};
/**
* Base event type for all mouse events. The mouse producing the event may be
* the user's local mouse (as with {@link Guacamole.Mouse}) or an emulated
* mouse (as with {@link Guacamole.Mouse.Touchpad}).
*
* @constructor
* @augments Guacamole.Event.DOMEvent
* @param {!string} type
* The type name of the event ("mousedown", "mouseup", etc.)
*
* @param {!Guacamole.Mouse.State} state
* The current mouse state.
*
* @param {Event|Event[]} [events=[]]
* The DOM events that are related to this event, if any.
*/
Guacamole.Mouse.Event = function MouseEvent(type, state, events) {
Guacamole.Event.DOMEvent.call(this, type, events);
/**
* The name of the event handler used by the Guacamole JavaScript API for
* this event prior to the migration to Guacamole.Event.Target.
*
* @private
* @constant
* @type {!string}
*/
var legacyHandlerName = 'on' + this.type;
/**
* The current mouse state at the time this event was fired.
*
* @type {!Guacamole.Mouse.State}
*/
this.state = state;
/**
* @inheritdoc
*/
this.invokeLegacyHandler = function invokeLegacyHandler(target) {
if (target[legacyHandlerName]) {
this.preventDefault();
this.stopPropagation();
target[legacyHandlerName](this.state);
}
};
};
/**
* An object which can dispatch {@link Guacamole.Mouse.Event} objects
* representing mouse events. These mouse events may be produced from an actual
* mouse device (as with {@link Guacamole.Mouse}), from an emulated mouse
* device (as with {@link Guacamole.Mouse.Touchpad}, or may be programmatically
* generated (using functions like [dispatch()]{@link Guacamole.Mouse.Event.Target#dispatch},
* [press()]{@link Guacamole.Mouse.Event.Target#press}, and
* [release()]{@link Guacamole.Mouse.Event.Target#release}).
*
* @constructor
* @augments Guacamole.Event.Target
*/
Guacamole.Mouse.Event.Target = function MouseEventTarget() {
Guacamole.Event.Target.call(this);
/**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
*
* @type {!Guacamole.Mouse.State}
*/
this.currentState = new Guacamole.Mouse.State();
/**
* Fired whenever a mouse button is effectively pressed. Depending on the
* object dispatching the event, this can be due to a true mouse button
* press ({@link Guacamole.Mouse}), an emulated mouse button press from a
* touch gesture ({@link Guacamole.Mouse.Touchpad} and
* {@link Guacamole.Mouse.Touchscreen}), or may be programmatically
* generated through [dispatch()]{@link Guacamole.Mouse.Event.Target#dispatch},
* [press()]{@link Guacamole.Mouse.Event.Target#press}, or
* [click()]{@link Guacamole.Mouse.Event.Target#click}.
*
* @event Guacamole.Mouse.Event.Target#mousedown
* @param {!Guacamole.Mouse.Event} event
* The mousedown event that was fired.
*/
/**
* Fired whenever a mouse button is effectively released. Depending on the
* object dispatching the event, this can be due to a true mouse button
* release ({@link Guacamole.Mouse}), an emulated mouse button release from
* a touch gesture ({@link Guacamole.Mouse.Touchpad} and
* {@link Guacamole.Mouse.Touchscreen}), or may be programmatically
* generated through [dispatch()]{@link Guacamole.Mouse.Event.Target#dispatch},
* [release()]{@link Guacamole.Mouse.Event.Target#release}, or
* [click()]{@link Guacamole.Mouse.Event.Target#click}.
*
* @event Guacamole.Mouse.Event.Target#mouseup
* @param {!Guacamole.Mouse.Event} event
* The mouseup event that was fired.
*/
/**
* Fired whenever the mouse pointer is effectively moved. Depending on the
* object dispatching the event, this can be due to true mouse movement
* ({@link Guacamole.Mouse}), emulated mouse movement from
* a touch gesture ({@link Guacamole.Mouse.Touchpad} and
* {@link Guacamole.Mouse.Touchscreen}), or may be programmatically
* generated through [dispatch()]{@link Guacamole.Mouse.Event.Target#dispatch},
* or [move()]{@link Guacamole.Mouse.Event.Target#move}.
*
* @event Guacamole.Mouse.Event.Target#mousemove
* @param {!Guacamole.Mouse.Event} event
* The mousemove event that was fired.
*/
/**
* Fired whenever the mouse pointer leaves the boundaries of the element
* being monitored for interaction. This will only ever be automatically
* fired due to movement of an actual mouse device via
* {@link Guacamole.Mouse} unless programmatically generated through
* [dispatch()]{@link Guacamole.Mouse.Event.Target#dispatch},
* or [out()]{@link Guacamole.Mouse.Event.Target#out}.
*
* @event Guacamole.Mouse.Event.Target#mouseout
* @param {!Guacamole.Mouse.Event} event
* The mouseout event that was fired.
*/
/**
* Presses the given mouse button, if it isn't already pressed. Valid
* button names are defined by {@link Guacamole.Mouse.State.Buttons} and
* correspond to the button-related properties of
* {@link Guacamole.Mouse.State}.
*
* @fires Guacamole.Mouse.Event.Target#mousedown
*
* @param {!string} button
* The name of the mouse button to press, as defined by
* {@link Guacamole.Mouse.State.Buttons}.
*
* @param {Event|Event[]} [events=[]]
* The DOM events that are related to the mouse button press, if any.
*/
this.press = function press(button, events) {
if (!this.currentState[button]) {
this.currentState[button] = true;
this.dispatch(new Guacamole.Mouse.Event('mousedown', this.currentState, events));
}
};
/**
* Releases the given mouse button, if it isn't already released. Valid
* button names are defined by {@link Guacamole.Mouse.State.Buttons} and
* correspond to the button-related properties of
* {@link Guacamole.Mouse.State}.
*
* @fires Guacamole.Mouse.Event.Target#mouseup
*
* @param {!string} button
* The name of the mouse button to release, as defined by
* {@link Guacamole.Mouse.State.Buttons}.
*
* @param {Event|Event[]} [events=[]]
* The DOM events related to the mouse button release, if any.
*/
this.release = function release(button, events) {
if (this.currentState[button]) {
this.currentState[button] = false;
this.dispatch(new Guacamole.Mouse.Event('mouseup', this.currentState, events));
}
};
/**
* Clicks (presses and releases) the given mouse button. Valid button
* names are defined by {@link Guacamole.Mouse.State.Buttons} and
* correspond to the button-related properties of
* {@link Guacamole.Mouse.State}.
*
* @fires Guacamole.Mouse.Event.Target#mousedown
* @fires Guacamole.Mouse.Event.Target#mouseup
*
* @param {!string} button
* The name of the mouse button to click, as defined by
* {@link Guacamole.Mouse.State.Buttons}.
*
* @param {Event|Event[]} [events=[]]
* The DOM events related to the click, if any.
*/
this.click = function click(button, events) {
this.press(button, events);
this.release(button, events);
};
/**
* Moves the mouse to the given coordinates.
*
* @fires Guacamole.Mouse.Event.Target#mousemove
*
* @param {!(Guacamole.Position|object)} position
* The new coordinates of the mouse pointer. This object may be a
* {@link Guacamole.Position} or any object with "x" and "y"
* properties.
*
* @param {Event|Event[]} [events=[]]
* The DOM events related to the mouse movement, if any.
*/
this.move = function move(position, events) {
if (this.currentState.x !== position.x || this.currentState.y !== position.y) {
this.currentState.x = position.x;
this.currentState.y = position.y;
this.dispatch(new Guacamole.Mouse.Event('mousemove', this.currentState, events));
}
};
/**
* Notifies event listeners that the mouse pointer has left the boundaries
* of the area being monitored for mouse events.
*
* @fires Guacamole.Mouse.Event.Target#mouseout
*
* @param {Event|Event[]} [events=[]]
* The DOM events related to the mouse leaving the boundaries of the
* monitored object, if any.
*/
this.out = function out(events) {
this.dispatch(new Guacamole.Mouse.Event('mouseout', this.currentState, events));
};
/**
* Releases all mouse buttons that are currently pressed. If all mouse
* buttons have already been released, this function has no effect.
*
* @fires Guacamole.Mouse.Event.Target#mouseup
*
* @param {Event|Event[]} [events=[]]
* The DOM event related to all mouse buttons being released, if any.
*/
this.reset = function reset(events) {
for (var button in Guacamole.Mouse.State.Buttons) {
this.release(Guacamole.Mouse.State.Buttons[button], events);
}
};
};
/**
* Provides cross-browser relative touch event translation for a given element.
*
* Touch events are translated into mouse events as if the touches occurred
* on a touchpad (drag to push the mouse pointer, tap to click).
*
* @example
* var touchpad = new Guacamole.Mouse.Touchpad(client.getDisplay().getElement());
*
* // Emulate a mouse using touchpad-style gestures, forwarding all mouse
* // interaction over Guacamole connection
* touchpad.onEach(['mousedown', 'mousemove', 'mouseup'], function sendMouseEvent(e) {
*
* // Re-show software mouse cursor if possibly hidden by a prior call to
* // showCursor(), such as a "mouseout" event handler that hides the
* // cursor
* client.getDisplay().showCursor(true);
*
* client.sendMouseState(e.state, true);
*
* });
*
* @constructor
* @augments Guacamole.Mouse.Event.Target
* @param {!Element} element
* The Element to use to provide touch events.
*/
Guacamole.Mouse.Touchpad = function Touchpad(element) {
Guacamole.Mouse.Event.Target.call(this);
/**
* The "mouseout" event will never be fired by Guacamole.Mouse.Touchpad.
*
* @ignore
* @event Guacamole.Mouse.Touchpad#mouseout
*/
/**
* Reference to this Guacamole.Mouse.Touchpad.
*
* @private
* @type {!Guacamole.Mouse.Touchpad}
*/
var guac_touchpad = this;
/**
* The distance a two-finger touch must move per scrollwheel event, in
* pixels.
*
* @type {!number}
*/
this.scrollThreshold = 20 * (window.devicePixelRatio || 1);
/**
* The maximum number of milliseconds to wait for a touch to end for the
* gesture to be considered a click.
*
* @type {!number}
*/
this.clickTimingThreshold = 250;
/**
* The maximum number of pixels to allow a touch to move for the gesture to
* be considered a click.
*
* @type {!number}
*/
this.clickMoveThreshold = 10 * (window.devicePixelRatio || 1);
/**
* The current mouse state. The properties of this state are updated when
* mouse events fire. This state object is also passed in as a parameter to
* the handler of any mouse events.
*
* @type {!Guacamole.Mouse.State}
*/
this.currentState = new Guacamole.Mouse.State();
var touch_count = 0;
var last_touch_x = 0;
var last_touch_y = 0;
var last_touch_time = 0;
var pixels_moved = 0;
var touch_buttons = {
1: "left",
2: "right",
3: "middle"
};
var gesture_in_progress = false;
var click_release_timeout = null;
element.addEventListener("touchend", function(e) {
e.preventDefault();
// If we're handling a gesture AND this is the last touch
if (gesture_in_progress && e.touches.length === 0) {
var time = new Date().getTime();
// Get corresponding mouse button
var button = touch_buttons[touch_count];
// If mouse already down, release anad clear timeout
if (guac_touchpad.currentState[button]) {
// Fire button up event
guac_touchpad.release(button, e);
// Clear timeout, if set
if (click_release_timeout) {
window.clearTimeout(click_release_timeout);
click_release_timeout = null;
}
}
// If single tap detected (based on time and distance)
if (time - last_touch_time <= guac_touchpad.clickTimingThreshold
&& pixels_moved < guac_touchpad.clickMoveThreshold) {
// Fire button down event
guac_touchpad.press(button, e);
// Delay mouse up - mouse up should be canceled if
// touchstart within timeout.
click_release_timeout = window.setTimeout(function() {
// Fire button up event
guac_touchpad.release(button, e);
// Gesture now over
gesture_in_progress = false;
}, guac_touchpad.clickTimingThreshold);
}
// If we're not waiting to see if this is a click, stop gesture
if (!click_release_timeout)
gesture_in_progress = false;
}
}, false);
element.addEventListener("touchstart", function(e) {
e.preventDefault();
// Track number of touches, but no more than three
touch_count = Math.min(e.touches.length, 3);
// Clear timeout, if set
if (click_release_timeout) {
window.clearTimeout(click_release_timeout);
click_release_timeout = null;
}
// Record initial touch location and time for touch movement
// and tap gestures
if (!gesture_in_progress) {
// Stop mouse events while touching
gesture_in_progress = true;
// Record touch location and time
var starting_touch = e.touches[0];
last_touch_x = starting_touch.clientX;
last_touch_y = starting_touch.clientY;
last_touch_time = new Date().getTime();
pixels_moved = 0;
}
}, false);
element.addEventListener("touchmove", function(e) {
e.preventDefault();
// Get change in touch location
var touch = e.touches[0];
var delta_x = touch.clientX - last_touch_x;
var delta_y = touch.clientY - last_touch_y;
// Track pixels moved
pixels_moved += Math.abs(delta_x) + Math.abs(delta_y);
// If only one touch involved, this is mouse move
if (touch_count === 1) {
// Calculate average velocity in Manhatten pixels per millisecond
var velocity = pixels_moved / (new Date().getTime() - last_touch_time);
// Scale mouse movement relative to velocity
var scale = 1 + velocity;
// Update mouse location
var position = new Guacamole.Position(guac_touchpad.currentState);
position.x += delta_x*scale;
position.y += delta_y*scale;
// Prevent mouse from leaving screen
position.x = Math.min(Math.max(0, position.x), element.offsetWidth - 1);
position.y = Math.min(Math.max(0, position.y), element.offsetHeight - 1);
// Fire movement event, if defined
guac_touchpad.move(position, e);
// Update touch location
last_touch_x = touch.clientX;
last_touch_y = touch.clientY;
}
// Interpret two-finger swipe as scrollwheel
else if (touch_count === 2) {
// If change in location passes threshold for scroll
if (Math.abs(delta_y) >= guac_touchpad.scrollThreshold) {
// Decide button based on Y movement direction
var button;
if (delta_y > 0) button = "down";
else button = "up";
guac_touchpad.click(button, e);
// Only update touch location after a scroll has been
// detected