From 0cb38cfdcbe67eb99625b3f78205ccd2ea4b49d5 Mon Sep 17 00:00:00 2001 From: Martin Sander Date: Thu, 14 Jan 2021 23:08:34 +0100 Subject: [PATCH 1/2] fire onSelectionChange on empty selection Fire the onSelectionChange event when a selection is cleared by single-clicking somewhere inside the terminal area. fixes #3193. This also slightly changes the behaviour when re-selecting the same area in the terminal. Before, this fired onSelectionChange, but it does not anymore, even when changing the direction of the selection. --- src/browser/services/SelectionService.ts | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/browser/services/SelectionService.ts b/src/browser/services/SelectionService.ts index a3d8d12c..9e7d6531 100644 --- a/src/browser/services/SelectionService.ts +++ b/src/browser/services/SelectionService.ts @@ -105,6 +105,9 @@ export class SelectionService extends Disposable implements ISelectionService { private _workCell: CellData = new CellData(); private _mouseDownTimeStamp: number = 0; + private _oldHasSelection: boolean = false; + private _oldSelectionStart: [number, number] | undefined = undefined; + private _oldSelectionEnd: [number, number] | undefined = undefined; private _onLinuxMouseSelection = this.register(new EventEmitter()); public get onLinuxMouseSelection(): IEvent { return this._onLinuxMouseSelection.event; } @@ -681,7 +684,26 @@ export class SelectionService extends Disposable implements ISelectionService { this._coreService.triggerDataEvent(sequence, true); } } - } else if (this.hasSelection) { + } else { + this._fireIfSelectionChanged(); + } + } + + private _fireIfSelectionChanged(): void { + const hasSelection = this.hasSelection; + if (!hasSelection && !this._oldHasSelection) { + return; + } + + const start = this._model.finalSelectionStart; + const end = this._model.finalSelectionEnd; + + if (start?.[0] !== this._oldSelectionStart?.[0] || start?.[1] !== this._oldSelectionStart?.[1] || + end?.[0] !== this._oldSelectionEnd?.[0] || end?.[1] !== this._oldSelectionEnd?.[1]) { + + this._oldSelectionStart = start; + this._oldSelectionEnd = end; + this._oldHasSelection = hasSelection; this._onSelectionChange.fire(); } } From 66c80d8b788dae552df63f3df7a32e198767ae8e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 15 Jan 2021 09:39:34 -0800 Subject: [PATCH 2/2] Simplify checks Co-authored-by: Megan Rogge --- src/browser/services/SelectionService.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/browser/services/SelectionService.ts b/src/browser/services/SelectionService.ts index 9e7d6531..f4c8f58c 100644 --- a/src/browser/services/SelectionService.ts +++ b/src/browser/services/SelectionService.ts @@ -690,16 +690,26 @@ export class SelectionService extends Disposable implements ISelectionService { } private _fireIfSelectionChanged(): void { + // Fire if there is no selection const hasSelection = this.hasSelection; - if (!hasSelection && !this._oldHasSelection) { + if (!hasSelection) { + if (this._oldHasSelection) { + this._onSelectionChange.fire(); + } return; } const start = this._model.finalSelectionStart; const end = this._model.finalSelectionEnd; - if (start?.[0] !== this._oldSelectionStart?.[0] || start?.[1] !== this._oldSelectionStart?.[1] || - end?.[0] !== this._oldSelectionEnd?.[0] || end?.[1] !== this._oldSelectionEnd?.[1]) { + // Sanity check, these should not be undefined as there is a selection + if (!start || !end) { + return; + } + + if (!this._oldSelectionStart || !this._oldSelectionEnd || ( + start[0] !== this._oldSelectionStart[0] || start[1] !== this._oldSelectionStart[1] || + end[0] !== this._oldSelectionEnd[0] || end[1] !== this._oldSelectionEnd[1])) { this._oldSelectionStart = start; this._oldSelectionEnd = end;