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