Skip to content

Commit

Permalink
Merge pull request #2205 from Tyriar/char_dim_service
Browse files Browse the repository at this point in the history
Create CharSizeService
  • Loading branch information
Tyriar committed Jun 8, 2019
2 parents 2eb77cc + 9df3a36 commit b9954ec
Show file tree
Hide file tree
Showing 19 changed files with 193 additions and 237 deletions.
54 changes: 0 additions & 54 deletions src/CharMeasure.test.ts

This file was deleted.

58 changes: 0 additions & 58 deletions src/CharMeasure.ts

This file was deleted.

7 changes: 2 additions & 5 deletions src/CompositionHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { assert } from 'chai';
import { CompositionHelper } from './CompositionHelper';
import { ITerminal } from './Types';
import { MockCharSizeService } from 'TestUtils.test';

describe('CompositionHelper', () => {
let terminal: ITerminal;
Expand Down Expand Up @@ -48,16 +49,12 @@ describe('CompositionHelper', () => {
buffer: {
isCursorInViewport: true
},
charMeasure: {
height: 10,
width: 10
},
options: {
lineHeight: 1
}
} as any;
handledText = '';
compositionHelper = new CompositionHelper(textarea, compositionView, terminal);
compositionHelper = new CompositionHelper(textarea, compositionView, terminal, new MockCharSizeService(10, 10));
});

describe('Input', () => {
Expand Down
8 changes: 5 additions & 3 deletions src/CompositionHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { ITerminal } from './Types';
import { ICharSizeService } from 'ui/services/Services';

interface IPosition {
start: number;
Expand Down Expand Up @@ -42,7 +43,8 @@ export class CompositionHelper {
constructor(
private _textarea: HTMLTextAreaElement,
private _compositionView: HTMLElement,
private _terminal: ITerminal
private _terminal: ITerminal,
private _charSizeService: ICharSizeService
) {
this._isComposing = false;
this._isSendingComposition = false;
Expand Down Expand Up @@ -195,9 +197,9 @@ export class CompositionHelper {
}

if (this._terminal.buffer.isCursorInViewport) {
const cellHeight = Math.ceil(this._terminal.charMeasure.height * this._terminal.options.lineHeight);
const cellHeight = Math.ceil(this._charSizeService.height * this._terminal.options.lineHeight);
const cursorTop = this._terminal.buffer.y * cellHeight;
const cursorLeft = this._terminal.buffer.x * this._terminal.charMeasure.width;
const cursorLeft = this._terminal.buffer.x * this._charSizeService.width;

this._compositionView.style.left = cursorLeft + 'px';
this._compositionView.style.top = cursorTop + 'px';
Expand Down
35 changes: 8 additions & 27 deletions src/MouseHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,43 @@
* @license MIT
*/

import jsdom = require('jsdom');
import { assert } from 'chai';
import { MouseHelper } from './MouseHelper';
import { MockCharMeasure, MockRenderer } from './TestUtils.test';
import { MockRenderer, MockCharSizeService } from './TestUtils.test';

const CHAR_WIDTH = 10;
const CHAR_HEIGHT = 20;

describe('MouseHelper.getCoords', () => {
let dom: jsdom.JSDOM;
let window: Window;
let document: Document;
let mouseHelper: MouseHelper;

let charMeasure: MockCharMeasure;

beforeEach(() => {
dom = new jsdom.JSDOM('');
window = dom.window;
document = window.document;
charMeasure = new MockCharMeasure();
charMeasure.width = CHAR_WIDTH;
charMeasure.height = CHAR_HEIGHT;
const renderer = new MockRenderer();
renderer.dimensions = <any>{
actualCellWidth: CHAR_WIDTH,
actualCellHeight: CHAR_HEIGHT
};
mouseHelper = new MouseHelper(renderer as any);
});

describe('when charMeasure is not initialized', () => {
it('should return null', () => {
charMeasure = new MockCharMeasure();
assert.equal(mouseHelper.getCoords({ clientX: 0, clientY: 0 }, document.createElement('div'), charMeasure, 10, 10), null);
});
mouseHelper = new MouseHelper(renderer as any, new MockCharSizeService(CHAR_WIDTH, CHAR_HEIGHT));
});

it('should return the cell that was clicked', () => {
let coords: [number, number];
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH / 2, clientY: CHAR_HEIGHT / 2 }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH / 2, clientY: CHAR_HEIGHT / 2 }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [1, 1]);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [1, 1]);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT + 1 }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH, clientY: CHAR_HEIGHT + 1 }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [1, 2]);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH + 1, clientY: CHAR_HEIGHT }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH + 1, clientY: CHAR_HEIGHT }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [2, 1]);
});

