Text scanner improvements (#1261)

* Add support for allowing TextScanner.search to force focus

* Simplify query parser searched event forwarding

* Defer fallback creation

* Simplify event listeners

* Change type to pointerType

* Change cause to eventType

* Change empty to passive; make .search function passive

* Remove unused input index
This commit is contained in:
toasted-nutbread 2021-01-17 11:43:05 -05:00 committed by GitHub
parent 5d9d96996e
commit be590004fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 35 deletions

View File

@ -82,22 +82,15 @@ class QueryParser extends EventDispatcher {
// Private
_onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, detail, error}) {
_onSearched(e) {
const {error} = e;
if (error !== null) {
yomichan.logError(error);
return;
}
if (type === null) { return; }
if (e.type === null) { return; }
this.trigger('searched', {
type,
definitions,
sentence,
inputInfo,
textSource,
optionsContext,
detail
});
this.trigger('searched', e);
}
_onParserChange(e) {

View File

@ -251,12 +251,12 @@ class Frontend {
}
}
_onSearched({type, definitions, sentence, inputInfo: {cause, empty}, textSource, optionsContext, detail: {documentTitle}, error}) {
_onSearched({type, definitions, sentence, inputInfo: {eventType, passive, detail}, textSource, optionsContext, detail: {documentTitle}, error}) {
const scanningOptions = this._options.scanning;
if (error !== null) {
if (yomichan.isExtensionUnloaded) {
if (textSource !== null && !empty) {
if (textSource !== null && !passive) {
this._showExtensionUnloaded(textSource);
}
} else {
@ -264,7 +264,11 @@ class Frontend {
}
} if (type !== null) {
this._stopClearSelectionDelayed();
const focus = (cause === 'mouseMove');
let focus = (eventType === 'mouseMove');
if (isObject(detail)) {
const focus2 = detail.focus;
if (typeof focus2 === 'boolean') { focus = focus2; }
}
this._showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext);
} else {
if (scanningOptions.autoHideResults) {

View File

@ -587,11 +587,11 @@ class Display extends EventDispatcher {
}
}
_onQueryParserSearch({type, definitions, sentence, inputInfo: {cause}, textSource, optionsContext}) {
_onQueryParserSearch({type, definitions, sentence, inputInfo: {eventType}, textSource, optionsContext}) {
const query = textSource.text();
const historyState = this._history.state;
const history = (
cause === 'click' ||
eventType === 'click' ||
!isObject(historyState) ||
historyState.cause !== 'queryParser'
);

View File

@ -283,8 +283,8 @@ class TextScanner extends EventDispatcher {
return false;
}
async search(textSource) {
const inputInfo = this._createInputInfo(-1, false, null, 'script', 'script', [], []);
async search(textSource, inputDetail) {
const inputInfo = this._createInputInfo(null, 'script', 'script', true, [], [], inputDetail);
return await this._search(textSource, this._searchTerms, this._searchKanji, inputInfo);
}
@ -391,7 +391,7 @@ class TextScanner extends EventDispatcher {
if (this._searchOnClick) {
const modifiers = DocumentUtil.getActiveModifiersAndButtons(e);
const modifierKeys = DocumentUtil.getActiveModifiers(e);
const inputInfo = this._createInputInfo(-1, false, null, 'mouse', 'click', modifiers, modifierKeys);
const inputInfo = this._createInputInfo(null, 'mouse', 'click', false, modifiers, modifierKeys);
this._searchAt(e.clientX, e.clientY, inputInfo);
}
@ -682,8 +682,8 @@ class TextScanner extends EventDispatcher {
}
}
for (const [node, type, listener, options] of eventListenerInfos) {
this._eventListeners.addEventListener(node, type, listener, options);
for (const args of eventListenerInfos) {
this._eventListeners.addEventListener(...args);
}
}
@ -840,7 +840,7 @@ class TextScanner extends EventDispatcher {
async _searchAtFromMouseMove(x, y, inputInfo) {
if (this._pendingLookup) { return; }
if (inputInfo.empty) {
if (inputInfo.passive) {
if (!await this._scanTimerWait()) {
// Aborted
return;
@ -871,10 +871,10 @@ class TextScanner extends EventDispatcher {
}
}
async _searchAtFromPen(e, x, y, cause, prevent) {
async _searchAtFromPen(e, x, y, eventType, prevent) {
if (this._pendingLookup) { return; }
const inputInfo = this._getMatchingInputGroupFromEvent('pen', cause, e);
const inputInfo = this._getMatchingInputGroupFromEvent('pen', eventType, e);
if (inputInfo === null) { return; }
const {input: {options}} = inputInfo;
@ -900,32 +900,37 @@ class TextScanner extends EventDispatcher {
}
}
_getMatchingInputGroupFromEvent(type, cause, event) {
_getMatchingInputGroupFromEvent(pointerType, eventType, event) {
const modifiers = DocumentUtil.getActiveModifiersAndButtons(event);
const modifierKeys = DocumentUtil.getActiveModifiers(event);
return this._getMatchingInputGroup(type, cause, modifiers, modifierKeys);
return this._getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys);
}
_getMatchingInputGroup(type, cause, modifiers, modifierKeys) {
let fallback = null;
_getMatchingInputGroup(pointerType, eventType, modifiers, modifierKeys) {
let fallbackIndex = -1;
const modifiersSet = new Set(modifiers);
for (let i = 0, ii = this._inputs.length; i < ii; ++i) {
const input = this._inputs[i];
const {include, exclude, types} = input;
if (!types.has(type)) { continue; }
if (!types.has(pointerType)) { continue; }
if (this._setHasAll(modifiersSet, include) && (exclude.length === 0 || !this._setHasAll(modifiersSet, exclude))) {
if (include.length > 0) {
return this._createInputInfo(i, false, input, type, cause, modifiers, modifierKeys);
} else if (fallback === null) {
fallback = this._createInputInfo(i, true, input, type, cause, modifiers, modifierKeys);
return this._createInputInfo(input, pointerType, eventType, false, modifiers, modifierKeys);
} else if (fallbackIndex < 0) {
fallbackIndex = i;
}
}
}
return fallback;
}
_createInputInfo(index, empty, input, type, cause, modifiers, modifierKeys) {
return {index, empty, input, type, cause, modifiers, modifierKeys};
return (
fallbackIndex >= 0 ?
this._createInputInfo(this._inputs[fallbackIndex], pointerType, eventType, true, modifiers, modifierKeys) :
null
);
}
_createInputInfo(input, pointerType, eventType, passive, modifiers, modifierKeys, detail) {
return {input, pointerType, eventType, passive, modifiers, modifierKeys, detail};
}
_setHasAll(set, values) {