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