Skip to content

Commit

Permalink
feat: add screen lock option (#367)
Browse files Browse the repository at this point in the history
* feat: add screen lock option
  • Loading branch information
Renji-XD authored Oct 6, 2024
1 parent 2d7ab63 commit bb7585c
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
43 changes: 42 additions & 1 deletion apps/web/src/lib/components/book-reader/book-reader.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import { iffBrowser } from '$lib/functions/rxjs/iff-browser';
import { reduceToEmptyString } from '$lib/functions/rxjs/reduce-to-empty-string';
import { writableSubject } from '$lib/functions/svelte/store';
import { logger } from '$lib/data/logger';
import { imageLoadingState } from './image-loading-state';
import { reactiveElements } from './reactive-elements';
import type { AutoScroller, BookmarkManager, PageManager } from './types';
import BookReaderPaginated from './book-reader-paginated/book-reader-paginated.svelte';
import { enableReaderWakeLock$ } from '$lib/data/store';
import { onDestroy } from 'svelte';
export let htmlContent: string;
Expand Down Expand Up @@ -102,6 +104,10 @@
let showBlurMessage = false;
let wakeLock: WakeLockSentinel | undefined;
let visibilityState: DocumentVisibilityState;
const mutationObserver: MutationObserver = new MutationObserver(handleMutation);
const width$ = new Subject<number>();
Expand All @@ -115,7 +121,15 @@
? firstDimensionMargin * 2
: 0;
onDestroy(() => mutationObserver.disconnect());
$: if ($enableReaderWakeLock$ && visibilityState === 'visible') {
setTimeout(requestWakeLock, 500);
}
onDestroy(() => {
mutationObserver.disconnect();
releaseWakeLock();
});
const computedStyle$ = combineLatest([
containerEl$.pipe(filter((el): el is HTMLElement => !!el)),
Expand Down Expand Up @@ -193,6 +207,32 @@
showBlurMessage = mutation.target.style.filter.includes('blur');
}
async function requestWakeLock() {
if (wakeLock && !wakeLock.released) {
return;
}
wakeLock = await navigator.wakeLock.request().catch(({ message }) => {
logger.error(`failed to request wakelock: ${message}`);
return undefined;
});
if (wakeLock) {
wakeLock.addEventListener('release', releaseWakeLock, false);
}
}
async function releaseWakeLock() {
if (wakeLock && !wakeLock.released) {
await wakeLock.release().catch(() => {
// no-op
});
}
wakeLock = undefined;
}
</script>

{#if showBlurMessage}
Expand Down Expand Up @@ -282,3 +322,4 @@
</div>
{$blurListener$ ?? ''}
{$reactiveElements$ ?? ''}
<svelte:document bind:visibilityState />
14 changes: 14 additions & 0 deletions apps/web/src/lib/components/settings/settings-content.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
export let writingMode: WritingMode;
export let enableReaderWakeLock: boolean;
export let showCharacterCounter: boolean;
export let secondDimensionMaxValue: number;
Expand Down Expand Up @@ -304,6 +306,7 @@
let autoReplicationTypeTooltip = '';
let trackerAutoPauseTooltip = '';
$: wakeLockSupported = browser && 'wakeLock' in navigator;
$: verticalMode = writingMode === 'vertical-rl';
$: fontCacheSupported = browser && 'caches' in window;
$: switch (furiganaStyle) {
Expand Down Expand Up @@ -562,6 +565,17 @@
<SettingsItemGroup title="Writing mode">
<ButtonToggleGroup options={optionsForWritingMode} bind:selectedOptionId={writingMode} />
</SettingsItemGroup>
{#if wakeLockSupported}
<SettingsItemGroup
title="Enable Screen Lock"
tooltip={'When enabled the reader site attempts to request a WakeLock that prevents device screens from dimming or locking'}
>
<ButtonToggleGroup
options={optionsForToggle}
bind:selectedOptionId={enableReaderWakeLock}
/>
</SettingsItemGroup>
{/if}
<SettingsItemGroup title="Show Character Counter">
<ButtonToggleGroup options={optionsForToggle} bind:selectedOptionId={showCharacterCounter} />
</SettingsItemGroup>
Expand Down
4 changes: 4 additions & 0 deletions apps/web/src/lib/data/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export const writingMode$ = writableStringLocalStorageSubject<WritingMode>()(
'writingMode',
'vertical-rl'
);
export const enableReaderWakeLock$ = writableBooleanLocalStorageSubject()(
'enableReaderWakeLock',
false
);
export const verticalMode$ = writingMode$.pipe(map((writingMode) => writingMode === 'vertical-rl'));
export const showCharacterCounter$ = writableBooleanLocalStorageSubject()(
'showCharacterCounter',
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/routes/settings/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
confirmClose$,
customReadingPointEnabled$,
disableWheelNavigation$,
enableReaderWakeLock$,
firstDimensionMargin$,
fontFamilyGroupOne$,
fontFamilyGroupTwo$,
Expand Down Expand Up @@ -117,6 +118,7 @@
bind:hideFurigana={$hideFurigana$}
bind:furiganaStyle={$furiganaStyle$}
bind:writingMode={$writingMode$}
bind:enableReaderWakeLock={$enableReaderWakeLock$}
bind:showCharacterCounter={$showCharacterCounter$}
bind:viewMode={$viewMode$}
bind:secondDimensionMaxValue={$secondDimensionMaxValue$}
Expand Down

0 comments on commit bb7585c

Please sign in to comment.