Remove Book and Close Book menu items and auto-close on navigation

Simplify sidebar navigation by removing the conditional Book and Close Book
items so users no longer need to manage a book left open in the background.

Because there is no longer a Book item to return to the reader, navigating
away from the reader to History or Custom Theme now flushes any pending
scroll position and closes the active or in-flight book. Opening the Settings
popup still keeps the book open behind the overlay, and clicking Files keeps
the current book open until a file is actually selected so cancelling the
file dialog preserves the current reading session.

This also fixes book-lifecycle edge cases exposed by closing on navigation:
- Cancel in-flight async loads in Book::close() and clear loading state so
  closing mid-load does not jump back to the reader or stall the spinner.
- Emit willCloseBook before resetting the reader so AppCoordinator snapshots
  final progress into HistoryModel and immediately clears UiState.lastFilePath.
- Mark Book as loading before clearing the outgoing reader when opening a new
  file to prevent HistoryPage from flashing between books, and fall back to
  History on load errors.
- Reset ScrollPageView and MangaPageView state when a book closes, and always
  navigate to the reader on restoreReady so CLI-opened files are not hidden
  behind a previously saved History page.
- Add coverage in BookTest and AppCoordinatorTest and document the behavior
  in AGENTS.md.
This commit is contained in:
Agel committed 2026-11-13 09:45:09 +01:00
1 parent 5940d86ed2
commit 5d96fbb0bb
12 files changed
+300 -46

No files matched your search

+134
View File
@@ -0,0 +1,134 @@
#include <QtTest>
#include <QDir>
#include <QFile>
#include <QImage>
#include <QSettings>
#include <QTemporaryDir>
#include <QTemporaryFile>
#include "backend/AppCoordinator.h"
#include "backend/AppState.h"
#include "backend/HistoryModel.h"
#include "backend/IFileDialogService.h"
#include "backend/ReadingModel.h"
#include "backend/book/Book.h"
#include "backend/formats/FormatRegistry.h"
#include "backend/formats/sources/DirByteSource.h"
#include "backend/state/BookPositionStore.h"
#include "backend/state/ReadingPreferences.h"
#include "backend/state/UiState.h"
class FakeDialogService : public IFileDialogService {
Q_OBJECT
public:
explicit FakeDialogService(QObject *parent = nullptr) : IFileDialogService(parent) {}
void open(const QStringList &) override {}
};
class AppCoordinatorTest : public QObject {
Q_OBJECT
private:
static QString createSampleComic(QTemporaryDir &tmp, int pages = 4)
{
if (!tmp.isValid()) return {};
for (int i = 1; i <= pages; ++i) {
QImage img(8, 8, QImage::Format_RGB888);
img.fill(Qt::blue);
img.save(tmp.filePath(QStringLiteral("page%1.png").arg(i, 2, 10, QLatin1Char('0'))), "PNG");
}
return tmp.path();
}
struct Ctx {
QTemporaryFile settingsFile;
std::unique_ptr<QSettings> settings;
std::unique_ptr<AppState> appState;
std::unique_ptr<ReadingPreferences> prefs;
std::unique_ptr<UiState> uiState;
std::unique_ptr<BookPositionStore> positions;
std::unique_ptr<FormatRegistry> registry;
std::unique_ptr<HistoryModel> history;
std::unique_ptr<FakeDialogService> dialog;
std::unique_ptr<Book> book;
std::unique_ptr<ReadingModel> readingModel;
std::unique_ptr<AppCoordinator> coordinator;
Ctx()
{
settingsFile.setFileTemplate(QDir::tempPath() + QStringLiteral("/coord-XXXXXX.ini"));
(void)settingsFile.open();
settingsFile.close();
settings = std::make_unique<QSettings>(settingsFile.fileName(), QSettings::IniFormat);
appState = std::make_unique<AppState>(*settings);
prefs = std::make_unique<ReadingPreferences>(*appState);
uiState = std::make_unique<UiState>(*appState);
positions = std::make_unique<BookPositionStore>(*appState);
registry = std::make_unique<FormatRegistry>(QList<FormatSpec>{
FormatSpec::comic({}, /*matchesDirectory=*/true, QStringLiteral("dir"),
[] { return std::make_unique<DirByteSource>(); }),
FormatSpec::txt(QStringLiteral("txt")),
});
history = std::make_unique<HistoryModel>(*registry);
dialog = std::make_unique<FakeDialogService>();
book = std::make_unique<Book>(*dialog, *registry);
readingModel = std::make_unique<ReadingModel>(*positions);
coordinator = std::make_unique<AppCoordinator>(
*book, *readingModel, *history, *uiState, *prefs, *appState, *settings);
}
};
private slots:
void closeBookSnapshotsHistoryAndClearsLastFilePath()
{
QTemporaryDir tmp;
const QString path = createSampleComic(tmp);
QVERIFY(!path.isEmpty());
Ctx ctx;
ctx.book->openFromPath(path);
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
QCOMPARE(ctx.uiState->lastFilePath(), path);
QCOMPARE(ctx.history->rowCount(), 1);
ctx.readingModel->setScrollInfo(2, 0.75);
ctx.book->close();
QVERIFY(!ctx.book->hasBook());
QCOMPARE(ctx.uiState->lastFilePath(), QString());
QCOMPARE(ctx.readingModel->progress(), 0.0);
QCOMPARE(ctx.readingModel->totalPages(), 0);
QCOMPARE(ctx.history->rowCount(), 1);
QCOMPARE(ctx.history->filePath(0), path);
QVERIFY(qFuzzyCompare(ctx.history->progress(0), 0.75));
}
void openingNewBookSnapshotsPreviousAndSwitchesLastFilePath()
{
QTemporaryDir tmp1;
QTemporaryDir tmp2;
const QString path1 = createSampleComic(tmp1);
const QString path2 = createSampleComic(tmp2);
QVERIFY(!path1.isEmpty());
QVERIFY(!path2.isEmpty());
Ctx ctx;
ctx.book->openFromPath(path1);
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook(), 5000);
ctx.readingModel->setScrollInfo(1, 0.5);
ctx.book->openFromPath(path2);
QTRY_VERIFY_WITH_TIMEOUT(ctx.book->hasBook() && ctx.book->filePath() == path2, 5000);
QCOMPARE(ctx.uiState->lastFilePath(), path2);
QCOMPARE(ctx.history->rowCount(), 2);
QCOMPARE(ctx.history->filePath(0), path2);
QCOMPARE(ctx.history->filePath(1), path1);
QVERIFY(qFuzzyCompare(ctx.history->progress(1), 0.5));
}
};
QTEST_MAIN(AppCoordinatorTest)
#include "AppCoordinatorTest.moc"