mirror of
https://github.com/Vikeo/LifeTrinket.git
synced 2025-11-14 15:07:59 +00:00
Better wakelock and fullscreen
This commit is contained in:
@@ -3,6 +3,7 @@ import styled, { css } from 'styled-components';
|
|||||||
import { Energy, Experience, PartnerTax, Poison } from '../../Icons/generated';
|
import { Energy, Experience, PartnerTax, Poison } from '../../Icons/generated';
|
||||||
import { Player, Rotation } from '../../Types/Player';
|
import { Player, Rotation } from '../../Types/Player';
|
||||||
import { WakeLock } from '../../Types/WakeLock';
|
import { WakeLock } from '../../Types/WakeLock';
|
||||||
|
import { useFullscreen } from '../../Hooks/useFullscreen';
|
||||||
|
|
||||||
type SettingsProps = {
|
type SettingsProps = {
|
||||||
player: Player;
|
player: Player;
|
||||||
@@ -131,6 +132,7 @@ const Settings = ({
|
|||||||
resetCurrentGame,
|
resetCurrentGame,
|
||||||
wakeLock,
|
wakeLock,
|
||||||
}: SettingsProps) => {
|
}: SettingsProps) => {
|
||||||
|
const { disableFullscreen, enableFullscreen, isFullscreen } = useFullscreen();
|
||||||
const isSide =
|
const isSide =
|
||||||
player.settings.rotation === Rotation.Side ||
|
player.settings.rotation === Rotation.Side ||
|
||||||
player.settings.rotation === Rotation.SideFlipped;
|
player.settings.rotation === Rotation.SideFlipped;
|
||||||
@@ -165,10 +167,10 @@ const Settings = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
const toggleFullscreen = () => {
|
const toggleFullscreen = () => {
|
||||||
if (!document.fullscreenElement) {
|
if (!isFullscreen) {
|
||||||
document.documentElement.requestFullscreen();
|
enableFullscreen();
|
||||||
} else if (document.exitFullscreen) {
|
} else {
|
||||||
document.exitFullscreen();
|
disableFullscreen();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -295,7 +297,7 @@ const Settings = ({
|
|||||||
Back to Start
|
Back to Start
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant={document.fullscreenElement ? 'contained' : 'outlined'}
|
||||||
style={{
|
style={{
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
userSelect: 'none',
|
userSelect: 'none',
|
||||||
@@ -307,7 +309,7 @@ const Settings = ({
|
|||||||
Fullscreen
|
Fullscreen
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
variant="contained"
|
variant={wakeLock.active ? 'contained' : 'outlined'}
|
||||||
style={{
|
style={{
|
||||||
cursor: 'pointer',
|
cursor: 'pointer',
|
||||||
userSelect: 'none',
|
userSelect: 'none',
|
||||||
@@ -316,7 +318,7 @@ const Settings = ({
|
|||||||
}}
|
}}
|
||||||
onClick={handleWakeLock}
|
onClick={handleWakeLock}
|
||||||
>
|
>
|
||||||
Wake Lock is {wakeLock.active ? 'on' : 'off'}
|
Keep Awake
|
||||||
</Button>
|
</Button>
|
||||||
</ButtonsSections>
|
</ButtonsSections>
|
||||||
</SettingsContainer>
|
</SettingsContainer>
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import { Player } from '../../Types/Player';
|
|||||||
import { WakeLock } from '../../Types/WakeLock';
|
import { WakeLock } from '../../Types/WakeLock';
|
||||||
|
|
||||||
const MainWrapper = styled.div`
|
const MainWrapper = styled.div`
|
||||||
width: 100vmax;
|
width: 100dvmax;
|
||||||
height: 100vmin;
|
height: 100dvmin;
|
||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import { InfoModal } from '../../Misc/InfoModal';
|
|||||||
import { SupportMe } from '../../Misc/SupportMe';
|
import { SupportMe } from '../../Misc/SupportMe';
|
||||||
import { H2, Paragraph } from '../../Misc/TextComponents';
|
import { H2, Paragraph } from '../../Misc/TextComponents';
|
||||||
import LayoutOptions from './LayoutOptions';
|
import LayoutOptions from './LayoutOptions';
|
||||||
|
import { useFullscreen } from '../../../Hooks/useFullscreen';
|
||||||
|
|
||||||
const MainWrapper = styled.div`
|
const MainWrapper = styled.div`
|
||||||
width: 100vw;
|
width: 100vw;
|
||||||
@@ -109,11 +110,14 @@ const Start = ({
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleWakeLock = () => {
|
const { enableFullscreen } = useFullscreen();
|
||||||
if (wakeLock.active) {
|
|
||||||
return;
|
const toggleWakeLock = ({ active }: { active: boolean }) => {
|
||||||
}
|
if (active) {
|
||||||
|
wakeLock.release();
|
||||||
|
} else {
|
||||||
wakeLock.request();
|
wakeLock.request();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const doStartGame = () => {
|
const doStartGame = () => {
|
||||||
@@ -124,12 +128,11 @@ const Start = ({
|
|||||||
analytics.trackEvent('game_started', { ...initialGameSettings });
|
analytics.trackEvent('game_started', { ...initialGameSettings });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
document.documentElement.requestFullscreen();
|
enableFullscreen();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
handleWakeLock();
|
|
||||||
setInitialGameSettings(initialGameSettings);
|
setInitialGameSettings(initialGameSettings);
|
||||||
setPlayers(createInitialPlayers(initialGameSettings));
|
setPlayers(createInitialPlayers(initialGameSettings));
|
||||||
};
|
};
|
||||||
@@ -192,7 +195,7 @@ const Start = ({
|
|||||||
<SupportMe />
|
<SupportMe />
|
||||||
|
|
||||||
<H2>Life Trinket</H2>
|
<H2>Life Trinket</H2>
|
||||||
{wakeLock.active ? 'hej' : 'noo'}
|
|
||||||
<FormControl focused={false} style={{ width: '80vw' }}>
|
<FormControl focused={false} style={{ width: '80vw' }}>
|
||||||
<FormLabel>Number of Players</FormLabel>
|
<FormLabel>Number of Players</FormLabel>
|
||||||
<Slider
|
<Slider
|
||||||
@@ -239,6 +242,12 @@ const Start = ({
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
/>
|
/>
|
||||||
|
<FormLabel>Keep Awake</FormLabel>
|
||||||
|
<Switch
|
||||||
|
checked={wakeLock.active}
|
||||||
|
defaultChecked={wakeLock.active}
|
||||||
|
onChange={() => toggleWakeLock({ active: wakeLock.active })}
|
||||||
|
/>
|
||||||
|
|
||||||
<FormLabel>Layout</FormLabel>
|
<FormLabel>Layout</FormLabel>
|
||||||
|
|
||||||
|
|||||||
41
src/Hooks/useFullscreen.ts
Normal file
41
src/Hooks/useFullscreen.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
|
type FullscreenHookReturnType = {
|
||||||
|
isFullscreen: boolean;
|
||||||
|
enableFullscreen: () => void;
|
||||||
|
disableFullscreen: () => void;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const useFullscreen = (): FullscreenHookReturnType => {
|
||||||
|
const [isFullscreen, setIsFullscreen] = useState(false);
|
||||||
|
|
||||||
|
const enableFullscreen = () => {
|
||||||
|
if (document?.documentElement.requestFullscreen) {
|
||||||
|
document.documentElement.requestFullscreen().then(() => {
|
||||||
|
setIsFullscreen(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const disableFullscreen = () => {
|
||||||
|
if (document.exitFullscreen) {
|
||||||
|
document.exitFullscreen().then(() => {
|
||||||
|
setIsFullscreen(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const fullscreenChangeHandler = () => {
|
||||||
|
setIsFullscreen(!!document.fullscreenElement);
|
||||||
|
};
|
||||||
|
|
||||||
|
document.addEventListener('fullscreenchange', fullscreenChangeHandler);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('fullscreenchange', fullscreenChangeHandler);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return { isFullscreen, enableFullscreen, disableFullscreen };
|
||||||
|
};
|
||||||
17
src/Hooks/useWakeLock.ts
Normal file
17
src/Hooks/useWakeLock.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { useWakeLock } from 'react-screen-wake-lock';
|
||||||
|
|
||||||
|
const { isSupported, release, released, request, type } = useWakeLock();
|
||||||
|
|
||||||
|
if (released === undefined) {
|
||||||
|
const isActive = false;
|
||||||
|
} else {
|
||||||
|
const isActive = !released;
|
||||||
|
}
|
||||||
|
|
||||||
|
const wakeLock = {
|
||||||
|
isSupported,
|
||||||
|
release,
|
||||||
|
active: isActive,
|
||||||
|
request,
|
||||||
|
type,
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user