Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a reader setting to keep the screen on while a book is open

Barnabascfa36b4794cb
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 381eb9ba..b9fa370a 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -4637,6 +4637,8 @@
"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.",
+ "readerKeepAwake": "Keep the screen on",
+ "readerKeepAwakeHint": "The screen won't time out while a book is open.",
"readerLayoutAuto": "Auto",
"readerLayoutSingle": "Single",
"readerLayoutTwoPage": "Two-page",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 5822abc4..737bbcb9 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -13911,6 +13911,18 @@ abstract class AppLocalizations {
/// **'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 @readerKeepAwake.
+ ///
+ /// In en, this message translates to:
+ /// **'Keep the screen on'**
+ String get readerKeepAwake;
+
+ /// No description provided for @readerKeepAwakeHint.
+ ///
+ /// In en, this message translates to:
+ /// **'The screen won\'t time out while a book is open.'**
+ String get readerKeepAwakeHint;
+
/// No description provided for @readerLayoutAuto.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index a8408857..98719e99 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -8171,6 +8171,13 @@ class AppLocalizationsDe extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Automatisch';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 27fdb2c3..59d48d06 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsEl extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 21236754..eb56d230 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsEn extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 32082d93..22951154 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -8056,6 +8056,13 @@ class AppLocalizationsEs extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 4c4929fc..3ca318d9 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -8105,6 +8105,13 @@ class AppLocalizationsFr extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index 3a96ef82..90555493 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsIt extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 4bd84518..ed257ea1 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsJa extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 22806dc7..e3d7060b 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsNo extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index 1e0af4c2..c1e33b0c 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsPt extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index 2671df3a..9fc7492c 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -8054,6 +8054,13 @@ class AppLocalizationsRo extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index 64497c21..85b065d9 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -8059,6 +8059,13 @@ class AppLocalizationsRu extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index 7fd7a00b..38f491e6 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -7962,6 +7962,13 @@ class AppLocalizationsZh extends AppLocalizations {
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 readerKeepAwake => 'Keep the screen on';
+
+ @override
+ String get readerKeepAwakeHint =>
+ 'The screen won\'t time out while a book is open.';
+
@override
String get readerLayoutAuto => 'Auto';
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index 53524c1a..104a412b 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -225,6 +225,9 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
// 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;
+ // Hold the screen on for the whole read, not only while auto scroll or
+ // read along moves the page (GH #384).
+ bool _keepAwake = true;
static const _spreadModes = [EpubSpread.auto, EpubSpread.none, EpubSpread.always];
// E-reader background theme (empty = follow the app's light/dark) and font.
String _themeId = '';
@@ -239,6 +242,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
static const _kTheme = 'ereader_theme';
static const _kFont = 'ereader_font';
static const _kPinTop = 'ereader_pin_top';
+ static const _kKeepAwake = 'ereader_keep_awake';
// Gates the WebView mount until the entering route animation completes, so
// the heavy platform view doesn't stutter the open transition.
@@ -417,6 +421,8 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_themeId = await ScopedPrefs.getString(_kTheme) ?? '';
_fontId = await ScopedPrefs.getString(_kFont) ?? 'original';
_pinTop = await ScopedPrefs.getBool(_kPinTop) ?? false;
+ _keepAwake = await ScopedPrefs.getBool(_kKeepAwake) ?? true;
+ if (mounted) _syncScreenWake();
_volumeNavMode = await PlayerSettings.getEreaderVolumeNav();
_volumeNavWhilePlaying = await PlayerSettings.getEreaderVolumeNavWhilePlaying();
_autoScrollSpeed = (await PlayerSettings.getEreaderAutoScrollSpeed())
@@ -584,6 +590,20 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
await ScopedPrefs.setInt(_kMarginH, margin);
}
+ /// The screen stays on while the setting, auto scroll or read along asks
+ /// for it. Every path that changes one of those ends here, and dispose
+ /// releases it outright since the platform never clears it on its own.
+ void _syncScreenWake() {
+ ScreenWake.keepOn(_keepAwake || _autoScroll || _readAlongOn);
+ }
+
+ Future<void> _updateKeepAwake(bool on) async {
+ if (on == _keepAwake) return;
+ setState(() => _keepAwake = on);
+ _syncScreenWake();
+ await ScopedPrefs.setBool(_kKeepAwake, on);
+ }
+
Future<void> _updatePinTop(bool on) async {
if (on == _pinTop) return;
// The WebView gets shorter or taller, so epub.js re-paginates and
@@ -608,10 +628,8 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
void dispose() {
_stopReadAlong();
_speedToast?.dismiss();
- if (_autoScroll) {
- _epubController?.autoScrollStop();
- ScreenWake.keepOn(false);
- }
+ if (_autoScroll) _epubController?.autoScrollStop();
+ ScreenWake.keepOn(false);
_quietLib.setReaderQuiet(false);
WidgetsBinding.instance.removeObserver(this);
_volumeNav.detach();
@@ -1589,6 +1607,17 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
_updatePinTop(v);
},
),
+ SwitchListTile(
+ contentPadding: EdgeInsets.zero,
+ title: Text(l.readerKeepAwake, style: tt.bodyMedium),
+ subtitle: Text(l.readerKeepAwakeHint,
+ style: tt.bodySmall?.copyWith(color: cs.onSurfaceVariant)),
+ value: _keepAwake,
+ onChanged: (v) {
+ setSheetState(() {});
+ _updateKeepAwake(v);
+ },
+ ),
const SizedBox(height: 8),
// Theme (background + text colors)
@@ -1788,11 +1817,11 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
final atEnd = args.isNotEmpty && args[0]?.toString() == 'end';
_speedToast?.dismiss();
_speedToast = null;
- ScreenWake.keepOn(false);
setState(() {
_autoScroll = false;
_autoScrollPaused = false;
});
+ _syncScreenWake();
final l = AppLocalizations.of(context)!;
showOverlayToast(
context,
@@ -1838,11 +1867,12 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
Future<void> _toggleAutoScroll() async {
if (_autoScroll) {
await _epubController?.autoScrollStop();
- ScreenWake.keepOn(false);
- if (mounted) setState(() {
+ if (!mounted) return;
+ setState(() {
_autoScroll = false;
_autoScrollPaused = false;
});
+ _syncScreenWake();
return;
}
// Read along and the blind both move the page; only one runs at a time.
@@ -3905,7 +3935,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
}
}
LyricsService.instance.readerOwns = false;
- if (!_autoScroll) ScreenWake.keepOn(false);
+ _syncScreenWake();
if (_readAlongStartedPipeline) {
LyricsService.instance.disable();
_readAlongStartedPipeline = false;