Pull getCoords into a Mouse util module

This is part of the work to prepare for the upcoing selection changes

Related #207
This commit is contained in:
Daniel Imms
2017-05-21 11:50:01 -07:00
parent a95ac990ff
commit 4d32826891
2 changed files with 52 additions and 46 deletions
+49
View File
@@ -0,0 +1,49 @@
/**
* @license MIT
*/
import { CharMeasure } from './CharMeasure';
export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] {
// ignore browsers without pageX for now
if (event.pageX == null) {
return null;
}
let x = event.pageX;
let y = event.pageY;
let el = rowContainer;
// should probably check offsetParent
// but this is more portable
while (el && el !== self.document.documentElement) {
x -= el.offsetLeft;
y -= el.offsetTop;
el = 'offsetParent' in el ? <HTMLElement>el.offsetParent : <HTMLElement>el.parentElement;
}
// convert to cols/rows
x = Math.ceil(x / charMeasure.width);
y = Math.ceil(y / charMeasure.height);
return [x, y];
}
export function getRawByteCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure, colCount: number, rowCount: number): { x: number, y: number } {
const coords = getCoords(event, rowContainer, charMeasure);
let x = coords[0];
let y = coords[1];
// be sure to avoid sending bad positions to the program
if (x < 0) x = 0;
if (x > colCount) x = colCount;
if (y < 0) y = 0;
if (y > rowCount) y = rowCount;
// xterm sends raw bytes and
// starts at 32 (SP) for each.
x += 32;
y += 32;
return { x, y };
}
+3 -46
View File
@@ -24,6 +24,7 @@ import { CharMeasure } from './utils/CharMeasure';
import * as Browser from './utils/Browser';
import * as Keyboard from './utils/Keyboard';
import { CHARSETS } from './Charsets';
import { getRawByteCoords } from './utils/Mouse';
/**
* Terminal Emulation References:
@@ -789,7 +790,7 @@ Terminal.prototype.bindMouse = function() {
button = getButton(ev);
// get mouse coordinates
pos = getCoords(ev);
pos = getRawByteCoords(ev, self.rowContainer, self.charMeasure, self.cols, self.rows);
if (!pos) return;
sendEvent(button, pos);
@@ -817,7 +818,7 @@ Terminal.prototype.bindMouse = function() {
var button = pressed
, pos;
pos = getCoords(ev);
pos = getRawByteCoords(ev, self.rowContainer, self.charMeasure, self.cols, self.rows);
if (!pos) return;
// buttons marked as motions
@@ -992,50 +993,6 @@ Terminal.prototype.bindMouse = function() {
return button;
}
// mouse coordinates measured in cols/rows
function getCoords(ev) {
var x, y, w, h, el;
// ignore browsers without pageX for now
if (ev.pageX == null) return;
x = ev.pageX;
y = ev.pageY;
el = self.element;
// should probably check offsetParent
// but this is more portable
while (el && el !== self.document.documentElement) {
x -= el.offsetLeft;
y -= el.offsetTop;
el = 'offsetParent' in el
? el.offsetParent
: el.parentNode;
}
// convert to cols/rows
x = Math.ceil(x / self.charMeasure.width);
y = Math.ceil(y / self.charMeasure.height);
// be sure to avoid sending
// bad positions to the program
if (x < 0) x = 0;
if (x > self.cols) x = self.cols;
if (y < 0) y = 0;
if (y > self.rows) y = self.rows;
// xterm sends raw bytes and
// starts at 32 (SP) for each.
x += 32;
y += 32;
return {
x: x,
y: y,
type: 'wheel'
};
}
on(el, 'mousedown', function(ev) {
if (!self.mouseEvents) return;