Open Lumen on a responsive bookshelf and land pending story work

- Start the app on a shelf of every installed book instead of the last
  book's home. Cards show the cover, translated metadata, available
  languages, restored patterns and either "Continue reading" or a way in.
- Keep each book's saved route: the shelf is never written to
  flutter_screen, so choosing a book resumes its own leaf.
- Lay the shelf out responsively: dense single-column cards on phones,
  a two-column grid from 720 logical pixels, a centred 860 px column on
  tablets and a chevron that exists in the bundled handwriting font.
- Keep Settings reachable from the shelf and let the home page's story
  library button open the shelf instead of a dialog.
- Add tool/shelf_screenshots_test.dart, which renders the shelf at phone
  and tablet metrics, checks that every card and Settings stay reachable
  without overflow, and writes artifacts/library/shelf-*.png.
- Commit the pending Midnight Invitation package (campaign, notebook,
  narration and artwork), cast drama and ambience tooling, region
  selection, phone/tablet integration coverage and related docs.

flutter analyze is clean and all 74 widget/domain tests pass.
This commit is contained in:
Hadrian Burkhardt
2026-11-05 14:29:50 +01:00
parent d45cd0698a
commit 0718e3f252
149 changed files with 16875 additions and 393 deletions
+408 -186
View File
@@ -53,6 +53,9 @@ Future<void> main() async {
final game = Game(book, progress, library: library)
..deviceLanguage = deviceLanguage;
await game.restore();
// The shelf is the first window. `restore()` has already positioned each
// book's saved leaf, so choosing a book resumes exactly where it was left.
game.showLibrary();
runApp(LumenApp(game: game));
}
@@ -126,11 +129,13 @@ class Notebook extends StatefulWidget {
required this.game,
required this.adsEnabled,
this.narrator,
this.ambience,
this.ads,
});
final Game game;
final bool adsEnabled;
final Narrator? narrator;
final Ambience? ambience;
final Ads? ads;
@override
State<Notebook> createState() => _NotebookState();
@@ -144,6 +149,8 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
late final Ads ads;
bool _foreground = true;
String _route = '';
Future<List<Book>>? _shelf;
String _shelfLanguage = '';
String get route =>
'${g.book.id}:${g.book.language}:${g.screen.name}:${g.screen == Screen.story ? g.storyIndex : g.campaignIndex}';
@override
@@ -151,7 +158,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
super.initState();
unawaited(initializeDateFormatting());
narrator = widget.narrator ?? Narrator(g.book.tracks);
ambience = Ambience();
ambience = widget.ambience ?? Ambience();
narrator.addListener(_syncAtmosphere);
ads = widget.ads ?? Ads(enabled: widget.adsEnabled);
g.addListener(_changed);
@@ -204,7 +211,13 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
Screen.reveal,
].contains(g.screen);
ambience.update(
scene: inStory && g.book.isMara ? soundscapeFor(g.campaignIndex) : null,
scene: !inStory
? null
: g.book.isMara
? soundscapeFor(g.campaignIndex)
: g.screen == Screen.story
? g.book.atmosphere[g.storyPage.id]
: null,
enabled: atmosphereEnabled,
foreground: _foreground,
narrating: narrator.playing || narrator.loading,
@@ -300,7 +313,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
Future<void> turn(int direction) async {
if (direction > 0 && g.storyIndex == g.book.pages.length - 1) {
crossToHarbour();
readingBreak(g.book.pages.length, crossToHarbour);
return;
}
final next = (g.storyIndex + direction).clamp(0, g.book.pages.length - 1);
@@ -318,7 +331,29 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
}
await prefs.set('notebook_read_ahead', true);
}
if (mounted && g.storyPage.id == pageId) g.showStory(next);
if (mounted && g.storyPage.id == pageId) {
if (direction > 0) {
readingBreak(next, () => g.showStory(next));
} else {
g.showStory(next);
}
}
}
void readingBreak(int position, VoidCallback next) {
final book = g.book;
final legacyLevels = book.continueAt == null
? 0
: book.levels.length - book.continueAt!;
final total = book.pages.length + legacyLevels * 3;
if (g.claimBookAd(position, total)) {
unawaited(narrator.stop());
ads.between(() {
if (mounted && g.book == book) next();
});
} else {
next();
}
}
void crossToHarbour() {
@@ -369,7 +404,10 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
void nextCampaign() {
final index = g.campaignIndex;
if (index == g.book.levels.length - 1) {
g.contents();
readingBreak(
g.book.pages.length + (index - (g.book.continueAt ?? 0) + 1) * 3,
g.contents,
);
return;
}
// Reading access is distinct from completed puzzles and their awards.
@@ -380,14 +418,10 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
if (mounted) g.briefing(index + 1);
}
if (g.recovered(index) &&
g.book.chapterLevels(g.book.chapterFor(index)).last == index &&
!prefs.flag('storyAd_$index')) {
unawaited(prefs.set('storyAd_$index', true));
ads.between(next);
} else {
next();
}
readingBreak(
g.book.pages.length + (index - (g.book.continueAt ?? 0) + 1) * 3,
next,
);
}
void back() {
@@ -395,64 +429,59 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
g.home();
return;
}
switch (g.screen) {
case Screen.story:
if (g.storyIndex > 0) {
turn(-1);
} else {
g.home();
}
case Screen.reveal:
g.startCampaign(g.campaignIndex);
case Screen.briefing:
g.contents();
case Screen.game:
if (g.mode == Mode.campaign) {
g.contents();
} else {
g.home();
}
case Screen.contents:
g.home();
case Screen.home:
break;
if (g.screen == Screen.library) return;
if (g.screen == Screen.home) {
// With a catalogue the shelf is the app's root; without one (widget tests
// and embedders) the book's home stays the first and last leaf.
if (g.library != null) g.showLibrary();
return;
}
g.home();
}
@override
Widget build(BuildContext context) => PopScope(
canPop: g.screen == Screen.home && !g.busy,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) back();
},
child: Scaffold(
body: Stack(
children: [
switch (g.screen) {
Screen.home => home(),
Screen.story => story(),
Screen.contents => contents(),
Screen.briefing => briefing(),
Screen.game => game(),
Screen.reveal => reveal(),
},
if (g.busy)
Positioned.fill(
child: ColoredBox(
color: cream.withValues(alpha: .9),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(color: blue),
writing('Finding a fresh pattern…'),
InkButton('Back to the notebook', g.home),
],
Widget build(BuildContext context) => PaperSkin(
// The shelf is shared by every book, so it uses the default notebook page
// rather than the paper of whichever book was last open.
asset: g.screen == Screen.library ? null : g.book.paperAsset,
child: PopScope(
canPop:
(g.screen == Screen.library ||
(g.screen == Screen.home && g.library == null)) &&
!g.busy,
onPopInvokedWithResult: (didPop, result) {
if (!didPop) back();
},
child: Scaffold(
body: Stack(
children: [
switch (g.screen) {
Screen.library => libraryScreen(),
Screen.home => home(),
Screen.story => story(),
Screen.contents => contents(),
Screen.briefing => briefing(),
Screen.game => game(),
Screen.reveal => reveal(),
},
if (g.busy)
Positioned.fill(
child: ColoredBox(
color: cream.withValues(alpha: .9),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const CircularProgressIndicator(color: blue),
writing('Finding a fresh pattern…'),
InkButton('Back to the notebook', g.home),
],
),
),
),
),
),
],
],
),
),
),
);
@@ -480,7 +509,233 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
),
);
Widget folio(String text) => writing(text, size: 15, color: blue);
/// The app's first window: every installed book on one shelf. Tapping a card
/// selects that story and lets `Game.restore()` reopen its saved leaf.
Widget libraryScreen() {
final catalogue = g.library;
if (catalogue == null) {
return Leaf(
key: const ValueKey('library'),
body: column([
folio('L U M E N / STORY LIBRARY'),
writing('No stories are installed.', size: 24, color: muted),
action('Back to the notebook', g.home),
]),
);
}
return Leaf(
key: const ValueKey('library'),
body: LayoutBuilder(
builder: (context, c) {
final compact = c.maxWidth < 480;
// Wide tablets get two columns of books; phones keep one dense column.
final width = math.min(c.maxWidth, 860.0);
final columns = width >= 720 ? 2 : 1;
return Center(
child: SizedBox(
width: width,
child: column([
folio('L U M E N / STORY LIBRARY'),
writing('Choose a book', size: compact ? 34 : 40),
writing(
'Open a book to continue where you left off.',
size: compact ? 19 : 23,
color: const Color(0xff70503b),
),
gap(compact ? 10 : 16),
FutureBuilder<List<Book>>(
future: shelfBooks(catalogue),
builder: (context, snapshot) {
if (snapshot.hasError) {
return column([
writing(
'This story could not be opened. Please try again.',
size: 21,
color: muted,
),
action(
'Try again',
() => setState(() {
_shelf = null;
_shelfLanguage = '';
}),
),
]);
}
final books = snapshot.data;
if (books == null) {
return const Padding(
padding: EdgeInsets.symmetric(vertical: 40),
child: Center(
child: CircularProgressIndicator(color: blue),
),
);
}
if (books.isEmpty) {
return writing(
'No stories are installed.',
size: 22,
color: muted,
);
}
final cards = [
for (final story in books)
bookCard(story, compact: compact),
];
if (columns == 1) return column(cards);
final cardWidth = (width - 14) / 2;
return Wrap(
spacing: 14,
runSpacing: 2,
children: [
for (final card in cards)
SizedBox(width: cardWidth, child: card),
],
);
},
),
gap(4),
action('Settings', settings),
]),
),
);
},
),
);
}
/// One book on the shelf: cover, translated metadata, progress and the way
/// back into the reader. A book with a saved leaf offers "Continue reading".
/// Phones get a denser card so several books fit the narrow notebook page.
Widget bookCard(Book story, {required bool compact}) {
final catalogue = g.library;
if (catalogue == null) return const SizedBox.shrink();
final store = g.store.forStory(story.id);
final restored = [
for (var i = 0; i < story.levels.length; i++)
store.flag('done_${story.puzzleKey(i)}'),
].where((done) => done).length;
final saved = store.text('flutter_screen', 'home');
final resuming = saved != Screen.home.name && saved != Screen.library.name;
final current = story.id == g.book.id;
final languages = catalogue
.definition(story.id)
.languages
.values
.map((entry) => entry['label'] as String? ?? '')
.where((label) => label.isNotEmpty)
.toList();
return Padding(
padding: EdgeInsets.symmetric(vertical: compact ? 5 : 7),
child: InkWell(
key: ValueKey('library-book-${story.id}'),
borderRadius: BorderRadius.circular(4),
onTap: () => unawaited(openStory(story.id)),
child: DecoratedBox(
decoration: BoxDecoration(
border: Border.all(
color: blue.withValues(alpha: current ? .55 : .18),
width: current ? 1.5 : 1,
),
borderRadius: BorderRadius.circular(4),
),
child: Padding(
padding: EdgeInsets.all(compact ? 9 : 12),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (story.coverAsset case final asset?)
Padding(
padding: EdgeInsets.only(right: compact ? 10 : 14),
child: SizedBox(
width: compact ? 54 : 78,
height: compact ? 74 : 104,
child: Image.asset(
asset,
fit: BoxFit.contain,
semanticLabel: story.title,
errorBuilder: (_, _, _) => const SizedBox.shrink(),
),
),
),
Expanded(
child: column([
writing(story.title, size: compact ? 23 : 29),
if (!compact && story.description.isNotEmpty)
writing(
story.description,
size: 20,
color: const Color(0xff70503b),
),
if (languages.length > 1)
writing(
languages.join(' · '),
size: compact ? 14 : 16,
color: muted,
),
if (story.levels.isNotEmpty)
writing(
'$restored / ${story.levels.length} memories restored',
size: compact ? 14 : 17,
color: muted,
),
writing(
resuming ? 'Continue reading' : 'Open this book ›',
size: compact ? 17 : 19,
color: blue,
),
]),
),
],
),
),
),
),
);
}
/// Loads the whole catalogue once per requested language. A broken package is
/// skipped so the rest of the shelf stays usable.
Future<List<Book>> shelfBooks(StoryLibrary catalogue) {
final language = g.requestedLanguage;
final cached = _shelf;
if (cached != null && _shelfLanguage == language) return cached;
_shelfLanguage = language;
return _shelf = () async {
final books = <Book>[];
for (final story in catalogue.stories) {
try {
books.add(await catalogue.open(story.id, language: language));
} catch (_) {
continue;
}
}
return books;
}();
}
/// Waits for the shelf before switching screens, so the home button never
/// flashes a loading state and its tap settles in one pump.
Future<void> openLibrary() async {
narrator.cancelAutoplay();
final catalogue = g.library;
if (catalogue == null) return;
await shelfBooks(catalogue);
if (mounted) g.showLibrary();
}
Future<void> openStory(String id) async {
narrator.cancelAutoplay();
try {
await g.selectStory(id);
} catch (_) {
if (mounted) toast('This story could not be opened. Please try again.');
}
}
Widget home() => ColoredBox(
key: ValueKey('home-${g.book.id}'),
color: cream,
child: SafeArea(
child: Column(
@@ -545,8 +800,8 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
g.openNotebook,
primary: true,
),
action('Story library', storyLibrary),
action('Language', chooseLanguage),
action('Story library', () => unawaited(openLibrary())),
action('Settings', settings),
if (g.requestedLanguage
.replaceAll('_', '-')
.split('-')
@@ -606,8 +861,6 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
() => unawaited(g.startPractice()),
),
action('How to play', rules),
action('Sound & atmosphere', soundSettings),
action('Privacy & advertising', privacy),
action(
'Open-source licenses',
() => showLicensePage(
@@ -630,62 +883,27 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
),
),
);
Future<void> storyLibrary() async {
narrator.cancelAutoplay();
final catalogue = g.library;
if (catalogue == null) return;
List<Book> books;
try {
books = await Future.wait(
catalogue.stories.map(
(story) => catalogue.open(story.id, language: g.requestedLanguage),
),
);
} catch (_) {
if (mounted) toast('This story could not be opened. Please try again.');
return;
}
if (!mounted) return;
final selected = await showDialog<String>(
context: context,
builder: (c) => AlertDialog(
title: const CopyText('Story library'),
content: SizedBox(
width: 520,
child: ListView(
shrinkWrap: true,
children: [
for (final story in books)
ListTile(
leading: story.coverAsset == null
? null
: Image.asset(story.coverAsset!, width: 56),
title: Text(story.title),
subtitle: Text(
'${story.description}\n${catalogue.definition(story.id).languages.values.map((v) => v['label']).join(' · ')}',
),
isThreeLine: true,
selected: story.id == g.book.id,
onTap: () => Navigator.pop(c, story.id),
),
],
Future<void> settings() => showDialog<void>(
context: context,
builder: (c) => SimpleDialog(
title: const CopyText('Settings'),
children: [
for (final option in <(String, VoidCallback)>[
('Language', chooseLanguage),
('Sound & atmosphere', soundSettings),
('Recording speed', speed),
('Privacy & advertising', privacy),
])
SimpleDialogOption(
onPressed: () {
Navigator.pop(c);
option.$2();
},
child: CopyText(option.$1),
),
),
actions: [
TextButton(
onPressed: () => Navigator.pop(c),
child: const CopyText('Close'),
),
],
),
);
if (selected == null || !mounted) return;
try {
await g.selectStory(selected);
} catch (_) {
if (mounted) toast('This story could not be opened. Please try again.');
}
}
],
),
);
Future<void> chooseLanguage() async {
narrator.cancelAutoplay();
@@ -770,20 +988,32 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
key: ValueKey('${g.book.id}-story-${p.id}'),
onTurn: (d) => turn(d),
body: column(body),
fittedBody: p.type == StoryPageType.puzzle
? fittedPuzzle(
details: [
areaPicker(),
...puzzleNotes(),
...heading,
if (p.text.isNotEmpty) reading(p.text),
if (p.continuation.isNotEmpty) reading(p.continuation),
if (g.book.tracks.containsKey(p.id)) audio(),
],
)
: null,
wideBody: p.type == StoryPageType.puzzle
? splitPuzzle(side: [...heading, areaPicker(), ...puzzleNotes()])
? splitPuzzle(
side: [
...heading,
if (p.text.isNotEmpty) reading(p.text, size: 18),
areaPicker(),
...puzzleNotes(),
],
)
: null,
footer: navigation(),
);
}
void bookmark() {
unawaited(
prefs.set('notebook_manual_bookmark', g.book.pages[g.storyIndex].id),
);
toast('A ribbon marks this page.');
}
Widget navigation() {
final wide = MediaQuery.orientationOf(context) == Orientation.landscape;
final previous = InkButton(
@@ -798,43 +1028,13 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
() => turn(1),
size: wide ? 19 : 21,
);
if (wide) {
// One row keeps the shorter landscape page free for the illustration
// and the puzzle beside it.
return Row(
children: [
Expanded(child: InkButton('Contents', g.contents, size: 19)),
const SizedBox(width: 6),
Expanded(child: InkButton('Bookmark', bookmark, size: 19)),
Expanded(child: InkButton('Language', chooseLanguage, size: 19)),
const SizedBox(width: 6),
Expanded(child: InkButton('Close book', g.home, size: 19)),
const SizedBox(width: 6),
Expanded(child: previous),
const SizedBox(width: 6),
Expanded(child: next),
],
);
}
return column([
Row(
children: [
Expanded(child: InkButton('Contents', g.contents, size: 20)),
const SizedBox(width: 6),
Expanded(child: InkButton('Bookmark', bookmark, size: 20)),
Expanded(child: InkButton('Language', chooseLanguage, size: 20)),
const SizedBox(width: 6),
Expanded(child: InkButton('Close book', g.home, size: 20)),
],
),
Row(
children: [
Expanded(child: previous),
const SizedBox(width: 8),
Expanded(child: next),
],
),
]);
return Row(
children: [
Expanded(child: previous),
const SizedBox(width: 8),
Expanded(child: next),
],
);
}
String get recordingId => g.screen == Screen.story
@@ -880,7 +1080,7 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
? 'Narrator · recorded story'
: g.book.isMara
? 'Mara · diary reading'
: '${g.book.tracks[recordingId]?['speaker'] ?? g.storyPage.speaker} · recorded story',
: '${narrator.page == recordingId ? narrator.speaker : g.book.tracks[recordingId]?['speaker'] ?? g.storyPage.speaker} · recorded story',
size: 17,
color: muted,
),
@@ -1012,8 +1212,8 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
body: column([
folio('${g.book.title.toUpperCase()} · CONTENTS'),
writing(g.book.levels[first].chapter, size: 34),
if (chapter == 0 && !g.book.isMara) ...[
reading(g.book.premise),
if (!g.book.isMara) ...[
if (chapter == 0) reading(g.book.premise),
action('Continue reading', g.openNotebook),
if (ribbon != null)
action(
@@ -1137,11 +1337,12 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
spacing: 6,
children: [
for (var i = 0; i < p.regions.length; i++)
InkButton(
'${p.regions[i].label} · ${List.generate(g.cells.length, (j) => j).where((j) => p.regions[i].contains(j, p.size) && g.cells[j] == 1).length} / ${p.regionTotal(p.regions[i])}',
() => g.area(i),
size: 21,
primary: i == g.selectedArea,
RegionButton(
key: ValueKey('area-$i'),
label:
'${p.regions[i].label} · ${List.generate(g.cells.length, (j) => j).where((j) => p.regions[i].contains(j, p.size) && g.cells[j] == 1).length} / ${p.regionTotal(p.regions[i])}',
onPressed: () => g.area(i),
selected: i == g.selectedArea,
),
],
),
@@ -1172,21 +1373,33 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
size: 18,
color: muted,
),
if (!g.won)
Row(
if (!g.won) InkButton('Hint · rewarded ad', hint, size: 22),
];
/// The board stays in the viewport while supporting text scrolls separately.
Widget fittedPuzzle({required List<Widget> details}) => LayoutBuilder(
builder: (context, constraints) {
final grid = math.min(
math.min(constraints.maxWidth, 350.0),
constraints.maxHeight * .70,
);
return Column(
children: [
SizedBox(
height: grid,
child: board(maxWidth: grid),
),
Expanded(
child: InkButton(
'Undo a mark',
g.canUndo ? g.undo : null,
size: 22,
child: SingleChildScrollView(
key: const ValueKey('puzzle-details-scroll'),
padding: const EdgeInsets.symmetric(horizontal: 4, vertical: 4),
child: column(details),
),
),
const SizedBox(width: 8),
Expanded(child: InkButton('Hint · rewarded ad', hint, size: 22)),
],
),
];
);
},
);
/// Landscape puzzle leaf: the grid keeps the left column at its full height
/// while the heading, area clues and notes sit beside it, so nothing has to
@@ -1284,6 +1497,15 @@ class _NotebookState extends State<Notebook> with WidgetsBindingObserver {
if (g.won) clarity,
for (final a in actions) action(a.label, a.run, primary: a.primary),
]),
fittedBody: fittedPuzzle(
details: [
areaPicker(),
...puzzleNotes(),
...heading(compact: true),
if (g.won) clarity,
for (final a in actions) action(a.label, a.run, primary: a.primary),
],
),
// Landscape: the heading and notes sit beside the grid while the leaf's
// actions stay in a compact bar that does not take room from the board.
wideBody: Column(