From 2eec18e7f5e1d386c4defc833e89814e81903f2e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Sat, 13 Jul 2019 14:35:31 -0700 Subject: [PATCH] Remove ITerminal from MouseZoneManager --- src/MouseZoneManager.ts | 38 +++++++++++++++++++++-------------- src/Terminal.ts | 10 ++++----- src/browser/Linkifier.test.ts | 2 +- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/MouseZoneManager.ts b/src/MouseZoneManager.ts index 92ae9b1d..42eb6545 100644 --- a/src/MouseZoneManager.ts +++ b/src/MouseZoneManager.ts @@ -3,11 +3,11 @@ * @license MIT */ -import { ITerminal } from './Types'; import { Disposable } from 'common/Lifecycle'; import { addDisposableDomListener } from 'browser/Lifecycle'; -import { IMouseService } from 'browser/services/Services'; +import { IMouseService, ISelectionService } from 'browser/services/Services'; import { IMouseZoneManager, IMouseZone } from 'browser/Types'; +import { IBufferService } from 'common/services/Services'; const HOVER_DURATION = 500; @@ -33,12 +33,15 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { private _initialSelectionLength: number; constructor( - private _terminal: ITerminal, - private _mouseService: IMouseService + private readonly _element: HTMLElement, + private readonly _screenElement: HTMLElement, + private readonly _bufferService: IBufferService, + private readonly _mouseService: IMouseService, + private readonly _selectionService: ISelectionService ) { super(); - this.register(addDisposableDomListener(this._terminal.element, 'mousedown', e => this._onMouseDown(e))); + this.register(addDisposableDomListener(this._element, 'mousedown', e => this._onMouseDown(e))); // These events are expensive, only listen to it when mouse zones are active this._mouseMoveListener = e => this._onMouseMove(e); @@ -67,7 +70,7 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { // Clear all if start/end weren't set if (!end) { start = 0; - end = this._terminal.rows - 1; + end = this._bufferService.rows - 1; } // Iterate through zones and clear them out if they're within the range @@ -93,18 +96,18 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { private _activate(): void { if (!this._areZonesActive) { this._areZonesActive = true; - this._terminal.element.addEventListener('mousemove', this._mouseMoveListener); - this._terminal.element.addEventListener('mouseleave', this._mouseLeaveListener); - this._terminal.element.addEventListener('click', this._clickListener); + this._element.addEventListener('mousemove', this._mouseMoveListener); + this._element.addEventListener('mouseleave', this._mouseLeaveListener); + this._element.addEventListener('click', this._clickListener); } } private _deactivate(): void { if (this._areZonesActive) { this._areZonesActive = false; - this._terminal.element.removeEventListener('mousemove', this._mouseMoveListener); - this._terminal.element.removeEventListener('mouseleave', this._mouseLeaveListener); - this._terminal.element.removeEventListener('click', this._clickListener); + this._element.removeEventListener('mousemove', this._mouseMoveListener); + this._element.removeEventListener('mouseleave', this._mouseLeaveListener); + this._element.removeEventListener('click', this._clickListener); } } @@ -162,7 +165,7 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { private _onMouseDown(e: MouseEvent): void { // Store current terminal selection length, to check if we're performing // a selection operation - this._initialSelectionLength = this._terminal.getSelection().length; + this._initialSelectionLength = this._getSelectionLength(); // Ignore the event if there are no zones active if (!this._areZonesActive) { @@ -196,7 +199,7 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { // Find the active zone and click it if found and no selection was // being performed const zone = this._findZoneEventAt(e); - const currentSelectionLength = this._terminal.getSelection().length; + const currentSelectionLength = this._getSelectionLength(); if (zone && currentSelectionLength === this._initialSelectionLength) { zone.clickCallback(e); @@ -205,8 +208,13 @@ export class MouseZoneManager extends Disposable implements IMouseZoneManager { } } + private _getSelectionLength(): number { + const selectionText = this._selectionService.selectionText; + return selectionText ? selectionText.length : 0; + } + private _findZoneEventAt(e: MouseEvent): IMouseZone { - const coords = this._mouseService.getCoords(e, this._terminal.screenElement, this._terminal.cols, this._terminal.rows); + const coords = this._mouseService.getCoords(e, this._screenElement, this._bufferService.cols, this._bufferService.rows); if (!coords) { return null; } diff --git a/src/Terminal.ts b/src/Terminal.ts index 6b0f4946..d9a6d77b 100644 --- a/src/Terminal.ts +++ b/src/Terminal.ts @@ -599,11 +599,6 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp this._soundService = new SoundService(this.optionsService); this._mouseService = new MouseService(this._renderService, this._charSizeService); - this._mouseZoneManager = new MouseZoneManager(this, this._mouseService); - this.register(this._mouseZoneManager); - this.register(this.onScroll(() => this._mouseZoneManager.clearAll())); - this.linkifier.attachToDom(this.element, this._mouseZoneManager); - this.viewport = new Viewport(this, this._viewportElement, this._viewportScrollArea, this._renderService.dimensions, this._charSizeService); this.viewport.onThemeChange(this._colorManager.colors); this.register(this.viewport); @@ -636,6 +631,11 @@ export class Terminal extends Disposable implements ITerminal, IDisposable, IInp })); this.register(addDisposableDomListener(this._viewportElement, 'scroll', () => this._selectionService.refresh())); + this._mouseZoneManager = new MouseZoneManager(this.element, this.screenElement, this._bufferService, this._mouseService, this._selectionService); + this.register(this._mouseZoneManager); + this.register(this.onScroll(() => this._mouseZoneManager.clearAll())); + this.linkifier.attachToDom(this.element, this._mouseZoneManager); + // apply mouse event classes set by escape codes before terminal was attached this.element.classList.toggle('enable-mouse-events', this.mouseEvents); if (this.mouseEvents) { diff --git a/src/browser/Linkifier.test.ts b/src/browser/Linkifier.test.ts index bf2cc984..f15eebcc 100644 --- a/src/browser/Linkifier.test.ts +++ b/src/browser/Linkifier.test.ts @@ -105,7 +105,7 @@ describe('Linkifier', () => { describe('after attachToDom', () => { beforeEach(() => { - linkifier.attachToDom(undefined as any, mouseZoneManager); + linkifier.attachToDom({} as any, mouseZoneManager); }); describe('link matcher', () => {