2019-09-15 14:16:32 +02:00
|
|
|
import Backbone from 'backbone';
|
|
|
|
import { Launcher } from 'comp/launcher';
|
|
|
|
import { Features } from 'util/features';
|
2019-01-14 08:56:33 +01:00
|
|
|
|
2019-01-21 20:52:40 +01:00
|
|
|
const FocusDetector = {
|
|
|
|
init() {
|
|
|
|
this.isFocused = true;
|
2019-09-15 08:11:11 +02:00
|
|
|
this.detectsFocusWithEvents = !Features.isDesktop && !Features.isMobile;
|
2019-02-09 12:11:32 +01:00
|
|
|
if (this.detectsFocusWithEvents) {
|
|
|
|
window.addEventListener('focus', () => {
|
|
|
|
if (!FocusDetector.isFocused) {
|
|
|
|
FocusDetector.isFocused = true;
|
|
|
|
Backbone.trigger('main-window-focus');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
window.addEventListener('blur', () => {
|
|
|
|
if (FocusDetector.isFocused) {
|
|
|
|
FocusDetector.isFocused = false;
|
|
|
|
Backbone.trigger('main-window-blur');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-01-21 20:52:40 +01:00
|
|
|
},
|
2019-01-14 08:56:33 +01:00
|
|
|
|
2019-01-21 20:52:40 +01:00
|
|
|
hasFocus() {
|
|
|
|
if (this.detectsFocusWithEvents) {
|
|
|
|
return this.isFocused;
|
|
|
|
} else if (Launcher) {
|
|
|
|
return Launcher.isAppFocused();
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-14 08:56:33 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-09-15 14:16:32 +02:00
|
|
|
export { FocusDetector };
|