Debian Codegraph @

node/@xterm/xterm/src/browser/services/MouseService.ts

raw
1 /**
2 * Copyright (c) 2017 The xterm.js authors. All rights reserved.
3 * @license MIT
4 */
5
6 import { ICharSizeService, IRenderService, IMouseService } from './Services';
7 import { getCoords, getCoordsRelativeToElement } from 'browser/input/Mouse';
8
9 export class MouseService implements IMouseService {
10 public serviceBrand: undefined;
11
12 constructor(
13 @IRenderService private readonly _renderService: IRenderService,
14 @ICharSizeService private readonly _charSizeService: ICharSizeService
15 ) {
16 }
17
18 public getCoords(event: {clientX: number, clientY: number}, element: HTMLElement, colCount: number, rowCount: number, isSelection?: boolean): [number, number] | undefined {
19 return getCoords(
20 window,
21 event,
22 element,
23 colCount,
24 rowCount,
25 this._charSizeService.hasValidSize,
26 this._renderService.dimensions.css.cell.width,
27 this._renderService.dimensions.css.cell.height,
28 isSelection
29 );
30 }
31
32 public getMouseReportCoords(event: MouseEvent, element: HTMLElement): { col: number, row: number, x: number, y: number } | undefined {
33 const coords = getCoordsRelativeToElement(window, event, element);
34 if (!this._charSizeService.hasValidSize) {
35 return undefined;
36 }
37 coords[0] = Math.min(Math.max(coords[0], 0), this._renderService.dimensions.css.canvas.width - 1);
38 coords[1] = Math.min(Math.max(coords[1], 0), this._renderService.dimensions.css.canvas.height - 1);
39 return {
40 col: Math.floor(coords[0] / this._renderService.dimensions.css.cell.width),
41 row: Math.floor(coords[1] / this._renderService.dimensions.css.cell.height),
42 x: Math.floor(coords[0]),
43 y: Math.floor(coords[1])
44 };
45 }
46 }