Feature Wake Lock Screen while reading (#1126)

* Feature wakeLock

* Fix style

* Fix

* Fix

* Fix

---------

Co-authored-by: Vicente <vicente@per>
This commit is contained in:
vtorres-t
2026-07-20 17:35:36 +02:00
committed by GitHub
parent 147ecdcd7b
commit 06f0768395
8 changed files with 76 additions and 0 deletions

View File

@@ -175,6 +175,7 @@ export interface IReaderSettingsGlobal {
shouldShowTapZoneLayoutPreview: boolean;
shouldInformAboutMissingChapter: boolean;
shouldInformAboutScanlatorChange: boolean;
shouldWakeLockScreen: boolean;
scrollAmount: ReaderScrollAmount;
shouldUseInfiniteScroll: boolean;
shouldShowTransitionPage: boolean;

View File

@@ -28,6 +28,7 @@ import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import type { NavbarContextType } from '@/features/navigation-bar/NavigationBar.types.ts';
import { withPropsFrom } from '@/base/hoc/withPropsFrom.tsx';
import { useReaderResetStates } from '@/features/reader/hooks/useReaderResetStates.ts';
import { useReaderWakeLock } from '@/features/reader/wake-lock/useReaderWakeLock.ts';
import { useReaderSetSettingsState } from '@/features/reader/hooks/useReaderSetSettingsState.ts';
import { useReaderShowSettingPreviewOnChange } from '@/features/reader/hooks/useReaderShowSettingPreviewOnChange.ts';
import { useReaderSetChaptersState } from '@/features/reader/hooks/useReaderSetChaptersState.ts';
@@ -69,6 +70,7 @@ const BaseReader = ({
backgroundColor,
shouldShowReadingModePreview,
shouldShowTapZoneLayoutPreview,
shouldWakeLockScreen,
useAutoBackgroundColorContinuousMode,
} = useReaderSettingsStore(
'shouldSkipDupChapters',
@@ -76,6 +78,7 @@ const BaseReader = ({
'backgroundColor',
'shouldShowReadingModePreview',
'shouldShowTapZoneLayoutPreview',
'shouldWakeLockScreen',
'useAutoBackgroundColorContinuousMode',
);
const readingMode = useReaderSettingsStore('readingMode');
@@ -157,6 +160,7 @@ const BaseReader = ({
}, [mangaResponse.data?.manga]);
useReaderResetStates();
useReaderWakeLock(shouldWakeLockScreen);
useReaderSetSettingsState(
mangaResponse,
defaultSettingsResponse,

View File

@@ -153,6 +153,7 @@ const GLOBAL_READER_SETTING_OBJECT: Record<keyof IReaderSettingsGlobal, undefine
shouldShowTapZoneLayoutPreview: undefined,
shouldInformAboutMissingChapter: undefined,
shouldInformAboutScanlatorChange: undefined,
shouldWakeLockScreen: undefined,
scrollAmount: undefined,
shouldUseInfiniteScroll: undefined,
shouldShowTransitionPage: undefined,
@@ -242,6 +243,7 @@ export const DEFAULT_READER_SETTINGS: IReaderSettings = {
shouldShowTapZoneLayoutPreview: true,
shouldInformAboutMissingChapter: true,
shouldInformAboutScanlatorChange: true,
shouldWakeLockScreen: true,
scrollAmount: ReaderScrollAmount.LARGE,
shouldUseInfiniteScroll: true,
shouldShowTransitionPage: true,

View File

@@ -136,6 +136,11 @@ export const ReaderBehaviourSettings = ({
checked={settings.shouldInformAboutScanlatorChange}
onChange={(_, checked) => updateSetting('shouldInformAboutScanlatorChange', checked)}
/>
<CheckboxInput
label={t`Keep screen on`}
checked={settings.shouldWakeLockScreen}
onChange={(_, checked) => updateSetting('shouldWakeLockScreen', checked)}
/>
<ReaderSettingAutoScroll
autoScroll={settings.autoScroll}
setAutoScroll={(...args) => updateSetting('autoScroll', ...args)}

View File

@@ -0,0 +1,56 @@
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { useEffect, useRef } from 'react';
export function useReaderWakeLock(shouldLock: boolean) {
const wakeLockSentinelRef = useRef<WakeLockSentinel | null>(null);
useEffect(() => {
if (!('wakeLock' in navigator)) {
return;
}
const requestWakeLock = async () => {
try {
if (wakeLockSentinelRef.current) {
return;
}
wakeLockSentinelRef.current = await navigator.wakeLock.request('screen');
wakeLockSentinelRef.current.addEventListener('release', () => {
wakeLockSentinelRef.current = null;
});
} catch {
wakeLockSentinelRef.current = null;
}
};
const releaseWakeLock = async () => {
if (wakeLockSentinelRef.current) {
await wakeLockSentinelRef.current.release();
wakeLockSentinelRef.current = null;
}
};
const handleVisibilityChange = async () => {
if (document.visibilityState === 'visible' && shouldLock) {
await requestWakeLock();
}
};
if (shouldLock) {
requestWakeLock();
document.addEventListener('visibilitychange', handleVisibilityChange);
} else {
releaseWakeLock();
}
return () => {
document.removeEventListener('visibilitychange', handleVisibilityChange);
releaseWakeLock();
};
}, [shouldLock]);
}