it('should ensure the coordinates are returned within the terminal bounds', () => {
let coords: [number, number];
coords = mouseHelper.getCoords({ clientX: -1, clientY: -1 }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: -1, clientY: -1 }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [1, 1]);
// Event are double the cols/rows
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH * 20, clientY: CHAR_HEIGHT * 20 }, document.createElement('div'), charMeasure, 10, 10);
coords = mouseHelper.getCoords({ clientX: CHAR_WIDTH * 20, clientY: CHAR_HEIGHT * 20 }, document.createElement('div'), 10, 10);
assert.deepEqual(coords, [10, 10], 'coordinates should never come back as larger than the terminal');
});
});
18 changes: 9 additions & 9 deletions src/MouseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
* @license MIT
*/

import { ICharMeasure, IMouseHelper } from './Types';
import { IMouseHelper } from './Types';
import { RenderCoordinator } from './renderer/RenderCoordinator';
import { ICharSizeService } from 'ui/services/Services';

export class MouseHelper implements IMouseHelper {
constructor(
private _renderCoordinator: RenderCoordinator
private _renderCoordinator: RenderCoordinator,
private _charSizeService: ICharSizeService
) {
}

Expand All @@ -23,16 +25,15 @@ export class MouseHelper implements IMouseHelper {
* little faster and this function is used in some low level code.
* @param event The mouse event.
* @param element The terminal's container element.
* @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 n the terminal.
* @param isSelection Whether the request is for the selection or not. This will
* apply an offset to the x value such that the left half of the cell will
* select that cell and the right half will select the next cell.
*/
public getCoords(event: {clientX: number, clientY: number}, element: HTMLElement, charMeasure: ICharMeasure, colCount: number, rowCount: number, isSelection?: boolean): [number, number] {
// Coordinates cannot be measured if charMeasure has not been initialized
if (!charMeasure.width || !charMeasure.height) {
public getCoords(event: {clientX: number, clientY: number}, element: HTMLElement, colCount: number, rowCount: number, isSelection?: boolean): [number, number] {
// Coordinates cannot be measured if there are no valid
if (!this._charSizeService.hasValidSize) {
return null;
}

Expand All @@ -59,12 +60,11 @@ export class MouseHelper implements IMouseHelper {
* as expected by xterm.
* @param event The mouse event.
* @param element The terminal's container element.
* @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.
*/
public getRawByteCoords(event: MouseEvent, element: HTMLElement, charMeasure: ICharMeasure, colCount: number, rowCount: number): { x: number, y: number } {
const coords = this.getCoords(event, element, charMeasure, colCount, rowCount);
public getRawByteCoords(event: MouseEvent, element: HTMLElement, colCount: number, rowCount: number): { x: number, y: number } {
const coords = this.getCoords(event, element, colCount, rowCount);
let x = coords[0];
let y = coords[1];

Expand Down
2 changes: 1 addition & 1 deletion src/MouseZoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager {
}

private _findZoneEventAt(e: MouseEvent): IMouseZone {
const coords = this._terminal.mouseHelper.getCoords(e, this._terminal.screenElement, this._terminal.charMeasure, this._terminal.cols, this._terminal.rows);
const coords = this._terminal.mouseHelper.getCoords(e, this._terminal.screenElement, this._terminal.cols, this._terminal.rows);
if (!coords) {
return null;
}
Expand Down
10 changes: 4 additions & 6 deletions src/SelectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
*/

import { assert } from 'chai';
import { CharMeasure } from './CharMeasure';
import { SelectionManager, SelectionMode } from './SelectionManager';
import { SelectionModel } from './SelectionModel';
import { BufferSet } from './BufferSet';
import { ITerminal, IBuffer } from './Types';
import { IBufferLine } from 'core/Types';
import { MockTerminal } from './TestUtils.test';
import { MockTerminal, MockCharSizeService } from './TestUtils.test';
import { BufferLine, CellData } from 'core/buffer/BufferLine';

class TestMockTerminal extends MockTerminal {
Expand All @@ -19,10 +18,9 @@ class TestMockTerminal extends MockTerminal {

class TestSelectionManager extends SelectionManager {
constructor(
terminal: ITerminal,
charMeasure: CharMeasure
terminal: ITerminal
) {
super(terminal, charMeasure);
super(terminal, new MockCharSizeService(10, 10));
}

public get model(): SelectionModel { return this._model; }
Expand Down Expand Up @@ -52,7 +50,7 @@ describe('SelectionManager', () => {
terminal.buffers = new BufferSet(terminal);
terminal.buffer = terminal.buffers.active;
buffer = terminal.buffer;
selectionManager = new TestSelectionManager(terminal, null);
selectionManager = new TestSelectionManager(terminal);
});

function stringToRow(text: string): IBufferLine {
Expand Down
8 changes: 4 additions & 4 deletions src/SelectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { ITerminal, ISelectionManager, IBuffer, ISelectionRedrawRequestEvent } f
import { IBufferLine } from 'core/Types';
import { MouseHelper } from './MouseHelper';
import * as Browser from 'common/Platform';
import { CharMeasure } from './CharMeasure';
import { SelectionModel } from './SelectionModel';
import { AltClickHandler } from './handlers/AltClickHandler';
import { CellData } from 'core/buffer/BufferLine';
import { IDisposable } from 'xterm';
import { EventEmitter2, IEvent } from 'common/EventEmitter2';
import { ICharSizeService } from 'ui/services/Services';

/**
* The number of pixels the mouse needs to be above or below the viewport in
Expand Down Expand Up @@ -117,7 +117,7 @@ export class SelectionManager implements ISelectionManager {

constructor(
private _terminal: ITerminal,
private _charMeasure: CharMeasure
private _charSizeService: ICharSizeService
) {
this._initListeners();
this.enable();
Expand Down Expand Up @@ -354,7 +354,7 @@ export class SelectionManager implements ISelectionManager {
* @param event The mouse event.
*/
private _getMouseBufferCoords(event: MouseEvent): [number, number] {
const coords = this._terminal.mouseHelper.getCoords(event, this._terminal.screenElement, this._charMeasure, this._terminal.cols, this._terminal.rows, true);
const coords = this._terminal.mouseHelper.getCoords(event, this._terminal.screenElement, this._terminal.cols, this._terminal.rows, true);
if (!coords) {
return null;
}
Expand All @@ -375,7 +375,7 @@ export class SelectionManager implements ISelectionManager {
*/
private _getMouseEventScrollAmount(event: MouseEvent): number {
let offset = MouseHelper.getCoordsRelativeToElement(event, this._terminal.screenElement)[1];
const terminalHeight = this._terminal.rows * Math.ceil(this._charMeasure.height * this._terminal.options.lineHeight);
const terminalHeight = this._terminal.rows * Math.ceil(this._charSizeService.height * this._terminal.options.lineHeight);
if (offset >= 0 && offset <= terminalHeight) {
return 0;
}
Expand Down

0 comments on commit b9954ec

Please sign in to comment.