Skip to content

Commit 56888fd

Browse files
committed
Game will now auto-focus and prevent focus loss for the keyboard when clicking away and back again
* Game has a new property `hasFocus` which is a read-only boolean that lets you know if the window the game is embedded in (including in an iframe) currently has focus or not. * Game.Config has a new property `autoFocus`, which is `true` by default, and will automatically call `window.focus()` when the game starts. * Clicking on the canvas will automatically call `window.focus`. This means in games that use keyboard controls if you tab or click away from the game, then click back on it again, the keys will carry on working (where-as before they would remain unfocused)
1 parent 4c17037 commit 56888fd

4 files changed

Lines changed: 42 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
### New Features
66

7+
* Game has a new property `hasFocus` which is a read-only boolean that lets you know if the window the game is embedded in (including in an iframe) currently has focus or not.
8+
* Game.Config has a new property `autoFocus`, which is `true` by default, and will automatically call `window.focus()` when the game starts.
9+
* Clicking on the canvas will automatically call `window.focus`. This means in games that use keyboard controls if you tab or click away from the game, then click back on it again, the keys will carry on working (where-as before they would remain unfocused)
10+
711
### Updates
812

913
* Container.setInteractive can now be called without any arguments as long as you have called Container.setSize first (thanks rex)
1014
* Bob.reset will now reset the position, frame, flip, visible and alpha values of the Bob.
15+
* VisibilityHandler now takes a game instance as its sole argument, instead of an event emitter.
1116

1217
### Bug Fixes
1318

src/boot/Config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ var ValueToColor = require('../display/color/ValueToColor');
6767
* @property {string} [title=''] - [description]
6868
* @property {string} [url='http://phaser.io'] - [description]
6969
* @property {string} [version=''] - [description]
70+
* @property {boolean} [autoFocus=true] - Automatically call window.focus() when the game boots.
7071
* @property {(boolean|object)} [input] - [description]
7172
* @property {boolean} [input.keyboard=true] - [description]
7273
* @property {*} [input.keyboard.target=window] - [description]
@@ -204,6 +205,11 @@ var Config = new Class({
204205
*/
205206
this.gameVersion = GetValue(config, 'version', '');
206207

208+
/**
209+
* @const {boolean} Phaser.Boot.Config#autoFocus - [description]
210+
*/
211+
this.autoFocus = GetValue(config, 'autoFocus', true);
212+
207213
// Input
208214

209215
/**

src/boot/Game.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,17 @@ var Game = new Class({
252252
*/
253253
this.removeCanvas = false;
254254

255+
/**
256+
* Does the window the game is running in currently have focus or not?
257+
* This is modified by the VisibilityHandler.
258+
*
259+
* @name Phaser.Game#hasFocus
260+
* @type {boolean}
261+
* @readOnly
262+
* @since 3.9.0
263+
*/
264+
this.hasFocus = false;
265+
255266
// Wait for the DOM Ready event, then call boot.
256267
DOMContentLoaded(this.boot.bind(this));
257268
},
@@ -317,7 +328,7 @@ var Game = new Class({
317328
this.loop.start(this.headlessStep.bind(this));
318329
}
319330

320-
VisibilityHandler(this.events);
331+
VisibilityHandler(this);
321332

322333
var eventEmitter = this.events;
323334

@@ -546,6 +557,8 @@ var Game = new Class({
546557
*/
547558
onBlur: function ()
548559
{
560+
this.hasFocus = false;
561+
549562
this.loop.blur();
550563
},
551564

@@ -559,6 +572,8 @@ var Game = new Class({
559572
*/
560573
onFocus: function ()
561574
{
575+
this.hasFocus = true;
576+
562577
this.loop.focus();
563578
},
564579

src/boot/VisibilityHandler.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,12 @@
4848
* @fires Phaser.Boot.VisibilityHandler#focus
4949
* @since 3.0.0
5050
*
51-
* @param {Phaser.Events.EventEmitter} eventEmitter - The EventEmitter that will emit the visibility events.
51+
* @param {Phaser.Game} game - The Game instance this Visibility Handler is working on.
5252
*/
53-
var VisibilityHandler = function (eventEmitter)
53+
var VisibilityHandler = function (game)
5454
{
5555
var hiddenVar;
56+
var eventEmitter = game.events;
5657

5758
if (document.hidden !== undefined)
5859
{
@@ -103,6 +104,18 @@ var VisibilityHandler = function (eventEmitter)
103104
{
104105
eventEmitter.emit('focus');
105106
};
107+
108+
// Automatically give the window focus unless config says otherwise
109+
if (window.focus && game.config.autoFocus)
110+
{
111+
window.focus();
112+
113+
game.canvas.addEventListener('mousedown', function () {
114+
115+
window.focus();
116+
117+
}, { passive: true });
118+
}
106119
};
107120

108121
module.exports = VisibilityHandler;

0 commit comments

Comments
 (0)