feat(reader): add epub reader tap navigation for mobile (#1909)

Co-authored-by: Alex <25013571+alexhb1@users.noreply.github.com>
This commit is contained in:
Joshua Schmitt
2026-09-18 21:56:15 +01:00
committed by GitHub
co-authored by Alex
parent 2b66ca6df8
commit c8a1a59bbf
14 changed files with 188 additions and 6 deletions
@@ -186,6 +186,7 @@ public class BookLoreUser {
private Integer maxInlineSize;
private String theme;
private String flow;
private Boolean tapToTurnPage;
}
@Data
@@ -91,6 +91,7 @@ public class DefaultUserSettingsProvider {
.maxInlineSize(720)
.theme("gray")
.flow("paginated")
.tapToTurnPage(true)
.build();
}
@@ -0,0 +1,29 @@
package org.booklore.service.user;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.settings.UserSettingKey;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
class DefaultUserSettingsProviderTest {
private DefaultUserSettingsProvider provider;
@BeforeEach
void setUp() {
provider = new DefaultUserSettingsProvider();
provider.init();
}
@Test
void ebookReaderDefaults_shouldEnableTapToTurnPage() {
Object value = provider.getDefaultValue(UserSettingKey.EBOOK_READER_SETTING);
BookLoreUser.UserSettings.EbookReaderSetting setting =
assertInstanceOf(BookLoreUser.UserSettings.EbookReaderSetting.class, value);
assertEquals(Boolean.TRUE, setting.getTapToTurnPage());
}
}
@@ -347,6 +347,7 @@ export interface EbookViewerSetting {
fontFamily: string | null;
isDark: boolean;
flow: 'paginated' | 'scrolled';
tapToTurnPage?: boolean;
}
export interface CbxViewerSetting {
@@ -64,6 +64,8 @@ describe('ReaderEventService', () => {
const next = vi.fn();
const getCFI = vi.fn();
const getContents = vi.fn();
let tapToTurnEnabled = true;
const isTapToTurnEnabled = vi.fn(() => tapToTurnEnabled);
const postMessageSpy = vi.spyOn(window, 'postMessage').mockImplementation(() => undefined);
let service: ReaderEventService;
@@ -130,6 +132,8 @@ describe('ReaderEventService', () => {
next.mockReset();
getCFI.mockReset();
getContents.mockReset();
tapToTurnEnabled = true;
isTapToTurnEnabled.mockClear();
postMessageSpy.mockClear();
doc = document.implementation.createHTMLDocument('reader-doc');
@@ -180,7 +184,7 @@ describe('ReaderEventService', () => {
view = createView();
emittedEvents = [];
service.events$.subscribe(event => emittedEvents.push(event));
service.initialize(view, {prev, next, getCFI, getContents});
service.initialize(view, {prev, next, getCFI, getContents, isTapToTurnEnabled});
});
afterEach(() => {
@@ -283,6 +287,62 @@ describe('ReaderEventService', () => {
expect(emittedEvents.at(-1)?.type).toBe('middle-single-tap');
});
it('turns pages via side-zone taps on touch devices when tap-to-turn is enabled', () => {
Object.defineProperty(navigator, 'maxTouchPoints', {value: 5, configurable: true});
tapToTurnEnabled = true;
privateService.longHoldTimeout = setTimeout(() => undefined, 1_000);
privateService.processIframeClick({
type: 'iframe-click',
clientX: 40,
clientY: 100,
iframeLeft: 0,
iframeWidth: 600,
eventClientX: 20,
});
expect(prev).toHaveBeenCalledTimes(1);
expect(next).not.toHaveBeenCalled();
privateService.isNavigating = false;
privateService.longHoldTimeout = setTimeout(() => undefined, 1_000);
privateService.processIframeClick({
type: 'iframe-click',
clientX: 560,
clientY: 100,
iframeLeft: 0,
iframeWidth: 600,
eventClientX: 540,
});
expect(next).toHaveBeenCalledTimes(1);
expect(emittedEvents.filter(event => event.type === 'middle-single-tap')).toHaveLength(0);
});
it('toggles bars instead of turning pages when tap-to-turn is disabled', () => {
tapToTurnEnabled = false;
privateService.longHoldTimeout = setTimeout(() => undefined, 1_000);
privateService.processIframeClick({
type: 'iframe-click',
clientX: 40,
clientY: 100,
iframeLeft: 0,
iframeWidth: 600,
eventClientX: 20,
});
privateService.processIframeClick({
type: 'iframe-click',
clientX: 560,
clientY: 100,
iframeLeft: 0,
iframeWidth: 600,
eventClientX: 540,
});
expect(prev).not.toHaveBeenCalled();
expect(next).not.toHaveBeenCalled();
expect(emittedEvents.filter(event => event.type === 'middle-single-tap')).toHaveLength(2);
});
it('processes touch gestures for swipe navigation, short taps, and selected text', () => {
privateService.longHoldTimeout = setTimeout(() => undefined, 1_000);
privateService.touchStartX = 150;
@@ -78,6 +78,7 @@ interface ViewCallbacks {
next: () => void;
getCFI: (index: number, range: Range) => string | null;
getContents: () => { index: number; doc: Document }[] | null;
isTapToTurnEnabled?: () => boolean;
}
@Injectable({
@@ -437,6 +438,8 @@ export class ReaderEventService {
}
if (touchDuration < this.LONG_HOLD_THRESHOLD_MS && Math.abs(deltaX) < 10 && deltaY < 10) {
const target = event.target as Element | null;
if (target?.closest('a[href]')) return;
const iframe = doc.defaultView?.frameElement as HTMLIFrameElement | null;
if (!iframe) return;
@@ -609,13 +612,13 @@ export class ReaderEventService {
const leftThreshold = width * this.LEFT_ZONE_PERCENT;
const rightThreshold = width * this.RIGHT_ZONE_PERCENT;
const isMobile = 'ontouchstart' in window || navigator.maxTouchPoints > 0;
const tapToTurnEnabled = this.viewCallbacks?.isTapToTurnEnabled?.() ?? true;
if (x < leftThreshold && !isMobile) {
if (x < leftThreshold && tapToTurnEnabled) {
this.isNavigating = true;
this.viewCallbacks?.prev();
setTimeout(() => this.isNavigating = false, 300);
} else if (x > rightThreshold && !isMobile) {
} else if (x > rightThreshold && tapToTurnEnabled) {
this.isNavigating = true;
this.viewCallbacks?.next();
setTimeout(() => this.isNavigating = false, 300);
@@ -51,6 +51,7 @@ describe('ReaderStyleService', () => {
fontFamily: 'custom:7',
isDark: true,
flow: 'paginated',
tapToTurnPage: true,
});
expect(epubCustomFontService.getCustomFontById).toHaveBeenCalledWith(7);
@@ -76,6 +77,7 @@ describe('ReaderStyleService', () => {
fontFamily: 'serif',
isDark: false,
flow: 'scrolled',
tapToTurnPage: true,
});
expect(epubCustomFontService.getCustomFontById).not.toHaveBeenCalled();
@@ -105,6 +107,7 @@ describe('ReaderStyleService', () => {
fontFamily: null,
isDark: true,
flow: 'paginated',
tapToTurnPage: true,
});
expect(renderer.setAttribute).toHaveBeenCalledWith('max-column-count', 3);
@@ -127,6 +130,7 @@ describe('ReaderStyleService', () => {
fontFamily: null,
isDark: true,
flow: 'scrolled',
tapToTurnPage: true,
});
expect(renderer.removeAttribute).toHaveBeenCalledWith('margin');
@@ -143,6 +147,7 @@ describe('ReaderStyleService', () => {
fontFamily: null,
isDark: true,
flow: 'scrolled',
tapToTurnPage: true,
})).not.toThrow();
});
});
@@ -5,6 +5,7 @@ import {ReaderAnnotationService, Annotation} from '../features/annotations/annot
import {ReaderEventService, ViewEvent, TextSelection} from './event.service';
import {PageInfo, ThemeInfo, PageDecorator} from '../shared/header-footer.util';
import {EpubStreamingService, EpubBookInfo} from './epub-streaming.service';
import {ReaderStateService} from '../state/reader-state.service';
export type {ViewEvent, TextSelection} from './event.service';
export type {PageInfo, ThemeInfo} from '../shared/header-footer.util';
@@ -108,6 +109,7 @@ export class ReaderViewManagerService {
private annotationService = inject(ReaderAnnotationService);
private eventService = inject(ReaderEventService);
private epubStreamingService = inject(EpubStreamingService);
private readerStateService = inject(ReaderStateService);
private view: FoliateViewElement | null = null;
public get events$(): Observable<ViewEvent> {
@@ -125,7 +127,8 @@ export class ReaderViewManagerService {
prev: () => this.prev(),
next: () => this.next(),
getCFI: (index: number, range: Range) => this.view?.getCFI(index, range) ?? null,
getContents: () => this.view?.renderer?.getContents() ?? null
getContents: () => this.view?.renderer?.getContents() ?? null,
isTapToTurnEnabled: () => this.readerStateService.state().tapToTurnPage
});
}
@@ -79,6 +79,7 @@ describe('ReaderStateService', () => {
isDark: false,
flow: 'scrolled',
theme: 'gray',
tapToTurnPage: false,
}
}
}));
@@ -89,6 +90,7 @@ describe('ReaderStateService', () => {
const grayTheme = themes.find(theme => theme.name === 'gray');
expect(bookService.getBookSetting).toHaveBeenCalledWith(11, 22);
expect(service.state().tapToTurnPage).toBe(false);
expect(service.state().fontSize).toBe(18);
expect(service.state().lineHeight).toBe(1.75);
expect(service.state().fontFamily).toBe('custom:5');
@@ -180,6 +182,30 @@ describe('ReaderStateService', () => {
expect(service.state().flow).toBe('scrolled');
});
it('defaults tap-to-turn-page to enabled when the setting is absent', async () => {
userService.getMyself.mockReturnValue(of({
userSettings: {
perBookSetting: {epub: 'Global'},
ebookReaderSetting: {fontSize: 16},
}
}));
bookService.getBookSetting.mockReturnValue(of({ebookSettings: {}}));
await firstValueFrom(service.initializeState(1, 2));
expect(service.state().tapToTurnPage).toBe(true);
});
it('toggles tap-to-turn-page through the mutator', () => {
expect(service.state().tapToTurnPage).toBe(true);
service.setTapToTurnPage(false);
expect(service.state().tapToTurnPage).toBe(false);
service.setTapToTurnPage(true);
expect(service.state().tapToTurnPage).toBe(true);
});
it('ignores unknown theme names', () => {
const initialTheme = service.state().theme;
@@ -19,6 +19,7 @@ export interface ReaderState {
fontFamily: string | null;
isDark: boolean;
flow: 'paginated' | 'scrolled';
tapToTurnPage: boolean;
}
interface LegacyViewerSetting {
@@ -59,6 +60,7 @@ export class ReaderStateService {
fontFamily: null,
isDark: true,
flow: 'paginated',
tapToTurnPage: true,
};
private readonly _state = signal<ReaderState>(this.defaultState);
@@ -131,6 +133,7 @@ export class ReaderStateService {
if (settings.maxInlineSize != null) newState.maxInlineSize = settings.maxInlineSize;
if (settings.maxBlockSize != null) newState.maxBlockSize = settings.maxBlockSize;
if (settings.isDark != null) newState.isDark = settings.isDark;
newState.tapToTurnPage = globalSettings.tapToTurnPage ?? true;
if (settings.flow) newState.flow = settings.flow;
if (settings.theme) {
const theme = this.themes.find(t => t.name === settings.theme);
@@ -237,6 +240,10 @@ export class ReaderStateService {
this.updateState({flow});
}
setTapToTurnPage(enabled: boolean): void {
this.updateState({tapToTurnPage: enabled});
}
private updateState(partial: Partial<ReaderState>): void {
this._state.update(current => ({...current, ...partial}));
}
@@ -287,5 +287,38 @@
</div>
</div>
</div>
<div class="settings-group">
<h3 class="group-title">{{ t('navigation') }}</h3>
<div class="setting-item">
<div class="setting-info">
<div class="setting-label-row">
<span class="setting-label">{{ t('tapToTurnPage') }}</span>
<div class="button-group">
<button
type="button"
class="option-button"
[class.selected]="!tapToTurnPage"
(click)="tapToTurnPage = false">
<i class="pi pi-times"></i>
<span class="option-text">{{ t('off') }}</span>
</button>
<button
type="button"
class="option-button"
[class.selected]="tapToTurnPage"
(click)="tapToTurnPage = true">
<i class="pi pi-check"></i>
<span class="option-text">{{ t('on') }}</span>
</button>
</div>
</div>
<p class="setting-description">
{{ t('tapToTurnPageDesc') }}
</p>
</div>
</div>
</div>
</div>
</ng-container>
@@ -191,6 +191,15 @@ export class EpubReaderPreferencesComponent {
this.readerPreferencesService.updatePreference(['ebookReaderSetting', 'flow'], value);
}
get tapToTurnPage(): boolean {
return this.userSettings().ebookReaderSetting.tapToTurnPage ?? true;
}
set tapToTurnPage(value: boolean) {
this.userSettings().ebookReaderSetting.tapToTurnPage = value;
this.readerPreferencesService.updatePreference(['ebookReaderSetting', 'tapToTurnPage'], value);
}
increaseFontSize() {
if (this.fontSize < 72) {
this.fontSize += 1;
@@ -186,6 +186,7 @@ export interface EbookReaderSetting {
fontFamily: string;
isDark: boolean;
flow: 'paginated' | 'scrolled';
tapToTurnPage?: boolean;
}
export interface EpubReaderSetting {
+4 -1
View File
@@ -296,7 +296,10 @@
"maxWidth": "Max Width",
"maxWidthDesc": "Limit the maximum width of the reading area for easier line tracking.",
"maxHeight": "Max Height",
"maxHeightDesc": "Limit the maximum height of the reading area to fit your screen."
"maxHeightDesc": "Limit the maximum height of the reading area to fit your screen.",
"navigation": "Navigation",
"tapToTurnPage": "Tap to Turn Page",
"tapToTurnPageDesc": "Tap the left or right margin to turn pages. Tapping the center toggles the toolbars."
},
"pdf": {
"sectionTitle": "PDF Reader: Default Settings",