From d60b010229cf9031029480b698e87f8b91fbf16f Mon Sep 17 00:00:00 2001 From: Kuuuube <61125188+Kuuuube@users.noreply.github.com> Date: Tue, 14 May 2024 10:42:05 -0400 Subject: Cache mousemove and hook keyboard to allow scanning at mouse position while mouse is no longer moving (#917) --- ext/js/language/text-scanner.js | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'ext') diff --git a/ext/js/language/text-scanner.js b/ext/js/language/text-scanner.js index bdbc117e..13db91d5 100644 --- a/ext/js/language/text-scanner.js +++ b/ext/js/language/text-scanner.js @@ -88,6 +88,9 @@ export class TextScanner extends EventDispatcher { /** @type {?import('text-scanner').SelectionRestoreInfo} */ this._selectionRestoreInfo = null; + /** @type {MouseEvent | null} */ + this._lastMouseMove = null; + /** @type {boolean} */ this._deepContentScan = false; /** @type {boolean} */ @@ -546,6 +549,7 @@ export class TextScanner extends EventDispatcher { */ _onMouseMove(e) { this._scanTimerClear(); + this._lastMouseMove = e; const inputInfo = this._getMatchingInputGroupFromEvent('mouse', 'mouseMove', e); if (inputInfo === null) { return; } @@ -553,6 +557,28 @@ export class TextScanner extends EventDispatcher { void this._searchAtFromMouseMove(e.clientX, e.clientY, inputInfo); } + /** + * @param {KeyboardEvent} e + */ + _onKeyDown(e) { + if (this._lastMouseMove !== null && (e.ctrlKey || e.shiftKey || e.altKey || e.metaKey)) { + const syntheticMouseEvent = new MouseEvent(this._lastMouseMove.type, { + screenX: this._lastMouseMove.screenX, + screenY: this._lastMouseMove.screenY, + clientX: this._lastMouseMove.clientX, + clientY: this._lastMouseMove.clientY, + ctrlKey: e.ctrlKey, + shiftKey: e.shiftKey, + altKey: e.altKey, + metaKey: e.metaKey, + button: this._lastMouseMove.button, + buttons: this._lastMouseMove.buttons, + relatedTarget: this._lastMouseMove.relatedTarget + }); + this._onMouseMove(syntheticMouseEvent); + } + } + /** * @param {MouseEvent} e * @returns {boolean|void} @@ -1050,7 +1076,7 @@ export class TextScanner extends EventDispatcher { } else if (this._arePointerEventsSupported()) { eventListenerInfos = this._getPointerEventListeners(capture); } else { - eventListenerInfos = this._getMouseEventListeners(capture); + eventListenerInfos = [...this._getMouseEventListeners(capture), ...this._getKeyboardEventListeners(capture)]; if (this._touchInputEnabled) { eventListenerInfos.push(...this._getTouchEventListeners(capture)); } @@ -1099,6 +1125,16 @@ export class TextScanner extends EventDispatcher { ]; } + /** + * @param {boolean} capture + * @returns {import('event-listener-collection').AddEventListenerArgs[]} + */ + _getKeyboardEventListeners(capture) { + return [ + [this._node, 'keydown', this._onKeyDown.bind(this), capture] + ]; + } + /** * @param {boolean} capture * @returns {import('event-listener-collection').AddEventListenerArgs[]} -- cgit v1.2.3