Skip to content
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
3 changes: 3 additions & 0 deletions web/src/components/ConnectionPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { canWatchAdvertisements, connectBluetoothCube, isWebBluetoothAvailable,
import { useCubeState, useCubeStore } from '../cube/hooks'
import { MockConnection } from '../cube/mock'
import type { GyroStatus } from '../cube/store'
import { useScreenWakeLock } from '../cube/wake-lock'
import { useSettings } from '../settings/hooks'
import { useDebug } from '../ui/prefs'
import { Dialog } from '../ui/Dialog'
Expand Down Expand Up @@ -136,6 +137,8 @@ function gyroNotice(useGyro: boolean, status: GyroStatus): { text: string; warn:
export function CubeNotices() {
const store = useCubeStore()
const state = useCubeState()
// キューブを触っている間(つながっている間)は、画面が消えないようにする
useScreenWakeLock(state.status === 'connected')
return (
<>
{state.error && (
Expand Down
30 changes: 30 additions & 0 deletions web/src/cube/wake-lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect } from 'react'

/**
* on の間、画面が自動で消えないようにする(Screen Wake Lock)。
* タブを裏に回すとブラウザが解除するので、表に戻ったときに取り直す。対応していないブラウザでは何もしない。
*/
export function useScreenWakeLock(on: boolean) {
useEffect(() => {
if (!on || !('wakeLock' in navigator)) return
let lock: WakeLockSentinel | null = null
let stopped = false
const acquire = async () => {
if (document.visibilityState !== 'visible' || (lock && !lock.released)) return
try {
const l = await navigator.wakeLock.request('screen')
if (stopped) void l.release()
else lock = l
} catch {
// 省電力モードなどで断られても、画面が消えるだけなので気にしない
}
}
void acquire()
document.addEventListener('visibilitychange', acquire)
return () => {
stopped = true
document.removeEventListener('visibilitychange', acquire)
void lock?.release()
}
}, [on])
}
Loading