Skip to content

Refactor game fullscreen code #2953

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

Merged
merged 3 commits into from
May 1, 2024
Merged
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
68 changes: 35 additions & 33 deletions src/pages/academy/game/Game.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Icon } from '@blueprintjs/core';
import { Button, Tooltip } from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import { useFullscreen } from '@mantine/hooks';
import React from 'react';
Expand Down Expand Up @@ -95,51 +95,53 @@ function Game() {

// Logic for the fullscreen button to dynamically adjust its size, position and padding
// based on the size of the game display.
const [iconSize, setIconSize] = React.useState(0);
const [iconLeft, setIconLeft] = React.useState('0px');
const [iconPadding, setIconPadding] = React.useState('0px');

React.useEffect(() => {
const handleResize = () => {
if (gameDisplayRef.current) {
const aspectRatio = 16 / 9;
const height = gameDisplayRef.current.offsetHeight;
const width = gameDisplayRef.current.offsetWidth;
const size = height / 40;
const padding = height / 50;
const leftOffset =
isFullscreen || height * aspectRatio > width ? 0 : (width - height * aspectRatio) / 2;
setIconSize(size);
setIconPadding(`${padding}px`);
setIconLeft(`${leftOffset}px`);
}
};
const handleResize = React.useCallback(() => {
if (gameDisplayRef.current) {
const aspectRatio = 16 / 9;
const height = gameDisplayRef.current.offsetHeight;
const width = gameDisplayRef.current.offsetWidth;
const padding = height / 50;
const leftOffset =
isFullscreen || height * aspectRatio > width ? 0 : (width - height * aspectRatio) / 2;
setIconPadding(`${padding}px`);
setIconLeft(`${leftOffset}px`);
}
}, [isFullscreen]);

// When exiting fullscreen, the browser might not have completed the transition
// at the time handleResize is called, so the height of gameDisplayRef.current
// is still the fullscreen height.
// To fix this, we delay handleResize by 100ms.
const delayedHandleResize = () => {
setTimeout(handleResize, 100);
};
// When exiting fullscreen, the browser might not have completed the transition
// at the time handleResize is called, so the height of gameDisplayRef.current
// is still the fullscreen height.
// To fix this, we delay handleResize by 100ms.
const delayedHandleResize = React.useCallback(() => {
setTimeout(handleResize, 100);
}, [handleResize]);

React.useEffect(() => {
window.addEventListener('resize', delayedHandleResize);
delayedHandleResize();

return () => window.removeEventListener('resize', delayedHandleResize);
}, [isFullscreen]);
}, [delayedHandleResize]);

return (
<>
<div id="game-display" ref={setGameDisplayRefs}>
<Icon
id="fullscreen-button"
icon={isFullscreen ? IconNames.MINIMIZE : IconNames.FULLSCREEN}
color="white"
htmlTitle={isFullscreen ? 'Exit full screen' : 'Full screen'}
size={iconSize}
onClick={enhancedToggleFullscreen}
style={{ left: iconLeft, padding: iconPadding }}
<Tooltip
className="fullscreen-button"
content={isFullscreen ? 'Exit fullscreen' : 'Fullscreen'}
portalContainer={gameDisplayRef.current || undefined}
renderTarget={({ isOpen, ...targetProps }) => (
<Button
{...targetProps}
minimal
icon={isFullscreen ? IconNames.MINIMIZE : IconNames.MAXIMIZE}
onClick={enhancedToggleFullscreen}
style={{ left: iconLeft, padding: iconPadding }}
/>
)}
/>
</div>
{isTestStudent && (
Expand Down
5 changes: 2 additions & 3 deletions src/styles/_game.scss
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
align-items: center;
}

#fullscreen-button {
.fullscreen-button {
position: absolute;
cursor: pointer;
z-index: 100;
z-index: 10;
}