2019-10-30 21:48:24 +08:00
|
|
|
import { bind } from "@ember/runloop";
|
2019-10-02 08:53:51 +08:00
|
|
|
import { getOwner } from "discourse-common/lib/get-owner";
|
2020-02-06 00:14:42 +08:00
|
|
|
import Mixin from "@ember/object/mixin";
|
2019-05-20 19:48:03 +08:00
|
|
|
|
2020-02-06 00:14:42 +08:00
|
|
|
export default Mixin.create({
|
2019-05-20 19:48:03 +08:00
|
|
|
ready() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-10-30 21:48:24 +08:00
|
|
|
this._onChangeHandler = bind(this, this._onChange);
|
2019-05-20 19:48:03 +08:00
|
|
|
|
|
|
|
// Default to true
|
|
|
|
Discourse.set("hasFocus", true);
|
|
|
|
|
2019-10-02 08:53:51 +08:00
|
|
|
document.addEventListener("visibilitychange", this._onChangeHandler);
|
|
|
|
document.addEventListener("resume", this._onChangeHandler);
|
|
|
|
document.addEventListener("freeze", this._onChangeHandler);
|
2019-05-20 19:48:03 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
2019-10-02 08:53:51 +08:00
|
|
|
document.removeEventListener("visibilitychange", this._onChangeHandler);
|
|
|
|
document.removeEventListener("resume", this._onChangeHandler);
|
|
|
|
document.removeEventListener("freeze", this._onChangeHandler);
|
|
|
|
|
|
|
|
this._onChangeHandler = null;
|
|
|
|
},
|
2019-05-20 19:48:03 +08:00
|
|
|
|
2019-10-02 08:53:51 +08:00
|
|
|
_onChange() {
|
|
|
|
const container = getOwner(this);
|
2019-10-16 16:50:43 +08:00
|
|
|
const appEvents = container.lookup("service:app-events");
|
2019-10-02 08:53:51 +08:00
|
|
|
|
|
|
|
if (document.visibilityState === "hidden") {
|
|
|
|
if (Discourse.hasFocus) {
|
|
|
|
Discourse.set("hasFocus", false);
|
|
|
|
appEvents.trigger("discourse:focus-changed", false);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!Discourse.hasFocus) {
|
2019-10-03 00:13:39 +08:00
|
|
|
Discourse.set("hasFocus", true);
|
2019-10-02 08:53:51 +08:00
|
|
|
appEvents.trigger("discourse:focus-changed", true);
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 19:48:03 +08:00
|
|
|
}
|
|
|
|
});
|