Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop the desktop reader undoing page turns and scrolls, count wheel scrolls as moving away from read along, and hide the scrollbar in scroll mode

Barnabas34a9cfd01391
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index e5a7386d..8d493b1a 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -501,9 +501,32 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
void _keyTurn(bool forward) {
if (_autoScroll) return;
_userMoved('keyboard');
+ _turnByUser(forward);
+ }
+
+ /// A turn asked for by tap or key. With no selection toolbar up, any
+ /// selection the page still holds is stale (a mouse leaves one behind
+ /// where a finger would not) and the viewer would refuse the turn for it.
+ void _turnByUser(bool forward) {
+ if (_selectionCfi == null) _epubController?.clearSelection();
_turnPage(forward);
}
+ /// The desktop WebView draws a full-height scrollbar down the page in
+ /// scroll mode; phones show a thin one that fades, so it is hidden only
+ /// where it would stay.
+ void _hideDesktopScrollbar() {
+ _epubController?.webViewController?.evaluateJavascript(source: r'''
+(function () {
+ if (document.getElementById('absorbNoBar')) return;
+ var s = document.createElement('style');
+ s.id = 'absorbNoBar';
+ s.textContent = '.epub-container::-webkit-scrollbar, #viewer::-webkit-scrollbar, body::-webkit-scrollbar { display: none; width: 0; height: 0; } ' +
+ '.epub-container, #viewer, body { scrollbar-width: none; }';
+ document.head.appendChild(s);
+})();''');
+ }
+
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
@@ -1160,10 +1183,10 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
void _commitReaderTap(String action) {
if (action == 'prev') {
_userMoved('tap');
- _turnPage(false);
+ _turnByUser(false);
} else if (action == 'next') {
_userMoved('tap');
- _turnPage(true);
+ _turnByUser(true);
} else if (action == 'menu') {
_toggleControls();
}
@@ -1438,6 +1461,17 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
/// (the same callHandler bridge the selection menu uses). Skips taps while
/// text is selected so highlighting still works.
void _setupTapHandler() {
+ // A mouse wheel moves the page in scroll mode without any touch, so it
+ // has to count as the reader moving, or read along pulls the page back.
+ _epubController?.webViewController?.addJavaScriptHandler(
+ handlerName: 'absorbReaderWheel',
+ callback: (args) {
+ if (!mounted || !_scroll) return;
+ _userMoved('scroll');
+ final cfi = _currentCfi;
+ if (cfi != null) _syncProgress(cfi, _progress);
+ },
+ );
_epubController?.webViewController?.addJavaScriptHandler(
handlerName: 'absorbReaderTap',
callback: (args) {
@@ -1497,6 +1531,13 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
doc.addEventListener('click', function(e){ onTapGuarded(e,'click'); }, true);
doc.addEventListener('pointerup', function(e){ onTapGuarded(e,'pointerup'); }, true);
doc.addEventListener('touchend', function(e){ onTapGuarded(e,'touchend'); }, true);
+ doc.addEventListener('wheel', function(){
+ var now=Date.now();
+ if(now-(window.__absorbWheelAt||0)<400)return;
+ window.__absorbWheelAt=now;
+ try { if(window.flutter_inappwebview){ window.flutter_inappwebview.callHandler('absorbReaderWheel'); return; } } catch(err){}
+ try { if(window.parent && window.parent.flutter_inappwebview){ window.parent.flutter_inappwebview.callHandler('absorbReaderWheel'); } } catch(err){}
+ }, {passive:true});
} catch(e){}
}
try { rendition.getContents().forEach(function(c){ attach(c.document); }); } catch(e){}
@@ -7350,6 +7391,13 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_setupPageInfoHandler();
_setupTapHandler();
_applyTurnStyle();
+ if (AppPlatform.lacksPhonePlugins) {
+ _hideDesktopScrollbar();
+ // Desktop page moves into the app log while the jump
+ // back after a turn is being chased.
+ _epubController?.webViewController?.evaluateJavascript(
+ source: 'window.__absorbLogMoves = true;');
+ }
_queueCurlShot();
_setupAutoScrollHandlers();
_setupFontInjector();
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 3d1b073b..81fcded1 100644
--- a/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
+++ b/packages/flutter_epub_viewer/lib/assets/webpage/html/epubView.js
@@ -2087,6 +2087,7 @@ function setupRenditionBlocking() {
rendition.next = function () {
if (hasActiveSelection()) {
console.log('Blocked next() - selection active');
+ if (window.__absorbLogMoves) blindNotify("absorbAutoScrollLog", "next blocked by a selection");
return Promise.resolve(rendition.location);
}
return originalNext();
@@ -2123,10 +2124,20 @@ function holdSpotThroughResize(r) {
var spot = null;
var resizing = 0;
var quietUntil = 0;
+ var lastW = 0, lastH = 0;
r.onResized = function (size, epubcfi) {
this.emit("resized", { width: size.width, height: size.height }, epubcfi);
if (!this.location || !this.location.start) return;
- var cfi = epubcfi || spot || this.location.start.cfi;
+ // A resize to the size the page already has moved nothing, so there is
+ // nothing to put back: going to the remembered spot from here would undo
+ // a page turn or a scroll that happened since. Windows fires these.
+ var same = size && size.width === lastW && size.height === lastH;
+ if (size) { lastW = size.width; lastH = size.height; }
+ var cfi = epubcfi || (same ? this.location.start.cfi : spot) || this.location.start.cfi;
+ if (window.__absorbLogMoves) {
+ blindNotify("absorbAutoScrollLog", "resize " + (size ? size.width + "x" + size.height : "?") +
+ (same ? " (same size)" : "") + " -> " + cfi);
+ }
resizing++;
// The relocated event for this display comes a couple of frames after it
// resolves.
@@ -2137,8 +2148,13 @@ function holdSpotThroughResize(r) {
Promise.resolve(this.display(cfi)).then(settle, settle);
};
r.on("relocated", function (location) {
+ var cfi = location && location.start ? location.start.cfi : null;
+ if (window.__absorbLogMoves) {
+ blindNotify("absorbAutoScrollLog", "relocated " + cfi +
+ (resizing > 0 ? " (during resize)" : Date.now() < quietUntil ? " (quiet)" : ""));
+ }
if (resizing > 0 || Date.now() < quietUntil) return;
- if (location && location.start && location.start.cfi) spot = location.start.cfi;
+ if (cfi) spot = cfi;
});
}
@@ -2147,6 +2163,7 @@ function next() {
// Block navigation if there's an active selection
if (hasActiveSelection()) {
console.log('Blocked next() - selection active');
+ if (window.__absorbLogMoves) blindNotify("absorbAutoScrollLog", "next blocked by a selection");
return;
}
if (rendition) {
@@ -2582,15 +2599,18 @@ function clearSelection() {
window.getSelection().removeAllRanges();
}
- // Notify Flutter that selection was cleared - only if we've actually sent a selection
- if (hasSentSelection && (isSelecting || lastCfiRange) && window.flutter_inappwebview && window.flutter_inappwebview.callHandler) {
- isSelecting = false;
- lastCfiRange = null;
- hasSentSelection = false;
- if (selectionTimeout) {
- clearTimeout(selectionTimeout);
- selectionTimeout = null;
- }
+ // The tracking flags come down whether or not Flutter ever heard of
+ // the selection: left set, hasActiveSelection() keeps blocking page
+ // turns with nothing on screen to show for it.
+ var tell = hasSentSelection && (isSelecting || lastCfiRange);
+ isSelecting = false;
+ lastCfiRange = null;
+ hasSentSelection = false;
+ if (selectionTimeout) {
+ clearTimeout(selectionTimeout);
+ selectionTimeout = null;
+ }
+ if (tell && window.flutter_inappwebview && window.flutter_inappwebview.callHandler) {
window.flutter_inappwebview.callHandler('selectionCleared');
}
} else {