2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2022-02-28 14:29:51 +00:00
|
|
|
* @copyright 2022 Photon Storm Ltd.
|
2019-05-10 15:15:04 +00:00
|
|
|
* @license {@link https://opensource.org/licenses/MIT|MIT License}
|
2018-02-12 16:01:20 +00:00
|
|
|
*/
|
|
|
|
|
2019-01-15 16:17:04 +00:00
|
|
|
var Events = require('./events');
|
2018-01-25 02:13:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Visibility Handler is responsible for listening out for document level visibility change events.
|
|
|
|
* This includes `visibilitychange` if the browser supports it, and blur and focus events. It then uses
|
|
|
|
* the provided Event Emitter and fires the related events.
|
2017-10-04 22:48:16 +00:00
|
|
|
*
|
2019-01-15 16:17:04 +00:00
|
|
|
* @function Phaser.Core.VisibilityHandler
|
|
|
|
* @fires Phaser.Core.Events#BLUR
|
|
|
|
* @fires Phaser.Core.Events#FOCUS
|
|
|
|
* @fires Phaser.Core.Events#HIDDEN
|
|
|
|
* @fires Phaser.Core.Events#VISIBLE
|
2017-10-04 22:48:16 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-05-18 16:37:45 +00:00
|
|
|
* @param {Phaser.Game} game - The Game instance this Visibility Handler is working on.
|
2017-10-04 22:48:16 +00:00
|
|
|
*/
|
2018-05-18 16:37:45 +00:00
|
|
|
var VisibilityHandler = function (game)
|
2017-05-09 00:24:46 +00:00
|
|
|
{
|
|
|
|
var hiddenVar;
|
2018-05-18 16:37:45 +00:00
|
|
|
var eventEmitter = game.events;
|
2017-05-09 00:24:46 +00:00
|
|
|
|
|
|
|
if (document.hidden !== undefined)
|
|
|
|
{
|
|
|
|
hiddenVar = 'visibilitychange';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var vendors = [ 'webkit', 'moz', 'ms' ];
|
|
|
|
|
|
|
|
vendors.forEach(function (prefix)
|
|
|
|
{
|
|
|
|
if (document[prefix + 'Hidden'] !== undefined)
|
|
|
|
{
|
|
|
|
document.hidden = function ()
|
|
|
|
{
|
|
|
|
return document[prefix + 'Hidden'];
|
|
|
|
};
|
|
|
|
|
|
|
|
hiddenVar = prefix + 'visibilitychange';
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var onChange = function (event)
|
|
|
|
{
|
|
|
|
if (document.hidden || event.type === 'pause')
|
|
|
|
{
|
2019-01-15 16:17:04 +00:00
|
|
|
eventEmitter.emit(Events.HIDDEN);
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-01-15 16:17:04 +00:00
|
|
|
eventEmitter.emit(Events.VISIBLE);
|
2017-05-09 00:24:46 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (hiddenVar)
|
|
|
|
{
|
|
|
|
document.addEventListener(hiddenVar, onChange, false);
|
|
|
|
}
|
2017-05-09 14:39:30 +00:00
|
|
|
|
|
|
|
window.onblur = function ()
|
|
|
|
{
|
2019-01-15 16:17:04 +00:00
|
|
|
eventEmitter.emit(Events.BLUR);
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.onfocus = function ()
|
|
|
|
{
|
2019-01-15 16:17:04 +00:00
|
|
|
eventEmitter.emit(Events.FOCUS);
|
2017-05-09 14:39:30 +00:00
|
|
|
};
|
2018-05-18 16:37:45 +00:00
|
|
|
|
|
|
|
// Automatically give the window focus unless config says otherwise
|
|
|
|
if (window.focus && game.config.autoFocus)
|
|
|
|
{
|
|
|
|
window.focus();
|
2018-06-11 11:32:25 +00:00
|
|
|
}
|
2017-05-09 00:24:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = VisibilityHandler;
|