Skip to content

Commit 6238f73

Browse files
committed
Application: new pauseOnBlur, resumeOnFocus and stopOnBlur properties to configure a game behavior on blur and focus events
- this will allow later to have different behavior based on game instance (see #1091) - `pauseOnBlur`, `resumeOnFocus` and `stopOnBlur` device properties are now deprecated and replaced by their Application counterpart
1 parent f2f7489 commit 6238f73

File tree

4 files changed

+73
-35
lines changed

4 files changed

+73
-35
lines changed

CHANGELOG.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22

3-
## [15.4.0] (melonJS 2) - _2023-05-xx_
3+
## [15.4.0] (melonJS 2) - _2023-06-xx_
4+
5+
### Added
6+
- Application: new `pauseOnBlur`, `resumeOnFocus` and `stopOnBlur` properties to configure a game behavior on blur and focus events
47

58
### Changed
69
- Core: visibility and focus/blur events are now managed internally through new global `BLUR` and `FOCUS` events
10+
- Device: `pauseOnBlur`, `resumeOnFocus` and `stopOnBlur` properties are now deprecated and replaced by their Application counterpart
711

812
### Fixed
913
- Renderable : fix a potential issue with a Tile Layer not being properly redrawn when adding or clearing individual tiles

src/application/application.js

+45
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,30 @@ import { CANVAS, WEBGL, AUTO } from "../const.js";
8585
*/
8686
this.settings = undefined;
8787

88+
/**
89+
* Specify whether to pause this app when losing focus
90+
* @type {boolean}
91+
* @default true
92+
* @example
93+
* // keep the default game instance running even when loosing focus
94+
* me.game.pauseOnBlur = false;
95+
*/
96+
this.pauseOnBlur = true;
97+
98+
/**
99+
* Specify whether to unpause this app when gaining back focus
100+
* @type {boolean}
101+
* @default true
102+
*/
103+
this.resumeOnFocus = true;
104+
105+
/**
106+
* Specify whether to stop this app when losing focus
107+
* @type {boolean}
108+
* @default false
109+
*/
110+
this.stopOnBlur = false;
111+
88112
// to know when we have to refresh the display
89113
this.isDirty = true;
90114

@@ -234,6 +258,27 @@ import { CANVAS, WEBGL, AUTO } from "../const.js";
234258
// render all game objects
235259
this.draw();
236260
}, this);
261+
262+
263+
// on blur event, pause the current
264+
event.on(event.BLUR, () => {
265+
if (this.stopOnBlur === true) {
266+
state.stop(true);
267+
}
268+
if (this.pauseOnBlur === true) {
269+
state.pause(true);
270+
}
271+
});
272+
273+
// on focus event, restart or resume the current
274+
event.on(event.FOCUS, () => {
275+
if (this.stopOnBlur === true) {
276+
state.restart(true);
277+
}
278+
if (this.resumeOnFocus === true) {
279+
state.resume(true);
280+
}
281+
});
237282
}
238283

239284
/**

src/state/state.js

-25
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { pauseTrack, resumeTrack } from "./../audio/audio.js";
22
import * as fctUtil from "./../utils/function.js";
33
import * as event from "./../system/event.js";
44
import { game } from "../index.js";
5-
import * as device from "./../system/device.js";
65
import Stage from "./../state/stage.js";
76
import DefaultLoadingScreen from "./../loader/loadingscreen.js";
87

@@ -134,30 +133,6 @@ event.on(event.BOOT, () => {
134133
event.on(event.VIDEO_INIT, () => {
135134
state.change(state.DEFAULT, true);
136135
});
137-
138-
// on blur event, pause the current
139-
event.on(event.BLUR, () => {
140-
if (device.stopOnBlur === true) {
141-
state.stop(true);
142-
}
143-
if (device.pauseOnBlur === true) {
144-
state.pause(true);
145-
}
146-
});
147-
148-
// on focus event, restart or resume the current
149-
event.on(event.FOCUS, () => {
150-
if (device.stopOnBlur === true) {
151-
state.restart(true);
152-
}
153-
if (device.resumeOnFocus === true) {
154-
state.resume(true);
155-
}
156-
// force focus if autofocus is on
157-
if (device.autoFocus === true) {
158-
device.focus();
159-
}
160-
});
161136
});
162137

163138
/**

src/system/device.js

+23-9
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,8 @@ export let alpha = 0;
350350
* Specify whether to pause the game when losing focus
351351
* @name pauseOnBlur
352352
* @memberof device
353+
* @deprecated since 15.4.0
354+
* @see Application.pauseOnBlur
353355
* @type {boolean}
354356
* @public
355357
* @default true
@@ -360,32 +362,36 @@ export let pauseOnBlur = true;
360362
* Specify whether to unpause the game when gaining focus
361363
* @name resumeOnFocus
362364
* @memberof device
365+
* @deprecated since 15.4.0
366+
* @see Application.resumeOnFocus
363367
* @type {boolean}
364368
* @public
365369
* @default true
366370
*/
367371
export let resumeOnFocus = true;
368372

369373
/**
370-
* Specify whether to automatically bring the window to the front
371-
* @name autoFocus
374+
* Specify whether to stop the game when losing focus or not.
375+
* The engine restarts on focus if this is enabled.
376+
* @name stopOnBlur
372377
* @memberof device
378+
* @deprecated since 15.4.0
379+
* @see Application.stopOnBlur
373380
* @type {boolean}
374381
* @public
375-
* @default true
382+
* @default false
376383
*/
377-
export let autoFocus = true;
384+
export let stopOnBlur = false;
378385

379386
/**
380-
* Specify whether to stop the game when losing focus or not.
381-
* The engine restarts on focus if this is enabled.
382-
* @name stopOnBlur
387+
* Specify whether to automatically bring the window to the front
388+
* @name autoFocus
383389
* @memberof device
384390
* @type {boolean}
385391
* @public
386-
* @default false
392+
* @default true
387393
*/
388-
export let stopOnBlur = false;
394+
export let autoFocus = true;
389395

390396
/**
391397
* specify a function to execute when the Device is fully loaded and ready
@@ -442,6 +448,10 @@ export function onReady(fn) {
442448
// set restart/resume action on gaining focus
443449
globalThis.addEventListener("focus", () => {
444450
event.emit(event.FOCUS);
451+
// force focus if autofocus is on
452+
if (autoFocus === true) {
453+
this.focus();
454+
}
445455
}, false);
446456
}
447457
if (typeof globalThis.document !== "undefined") {
@@ -450,6 +460,10 @@ export function onReady(fn) {
450460
globalThis.document.addEventListener("visibilitychange", () => {
451461
if (globalThis.document.visibilityState === "visible") {
452462
event.emit(event.FOCUS);
463+
// force focus if autofocus is on
464+
if (autoFocus === true) {
465+
this.focus();
466+
}
453467
} else {
454468
event.emit(event.BLUR);
455469
}

0 commit comments

Comments
 (0)