Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2162 and #6477: Fullscreen-dependent functions not working in browsers #7538

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 60 additions & 35 deletions GDJS/Runtime/pixi-renderers/runtimegame-pixi-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,45 @@ namespace gdjs {
this._marginBottom =
0;
this._setupOrientation();
this._setupFullscreenListeners();
this._applyFullscreenPatch();
}

private _setupFullscreenListeners(): void {
document.addEventListener('fullscreenchange', this._onFullscreenChange.bind(this));
document.addEventListener('webkitfullscreenchange', this._onFullscreenChange.bind(this));
document.addEventListener('mozfullscreenchange', this._onFullscreenChange.bind(this));
}

private _applyFullscreenPatch(): void {
if (!(window as any)._gdjsFullScreenPatch) {
(window as any)._gdjsFullScreenPatch = true;
document.addEventListener('fullscreenchange', () => {
this.setFullScreen(document.fullscreenElement != null);
});
document.addEventListener('webkitfullscreenchange', () => {
this.setFullScreen(document.fullscreenElement != null);
});
document.addEventListener('mozfullscreenchange', () => {
this.setFullScreen(document.fullscreenElement != null);
});
}
}

private _onFullscreenChange(): void {
// Use type assertions to handle vendor-specific properties
const doc = document as any;
this._isFullscreen = !!(
doc.fullscreenElement ||
doc.webkitFullscreenElement ||
doc.mozFullScreenElement
);

// Force a resize of the canvas to update UI elements
this._resizeCanvas();

// Notify the game that the window size has changed
this._game.onWindowInnerSizeChanged();
}

/**
Expand Down Expand Up @@ -446,46 +485,31 @@ namespace gdjs {
}
} else {
// Use HTML5 Fullscreen API
//TODO: Do this on a user gesture, otherwise most browsers won't activate fullscreen
const doc = document as any;
const docElement = document.documentElement as any;

if (this._isFullscreen) {
// @ts-ignore
if (document.documentElement.requestFullscreen) {
// @ts-ignore
document.documentElement.requestFullscreen();
} else {
// @ts-ignore
if (document.documentElement.mozRequestFullScreen) {
// @ts-ignore
document.documentElement.mozRequestFullScreen();
} else {
// @ts-ignore
if (document.documentElement.webkitRequestFullScreen) {
// @ts-ignore
document.documentElement.webkitRequestFullScreen();
}
}
if (docElement.requestFullscreen) {
docElement.requestFullscreen();
} else if (docElement.webkitRequestFullscreen) {
docElement.webkitRequestFullscreen();
} else if (docElement.mozRequestFullScreen) {
docElement.mozRequestFullScreen();
}
} else {
// @ts-ignore
if (document.exitFullscreen) {
// @ts-ignore
document.exitFullscreen();
} else {
// @ts-ignore
if (document.mozCancelFullScreen) {
// @ts-ignore
document.mozCancelFullScreen();
} else {
// @ts-ignore
if (document.webkitCancelFullScreen) {
// @ts-ignore
document.webkitCancelFullScreen();
}
}
if (doc.exitFullscreen) {
doc.exitFullscreen();
} else if (doc.webkitExitFullscreen) {
doc.webkitExitFullscreen();
} else if (doc.mozCancelFullScreen) {
doc.mozCancelFullScreen();
}
}
}
// Force a resize of the canvas to update UI elements
this._resizeCanvas();
// Notify the game that the window size has changed
this._game.onWindowInnerSizeChanged();
}
}

Expand All @@ -503,8 +527,7 @@ namespace gdjs {
}
}

// Height check is used to detect user triggered full screen (for example F11 shortcut).
return this._isFullscreen || window.screen.height === window.innerHeight;
return this._isFullscreen;
}

/**
Expand Down Expand Up @@ -1064,3 +1087,5 @@ namespace gdjs {
export type RuntimeGameRenderer = RuntimeGamePixiRenderer;
export const RuntimeGameRenderer = RuntimeGamePixiRenderer;
}


Loading