Skip to content

Commit

Permalink
Cache mousemove and hook keyboard to allow scanning at mouse position…
Browse files Browse the repository at this point in the history
… while mouse is no longer moving (#917)
  • Loading branch information
Kuuuube committed May 14, 2024
1 parent a0f92f6 commit d60b010
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion ext/js/language/text-scanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -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} */
Expand Down Expand Up @@ -546,13 +549,36 @@ export class TextScanner extends EventDispatcher {
*/
_onMouseMove(e) {
this._scanTimerClear();
this._lastMouseMove = e;

const inputInfo = this._getMatchingInputGroupFromEvent('mouse', 'mouseMove', e);
if (inputInfo === null) { return; }

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}
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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[]}
Expand Down

0 comments on commit d60b010

Please sign in to comment.