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 // Private
_onSearched({type, definitions, sentence, inputInfo, textSource, optionsContext, detail, error}) { _onSearched(e) {
const {error} = e;
if (error !== null) { if (error !== null) {
yomichan.logError(error); yomichan.logError(error);
return; return;
} }
if (type === null) { return; } if (e.type === null) { return; }
this.trigger('searched', { this.trigger('searched', e);
type,
definitions,
sentence,
inputInfo,
textSource,
optionsContext,
detail
});
} }
_onParserChange(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; const scanningOptions = this._options.scanning;
if (error !== null) { if (error !== null) {
if (yomichan.isExtensionUnloaded) { if (yomichan.isExtensionUnloaded) {
if (textSource !== null && !empty) { if (textSource !== null && !passive) {
this._showExtensionUnloaded(textSource); this._showExtensionUnloaded(textSource);
} }
} else { } else {
@ -264,7 +264,11 @@ class Frontend {
} }
} if (type !== null) { } if (type !== null) {
this._stopClearSelectionDelayed(); 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); this._showContent(textSource, focus, definitions, type, sentence, documentTitle, optionsContext);
} else { } else {
if (scanningOptions.autoHideResults) { 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 query = textSource.text();
const historyState = this._history.state; const historyState = this._history.state;
const history = ( const history = (
cause === 'click' || eventType === 'click' ||
!isObject(historyState) || !isObject(historyState) ||
historyState.cause !== 'queryParser' historyState.cause !== 'queryParser'
); );

View File

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