225 lines
7.3 KiB
QML
225 lines
7.3 KiB
QML
import QtQuick
|
|
import QtQuick.Controls
|
|
import Asteria
|
|
|
|
Rectangle {
|
|
focus: true
|
|
|
|
// Width reserved for the vertical scrollbar on the right edge.
|
|
readonly property int scrollbarWidth: 18
|
|
|
|
property bool contentReady: false
|
|
property bool resizing: false
|
|
property bool restoreReady: false
|
|
property int initialIndex: -1
|
|
property int initialOffset: -1
|
|
|
|
// Animation for smooth scrolling
|
|
property Animation scrollAnimation: PropertyAnimation {
|
|
target: chunkListView
|
|
property: "contentY"
|
|
duration: 200
|
|
easing.type: Easing.InOutQuad
|
|
}
|
|
|
|
// Public methods for global keyboard navigation
|
|
function handlePageUp() {
|
|
var scrollAmount = chunkListView.height * 0.95;
|
|
var targetY = Math.max(0, chunkListView.contentY - scrollAmount);
|
|
animateScrollTo(targetY);
|
|
}
|
|
|
|
function handlePageDown() {
|
|
var scrollAmount = chunkListView.height * 0.95;
|
|
var maxY = Math.max(0, chunkListView.contentHeight - chunkListView.height);
|
|
var targetY = Math.min(maxY, chunkListView.contentY + scrollAmount);
|
|
animateScrollTo(targetY);
|
|
}
|
|
|
|
function handleScrollUp() {
|
|
var lineHeight = readingPreferences.fontSize * readingPreferences.lineSpacing;
|
|
var scrollAmount = lineHeight * 3;
|
|
var targetY = Math.max(0, chunkListView.contentY - scrollAmount);
|
|
animateScrollTo(targetY);
|
|
}
|
|
|
|
function handleScrollDown() {
|
|
var lineHeight = readingPreferences.fontSize * readingPreferences.lineSpacing;
|
|
var scrollAmount = lineHeight * 3;
|
|
var maxY = Math.max(0, chunkListView.contentHeight - chunkListView.height);
|
|
var targetY = Math.min(maxY, chunkListView.contentY + scrollAmount);
|
|
animateScrollTo(targetY);
|
|
}
|
|
|
|
function animateScrollTo(targetY) {
|
|
scrollAnimation.stop();
|
|
scrollAnimation.to = targetY;
|
|
scrollAnimation.start();
|
|
}
|
|
|
|
gradient: Gradient {
|
|
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
|
|
GradientStop { position: 1.0; color: ThemeManager.currentTheme.gradientBottom }
|
|
}
|
|
|
|
function updateContentWidth() {
|
|
book.setContentWidth(chunkListView.width - scrollbarWidth - 2 * readingPreferences.horizontalMargin)
|
|
}
|
|
|
|
Component.onCompleted: updateContentWidth()
|
|
onWidthChanged: {
|
|
resizing = true;
|
|
remeasureTimer.restart()
|
|
}
|
|
|
|
Timer {
|
|
id: remeasureTimer
|
|
interval: 150
|
|
repeat: false
|
|
onTriggered: {
|
|
updateContentWidth()
|
|
resizing = false;
|
|
}
|
|
}
|
|
|
|
ListView {
|
|
id: chunkListView
|
|
anchors.fill: parent
|
|
anchors.topMargin: readingPreferences.verticalMargin
|
|
anchors.bottomMargin: readingPreferences.verticalMargin
|
|
model: contentReady ? readingModel : null
|
|
cacheBuffer: 20
|
|
clip: true
|
|
maximumFlickVelocity: 99999
|
|
|
|
delegate: Item {
|
|
id: chunkDelegate
|
|
readonly property int chunkIdx: model.index
|
|
width: chunkListView.width
|
|
height: resizing || readingModel.measuring ? contentText.implicitHeight : model.pixelHeight
|
|
|
|
Text {
|
|
visible: isDebugBuild
|
|
leftPadding: 0
|
|
text: model.index
|
|
color: "#FFFFFF"
|
|
height: 0
|
|
}
|
|
Text {
|
|
id: contentText
|
|
width: parent.width - scrollbarWidth
|
|
leftPadding: readingPreferences.horizontalMargin
|
|
rightPadding: readingPreferences.horizontalMargin
|
|
wrapMode: Text.WrapAtWordBoundaryOrAnywhere
|
|
textFormat: Text.RichText
|
|
font.pixelSize: readingPreferences.fontSize
|
|
lineHeight: readingPreferences.lineSpacing
|
|
color: ThemeManager.currentTheme.readingText
|
|
text: model.content
|
|
visible: model.content !== ""
|
|
onImplicitHeightChanged: {
|
|
if (isDebugBuild && model.pixelHeight > 0 && implicitHeight > 0) {
|
|
const diff = model.pixelHeight - implicitHeight
|
|
if (Math.abs(diff) > 1) {
|
|
console.warn("Chunk", model.index,
|
|
"height mismatch: measured=" + model.pixelHeight,
|
|
"actual=" + Math.round(implicitHeight),
|
|
"diff=" + Math.round(diff))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
onContentYChanged: {
|
|
scrollTrackTimer.restart()
|
|
}
|
|
|
|
ScrollBar.vertical: ScrollBar {
|
|
policy: ScrollBar.AsNeeded
|
|
padding: 0
|
|
contentItem: Rectangle {
|
|
implicitWidth: scrollbarWidth
|
|
radius: 3
|
|
color: ThemeManager.currentTheme.scrollbarThumb
|
|
}
|
|
background: Rectangle {
|
|
implicitWidth: scrollbarWidth
|
|
color: ThemeManager.currentTheme.scrollbarTrack
|
|
}
|
|
}
|
|
|
|
MouseArea {
|
|
anchors.fill: parent
|
|
anchors.rightMargin: scrollbarWidth
|
|
z: 1
|
|
onClicked: clickDebounce.restart()
|
|
onDoubleClicked: {
|
|
clickDebounce.stop()
|
|
window.visibility = (window.visibility === Window.FullScreen) ? Window.Windowed : Window.FullScreen
|
|
}
|
|
}
|
|
|
|
Timer {
|
|
id: clickDebounce
|
|
interval: 150
|
|
onTriggered: {
|
|
window.barsVisible = !window.barsVisible
|
|
uiState.sidebarOpen = !uiState.sidebarOpen
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
Timer {
|
|
id: scrollTrackTimer
|
|
interval: 300
|
|
repeat: false
|
|
onTriggered: {
|
|
if (book.hasBook && contentReady && restoreReady) {
|
|
var topIdx = chunkListView.indexAt(0, chunkListView.contentY)
|
|
if (topIdx < 0) topIdx = 0
|
|
var topItem = chunkListView.itemAtIndex(topIdx)
|
|
var intraOffset = topItem ? (chunkListView.contentY - topItem.y) : 0
|
|
readingModel.setScrollInfo(topIdx, chunkListView.visibleArea.yPosition)
|
|
book.saveCurrentPosition(topIdx, intraOffset)
|
|
}
|
|
}
|
|
}
|
|
|
|
Connections {
|
|
target: book
|
|
function onLoadingChanged() {
|
|
if (book.loading)
|
|
contentReady = false
|
|
}
|
|
}
|
|
|
|
Connections {
|
|
target: readingModel
|
|
function onRestoreReady(chunkIndex, intraOffset) {
|
|
contentReady = true
|
|
initialIndex = chunkIndex
|
|
initialOffset = intraOffset
|
|
restoreTimer.start()
|
|
}
|
|
function onScrollToChunkRequested(chunkIndex) {
|
|
chunkListView.positionViewAtIndex(chunkIndex, ListView.Beginning)
|
|
}
|
|
}
|
|
|
|
Timer {
|
|
id: restoreTimer
|
|
interval: 1
|
|
repeat: false
|
|
onTriggered: {
|
|
if (isDebugBuild)
|
|
console.info("Restoring contentHeight:", chunkListView.contentHeight, "originY:", chunkListView.originY, "chunkIndex:", initialIndex, "intraOffset:", initialOffset)
|
|
chunkListView.positionViewAtIndex(initialIndex, ListView.Beginning)
|
|
chunkListView.contentY += initialOffset
|
|
restoreReady = true
|
|
}
|
|
}
|
|
|
|
}
|