Improve audio auto play cancellation (#1873)

* Cancel automatic audio playback when hiding the popup

* Don't play audio if the popup is hidden
This commit is contained in:
toasted-nutbread 2021-08-02 19:25:10 -04:00 committed by GitHub
parent b0df8045f6
commit edb0aaf6b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 2 deletions

View File

@ -38,6 +38,7 @@ class Popup extends EventDispatcher {
this._injectPromise = null;
this._injectPromiseComplete = false;
this._visible = new DynamicProperty(false);
this._visibleValue = false;
this._options = null;
this._optionsContext = null;
this._contentScale = 1.0;
@ -441,7 +442,10 @@ class Popup extends EventDispatcher {
}
_onVisibleChange({value}) {
if (this._visibleValue === value) { return; }
this._visibleValue = value;
this._frame.style.setProperty('visibility', value ? 'visible' : 'hidden', 'important');
this._invokeSafe('visibilityChanged', {value});
}
_focusParent() {

View File

@ -77,7 +77,7 @@ class DisplayAudio {
}
setupEntriesComplete() {
if (!this._autoPlay) { return; }
if (!this._autoPlay || !this._display.popupVisible) { return; }
this.clearAutoPlayTimer();

View File

@ -111,6 +111,7 @@ class Display extends EventDispatcher {
this._optionToggleHotkeyHandler = new OptionToggleHotkeyHandler(this);
this._elementOverflowController = new ElementOverflowController();
this._displayAnki = new DisplayAnki(this, japaneseUtil);
this._popupVisible = false;
this._hotkeyHandler.registerActions([
['close', () => { this._onHotkeyClose(); }],
@ -132,7 +133,8 @@ class Display extends EventDispatcher {
['clearAutoPlayTimer', {async: false, handler: this._onMessageClearAutoPlayTimer.bind(this)}],
['setCustomCss', {async: false, handler: this._onMessageSetCustomCss.bind(this)}],
['setContentScale', {async: false, handler: this._onMessageSetContentScale.bind(this)}],
['configure', {async: true, handler: this._onMessageConfigure.bind(this)}]
['configure', {async: true, handler: this._onMessageConfigure.bind(this)}],
['visibilityChanged', {async: false, handler: this._onMessageVisibilityChanged.bind(this)}]
]);
this.registerWindowMessageHandlers([
['extensionUnloaded', {async: false, handler: this._onMessageExtensionUnloaded.bind(this)}]
@ -208,6 +210,10 @@ class Display extends EventDispatcher {
return this._fullQuery;
}
get popupVisible() {
return this._popupVisible;
}
async prepare() {
// State setup
const {documentElement} = document;
@ -520,6 +526,14 @@ class Display extends EventDispatcher {
await this.setOptionsContext(optionsContext);
}
_onMessageVisibilityChanged({value}) {
this._popupVisible = value;
if (!value) {
this._displayAudio.clearAutoPlayTimer();
this._displayAudio.stopAudio();
}
}
_onMessageExtensionUnloaded() {
if (yomichan.isExtensionUnloaded) { return; }
yomichan.triggerExtensionUnloaded();