Skip to content

device.autoFocus is documented as user-settable but has no setter (ESM binding is read-only) #1486

@obiot

Description

@obiot

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions