Summary
device.autoFocus is a let-declared module-level flag (packages/melonjs/src/system/device.ts:290) documented as user-settable:
Specify whether to automatically bring the window to the front
@default true
But ESM read-only namespace bindings mean device.autoFocus = false via import * as device from "melonjs/system/device" (or me.device.autoFocus = false through the package re-export) throws TypeError: Cannot assign to read only property 'autoFocus'. There's no setAutoFocus() setter either. The only consumers (initVisibilityEvents focus-handler at device.ts:359/372, pointerevent.ts:176) read it; nothing writes it; users cannot turn it off.
So users who want focus-on-window-restore disabled — e.g. games rendered inside an iframe or alongside another input-capturing widget where stealing focus is unwanted — have no public way to do it.
Repro
import * as device from "melonjs/system/device";
device.autoFocus = false;
// TypeError: Cannot assign to read only property 'autoFocus' of object '[object Module]'
Or:
import * as me from "melonjs";
me.device.autoFocus = false; // same TypeError
Proposed fix
Add a setter and document the JSDoc accordingly. Two options:
Option A — function setter (consistent with enableSwipe, requestFullscreen etc. in the same module):
export function setAutoFocus(enable: boolean) {
autoFocus = enable;
}
Option B — wrap the flag in a small state object so external code can mutate the property:
export const focusOptions = { auto: true };
Then initVisibilityEvents reads focusOptions.auto. Object-property writes survive ESM namespace import.
Option A keeps the existing autoFocus read access working (if (device.autoFocus)) and only adds a new function — preferred for backwards compatibility. Option B is a clean rename but breaks the existing read sites.
Notes
Summary
device.autoFocusis alet-declared module-level flag (packages/melonjs/src/system/device.ts:290) documented as user-settable:But ESM read-only namespace bindings mean
device.autoFocus = falseviaimport * as device from "melonjs/system/device"(orme.device.autoFocus = falsethrough the package re-export) throwsTypeError: Cannot assign to read only property 'autoFocus'. There's nosetAutoFocus()setter either. The only consumers (initVisibilityEventsfocus-handler atdevice.ts:359/372,pointerevent.ts:176) read it; nothing writes it; users cannot turn it off.So users who want focus-on-window-restore disabled — e.g. games rendered inside an iframe or alongside another input-capturing widget where stealing focus is unwanted — have no public way to do it.
Repro
Or:
Proposed fix
Add a setter and document the JSDoc accordingly. Two options:
Option A — function setter (consistent with
enableSwipe,requestFullscreenetc. in the same module):Option B — wrap the flag in a small state object so external code can mutate the property:
Then
initVisibilityEventsreadsfocusOptions.auto. Object-property writes survive ESM namespace import.Option A keeps the existing
autoFocusread access working (if (device.autoFocus)) and only adds a new function — preferred for backwards compatibility. Option B is a clean rename but breaks the existing read sites.Notes
isMobile/ platform detection — UA sniffing misses iPadOS 13+, includes dead platforms #1467 / feat: modernize platform/device detection + Application fullscreen API (#1467) #1485 (device.js→device.tsconversion). The originaldevice.jsform had the same defect — theletdeclaration was always vestigial because the module never reassigned it.letform either. Tracking so it doesn't get lost.