From 06f0768395ddab5044026a20a53430aa3f79de1b Mon Sep 17 00:00:00 2001 From: vtorres-t <73935362+vtorres-t@users.noreply.github.com> Date: Mon, 20 Jul 2026 17:35:36 +0200 Subject: [PATCH] Feature Wake Lock Screen while reading (#1126) * Feature wakeLock * Fix style * Fix * Fix * Fix --------- Co-authored-by: Vicente --- CHANGELOG.md | 1 + src/features/metadata/Metadata.constants.ts | 3 + src/features/reader/Reader.types.ts | 1 + src/features/reader/screens/Reader.tsx | 4 ++ .../settings/ReaderSettings.constants.tsx | 2 + .../behaviour/ReaderBehaviourSettings.tsx | 5 ++ .../reader/wake-lock/useReaderWakeLock.ts | 56 +++++++++++++++++++ src/i18n/locales/en.po | 4 ++ 8 files changed, 76 insertions(+) create mode 100644 src/features/reader/wake-lock/useReaderWakeLock.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index a85e16ee..f22eba0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/). - (**Migration**) Add "in library" indicator - (**Settings/WebView**) Add setting to enable/disable WebView - (**Settings/Sync**) Add sync settings +- (**Reader**) Add Wake lock screen while reading - (**Extension**) Support installing external JARs - (**Reader**) Add auto background color setting diff --git a/src/features/metadata/Metadata.constants.ts b/src/features/metadata/Metadata.constants.ts index b25cf2da..0e3a0a17 100644 --- a/src/features/metadata/Metadata.constants.ts +++ b/src/features/metadata/Metadata.constants.ts @@ -332,6 +332,9 @@ export const APP_METADATA: Record< shouldInformAboutScanlatorChange: { convert: convertToBoolean, }, + shouldWakeLockScreen: { + convert: convertToBoolean, + }, hideHistory: { convert: convertToBoolean, }, diff --git a/src/features/reader/Reader.types.ts b/src/features/reader/Reader.types.ts index 31b81755..293f59a0 100644 --- a/src/features/reader/Reader.types.ts +++ b/src/features/reader/Reader.types.ts @@ -175,6 +175,7 @@ export interface IReaderSettingsGlobal { shouldShowTapZoneLayoutPreview: boolean; shouldInformAboutMissingChapter: boolean; shouldInformAboutScanlatorChange: boolean; + shouldWakeLockScreen: boolean; scrollAmount: ReaderScrollAmount; shouldUseInfiniteScroll: boolean; shouldShowTransitionPage: boolean; diff --git a/src/features/reader/screens/Reader.tsx b/src/features/reader/screens/Reader.tsx index 89101965..891b74e4 100644 --- a/src/features/reader/screens/Reader.tsx +++ b/src/features/reader/screens/Reader.tsx @@ -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, diff --git a/src/features/reader/settings/ReaderSettings.constants.tsx b/src/features/reader/settings/ReaderSettings.constants.tsx index e67ac9df..a7ee5dd3 100644 --- a/src/features/reader/settings/ReaderSettings.constants.tsx +++ b/src/features/reader/settings/ReaderSettings.constants.tsx @@ -153,6 +153,7 @@ const GLOBAL_READER_SETTING_OBJECT: Record updateSetting('shouldInformAboutScanlatorChange', checked)} /> + updateSetting('shouldWakeLockScreen', checked)} + /> updateSetting('autoScroll', ...args)} diff --git a/src/features/reader/wake-lock/useReaderWakeLock.ts b/src/features/reader/wake-lock/useReaderWakeLock.ts new file mode 100644 index 00000000..3e5882b9 --- /dev/null +++ b/src/features/reader/wake-lock/useReaderWakeLock.ts @@ -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(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]); +} diff --git a/src/i18n/locales/en.po b/src/i18n/locales/en.po index 99772518..c3655b04 100644 --- a/src/i18n/locales/en.po +++ b/src/i18n/locales/en.po @@ -2166,6 +2166,10 @@ msgstr "Keep previous and upcoming chapters visible while reading in continuous msgid "Keep remote" msgstr "Keep remote" +#: src/features/reader/settings/behaviour/ReaderBehaviourSettings.tsx +msgid "Keep screen on" +msgstr "Keep screen on" + #: src/features/reader/settings/ReaderSettings.constants.tsx msgid "Keybinds" msgstr "Keybinds"