Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a reader setting to keep the title and progress bar on screen, turn the card's live transcript into read along for books with an epub, and wait paused while the transcript gets ahead

Barnabas61c3ab982e67
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index de297e40..479be134 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -4635,6 +4635,8 @@
"readerSideMargins": "Side margins",
"readerTopBottom": "Top & bottom",
"readerPageLayout": "Page layout",
+ "readerPinTopBar": "Keep the title and progress bar on screen",
+ "readerPinTopBarHint": "Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.",
"readerLayoutAuto": "Auto",
"readerLayoutSingle": "Single",
"readerLayoutTwoPage": "Two-page",
@@ -4897,6 +4899,7 @@
"readAlongFollowSentence": "Sentence",
"readAlongColor": "Read along color",
"readAlongLost": "Can't find the narration on the page",
+ "readAlongReady": "Read along is ready",
"@readAlongLost": {
"description": "Read along pill after 45s of narration that matches nothing on the page"
},
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 1916e161..da9d668c 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -13899,6 +13899,18 @@ abstract class AppLocalizations {
/// **'Page layout'**
String get readerPageLayout;
+ /// No description provided for @readerPinTopBar.
+ ///
+ /// In en, this message translates to:
+ /// **'Keep the title and progress bar on screen'**
+ String get readerPinTopBar;
+
+ /// No description provided for @readerPinTopBarHint.
+ ///
+ /// In en, this message translates to:
+ /// **'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.'**
+ String get readerPinTopBarHint;
+
/// No description provided for @readerLayoutAuto.
///
/// In en, this message translates to:
@@ -15063,6 +15075,12 @@ abstract class AppLocalizations {
/// **'Can\'t find the narration on the page'**
String get readAlongLost;
+ /// No description provided for @readAlongReady.
+ ///
+ /// In en, this message translates to:
+ /// **'Read along is ready'**
+ String get readAlongReady;
+
/// No description provided for @findInEbook.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 913d05e8..cfce4460 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -8164,6 +8164,13 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get readerPageLayout => 'Seitenlayout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Automatisch';
@@ -8835,6 +8842,9 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index a22d6a4f..36c7cc06 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 0efbb2af..9d91f8fe 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index f4e6a52d..67ab1c70 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -8049,6 +8049,13 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8716,6 +8723,9 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index db451439..fd292321 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -8098,6 +8098,13 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get readerPageLayout => 'Mise en page';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8767,6 +8774,9 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 191752e9..1184dff8 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index ce3f49a5..f5ee4c9b 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 3038b637..f3aef734 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 9c394de2..a07f7050 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index e84a4e20..13e67b41 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -8047,6 +8047,13 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8714,6 +8721,9 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index 974b4d6a..f9bcc807 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -8052,6 +8052,13 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8719,6 +8726,9 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 9831d3ec..36e5de1a 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -7955,6 +7955,13 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get readerPageLayout => 'Page layout';
+ @override
+ String get readerPinTopBar => 'Keep the title and progress bar on screen';
+
+ @override
+ String get readerPinTopBarHint =>
+ 'Pins them above the page instead of showing them on tap. The page gets a little shorter so no text is covered.';
+
@override
String get readerLayoutAuto => 'Auto';
@@ -8622,6 +8629,9 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get readAlongLost => 'Can\'t find the narration on the page';
+ @override
+ String get readAlongReady => 'Read along is ready';
+
@override
String get findInEbook => 'Find in ebook';
diff --git a/lib/widgets/absorbing_card.dart b/lib/widgets/absorbing_card.dart
index 55dd36e4..a3363707 100644
--- a/lib/widgets/absorbing_card.dart
+++ b/lib/widgets/absorbing_card.dart
@@ -1216,6 +1216,22 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ebookFile);
}
+ void _openReadAlong() async {
+ var ebookFile = _ebookFile;
+ ebookFile ??= await cachedEbookFileFor(_itemId);
+ if (ebookFile == null) {
+ await _fetchChaptersIfNeeded();
+ ebookFile = _ebookFile;
+ }
+ if (!mounted) return;
+ if (ebookFile == null) {
+ showOverlayToast(context, AppLocalizations.of(context)!.noEbookFileFound, icon: Icons.menu_book_outlined);
+ return;
+ }
+ openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ebookFile,
+ startReadAlong: true);
+ }
+
void _findInEbook() async {
var ebookFile = _ebookFile;
ebookFile ??= await cachedEbookFileFor(_itemId);
@@ -1360,8 +1376,10 @@ class AbsorbingCardState extends State<AbsorbingCard> with AutomaticKeepAliveCli
PlayerSettings.setCardButtonVisibleCount(newCount);
},
isEbookPdf: _ebookExt == 'pdf',
+ isEbookEpub: _ebookExt == 'epub',
onEbookTap: _openEbookReader,
onFindInEbookTap: _findInEbook,
+ onReadAlongTap: _openReadAlong,
);
int get _visibleButtonCount => _buttonVisibleCount;
diff --git a/lib/widgets/card_buttons.dart b/lib/widgets/card_buttons.dart
index d4d03139..521ae5d0 100644
--- a/lib/widgets/card_buttons.dart
+++ b/lib/widgets/card_buttons.dart
@@ -1502,8 +1502,12 @@ class CardActionDelegate {
final VoidCallback? onRemoveExtra;
final void Function(List<String>, int) onReorder;
final bool isEbookPdf;
+ // An epub beside the audio turns the live transcript button into Read
+ // along: the reader lights the book's own words instead of a card overlay.
+ final bool isEbookEpub;
final VoidCallback? onEbookTap;
final VoidCallback? onFindInEbookTap;
+ final VoidCallback? onReadAlongTap;
CardActionDelegate({
required this.context,
@@ -1532,10 +1536,15 @@ class CardActionDelegate {
this.onRemoveExtra,
required this.onReorder,
this.isEbookPdf = false,
+ this.isEbookEpub = false,
this.onEbookTap,
this.onFindInEbookTap,
+ this.onReadAlongTap,
});
+ bool get _readAlongInstead =>
+ isEbookEpub && episodeId == null && onReadAlongTap != null;
+
Map<String, dynamic> get _media => item['media'] as Map<String, dynamic>? ?? {};
/// Which episode the bookmark surfaces belong to. A card that knows its own
@@ -1871,6 +1880,14 @@ class CardActionDelegate {
onTap: () => onFindInEbookTap?.call(),
);
case 'lyrics':
+ if (_readAlongInstead) {
+ return CardWideButton(
+ icon: Icons.auto_stories_rounded,
+ label: l.readAlong,
+ accent: accent, isActive: true, alwaysEnabled: true, large: large, compact: compact, iconsOnly: iconsOnly,
+ onTap: () => onReadAlongTap?.call(),
+ );
+ }
// Usable with nothing playing: the handler hot-loads this card's
// book paused so the runway can build before the user presses play.
return CardWideButton(
@@ -2064,6 +2081,16 @@ class CardActionDelegate {
},
);
case 'lyrics':
+ if (_readAlongInstead) {
+ return MoreMenuItem(
+ icon: Icons.auto_stories_rounded,
+ label: l.readAlong, accent: accent,
+ onTap: () {
+ Navigator.pop(ctx);
+ onReadAlongTap?.call();
+ },
+ );
+ }
return MoreMenuItem(
icon: Icons.subtitles_rounded,
label: l.lyricsMode, accent: accent,
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index d21030c4..f0aa58ec 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -70,6 +70,9 @@ class EbookReaderView extends StatefulWidget {
/// The audio position [findText] was transcribed at, so a not-confident
/// first pass can transcribe a longer window and retry.
final double? findPositionSeconds;
+ /// Turn read along on as soon as the book is displayed, for the card's
+ /// Read along button.
+ final bool startReadAlong;
const EbookReaderView({
super.key,
@@ -80,6 +83,7 @@ class EbookReaderView extends StatefulWidget {
this.findText,
this.findChapterHint,
this.findPositionSeconds,
+ this.startReadAlong = false,
});
@override
@@ -160,6 +164,14 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
bool _transcriptionOn = false;
// Read along: follow the audiobook by coloring the words being spoken.
bool _readAlongOn = false;
+ bool _didAutoReadAlong = false;
+ // Listening ahead: the runway fills faster with the playhead standing
+ // still, and nobody wants to hear words the page can't show yet. Armed at
+ // start; the first gated tick pauses a playing book (resumed by itself
+ // when ready) or notes a paused one (a toast when ready).
+ bool _readAlongGateArmed = false;
+ bool _readAlongGatePaused = false;
+ bool _readAlongGateWaiting = false;
Timer? _readAlongTimer;
double? _readAlongLineStart;
double _readAlongLineLastWord = 0;
@@ -205,6 +217,9 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
// Page layout: auto shows two pages on wide screens (tablets), single forces
// one page, two forces a spread. Stored as index 0=auto/1=single/2=two.
EpubSpread _spread = EpubSpread.auto;
+ // Keep the title and progress bar on screen above the page instead of
+ // fading them in over it. The page gets shorter, never covered.
+ bool _pinTop = false;
static const _spreadModes = [EpubSpread.auto, EpubSpread.none, EpubSpread.always];
// E-reader background theme (empty = follow the app's light/dark) and font.
String _themeId = '';
@@ -218,6 +233,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
static const _kSpread = 'ereader_spread';
static const _kTheme = 'ereader_theme';
static const _kFont = 'ereader_font';
+ static const _kPinTop = 'ereader_pin_top';
// Gates the WebView mount until the entering route animation completes, so
// the heavy platform view doesn't stutter the open transition.
@@ -395,6 +411,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_spread = _spreadModes[si];
_themeId = await ScopedPrefs.getString(_kTheme) ?? '';
_fontId = await ScopedPrefs.getString(_kFont) ?? 'original';
+ _pinTop = await ScopedPrefs.getBool(_kPinTop) ?? false;
_volumeNavMode = await PlayerSettings.getEreaderVolumeNav();
_volumeNavWhilePlaying = await PlayerSettings.getEreaderVolumeNavWhilePlaying();
_autoScrollSpeed = (await PlayerSettings.getEreaderAutoScrollSpeed())
@@ -562,6 +579,20 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
await ScopedPrefs.setInt(_kMarginH, margin);
}
+ Future<void> _updatePinTop(bool on) async {
+ if (on == _pinTop) return;
+ // The WebView gets shorter or taller, so epub.js re-paginates and
+ // re-seats the current page on its own, like a side margin change. The
+ // blind's live line has to be found again on the new page.
+ setState(() => _pinTop = on);
+ await ScopedPrefs.setBool(_kPinTop, on);
+ if (_autoScroll) {
+ Future.delayed(const Duration(milliseconds: 500), () async {
+ if (mounted && _autoScroll) await _epubController?.autoScrollResync();
+ });
+ }
+ }
+
Future<void> _updateMarginV(int margin) async {
setState(() => _marginV = margin);
_applySettings();
@@ -1532,7 +1563,18 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
},
),
),
- const SizedBox(height: 16),
+ SwitchListTile(
+ contentPadding: EdgeInsets.zero,
+ title: Text(l.readerPinTopBar, style: tt.bodyMedium),
+ subtitle: Text(l.readerPinTopBarHint,
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _pinTop,
+ onChanged: (v) {
+ setSheetState(() {});
+ _updatePinTop(v);
+ },
+ ),
+ const SizedBox(height: 8),
// Theme (background + text colors)
Row(children: [
@@ -3458,6 +3500,9 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
await wc?.evaluateJavascript(
source: 'window.__absorbRA && __absorbRA.start()');
if (mounted) setState(() => _readAlongOn = true);
+ _readAlongGateArmed = true;
+ _readAlongGatePaused = false;
+ _readAlongGateWaiting = false;
// The first anchor may be behind the page you left the reader on.
_readAlongAllowBack = true;
_readAlongLastMatch = DateTime.now();
@@ -3610,6 +3655,18 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_readAlongSeekAt = null;
_readAlongLastMatch = null;
_readAlongLost = false;
+ _readAlongGateArmed = false;
+ _readAlongGateWaiting = false;
+ // Leaving during the wait: put playback back the way it was found, or
+ // the book sits paused with nothing on screen to say why.
+ if (_readAlongGatePaused) {
+ _readAlongGatePaused = false;
+ final player = AudioPlayerService();
+ if (!player.isPlaying && player.currentItemId == widget.itemId) {
+ debugPrint('[ReadAlong] stopped while paused for the runway, resuming');
+ unawaited(player.play(logDetail: 'read along stopped', fromUi: true));
+ }
+ }
LyricsService.instance.readerOwns = false;
if (!_autoScroll) ScreenWake.keepOn(false);
if (_readAlongStartedPipeline) {
@@ -3641,7 +3698,36 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
}
// Same runway gate as the live transcript: painting before it is banked
// means the first stall leaves the page stuck on a stale sentence.
- if (svc.gateProgress != null) return;
+ if (svc.gateProgress != null) {
+ if (_readAlongGateArmed) {
+ _readAlongGateArmed = false;
+ if (player.isPlaying) {
+ _readAlongGatePaused = true;
+ debugPrint('[ReadAlong] pausing while the runway builds');
+ await player.pause();
+ } else {
+ _readAlongGateWaiting = true;
+ }
+ } else if (_readAlongGatePaused && player.isPlaying) {
+ // Pressed play during the wait: their call, don't touch it again.
+ _readAlongGatePaused = false;
+ }
+ return;
+ }
+ _readAlongGateArmed = false;
+ if (_readAlongGatePaused) {
+ _readAlongGatePaused = false;
+ if (!player.isPlaying) {
+ debugPrint('[ReadAlong] runway ready, resuming');
+ await player.play(logDetail: 'read along ready', fromUi: true);
+ }
+ } else if (_readAlongGateWaiting) {
+ _readAlongGateWaiting = false;
+ if (!player.isPlaying && mounted) {
+ showOverlayToast(context, AppLocalizations.of(context)!.readAlongReady,
+ icon: Icons.auto_stories_rounded);
+ }
+ }
final pos = player.position.inMilliseconds / 1000.0;
// Same sync offset the player uses, so headphones don't run the reader's
// coloring ahead of the voice either.
@@ -4264,13 +4350,96 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
EdgeInsets? _viewerPadding;
Size? _viewerPaddingSize;
+ Widget _buildTopBarContent(Color fg, Color fgDim, Color accent) {
+ return Padding(
+ padding: const EdgeInsets.fromLTRB(20, 8, 20, 12),
+ child: Column(
+ mainAxisSize: MainAxisSize.min,
+ children: [
+ Text(
+ widget.title,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ textAlign: TextAlign.center,
+ style: TextStyle(
+ color: fg,
+ fontWeight: FontWeight.w600,
+ fontSize: 16,
+ ),
+ ),
+ const SizedBox(height: 8),
+ ClipRRect(
+ borderRadius: BorderRadius.circular(2),
+ child: LinearProgressIndicator(
+ value: _progress.clamp(0.0, 1.0),
+ minHeight: 3,
+ backgroundColor: fg.withValues(alpha: 0.1),
+ valueColor: AlwaysStoppedAnimation(accent),
+ ),
+ ),
+ const SizedBox(height: 4),
+ Row(
+ children: [
+ if (_chapterPageTotal > 0)
+ Text(
+ '$_chapterPage / $_chapterPageTotal',
+ style: TextStyle(color: fgDim, fontSize: 11),
+ ),
+ Expanded(
+ child: Padding(
+ padding: const EdgeInsets.symmetric(horizontal: 8),
+ child: Text(
+ _currentChapterTitle ?? '',
+ textAlign: TextAlign.center,
+ maxLines: 1,
+ overflow: TextOverflow.ellipsis,
+ style: TextStyle(color: fgDim, fontSize: 11),
+ ),
+ ),
+ ),
+ Text(
+ '${(_progress * 100).toStringAsFixed(1)}%',
+ style: TextStyle(color: fgDim, fontSize: 11),
+ ),
+ ],
+ ),
+ ],
+ ),
+ );
+ }
+
+ /// The page on its own, or under the always-visible bar when pinned. The
+ /// bar takes the frozen cutout padding (not a live SafeArea) so the system
+ /// bars flashing in during a recents swipe can't resize the WebView.
+ Widget _withPinnedTopBar(
+ Color bg, Color fg, Color fgDim, Color accent, Widget viewerArea) {
+ // Same column in both modes, with an empty slot when the bar is off. If
+ // the tree around the viewer changed shape, Flutter would recreate the
+ // WebView and the book would reopen at the start with its handlers gone.
+ final frozen = _frozenViewerPadding();
+ return Column(
+ children: [
+ if (_pinTop)
+ Container(
+ color: bg,
+ padding: EdgeInsets.only(
+ top: frozen.top, left: frozen.left, right: frozen.right),
+ child: _buildTopBarContent(fg, fgDim, accent),
+ )
+ else
+ const SizedBox.shrink(),
+ Expanded(child: viewerArea),
+ ],
+ );
+ }
+
/// Padded with the frozen safe-area padding above so the page never slides
/// under the camera cutout but also never resizes when the system bars flash
/// back in. Side margins are applied here (horizontal padding shrinks the
/// WebView so epub.js paginates into the narrower box) - epub.js's column
/// layout ignores horizontal body padding, so CSS only handles the vertical
/// margins.
- Widget _buildViewerArea(Widget viewer) {
+ EdgeInsets _frozenViewerPadding() {
final size = MediaQuery.sizeOf(context);
final pad = MediaQuery.paddingOf(context);
final prev = _viewerPadding;
@@ -4285,8 +4454,15 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
bottom: min(prev.bottom, pad.bottom),
);
}
+ return _viewerPadding!;
+ }
+
+ Widget _buildViewerArea(Widget viewer) {
+ final frozen = _frozenViewerPadding();
+ // With the bar pinned above the page, the bar already clears the cutout.
+ final padding = _pinTop ? frozen.copyWith(top: 0) : frozen;
return Padding(
- padding: _viewerPadding!,
+ padding: padding,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: _marginH.toDouble()),
child: LayoutBuilder(
@@ -4466,8 +4642,11 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
// EpubViewer + overlays, each wrapped in a SafeArea for camera cutouts.
final viewerBody = Stack(
children: [
- // Epub viewer with safe area padding for camera cutouts
- _buildViewerArea(EpubViewer(
+ // Epub viewer with safe area padding for camera cutouts. With the
+ // bar pinned, it is a shorter box under the bar rather than a full
+ // page with the bar over it, so nothing the page shows is hidden
+ // and read along and auto scroll measure the page they can see.
+ _withPinnedTopBar(bg, fg, fgDim, accent, _buildViewerArea(EpubViewer(
key: ValueKey(_viewerKey),
epubSource: EpubSource.fromFile(_cachedFile!),
epubController: _epubController!,
@@ -4553,6 +4732,12 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_setupFontInjector();
_applyFontFace();
}
+ if (widget.startReadAlong && !_didAutoReadAlong) {
+ _didAutoReadAlong = true;
+ Future.delayed(const Duration(milliseconds: 900), () {
+ if (mounted && !_readAlongOn) _toggleReadAlong();
+ });
+ }
final findText = widget.findText;
if (findText != null && findText.isNotEmpty && !_didStartFind) {
_didStartFind = true;
@@ -4605,89 +4790,37 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
if (dx > 0.05 || dy > 0.05) return;
_readerTapAt(x, 'touch');
},
- )),
+ ))),
// Top overlay: what you are reading and how far in. Controls live
- // at the bottom now, in thumb reach.
- AnimatedOpacity(
- opacity: _showControls ? 1.0 : 0.0,
- duration: const Duration(milliseconds: 200),
- child: IgnorePointer(
- ignoring: !_showControls,
- child: Container(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- bg.withValues(alpha: 1.0),
- bg.withValues(alpha: 1.0),
- bg.withValues(alpha: 0.0),
- ],
- stops: const [0.0, 0.86, 1.0],
- ),
- ),
- child: SafeArea(
- bottom: false,
- child: Padding(
- padding: const EdgeInsets.fromLTRB(20, 8, 20, 12),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- children: [
- Text(
- widget.title,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- textAlign: TextAlign.center,
- style: TextStyle(
- color: fg,
- fontWeight: FontWeight.w600,
- fontSize: 16,
- ),
- ),
- const SizedBox(height: 8),
- ClipRRect(
- borderRadius: BorderRadius.circular(2),
- child: LinearProgressIndicator(
- value: _progress.clamp(0.0, 1.0),
- minHeight: 3,
- backgroundColor: fg.withValues(alpha: 0.1),
- valueColor: AlwaysStoppedAnimation(accent),
- ),
- ),
- const SizedBox(height: 4),
- Row(
- children: [
- if (_chapterPageTotal > 0)
- Text(
- '$_chapterPage / $_chapterPageTotal',
- style: TextStyle(color: fgDim, fontSize: 11),
- ),
- Expanded(
- child: Padding(
- padding: const EdgeInsets.symmetric(horizontal: 8),
- child: Text(
- _currentChapterTitle ?? '',
- textAlign: TextAlign.center,
- maxLines: 1,
- overflow: TextOverflow.ellipsis,
- style: TextStyle(color: fgDim, fontSize: 11),
- ),
- ),
- ),
- Text(
- '${(_progress * 100).toStringAsFixed(1)}%',
- style: TextStyle(color: fgDim, fontSize: 11),
- ),
- ],
- ),
+ // at the bottom now, in thumb reach. Pinned, it sits above the page
+ // in the column built below instead of fading in over it.
+ if (!_pinTop)
+ AnimatedOpacity(
+ opacity: _showControls ? 1.0 : 0.0,
+ duration: const Duration(milliseconds: 200),
+ child: IgnorePointer(
+ ignoring: !_showControls,
+ child: Container(
+ decoration: BoxDecoration(
+ gradient: LinearGradient(
+ begin: Alignment.topCenter,
+ end: Alignment.bottomCenter,
+ colors: [
+ bg.withValues(alpha: 1.0),
+ bg.withValues(alpha: 1.0),
+ bg.withValues(alpha: 0.0),
],
+ stops: const [0.0, 0.86, 1.0],
),
),
+ child: SafeArea(
+ bottom: false,
+ child: _buildTopBarContent(fg, fgDim, accent),
+ ),
),
),
),
- ),
// Bottom overlay: every control, centred and within thumb reach.
Positioned(
diff --git a/lib/widgets/ebook_router.dart b/lib/widgets/ebook_router.dart
index 3b08d0e0..6c2fe7fc 100644
--- a/lib/widgets/ebook_router.dart
+++ b/lib/widgets/ebook_router.dart
@@ -32,6 +32,7 @@ Future<void> openEbookReader(
String? findText,
String? findChapterHint,
double? findPositionSeconds,
+ bool startReadAlong = false,
}) async {
final ext = ebookExt(ebookFile);
final Widget viewer;
@@ -39,7 +40,7 @@ Future<void> openEbookReader(
viewer = EbookReaderView(
itemId: itemId, title: title, ebookFile: ebookFile, openAtCfi: openAtCfi,
findText: findText, findChapterHint: findChapterHint,
- findPositionSeconds: findPositionSeconds);
+ findPositionSeconds: findPositionSeconds, startReadAlong: startReadAlong);
} else if (ext == 'pdf') {
viewer = PdfReaderView(itemId: itemId, title: title, ebookFile: ebookFile);
} else if (foliateEbookFormats.contains(ext)) {
diff --git a/lib/widgets/expanded_card.dart b/lib/widgets/expanded_card.dart
index b91c3b8b..73f01cab 100644
--- a/lib/widgets/expanded_card.dart
+++ b/lib/widgets/expanded_card.dart
@@ -1233,8 +1233,10 @@ class _ExpandedCardState extends State<ExpandedCard> {
PlayerSettings.setCardButtonVisibleCount(newCount);
},
isEbookPdf: _ebookExt == 'pdf',
+ isEbookEpub: _ebookExt == 'epub',
onEbookTap: _openReader,
onFindInEbookTap: _findInEbook,
+ onReadAlongTap: _openReadAlong,
);
Map<String, dynamic>? get _ebookFile =>
@@ -1265,6 +1267,24 @@ class _ExpandedCardState extends State<ExpandedCard> {
openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ef);
}
+ void _openReadAlong() async {
+ var ef = _ebookFile;
+ ef ??= await cachedEbookFileFor(_itemId);
+ if (ef == null) {
+ await _fetchChaptersIfNeeded();
+ ef = _ebookFile;
+ }
+ if (!mounted) return;
+ if (ef == null) {
+ showOverlayToast(context, AppLocalizations.of(context)!.noEbookFileFound, icon: Icons.menu_book_outlined);
+ return;
+ }
+ // The reader checks the rest: loads this book if something else is
+ // playing, the transcription setting, the download prompt.
+ openEbookReader(context, itemId: _itemId, title: _title, ebookFile: ef,
+ startReadAlong: true);
+ }
+
void _findInEbook() async {
var ef = _ebookFile;
ef ??= await cachedEbookFileFor(_itemId);