discourse/app/assets/javascripts/discourse-common/mixins/focus-event.js.es6
Sam Saffron 499472b6a0 FIX: change focus when application resumes in android
Per new lifecycle https://developers.google.com/web/updates/2018/07/page-lifecycle-api

On Android and latest Chrome when an app transitions from "frozen" to
active the new "resume" event fires with no accompanying "visibilitychange"
event.

This means that often background tabs may be stuck thinking that discourse
has no focus when, indeed, it has.

This leads to cases where no posts are marked read anymore.
2019-10-01 12:00:07 +10:00

41 lines
911 B
JavaScript

function gotFocus() {
if (!Discourse.get("hasFocus")) {
Discourse.setProperties({ hasFocus: true, notify: false });
}
}
function lostFocus() {
if (Discourse.get("hasFocus")) {
Discourse.set("hasFocus", false);
}
}
let onchange;
export default Ember.Mixin.create({
ready() {
this._super(...arguments);
onchange = () => {
document.visibilityState === "hidden" ? lostFocus() : gotFocus();
};
// Default to true
Discourse.set("hasFocus", true);
document.addEventListener("visibilitychange", onchange);
document.addEventListener("resume", onchange);
document.addEventListener("freeze", onchange);
},
reset() {
this._super(...arguments);
document.removeEventListener("visibilitychange", onchange);
document.removeEventListener("resume", onchange);
document.removeEventListener("freeze", onchange);
onchange = undefined;
}
});