Selection clear improvements (#1690)

* Update selection clear process

* Clean up old clearSelection API

* Update clear order

* Add clear event handling
This commit is contained in:
toasted-nutbread 2021-05-19 20:09:37 -04:00 committed by GitHub
parent eddd028864
commit 6184bcb812
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 15 deletions

View File

@ -121,7 +121,7 @@ class Frontend {
yomichan.on('closePopups', this._onClosePopups.bind(this)); yomichan.on('closePopups', this._onClosePopups.bind(this));
chrome.runtime.onMessage.addListener(this._onRuntimeMessage.bind(this)); chrome.runtime.onMessage.addListener(this._onRuntimeMessage.bind(this));
this._textScanner.on('clearSelection', this._onClearSelection.bind(this)); this._textScanner.on('clear', this._onTextScannerClear.bind(this));
this._textScanner.on('searched', this._onSearched.bind(this)); this._textScanner.on('searched', this._onSearched.bind(this));
yomichan.crossFrame.registerHandlers([ yomichan.crossFrame.registerHandlers([
@ -253,13 +253,8 @@ class Frontend {
this._updateContentScale(); this._updateContentScale();
} }
_onClearSelection({passive}) { _onTextScannerClear() {
this._stopClearSelectionDelayed(); this._clearSelection(false);
if (this._popup !== null) {
this._popup.hide(!passive);
this._popup.clearAutoPlayTimer();
this._isPointerOverPopup = false;
}
} }
_onSearched({type, dictionaryEntries, sentence, inputInfo: {eventType, passive, detail}, textSource, optionsContext, detail: {documentTitle}, error}) { _onSearched({type, dictionaryEntries, sentence, inputInfo: {eventType, passive, detail}, textSource, optionsContext, detail: {documentTitle}, error}) {
@ -299,7 +294,12 @@ class Frontend {
_clearSelection(passive) { _clearSelection(passive) {
this._stopClearSelectionDelayed(); this._stopClearSelectionDelayed();
this._textScanner.clearSelection(passive); if (this._popup !== null) {
this._popup.clearAutoPlayTimer();
this._popup.hide(!passive);
this._isPointerOverPopup = false;
}
this._textScanner.clearSelection();
} }
_clearSelectionDelayed(delay, restart, passive) { _clearSelectionDelayed(delay, restart, passive) {
@ -566,6 +566,7 @@ class Frontend {
_updateTextScannerEnabled() { _updateTextScannerEnabled() {
const enabled = (this._options !== null && this._options.general.enable && !this._disabledOverride); const enabled = (this._options !== null && this._options.general.enable && !this._disabledOverride);
this._textScanner.setEnabled(enabled); this._textScanner.setEnabled(enabled);
this._clearSelection(true);
} }
_updateContentScale() { _updateContentScale() {

View File

@ -1771,6 +1771,7 @@ class Display extends EventDispatcher {
if (!options.scanning.enablePopupSearch) { if (!options.scanning.enablePopupSearch) {
if (this._contentTextScanner !== null) { if (this._contentTextScanner !== null) {
this._contentTextScanner.setEnabled(false); this._contentTextScanner.setEnabled(false);
this._contentTextScanner.clearSelection();
} }
return; return;
} }
@ -1788,6 +1789,7 @@ class Display extends EventDispatcher {
this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *'; this._contentTextScanner.includeSelector = '.click-scannable,.click-scannable *';
this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *'; this._contentTextScanner.excludeSelector = '.scan-disable,.scan-disable *';
this._contentTextScanner.prepare(); this._contentTextScanner.prepare();
this._contentTextScanner.on('clear', this._onContentTextScannerClear.bind(this));
this._contentTextScanner.on('searched', this._onContentTextScannerSearched.bind(this)); this._contentTextScanner.on('searched', this._onContentTextScannerSearched.bind(this));
} }
@ -1821,6 +1823,10 @@ class Display extends EventDispatcher {
this._contentTextScanner.setEnabled(true); this._contentTextScanner.setEnabled(true);
} }
_onContentTextScannerClear() {
this._contentTextScanner.clearSelection();
}
_onContentTextScannerSearched({type, dictionaryEntries, sentence, textSource, optionsContext, error}) { _onContentTextScannerSearched({type, dictionaryEntries, sentence, textSource, optionsContext, error}) {
if (error !== null && !yomichan.isExtensionUnloaded) { if (error !== null && !yomichan.isExtensionUnloaded) {
log.error(error); log.error(error);
@ -1851,7 +1857,7 @@ class Display extends EventDispatcher {
contentOrigin: this.getContentOrigin() contentOrigin: this.getContentOrigin()
} }
}; };
this._contentTextScanner.clearSelection(true); this._contentTextScanner.clearSelection();
this.setContent(details); this.setContent(details);
} }

View File

@ -47,6 +47,7 @@ class QueryParser extends EventDispatcher {
prepare() { prepare() {
this._textScanner.prepare(); this._textScanner.prepare();
this._textScanner.on('clear', this._onTextScannerClear.bind(this));
this._textScanner.on('searched', this._onSearched.bind(this)); this._textScanner.on('searched', this._onSearched.bind(this));
this._queryParserModeSelect.addEventListener('change', this._onParserChange.bind(this), false); this._queryParserModeSelect.addEventListener('change', this._onParserChange.bind(this), false);
} }
@ -86,6 +87,10 @@ class QueryParser extends EventDispatcher {
// Private // Private
_onTextScannerClear() {
this._textScanner.clearSelection();
}
_onSearched(e) { _onSearched(e) {
const {error} = e; const {error} = e;
if (error !== null) { if (error !== null) {

View File

@ -139,8 +139,6 @@ class TextScanner extends EventDispatcher {
if (value) { if (value) {
this._hookEvents(); this._hookEvents();
} else {
this.clearSelection(true);
} }
} }
@ -259,7 +257,7 @@ class TextScanner extends EventDispatcher {
return (this._textSourceCurrent !== null); return (this._textSourceCurrent !== null);
} }
clearSelection(passive) { clearSelection() {
if (!this._canClearSelection) { return; } if (!this._canClearSelection) { return; }
if (this._textSourceCurrent !== null) { if (this._textSourceCurrent !== null) {
if (this._textSourceCurrentSelected) { if (this._textSourceCurrentSelected) {
@ -273,7 +271,6 @@ class TextScanner extends EventDispatcher {
this._textSourceCurrentSelected = false; this._textSourceCurrentSelected = false;
this._inputInfoCurrent = null; this._inputInfoCurrent = null;
} }
this.trigger('clearSelection', {passive});
} }
getCurrentTextSource() { getCurrentTextSource() {
@ -431,7 +428,7 @@ class TextScanner extends EventDispatcher {
case 0: // Primary case 0: // Primary
if (this._searchOnClick) { this._resetPreventNextClickScan(); } if (this._searchOnClick) { this._resetPreventNextClickScan(); }
this._scanTimerClear(); this._scanTimerClear();
this.clearSelection(false); this._triggerClear('mousedown');
break; break;
case 1: // Middle case 1: // Middle
if (this._preventMiddleMouse) { if (this._preventMiddleMouse) {
@ -1105,4 +1102,8 @@ class TextScanner extends EventDispatcher {
} }
} }
} }
_triggerClear(reason) {
this.trigger('clear', {reason});
}
} }