142 lines
4.5 KiB
QML
142 lines
4.5 KiB
QML
import QtQuick
|
|
import QtQuick.Controls
|
|
import Asteria
|
|
|
|
Rectangle {
|
|
focus: true
|
|
|
|
Keys.onLeftPressed: readingModel.previousChapter()
|
|
Keys.onRightPressed: readingModel.nextChapter()
|
|
|
|
gradient: Gradient {
|
|
GradientStop { position: 0.0; color: Theme.gradientTop }
|
|
GradientStop { position: 1.0; color: Theme.gradientBottom }
|
|
}
|
|
|
|
ListView {
|
|
id: chunkListView
|
|
anchors.fill: parent
|
|
anchors.leftMargin: 12
|
|
model: bookController.hasBook ? readingModel : null
|
|
cacheBuffer: 2000
|
|
clip: true
|
|
|
|
delegate: Text {
|
|
width: ListView.view.width - 18
|
|
rightPadding: 2
|
|
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
|
textFormat: Text.RichText
|
|
font.pixelSize: 16
|
|
lineHeight: 1.6
|
|
color: Theme.readingText
|
|
text: model.content
|
|
}
|
|
|
|
onContentYChanged: {
|
|
scrollTrackTimer.restart()
|
|
if (!viewportSizeTimer.running) {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
onWidthChanged: viewportSizeTimer.restart()
|
|
onHeightChanged: viewportSizeTimer.restart()
|
|
|
|
ScrollBar.vertical: ScrollBar {
|
|
policy: ScrollBar.AsNeeded
|
|
padding: 0
|
|
contentItem: Rectangle {
|
|
implicitWidth: 18
|
|
radius: 3
|
|
color: Theme.scrollbarThumb
|
|
}
|
|
background: Rectangle {
|
|
implicitWidth: 18
|
|
color: Theme.scrollbarTrack
|
|
}
|
|
}
|
|
|
|
// Placeholder when no book is open
|
|
Text {
|
|
anchors.centerIn: parent
|
|
visible: !bookController.hasBook
|
|
text: "Open a book to start reading"
|
|
font.pixelSize: 16
|
|
color: Theme.readingText
|
|
}
|
|
}
|
|
|
|
Timer {
|
|
id: viewportSizeTimer
|
|
interval: 50
|
|
repeat: false
|
|
onTriggered: readingModel.setViewportSize(chunkListView.width, chunkListView.height)
|
|
}
|
|
|
|
Timer {
|
|
id: scrollTrackTimer
|
|
interval: 300
|
|
repeat: false
|
|
onTriggered: {
|
|
var idx = chunkListView.indexAt(0, chunkListView.contentY)
|
|
appState.lastScrollIndex = idx
|
|
var item = chunkListView.itemAtIndex(idx)
|
|
var offset = item ? chunkListView.contentY - item.y : 0
|
|
appState.lastScrollOffset = offset
|
|
appState.lastScrollPosition = chunkListView.contentY
|
|
if (bookController.hasBook)
|
|
appState.saveBookPosition(bookController.currentFilePath,
|
|
readingModel.currentChapter, idx, offset)
|
|
}
|
|
}
|
|
|
|
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"]
|
|
var savedScrollOffset = pos["scrollOffset"]
|
|
|
|
if (savedChapter > 0)
|
|
readingModel.goToChapter(savedChapter)
|
|
|
|
Qt.callLater(function() {
|
|
if (savedScrollIndex > 0) {
|
|
chunkListView.positionViewAtIndex(savedScrollIndex, ListView.Beginning)
|
|
chunkListView.contentY += savedScrollOffset
|
|
}
|
|
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
|
|
})
|
|
}
|
|
}
|
|
|
|
Connections {
|
|
target: readingModel
|
|
function onChapterChanged() {
|
|
if (!_restoring) {
|
|
Qt.callLater(function() {
|
|
readingModel.setViewportSize(chunkListView.width, chunkListView.height)
|
|
})
|
|
}
|
|
}
|
|
}
|
|
}
|