]> git.proxmox.com Git - mirror_novnc.git/blob - core/input/mouse.js
Move wheel event handling to RFB class
[mirror_novnc.git] / core / input / mouse.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 or any later version (see LICENSE.txt)
5 */
6
7 import * as Log from '../util/logging.js';
8 import { setCapture, stopEvent, getPointerEvent } from '../util/events.js';
9
10 export default class Mouse {
11 constructor(target) {
12 this._target = target || document;
13
14 this._pos = null;
15
16 this._eventHandlers = {
17 'mousedown': this._handleMouseDown.bind(this),
18 'mouseup': this._handleMouseUp.bind(this),
19 'mousemove': this._handleMouseMove.bind(this),
20 'mousedisable': this._handleMouseDisable.bind(this)
21 };
22
23 // ===== EVENT HANDLERS =====
24
25 this.onmousebutton = () => {}; // Handler for mouse button press/release
26 this.onmousemove = () => {}; // Handler for mouse movement
27 }
28
29 // ===== PRIVATE METHODS =====
30
31 _resetDoubleClickTimer() {
32 this._doubleClickTimer = null;
33 }
34
35 _handleMouseButton(e, down) {
36 this._updateMousePosition(e);
37 let pos = this._pos;
38
39 let bmask = 1 << e.button;
40
41 Log.Debug("onmousebutton " + (down ? "down" : "up") +
42 ", x: " + pos.x + ", y: " + pos.y + ", bmask: " + bmask);
43 this.onmousebutton(pos.x, pos.y, down, bmask);
44
45 stopEvent(e);
46 }
47
48 _handleMouseDown(e) {
49 setCapture(this._target);
50
51 this._handleMouseButton(e, 1);
52 }
53
54 _handleMouseUp(e) {
55 this._handleMouseButton(e, 0);
56 }
57
58 _handleMouseMove(e) {
59 this._updateMousePosition(e);
60 this.onmousemove(this._pos.x, this._pos.y);
61 stopEvent(e);
62 }
63
64 _handleMouseDisable(e) {
65 /*
66 * Stop propagation if inside canvas area
67 * Note: This is only needed for the 'click' event as it fails
68 * to fire properly for the target element so we have
69 * to listen on the document element instead.
70 */
71 if (e.target == this._target) {
72 stopEvent(e);
73 }
74 }
75
76 // Update coordinates relative to target
77 _updateMousePosition(e) {
78 e = getPointerEvent(e);
79 const bounds = this._target.getBoundingClientRect();
80 let x;
81 let y;
82 // Clip to target bounds
83 if (e.clientX < bounds.left) {
84 x = 0;
85 } else if (e.clientX >= bounds.right) {
86 x = bounds.width - 1;
87 } else {
88 x = e.clientX - bounds.left;
89 }
90 if (e.clientY < bounds.top) {
91 y = 0;
92 } else if (e.clientY >= bounds.bottom) {
93 y = bounds.height - 1;
94 } else {
95 y = e.clientY - bounds.top;
96 }
97 this._pos = {x: x, y: y};
98 }
99
100 // ===== PUBLIC METHODS =====
101
102 grab() {
103 const t = this._target;
104 t.addEventListener('mousedown', this._eventHandlers.mousedown);
105 t.addEventListener('mouseup', this._eventHandlers.mouseup);
106 t.addEventListener('mousemove', this._eventHandlers.mousemove);
107
108 // Prevent middle-click pasting (see above for why we bind to document)
109 document.addEventListener('click', this._eventHandlers.mousedisable);
110
111 // preventDefault() on mousedown doesn't stop this event for some
112 // reason so we have to explicitly block it
113 t.addEventListener('contextmenu', this._eventHandlers.mousedisable);
114 }
115
116 ungrab() {
117 const t = this._target;
118
119 t.removeEventListener('mousedown', this._eventHandlers.mousedown);
120 t.removeEventListener('mouseup', this._eventHandlers.mouseup);
121 t.removeEventListener('mousemove', this._eventHandlers.mousemove);
122
123 document.removeEventListener('click', this._eventHandlers.mousedisable);
124
125 t.removeEventListener('contextmenu', this._eventHandlers.mousedisable);
126 }
127 }