Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Turn pages with the arrow and page keys in the desktop reader

Barnabas6d4f1fd43d51
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index bb558e2f..e5a7386d 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -496,6 +496,14 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
},
);
+ /// Arrow and page keys on desktop: the same path as a volume key, so read
+ /// along knows the reader moved on purpose and doesn't pull the page back.
+ void _keyTurn(bool forward) {
+ if (_autoScroll) return;
+ _userMoved('keyboard');
+ _turnPage(forward);
+ }
+
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
@@ -7243,6 +7251,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
epubController: _epubController!,
initialCfi: _initialCfi,
cachedLocations: _cachedLocations,
+ onKeyTurn: _keyTurn,
onLocationsGenerated: (json) {
debugPrint('[EbookReader] locations generated item=${widget.itemId} chars=${json.length}');
final file = _cachedFile;
@@ -7618,7 +7627,23 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
],
);
- return _wrap(viewerBody, bg);
+ // Keys land here when the Flutter side of the reader has focus (after a
+ // tap on an overlay button, say); the web view handles its own.
+ return Focus(
+ autofocus: AppPlatform.lacksPhonePlugins,
+ onKeyEvent: (node, event) {
+ if (!node.hasPrimaryFocus || event is KeyUpEvent) return KeyEventResult.ignored;
+ final key = event.logicalKey;
+ final forward = key == LogicalKeyboardKey.arrowRight ||
+ (!_scroll && (key == LogicalKeyboardKey.pageDown || key == LogicalKeyboardKey.space));
+ final back = key == LogicalKeyboardKey.arrowLeft ||
+ (!_scroll && key == LogicalKeyboardKey.pageUp);
+ if (!forward && !back) return KeyEventResult.ignored;
+ _keyTurn(forward);
+ return KeyEventResult.handled;
+ },
+ child: _wrap(viewerBody, bg),
+ );
}
}
diff --git a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
index 2487a378..3d1b073b 100644
--- a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
+++ b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
@@ -256,6 +256,26 @@ function loadBook(data, cfi, initialXPath, manager, flow, spread, snap, allowScr
setupRenditionBlocking();
});
+ // Arrow and page keys turn pages, from inside the book's frame (epub.js
+ // forwards its keydown) and from the outer page. The app does the turn so
+ // page animations and position tracking stay on the one path. In scrolled
+ // mode the page keys and space scroll as they would anywhere.
+ var scrolledFlow = String(flow || '').indexOf('scrolled') === 0;
+ window.__keyTurn = function (e) {
+ if (!e || e.defaultPrevented || e.ctrlKey || e.metaKey || e.altKey) return;
+ var k = e.key;
+ var forward = k === 'ArrowRight' || (!scrolledFlow && (k === 'PageDown' || k === ' ' || k === 'Spacebar'));
+ var back = k === 'ArrowLeft' || (!scrolledFlow && k === 'PageUp');
+ if (!forward && !back) return;
+ e.preventDefault();
+ window.flutter_inappwebview.callHandler('keyTurn', forward ? 'next' : 'prev');
+ };
+ rendition.on('keydown', function (e) { window.__keyTurn(e); });
+ if (!window.__keyTurnBound) {
+ window.__keyTurnBound = true;
+ window.addEventListener('keydown', function (e) { window.__keyTurn(e); });
+ }
+
// Reset selection state tracking (use global variables)
selectionTimeout = null;
isSelecting = false;
diff --git a/packages/flutter_epub_viewer/lib/src/epub_viewer.dart b/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
index d776b809..a5936801 100644
--- a/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
+++ b/packages/flutter_epub_viewer/lib/src/epub_viewer.dart
@@ -39,6 +39,7 @@ class EpubViewer extends StatefulWidget {
this.onChaptersLoaded,
this.onEpubLoaded,
this.onLocationLoaded,
+ this.onKeyTurn,
this.onRelocated,
this.onTextSelected,
this.displaySettings,
@@ -80,6 +81,10 @@ class EpubViewer extends StatefulWidget {
/// Callback when the location are generated for epub, progress will be only available after this
final VoidCallback? onLocationLoaded;
+ /// A page turn asked for with the keyboard inside the viewer: arrow and
+ /// page keys, true for forward.
+ final void Function(bool forward)? onKeyTurn;
+
/// Location index saved from an earlier open of this same file (epub.js
/// JSON); handed to the page so it can skip generating it.
final String? cachedLocations;
@@ -467,6 +472,13 @@ class _EpubViewerState extends State<EpubViewer> {
},
);
+ webViewController?.addJavaScriptHandler(
+ handlerName: 'keyTurn',
+ callback: (arguments) {
+ widget.onKeyTurn?.call(arguments.isNotEmpty && arguments.first == 'next');
+ },
+ );
+
webViewController?.addJavaScriptHandler(
handlerName: 'loadError',
callback: (data) {