Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/browser/mouseEvent.ts
3292 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as browser from './browser.js';
7
import { IframeUtils } from './iframe.js';
8
import * as platform from '../common/platform.js';
9
10
export interface IMouseEvent {
11
readonly browserEvent: MouseEvent;
12
readonly leftButton: boolean;
13
readonly middleButton: boolean;
14
readonly rightButton: boolean;
15
readonly buttons: number;
16
readonly target: HTMLElement;
17
readonly detail: number;
18
readonly posx: number;
19
readonly posy: number;
20
readonly ctrlKey: boolean;
21
readonly shiftKey: boolean;
22
readonly altKey: boolean;
23
readonly metaKey: boolean;
24
readonly timestamp: number;
25
readonly defaultPrevented: boolean;
26
27
preventDefault(): void;
28
stopPropagation(): void;
29
}
30
31
export class StandardMouseEvent implements IMouseEvent {
32
33
public readonly browserEvent: MouseEvent;
34
35
public readonly leftButton: boolean;
36
public readonly middleButton: boolean;
37
public readonly rightButton: boolean;
38
public readonly buttons: number;
39
public readonly target: HTMLElement;
40
public detail: number;
41
public readonly posx: number;
42
public readonly posy: number;
43
public readonly ctrlKey: boolean;
44
public readonly shiftKey: boolean;
45
public readonly altKey: boolean;
46
public readonly metaKey: boolean;
47
public readonly timestamp: number;
48
public readonly defaultPrevented: boolean;
49
50
constructor(targetWindow: Window, e: MouseEvent) {
51
this.timestamp = Date.now();
52
this.browserEvent = e;
53
this.leftButton = e.button === 0;
54
this.middleButton = e.button === 1;
55
this.rightButton = e.button === 2;
56
this.buttons = e.buttons;
57
this.defaultPrevented = e.defaultPrevented;
58
59
this.target = <HTMLElement>e.target;
60
61
this.detail = e.detail || 1;
62
if (e.type === 'dblclick') {
63
this.detail = 2;
64
}
65
this.ctrlKey = e.ctrlKey;
66
this.shiftKey = e.shiftKey;
67
this.altKey = e.altKey;
68
this.metaKey = e.metaKey;
69
70
if (typeof e.pageX === 'number') {
71
this.posx = e.pageX;
72
this.posy = e.pageY;
73
} else {
74
// Probably hit by MSGestureEvent
75
this.posx = e.clientX + this.target.ownerDocument.body.scrollLeft + this.target.ownerDocument.documentElement.scrollLeft;
76
this.posy = e.clientY + this.target.ownerDocument.body.scrollTop + this.target.ownerDocument.documentElement.scrollTop;
77
}
78
79
// Find the position of the iframe this code is executing in relative to the iframe where the event was captured.
80
const iframeOffsets = IframeUtils.getPositionOfChildWindowRelativeToAncestorWindow(targetWindow, e.view);
81
this.posx -= iframeOffsets.left;
82
this.posy -= iframeOffsets.top;
83
}
84
85
public preventDefault(): void {
86
this.browserEvent.preventDefault();
87
}
88
89
public stopPropagation(): void {
90
this.browserEvent.stopPropagation();
91
}
92
}
93
94
export class DragMouseEvent extends StandardMouseEvent {
95
96
public readonly dataTransfer: DataTransfer;
97
98
constructor(targetWindow: Window, e: MouseEvent) {
99
super(targetWindow, e);
100
this.dataTransfer = (<any>e).dataTransfer;
101
}
102
}
103
104
export interface IMouseWheelEvent extends MouseEvent {
105
readonly wheelDelta: number;
106
readonly wheelDeltaX: number;
107
readonly wheelDeltaY: number;
108
109
readonly deltaX: number;
110
readonly deltaY: number;
111
readonly deltaZ: number;
112
readonly deltaMode: number;
113
}
114
115
interface IWebKitMouseWheelEvent {
116
wheelDeltaY: number;
117
wheelDeltaX: number;
118
}
119
120
interface IGeckoMouseWheelEvent {
121
HORIZONTAL_AXIS: number;
122
VERTICAL_AXIS: number;
123
axis: number;
124
detail: number;
125
}
126
127
export class StandardWheelEvent {
128
129
public readonly browserEvent: IMouseWheelEvent | null;
130
public readonly deltaY: number;
131
public readonly deltaX: number;
132
public readonly target: Node;
133
134
constructor(e: IMouseWheelEvent | null, deltaX: number = 0, deltaY: number = 0) {
135
136
this.browserEvent = e || null;
137
this.target = e ? (e.target || (<any>e).targetNode || e.srcElement) : null;
138
139
this.deltaY = deltaY;
140
this.deltaX = deltaX;
141
142
let shouldFactorDPR: boolean = false;
143
if (browser.isChrome) {
144
// Chrome version >= 123 contains the fix to factor devicePixelRatio into the wheel event.
145
// See https://chromium.googlesource.com/chromium/src.git/+/be51b448441ff0c9d1f17e0f25c4bf1ab3f11f61
146
const chromeVersionMatch = navigator.userAgent.match(/Chrome\/(\d+)/);
147
const chromeMajorVersion = chromeVersionMatch ? parseInt(chromeVersionMatch[1]) : 123;
148
shouldFactorDPR = chromeMajorVersion <= 122;
149
}
150
151
if (e) {
152
// Old (deprecated) wheel events
153
const e1 = <IWebKitMouseWheelEvent><any>e;
154
const e2 = <IGeckoMouseWheelEvent><any>e;
155
const devicePixelRatio = e.view?.devicePixelRatio || 1;
156
157
// vertical delta scroll
158
if (typeof e1.wheelDeltaY !== 'undefined') {
159
if (shouldFactorDPR) {
160
// Refs https://github.com/microsoft/vscode/issues/146403#issuecomment-1854538928
161
this.deltaY = e1.wheelDeltaY / (120 * devicePixelRatio);
162
} else {
163
this.deltaY = e1.wheelDeltaY / 120;
164
}
165
} else if (typeof e2.VERTICAL_AXIS !== 'undefined' && e2.axis === e2.VERTICAL_AXIS) {
166
this.deltaY = -e2.detail / 3;
167
} else if (e.type === 'wheel') {
168
// Modern wheel event
169
// https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
170
const ev = <WheelEvent><unknown>e;
171
172
if (ev.deltaMode === ev.DOM_DELTA_LINE) {
173
// the deltas are expressed in lines
174
if (browser.isFirefox && !platform.isMacintosh) {
175
this.deltaY = -e.deltaY / 3;
176
} else {
177
this.deltaY = -e.deltaY;
178
}
179
} else {
180
this.deltaY = -e.deltaY / 40;
181
}
182
}
183
184
// horizontal delta scroll
185
if (typeof e1.wheelDeltaX !== 'undefined') {
186
if (browser.isSafari && platform.isWindows) {
187
this.deltaX = - (e1.wheelDeltaX / 120);
188
} else if (shouldFactorDPR) {
189
// Refs https://github.com/microsoft/vscode/issues/146403#issuecomment-1854538928
190
this.deltaX = e1.wheelDeltaX / (120 * devicePixelRatio);
191
} else {
192
this.deltaX = e1.wheelDeltaX / 120;
193
}
194
} else if (typeof e2.HORIZONTAL_AXIS !== 'undefined' && e2.axis === e2.HORIZONTAL_AXIS) {
195
this.deltaX = -e.detail / 3;
196
} else if (e.type === 'wheel') {
197
// Modern wheel event
198
// https://developer.mozilla.org/en-US/docs/Web/API/WheelEvent
199
const ev = <WheelEvent><unknown>e;
200
201
if (ev.deltaMode === ev.DOM_DELTA_LINE) {
202
// the deltas are expressed in lines
203
if (browser.isFirefox && !platform.isMacintosh) {
204
this.deltaX = -e.deltaX / 3;
205
} else {
206
this.deltaX = -e.deltaX;
207
}
208
} else {
209
this.deltaX = -e.deltaX / 40;
210
}
211
}
212
213
// Assume a vertical scroll if nothing else worked
214
if (this.deltaY === 0 && this.deltaX === 0 && e.wheelDelta) {
215
if (shouldFactorDPR) {
216
// Refs https://github.com/microsoft/vscode/issues/146403#issuecomment-1854538928
217
this.deltaY = e.wheelDelta / (120 * devicePixelRatio);
218
} else {
219
this.deltaY = e.wheelDelta / 120;
220
}
221
}
222
}
223
}
224
225
public preventDefault(): void {
226
this.browserEvent?.preventDefault();
227
}
228
229
public stopPropagation(): void {
230
this.browserEvent?.stopPropagation();
231
}
232
}
233
234