Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull get coords into module #669

Merged
merged 4 commits into from
May 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/utils/Mouse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license MIT
*/

import { CharMeasure } from './CharMeasure';

/**
* Gets coordinates within the terminal for a particular mouse event. The result
* is returned as an array in the form [x, y] instead of an object as it's a
* little faster and this function is used in some low level code.
* @param event The mouse event.
* @param rowContainer The terminal's row container.
* @param charMeasure The char measure object used to determine character sizes.
*/
export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] {
// Ignore browsers that don't support MouseEvent.pageX
if (event.pageX == null) {
return null;
}

let x = event.pageX;
let y = event.pageY;
let el = rowContainer;

// Converts the coordinates from being relative to the document to being
// relative to the terminal.
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];
}

/**
* Gets coordinates within the terminal for a particular mouse event, wrapping
* them to the bounds of the terminal and adding 32 to both the x and y values
* as expected by xterm.
* @param event The mouse event.
* @param rowContainer The terminal's row container.
* @param charMeasure The char measure object used to determine character sizes.
* @param colCount The number of columns in the terminal.
* @param rowCount The number of rows in the terminal.
*/
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];

// Ensure coordinates are within the terminal viewport.
x = Math.min(Math.max(x, 0), colCount);
y = Math.min(Math.max(y, 0), rowCount);

// xterm sends raw bytes and starts at 32 (SP) for each.
x += 32;
y += 32;

return { x, y };
}
49 changes: 3 additions & 46 deletions src/xterm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down