Debian Codegraph @

node/@xterm/xterm/src/browser/input/Mouse.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 export function getCoordsRelativeToElement(window: Pick<Window, 'getComputedStyle'>, event: {clientX: number, clientY: number}, element: HTMLElement): [number, number] {
7 const rect = element.getBoundingClientRect();
8 const elementStyle = window.getComputedStyle(element);
9 const leftPadding = parseInt(elementStyle.getPropertyValue('padding-left'));
10 const topPadding = parseInt(elementStyle.getPropertyValue('padding-top'));
11 return [
12 event.clientX - rect.left - leftPadding,
13 event.clientY - rect.top - topPadding
14 ];
15 }
16
17 /**
18 * Gets coordinates within the terminal for a particular mouse event. The result
19 * is returned as an array in the form [x, y] instead of an object as it's a
20 * little faster and this function is used in some low level code.
21 * @param window The window object the element belongs to.
22 * @param event The mouse event.
23 * @param element The terminal's container element.
24 * @param colCount The number of columns in the terminal.
25 * @param rowCount The number of rows n the terminal.
26 * @param hasValidCharSize Whether there is a valid character size available.
27 * @param cssCellWidth The cell width device pixel render dimensions.
28 * @param cssCellHeight The cell height device pixel render dimensions.
29 * @param isSelection Whether the request is for the selection or not. This will
30 * apply an offset to the x value such that the left half of the cell will
31 * select that cell and the right half will select the next cell.
32 */
33 export function getCoords(window: Pick<Window, 'getComputedStyle'>, event: Pick<MouseEvent, 'clientX' | 'clientY'>, element: HTMLElement, colCount: number, rowCount: number, hasValidCharSize: boolean, cssCellWidth: number, cssCellHeight: number, isSelection?: boolean): [number, number] | undefined {
34 // Coordinates cannot be measured if there are no valid
35 if (!hasValidCharSize) {
36 return undefined;
37 }
38
39 const coords = getCoordsRelativeToElement(window, event, element);
40 if (!coords) {
41 return undefined;
42 }
43
44 coords[0] = Math.ceil((coords[0] + (isSelection ? cssCellWidth / 2 : 0)) / cssCellWidth);
45 coords[1] = Math.ceil(coords[1] / cssCellHeight);
46
47 // Ensure coordinates are within the terminal viewport. Note that selections
48 // need an addition point of precision to cover the end point (as characters
49 // cover half of one char and half of the next).
50 coords[0] = Math.min(Math.max(coords[0], 1), colCount + (isSelection ? 1 : 0));
51 coords[1] = Math.min(Math.max(coords[1], 1), rowCount);
52
53 return coords;
54 }