Files
asteria-qt/src/qml/Main.qml
T
2026-04-11 22:22:14 +02:00

287 lines
8.1 KiB
QML

import QtQuick
import QtQuick.Controls
import Asteria
import QtQml
ApplicationWindow {
id: window
width: 1024
height: 768
opacity: 0
visible: true
title: "Asteria"
color: ThemeManager.currentTheme.backgroundDeepest
FontLoader {
id: materialIcons
source: "qrc:/Asteria/src/assets/MaterialIconsOutlined-Regular.otf"
}
property bool sidebarOpen: uiState.sidebarOpen
onSidebarOpenChanged: uiState.sidebarOpen = sidebarOpen
property string currentPage: "starter"
property bool barsVisible: true
// Set to true on the first book open at startup so we restore the last page.
property bool _startupRestore: false
property bool _bookInitializing: false
onCurrentPageChanged: uiState.lastPage = currentPage
Component.onCompleted: {
width = uiState.windowWidth
height = uiState.windowHeight
if (uiState.windowX >= 0 && uiState.windowY >= 0) {
x = uiState.windowX
y = uiState.windowY
}
if (uiState.themeName !== "")
ThemeManager.currentThemeName = uiState.themeName
}
Connections {
target: uiState
function onThemeNameChanged() {
ThemeManager.currentThemeName = uiState.themeName
}
}
Connections {
target: book
function onLoadingChanged() {
if (book.loading) window._bookInitializing = true
}
function onErrorMessageChanged() {
if (book.errorMessage !== "") window._bookInitializing = false
}
}
Connections {
target: readingModel
function onMeasuringChanged() {
if (!readingModel.measuring) window._bookInitializing = false
}
}
Timer {
id: geometrySaveTimer
interval: 250
repeat: false
onTriggered: {
uiState.windowWidth = window.width
uiState.windowHeight = window.height
uiState.windowX = window.x
uiState.windowY = window.y
}
}
onWidthChanged: geometrySaveTimer.restart()
onHeightChanged: geometrySaveTimer.restart()
onXChanged: geometrySaveTimer.restart()
onYChanged: geometrySaveTimer.restart()
Row {
anchors.fill: parent
SideBar {
id: sidebar
height: parent.height
sidebarOpen: window.sidebarOpen
onCloseDrawer: window.sidebarOpen = false
onNavigateTo: function(page) {
window.currentPage = page
}
onOpenFile: {
book.browseAndOpen()
}
onCloseBook: {
book.close()
}
}
Column {
width: parent.width - sidebar.width
height: parent.height
Header {
id: header
width: parent.width
sidebarOpen: window.sidebarOpen
currentPage: window.currentPage
barsVisible: window.currentPage !== "reader" || window.barsVisible
onMenuClicked: window.sidebarOpen = true
}
Item {
id: contentArea
width: parent.width
height: parent.height - header.height - (footerLoader.active ? ThemeManager.footerHeight : 0)
StarterPage {
anchors.fill: parent
visible: window.currentPage === "starter"
}
ReaderPage {
id: readerPage
anchors.fill: parent
visible: window.currentPage === "reader"
}
SettingsPage {
anchors.fill: parent
visible: window.currentPage === "settings"
}
HistoryPage {
anchors.fill: parent
visible: window.currentPage === "history"
}
BusyIndicator {
anchors.centerIn: parent
running: window._bookInitializing
visible: running
z: 10
}
}
Loader {
id: footerLoader
width: parent.width
active: window.currentPage === "reader" && window.barsVisible
sourceComponent: Footer {}
}
}
}
ErrorPopup {
anchors.fill: parent
z: 3000
}
// Navigate once measurement (and position restoration) is done
Connections {
target: readingModel
function onRestoreReady(chunkIndex, intraOffset) {
if (!book.hasBook) return
if (window._startupRestore) {
window._startupRestore = false
var savedPage = uiState.lastPage
if (savedPage === "history" || savedPage === "settings")
window.currentPage = savedPage
else
window.currentPage = "reader"
} else {
window.currentPage = "reader"
}
}
}
Connections {
target: book
function onBookChanged() {
if (!book.hasBook && window.currentPage === "reader")
window.currentPage = "starter"
}
}
// Global keyboard shortcuts - work regardless of focus
Shortcut {
sequence: "F"
context: Qt.ApplicationShortcut
onActivated: window.visibility = (window.visibility === Window.FullScreen) ? Window.Windowed : Window.FullScreen
}
Shortcut {
sequence: "F11"
context: Qt.ApplicationShortcut
onActivated: window.visibility = (window.visibility === Window.FullScreen) ? Window.Windowed : Window.FullScreen
}
Shortcut {
sequence: "Up"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handleScrollUp();
}
}
}
Shortcut {
sequence: "Down"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handleScrollDown();
}
}
}
Shortcut {
sequence: "PgUp"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handlePageUp();
}
}
}
Shortcut {
sequence: "PgDown"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handlePageDown();
}
}
}
Shortcut {
sequence: "Prior"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handlePageUp();
}
}
}
Shortcut {
sequence: "Next"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage?.pageView) {
readerPage.pageView.handlePageDown();
}
}
}
Shortcut {
sequence: "Space"
context: Qt.ApplicationShortcut
onActivated: {
if (currentPage === "reader" && readerPage) {
readerPage.pageView.handlePageDown();
}
}
}
Timer {
running: true
repeat: false
interval: 1
triggeredOnStart: false
onTriggered: {
window.opacity = 1
readerPage.pageView.updateContentWidth()
if (uiState.lastFilePath !== "") {
window._startupRestore = true
book.openFromPath(uiState.lastFilePath)
} else {
var savedPage = uiState.lastPage
if (savedPage === "history" || savedPage === "settings")
window.currentPage = savedPage
window.sidebarOpen = true
}
}
}
}