Files
asteria-qt/src/qml/components/MangaPageView.qml
T
2026-10-25 19:59:23 +01:00

213 lines
6.9 KiB
QML

import QtQuick
import QtQuick.Controls
import Asteria
Rectangle {
id: root
focus: true
property bool contentReady: false
property int currentPair: 0
readonly property bool landscape: width > height
gradient: Gradient {
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
GradientStop { position: 1.0; color: ThemeManager.currentTheme.gradientBottom }
}
// Keyboard navigation — same names as ScrollPageView so PageView can forward blindly.
function handlePageDown() { nextPair() }
function handlePageUp() { previousPair() }
function handleScrollDown() { nextPair() }
function handleScrollUp() { previousPair() }
function nextPair() {
if (currentPair < readingModel.totalPagePairs - 1)
showPair(currentPair + 1)
}
function previousPair() {
if (currentPair > 0)
showPair(currentPair - 1)
}
// Single entry point for displaying a pair: sets the index, refreshes the
// displayed pair contents, and syncs scroll/position state.
function showPair(pairIndex) {
currentPair = pairIndex
pageArea.pair = contentReady ? readingModel.pagePair(pairIndex) : []
const pair = pageArea.pair
if (pair.length === 0) return
const firstChunk = pair[0]
// Keep scroll model in sync so Footer page counter and chapter tracking work.
readingModel.setScrollInfo(firstChunk, pairIndex / Math.max(1, readingModel.totalPagePairs - 1))
}
// Notify model when orientation changes (triggers pair rebuild).
onLandscapeChanged: landscapeDebounce.restart()
Timer {
id: landscapeDebounce
interval: 150
repeat: false
onTriggered: readingModel.setMangaLandscape(landscape)
}
// Initialise landscape state once visible.
Component.onCompleted: readingModel.setMangaLandscape(landscape)
// Page display area.
Item {
id: pageArea
anchors.fill: parent
anchors.margins: readingPreferences.verticalMargin
// Display pair, updated by showPair(). For RTL mode pages are reversed
// (higher index on the left) via leftChunk/rightChunk below.
property var pair: []
property int leftChunk: {
if (pair.length === 0) return -1
if (pair.length === 1) return pair[0]
return readingModel.mangaRTL ? pair[1] : pair[0]
}
property int rightChunk: {
if (pair.length < 2) return -1
return readingModel.mangaRTL ? pair[0] : pair[1]
}
property bool hasTwoPages: pair.length === 2
Behavior on opacity { NumberAnimation { duration: 120 } }
// Left (or solo) page.
Image {
id: leftImg
visible: pageArea.leftChunk >= 0
x: 0
y: 0
width: pageArea.hasTwoPages ? parent.width / 2 : parent.width
height: parent.height
fillMode: Image.PreserveAspectFit
// When paired, pin content to the spine (right edge) so there's no gap between pages.
horizontalAlignment: pageArea.hasTwoPages ? Image.AlignRight : Image.AlignHCenter
asynchronous: true
cache: true
source: pageArea.leftChunk >= 0 ? readingModel.imageUrlForChunk(pageArea.leftChunk) : ""
}
// Right page (landscape two-page spread only).
Image {
id: rightImg
visible: pageArea.rightChunk >= 0
x: parent.width / 2
y: 0
width: parent.width / 2
height: parent.height
fillMode: Image.PreserveAspectFit
// Pin content to the spine (left edge) so it meets the left page with no gap.
horizontalAlignment: Image.AlignLeft
asynchronous: true
cache: true
source: pageArea.rightChunk >= 0 ? readingModel.imageUrlForChunk(pageArea.rightChunk) : ""
}
// Loading indicator while images load.
BusyIndicator {
anchors.centerIn: parent
running: (leftImg.status === Image.Loading) || (rightImg.visible && rightImg.status === Image.Loading)
visible: running
z: 5
}
}
// Swipe / tap input.
MouseArea {
id: inputArea
anchors.fill: parent
z: 10
property real pressX: 0
property real startPressX: 0
property bool dragging: false
onPressed: function(mouse) {
pressX = mouse.x
startPressX = mouse.x
dragging = false
}
onPositionChanged: function(mouse) {
if (Math.abs(mouse.x - startPressX) > 8)
dragging = true
}
onReleased: function(mouse) {
const dx = mouse.x - startPressX
if (Math.abs(dx) > root.width * 0.12) {
// Swipe gesture.
if (readingModel.mangaRTL) {
if (dx < 0) nextPair(); else previousPair()
} else {
if (dx > 0) nextPair(); else previousPair()
}
return
}
if (dragging) return
// Tap zones — only if not a swipe.
clickDebounce.clickX = mouse.x
clickDebounce.restart()
}
onDoubleClicked: {
clickDebounce.stop()
window.visibility = (window.visibility === Window.FullScreen) ? Window.Windowed : Window.FullScreen
}
}
Timer {
id: clickDebounce
interval: 150
property real clickX: 0
onTriggered: {
const third = root.width / 3
if (clickX < third) {
// Left third tap.
if (readingModel.mangaRTL) nextPair(); else previousPair()
} else if (clickX > root.width - third) {
// Right third tap.
if (readingModel.mangaRTL) previousPair(); else nextPair()
} else {
// Middle — toggle chrome.
window.toggleChrome()
}
}
}
// Restore position when a new book is loaded.
Connections {
target: readingModel
function onRestoreReady(chunkIndex) {
contentReady = true
showPair(readingModel.pairIndexForChunk(chunkIndex))
}
function onTotalPagePairsChanged() {
// Re-clamp after pair rebuild (resize / orientation flip).
showPair(Math.min(currentPair, Math.max(0, readingModel.totalPagePairs - 1)))
}
function onPagePairsRebuilt(newPairIndex) {
// setPageShift rebuilt the pairs; refresh even when the index is
// unchanged since the pair contents may differ.
showPair(newPairIndex)
}
}
Connections {
target: book
function onLoadingChanged() {
if (book.loading) contentReady = false
}
}
}