]>
git.proxmox.com Git - mirror_novnc.git/blob - core/input/mouse.js
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)
7 import * as Log
from '../util/logging.js';
8 import { setCapture
, stopEvent
, getPointerEvent
} from '../util/events.js';
10 export default class Mouse
{
12 this._target
= target
|| document
;
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)
23 // ===== EVENT HANDLERS =====
25 this.onmousebutton
= () => {}; // Handler for mouse button press/release
26 this.onmousemove
= () => {}; // Handler for mouse movement
29 // ===== PRIVATE METHODS =====
31 _resetDoubleClickTimer() {
32 this._doubleClickTimer
= null;
35 _handleMouseButton(e
, down
) {
36 this._updateMousePosition(e
);
39 let bmask
= 1 << e
.button
;
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
);
49 setCapture(this._target
);
51 this._handleMouseButton(e
, 1);
55 this._handleMouseButton(e
, 0);
59 this._updateMousePosition(e
);
60 this.onmousemove(this._pos
.x
, this._pos
.y
);
64 _handleMouseDisable(e
) {
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.
71 if (e
.target
== this._target
) {
76 // Update coordinates relative to target
77 _updateMousePosition(e
) {
78 e
= getPointerEvent(e
);
79 const bounds
= this._target
.getBoundingClientRect();
82 // Clip to target bounds
83 if (e
.clientX
< bounds
.left
) {
85 } else if (e
.clientX
>= bounds
.right
) {
88 x
= e
.clientX
- bounds
.left
;
90 if (e
.clientY
< bounds
.top
) {
92 } else if (e
.clientY
>= bounds
.bottom
) {
93 y
= bounds
.height
- 1;
95 y
= e
.clientY
- bounds
.top
;
97 this._pos
= {x
: x
, y
: y
};
100 // ===== PUBLIC METHODS =====
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
);
108 // Prevent middle-click pasting (see above for why we bind to document)
109 document
.addEventListener('click', this._eventHandlers
.mousedisable
);
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
);
117 const t
= this._target
;
119 t
.removeEventListener('mousedown', this._eventHandlers
.mousedown
);
120 t
.removeEventListener('mouseup', this._eventHandlers
.mouseup
);
121 t
.removeEventListener('mousemove', this._eventHandlers
.mousemove
);
123 document
.removeEventListener('click', this._eventHandlers
.mousedisable
);
125 t
.removeEventListener('contextmenu', this._eventHandlers
.mousedisable
);