diff --git a/CLAUDE.md b/CLAUDE.md index 6ad8178..4602b9b 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -278,4 +278,4 @@ All colors are defined in `src/qml/Theme.qml` (a QML singleton registered via `q - `QSettings` for persistent state - Qt naming: `camelCase` properties/methods, `PascalCase` QML component names - `Q_INVOKABLE` for QML-callable methods; signals for C++→QML notifications -- **Never use timers or `Qt.callLater` to wait for state to settle** — always use signals (`onRunningChanged`, `onCompleted`, etc.) or direct synchronous calls. Timeouts are fragile and mask real sequencing problems. +- **Never use timers to wait for state to settle** — always use signals (`onRunningChanged`, `onCompleted`, etc.) or direct synchronous calls. Timeouts are fragile and mask real sequencing problems. diff --git a/src/qml/components/PageView.qml b/src/qml/components/PageView.qml index 41e68c2..5f4d5b4 100644 --- a/src/qml/components/PageView.qml +++ b/src/qml/components/PageView.qml @@ -95,9 +95,12 @@ Rectangle { } } + property bool _restoring: false + Connections { target: bookController function onBookReady() { + _restoring = true var pos = appState.loadBookPosition(bookController.currentFilePath) var savedChapter = pos["chapter"] var savedScrollIndex = pos["scrollIndex"] @@ -105,13 +108,22 @@ Rectangle { if (savedChapter > 0) readingModel.goToChapter(savedChapter) - if (savedScrollIndex > 0) - Qt.callLater(function() { + + Qt.callLater(function() { + if (savedScrollIndex > 0) { chunkListView.positionViewAtIndex(savedScrollIndex, ListView.Beginning) chunkListView.contentY += savedScrollOffset - }) - Qt.callLater(function() { + } readingModel.setViewportSize(chunkListView.width, chunkListView.height) + if (savedScrollIndex > 0) { + var idx = chunkListView.indexAt(0, chunkListView.contentY) + if (idx >= 0) { + var item = chunkListView.itemAtIndex(idx) + var offset = item ? chunkListView.contentY - item.y : 0 + readingModel.setScrollInfo(idx, offset, chunkListView.atYEnd) + } + } + _restoring = false }) } } @@ -119,9 +131,11 @@ Rectangle { Connections { target: readingModel function onChapterChanged() { - Qt.callLater(function() { - readingModel.setViewportSize(chunkListView.width, chunkListView.height) - }) + if (!_restoring) { + Qt.callLater(function() { + readingModel.setViewportSize(chunkListView.width, chunkListView.height) + }) + } } } }