Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Add a left aligned text option to the ebook reader

Barnabasf1b698d81d71
diff --git a/lib/l10n/app_en.arb b/lib/l10n/app_en.arb
index 01411500..9a293802 100644
--- a/lib/l10n/app_en.arb
+++ b/lib/l10n/app_en.arb
@@ -4750,6 +4750,9 @@
"readerSettings": "Reader Settings",
"readerFontSize": "Font Size",
"readerLineSpacing": "Line Spacing",
+ "readerTextAlignment": "Alignment",
+ "readerAlignJustified": "Justified",
+ "readerAlignLeft": "Left",
"readerSideMargins": "Side margins",
"readerTopBottom": "Top & bottom",
"readerPageLayout": "Page layout",
diff --git a/lib/l10n/app_localizations.dart b/lib/l10n/app_localizations.dart
index 680d4aae..0d02d117 100644
--- a/lib/l10n/app_localizations.dart
+++ b/lib/l10n/app_localizations.dart
@@ -14223,6 +14223,24 @@ abstract class AppLocalizations {
/// **'Line Spacing'**
String get readerLineSpacing;
+ /// No description provided for @readerTextAlignment.
+ ///
+ /// In en, this message translates to:
+ /// **'Alignment'**
+ String get readerTextAlignment;
+
+ /// No description provided for @readerAlignJustified.
+ ///
+ /// In en, this message translates to:
+ /// **'Justified'**
+ String get readerAlignJustified;
+
+ /// No description provided for @readerAlignLeft.
+ ///
+ /// In en, this message translates to:
+ /// **'Left'**
+ String get readerAlignLeft;
+
/// No description provided for @readerSideMargins.
///
/// In en, this message translates to:
diff --git a/lib/l10n/app_localizations_de.dart b/lib/l10n/app_localizations_de.dart
index 2fb6fba8..01c45145 100644
--- a/lib/l10n/app_localizations_de.dart
+++ b/lib/l10n/app_localizations_de.dart
@@ -8369,6 +8369,15 @@ class AppLocalizationsDe extends AppLocalizations {
@override
String get readerLineSpacing => 'Zeilenabstand';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Seitenrand';
diff --git a/lib/l10n/app_localizations_el.dart b/lib/l10n/app_localizations_el.dart
index 17ac9006..ccf5839a 100644
--- a/lib/l10n/app_localizations_el.dart
+++ b/lib/l10n/app_localizations_el.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsEl extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_en.dart b/lib/l10n/app_localizations_en.dart
index 24500953..365033dc 100644
--- a/lib/l10n/app_localizations_en.dart
+++ b/lib/l10n/app_localizations_en.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsEn extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_es.dart b/lib/l10n/app_localizations_es.dart
index 7a5dc472..5096c31e 100644
--- a/lib/l10n/app_localizations_es.dart
+++ b/lib/l10n/app_localizations_es.dart
@@ -8254,6 +8254,15 @@ class AppLocalizationsEs extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_fr.dart b/lib/l10n/app_localizations_fr.dart
index 6b45f15f..1248f1d7 100644
--- a/lib/l10n/app_localizations_fr.dart
+++ b/lib/l10n/app_localizations_fr.dart
@@ -8303,6 +8303,15 @@ class AppLocalizationsFr extends AppLocalizations {
@override
String get readerLineSpacing => 'Interligne';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Marges latérales';
diff --git a/lib/l10n/app_localizations_it.dart b/lib/l10n/app_localizations_it.dart
index c0850a2b..d271fef3 100644
--- a/lib/l10n/app_localizations_it.dart
+++ b/lib/l10n/app_localizations_it.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsIt extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_ja.dart b/lib/l10n/app_localizations_ja.dart
index 86e59883..91737ca1 100644
--- a/lib/l10n/app_localizations_ja.dart
+++ b/lib/l10n/app_localizations_ja.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsJa extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_no.dart b/lib/l10n/app_localizations_no.dart
index 709ae59b..e5241bf3 100644
--- a/lib/l10n/app_localizations_no.dart
+++ b/lib/l10n/app_localizations_no.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsNo extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_pt.dart b/lib/l10n/app_localizations_pt.dart
index e6152c78..9975ab77 100644
--- a/lib/l10n/app_localizations_pt.dart
+++ b/lib/l10n/app_localizations_pt.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsPt extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_ro.dart b/lib/l10n/app_localizations_ro.dart
index d61d5ae8..3ae2fbfe 100644
--- a/lib/l10n/app_localizations_ro.dart
+++ b/lib/l10n/app_localizations_ro.dart
@@ -8252,6 +8252,15 @@ class AppLocalizationsRo extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_ru.dart b/lib/l10n/app_localizations_ru.dart
index a20865a0..5cf7d0cb 100644
--- a/lib/l10n/app_localizations_ru.dart
+++ b/lib/l10n/app_localizations_ru.dart
@@ -8257,6 +8257,15 @@ class AppLocalizationsRu extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/l10n/app_localizations_zh.dart b/lib/l10n/app_localizations_zh.dart
index bba9cc8c..310187a8 100644
--- a/lib/l10n/app_localizations_zh.dart
+++ b/lib/l10n/app_localizations_zh.dart
@@ -8160,6 +8160,15 @@ class AppLocalizationsZh extends AppLocalizations {
@override
String get readerLineSpacing => 'Line Spacing';
+ @override
+ String get readerTextAlignment => 'Alignment';
+
+ @override
+ String get readerAlignJustified => 'Justified';
+
+ @override
+ String get readerAlignLeft => 'Left';
+
@override
String get readerSideMargins => 'Side margins';
diff --git a/lib/services/backup_service.dart b/lib/services/backup_service.dart
index 149146c0..092c10bc 100644
--- a/lib/services/backup_service.dart
+++ b/lib/services/backup_service.dart
@@ -327,6 +327,7 @@ class BackupService {
final ereader = <String, dynamic>{
'fontSize': await ScopedPrefs.getInt('ereader_fontSize'),
'lineHeight': await ScopedPrefs.getDouble('ereader_lineHeight'),
+ 'alignLeft': await ScopedPrefs.getBool('ereader_alignLeft'),
'marginH': await ScopedPrefs.getInt('ereader_margin_h'),
'marginV': await ScopedPrefs.getInt('ereader_margin_v'),
'spread': await ScopedPrefs.getInt('ereader_spread'),
@@ -1001,6 +1002,7 @@ class BackupService {
if (er != null) {
if (er['fontSize'] != null) await ScopedPrefs.setInt('ereader_fontSize', er['fontSize'] as int);
if (er['lineHeight'] != null) await ScopedPrefs.setDouble('ereader_lineHeight', (er['lineHeight'] as num).toDouble());
+ if (er['alignLeft'] != null) await ScopedPrefs.setBool('ereader_alignLeft', er['alignLeft'] as bool);
if (er['marginH'] != null) await ScopedPrefs.setInt('ereader_margin_h', er['marginH'] as int);
if (er['marginV'] != null) await ScopedPrefs.setInt('ereader_margin_v', er['marginV'] as int);
if (er['spread'] != null) await ScopedPrefs.setInt('ereader_spread', er['spread'] as int);
diff --git a/lib/widgets/ebook_reader_view.dart b/lib/widgets/ebook_reader_view.dart
index 14d7cd50..774d07a5 100644
--- a/lib/widgets/ebook_reader_view.dart
+++ b/lib/widgets/ebook_reader_view.dart
@@ -232,6 +232,9 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
// Reader settings
int _fontSize = 16;
double _lineHeight = 1.4;
+ // Books justify their text by default; on a narrow screen that opens wide
+ // gaps between words, so the reader can force a ragged right edge.
+ bool _alignLeft = false;
int _marginH = 16; // left + right
String _volumeNavMode = 'off';
bool _volumeNavWhilePlaying = false;
@@ -266,6 +269,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
static const _kFontSize = 'ereader_fontSize';
static const _kLineHeight = 'ereader_lineHeight';
+ static const _kAlignLeft = 'ereader_alignLeft';
static const _kMargin = 'ereader_margin'; // legacy single margin (migrated)
static const _kMarginH = 'ereader_margin_h';
static const _kMarginV = 'ereader_margin_v';
@@ -455,6 +459,7 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
Future<void> _loadSettings() async {
_fontSize = await ScopedPrefs.getInt(_kFontSize) ?? 16;
_lineHeight = await ScopedPrefs.getDouble(_kLineHeight) ?? 1.4;
+ _alignLeft = await ScopedPrefs.getBool(_kAlignLeft) ?? false;
final legacyMargin = await ScopedPrefs.getInt(_kMargin) ?? 16;
_marginH = await ScopedPrefs.getInt(_kMarginH) ?? legacyMargin;
_marginV = await ScopedPrefs.getInt(_kMarginV) ?? legacyMargin;
@@ -592,6 +597,11 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
'will-change': 'scroll-position',
};
final textRule = <String, String>{'color': 'inherit !important'};
+ if (_alignLeft) {
+ body['text-align'] = 'left !important';
+ textRule['text-align'] = 'left !important';
+ textRule['text-align-last'] = 'left !important';
+ }
if (font.family.isNotEmpty) {
final fam = font.downloadable ? '"${font.family}"' : font.family;
body['font-family'] = '$fam !important';
@@ -626,6 +636,12 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
await ScopedPrefs.setDouble(_kLineHeight, height);
}
+ Future<void> _updateAlignLeft(bool left) async {
+ setState(() => _alignLeft = left);
+ _applySettings();
+ await ScopedPrefs.setBool(_kAlignLeft, left);
+ }
+
Future<void> _updateMarginH(int margin) async {
// Side margins resize the WebView (handled in _buildViewerArea), so just
// rebuild - no CSS re-apply needed.
@@ -1600,6 +1616,28 @@ class EbookReaderViewState extends State<EbookReaderView> with WidgetsBindingObs
),
const SizedBox(height: 8),
+ // Text alignment
+ Row(children: [
+ Icon(Icons.format_align_left_rounded, size: 20, color: cs.onSurfaceVariant),
+ const SizedBox(width: 12),
+ Text(l.readerTextAlignment, style: tt.bodyMedium),
+ const Spacer(),
+ SegmentedButton<bool>(
+ showSelectedIcon: false,
+ style: const ButtonStyle(visualDensity: VisualDensity.compact),
+ segments: [
+ ButtonSegment(value: false, label: Text(l.readerAlignJustified)),
+ ButtonSegment(value: true, label: Text(l.readerAlignLeft)),
+ ],
+ selected: {_alignLeft},
+ onSelectionChanged: (sel) {
+ setSheetState(() {});
+ _updateAlignLeft(sel.first);
+ },
+ ),
+ ]),
+ const SizedBox(height: 8),
+
// Side margins (left + right)
Row(children: [
Icon(Icons.padding_rounded, size: 20, color: cs.onSurfaceVariant),