Files
asteria-qt/src/qml/components/MangaPageView.qml
T
2026-10-30 16:48:58 +01:00

223 lines
7.3 KiB
QML

import QtQuick
import QtQuick.Controls.Basic
import Asteria
Rectangle {
id: root
readonly property bool landscape: root.width > root.height
property bool contentReady: false
property int currentPair: 0
focus: true
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() { root.nextPair() }
function handlePageUp() { root.previousPair() }
function handleScrollDown() { root.nextPair() }
function handleScrollUp() { root.previousPair() }
function nextPair() {
if (root.currentPair < ReadingModel.totalPagePairs - 1)
root.showPair(root.currentPair + 1)
}
function previousPair() {
if (root.currentPair > 0)
root.showPair(root.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) {
root.currentPair = pairIndex
pageArea.pair = root.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()
// Initialise landscape state once visible.
Component.onCompleted: ReadingModel.setMangaLandscape(root.landscape)
Timer {
id: landscapeDebounce
interval: 150
repeat: false
onTriggered: ReadingModel.setMangaLandscape(root.landscape)
}
// Page display area.
Item {
id: pageArea
// 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
anchors {
fill: parent
margins: ReadingPreferences.verticalMargin
}
Behavior on opacity { NumberAnimation { duration: 120 } }
// Left (or solo) page.
Image {
id: leftImg
x: 0
y: 0
width: pageArea.hasTwoPages ? parent.width / 2 : parent.width
height: parent.height
visible: pageArea.leftChunk >= 0
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
sourceSize: Qt.size(width, height)
source: pageArea.leftChunk >= 0 ? ReadingModel.imageUrlForChunk(pageArea.leftChunk) : ""
}
// Right page (landscape two-page spread only).
Image {
id: rightImg
x: parent.width / 2
y: 0
width: parent.width / 2
height: parent.height
visible: pageArea.rightChunk >= 0
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
sourceSize: Qt.size(width, height)
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
property real pressX: 0
property real startPressX: 0
property bool dragging: false
anchors.fill: parent
z: 10
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) root.nextPair(); else root.previousPair()
} else {
if (dx > 0) root.nextPair(); else root.previousPair()
}
return
}
if (dragging) return
// Tap zones — only if not a swipe.
clickDebounce.clickX = mouse.x
clickDebounce.restart()
}
onDoubleClicked: {
clickDebounce.stop()
if (root.Window.window) {
root.Window.window.visibility = (root.Window.window.visibility === Window.FullScreen)
? Window.Windowed : Window.FullScreen
}
}
}
Timer {
id: clickDebounce
property real clickX: 0
interval: 150
onTriggered: {
const third = root.width / 3
if (clickX < third) {
// Left third tap.
if (ReadingModel.mangaRTL) root.nextPair(); else root.previousPair()
} else if (clickX > root.width - third) {
// Right third tap.
if (ReadingModel.mangaRTL) root.previousPair(); else root.nextPair()
} else {
// Middle — toggle chrome.
UiState.barsVisible = !UiState.barsVisible
}
}
}
// Restore position when a new book is loaded.
Connections {
target: ReadingModel
function onRestoreReady(chunkIndex) {
root.contentReady = true
root.showPair(ReadingModel.pairIndexForChunk(chunkIndex))
}
function onTotalPagePairsChanged() {
// Re-clamp after pair rebuild (resize / orientation flip).
root.showPair(Math.min(root.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.
root.showPair(newPairIndex)
}
}
Connections {
target: Book
function onLoadingChanged() {
if (Book.loading) root.contentReady = false
}
}
}