Add DarkTheme and LightTheme switching

This commit is contained in:
Agel committed 2026-03-31 20:43:37 +02:00
1 parent 7409ba789a
commit ff88cf581b
17 files changed
+206 -73

No files matched your search

+8 -2
View File
@@ -101,7 +101,11 @@ target_link_libraries(asteria-qt PRIVATE PkgConfig::MINIZIP Qt6::Xml)
# Set resource aliases so src/qml/* maps to qrc:/Asteria/*
set_source_files_properties(src/qml/Main.qml PROPERTIES QT_RESOURCE_ALIAS Main.qml)
set_source_files_properties(src/qml/Theme.qml PROPERTIES QT_RESOURCE_ALIAS Theme.qml
set_source_files_properties(src/qml/DarkTheme.qml PROPERTIES QT_RESOURCE_ALIAS DarkTheme.qml
QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(src/qml/LightTheme.qml PROPERTIES QT_RESOURCE_ALIAS LightTheme.qml
QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(src/qml/ThemeManager.qml PROPERTIES QT_RESOURCE_ALIAS ThemeManager.qml
QT_QML_SINGLETON_TYPE TRUE)
set_source_files_properties(src/qml/components/Header.qml PROPERTIES QT_RESOURCE_ALIAS components/Header.qml)
set_source_files_properties(src/qml/components/SideBar.qml PROPERTIES QT_RESOURCE_ALIAS components/SideBar.qml)
@@ -122,7 +126,9 @@ qt_add_qml_module(asteria-qt
VERSION 1.0
QML_FILES
src/qml/Main.qml
src/qml/Theme.qml
src/qml/DarkTheme.qml
src/qml/LightTheme.qml
src/qml/ThemeManager.qml
src/qml/components/Header.qml
src/qml/components/SideBar.qml
src/qml/components/PageView.qml
+12
View File
@@ -189,6 +189,16 @@ void AppState::setSidebarOpen(bool open)
emit sidebarOpenChanged();
}
QString AppState::themeName() const { return m_themeName; }
void AppState::setThemeName(const QString &themeName)
{
if (m_themeName == themeName)
return;
m_themeName = themeName;
emit themeNameChanged();
}
void AppState::addOrUpdateLibraryBook(const QString &filePath, const QString &title, const QString &author, double progress)
{
QString lastOpened = QDateTime::currentDateTime().toString(Qt::ISODate);
@@ -284,6 +294,7 @@ void AppState::save()
s.setValue(QStringLiteral("preferences/verticalMargin"), m_verticalMargin);
s.setValue(QStringLiteral("preferences/fontSize"), m_fontSize);
s.setValue(QStringLiteral("preferences/lineSpacing"), m_lineSpacing);
s.setValue(QStringLiteral("preferences/themeName"), m_themeName);
s.setValue(QStringLiteral("window/width"), m_windowWidth);
s.setValue(QStringLiteral("window/height"), m_windowHeight);
s.setValue(QStringLiteral("window/x"), m_windowX);
@@ -305,6 +316,7 @@ void AppState::restore()
m_verticalMargin = s.value(QStringLiteral("preferences/verticalMargin"), 8).toInt();
m_fontSize = s.value(QStringLiteral("preferences/fontSize"), 16).toInt();
m_lineSpacing = s.value(QStringLiteral("preferences/lineSpacing"), 1.6).toDouble();
m_themeName = s.value(QStringLiteral("preferences/themeName"), QStringLiteral("dark")).toString();
m_windowWidth = s.value(QStringLiteral("window/width"), 1024).toInt();
m_windowHeight = s.value(QStringLiteral("window/height"), 768).toInt();
m_windowX = s.value(QStringLiteral("window/x"), -1).toInt();
+6
View File
@@ -25,10 +25,14 @@ class AppState : public QObject {
Q_PROPERTY(QVariantList libraryBooks READ libraryBooks NOTIFY libraryBooksChanged)
Q_PROPERTY(QString lastPage READ lastPage WRITE setLastPage NOTIFY lastPageChanged)
Q_PROPERTY(bool sidebarOpen READ sidebarOpen WRITE setSidebarOpen NOTIFY sidebarOpenChanged)
Q_PROPERTY(QString themeName READ themeName WRITE setThemeName NOTIFY themeNameChanged)
public:
explicit AppState(QObject *parent = nullptr);
QString themeName() const;
void setThemeName(const QString &themeName);
QString lastFilePath() const;
void setLastFilePath(const QString &path);
@@ -104,6 +108,7 @@ signals:
void libraryBooksChanged();
void lastPageChanged();
void sidebarOpenChanged();
void themeNameChanged();
private:
void loadLibraryBooks();
@@ -124,6 +129,7 @@ private:
QVariantList m_libraryBooks;
QString m_lastPage = QStringLiteral("starter");
bool m_sidebarOpen = false;
QString m_themeName = QStringLiteral("dark");
};
#endif // APPSTATE_H
+1 -8
View File
@@ -18,16 +18,9 @@ QtObject {
readonly property color dimText: "#71738c"
readonly property color counterText: "#606175"
readonly property int sidebarWidth: 240
readonly property color gradientTop: "#0e1125"
readonly property color gradientBottom: "#191d35"
readonly property color divider: Qt.rgba(1, 1, 1, 0.12)
readonly property color overlayBg: Qt.rgba(0.055, 0.055, 0.067, 0.4)
readonly property color actionDisabled: "#141724"
readonly property int headerHeight: 64
readonly property int footerHeight: 28
readonly property int animDurationOpen: 225
readonly property int animDurationClose: 195
}
}
+26
View File
@@ -0,0 +1,26 @@
pragma Singleton
import QtQuick
QtObject {
readonly property color backgroundDeepest: "#f5f5f5"
readonly property color backgroundDefault: "#ffffff"
readonly property color surface: "#f0f0f0"
readonly property color headerBg: "#e8e8e8"
readonly property color footerBg: "#e0e0e0"
readonly property color scrollbarTrack: "#d0d0d0"
readonly property color scrollbarThumb: "#a0a0a0"
readonly property color primaryAccent: "#3a5688"
readonly property color buttonAccent: "#5785c1"
readonly property color sidebarButtonText: "#333333"
readonly property color readingText: "#222222"
readonly property color uiText: "#444444"
readonly property color headerText: "#555555"
readonly property color dimText: "#666666"
readonly property color counterText: "#777777"
readonly property color gradientTop: "#f8f8f8"
readonly property color gradientBottom: "#f0f0f0"
readonly property color divider: Qt.rgba(0, 0, 0, 0.12)
readonly property color overlayBg: Qt.rgba(0.95, 0.95, 0.95, 0.4)
readonly property color actionDisabled: "#e0e0e0"
}
+16 -2
View File
@@ -1,6 +1,7 @@
import QtQuick
import QtQuick.Controls
import Asteria
import QtQml
ApplicationWindow {
id: window
@@ -8,7 +9,7 @@ ApplicationWindow {
height: 768
visible: true
title: "Asteria"
color: Theme.backgroundDeepest
color: ThemeManager.currentTheme.backgroundDeepest
FontLoader {
id: materialIcons
@@ -30,6 +31,12 @@ ApplicationWindow {
x = appState.windowX
y = appState.windowY
}
// Initialize theme from AppState
if (appState.themeName !== "") {
ThemeManager.currentThemeName = appState.themeName
}
if (appState.lastFilePath !== "") {
window._restoringState = true
bookController.openBook(appState.lastFileUrl())
@@ -40,6 +47,13 @@ ApplicationWindow {
window.currentPage = savedPage
}
}
Connections {
target: appState
function onThemeNameChanged() {
ThemeManager.currentThemeName = appState.themeName
}
}
Timer {
id: geometrySaveTimer
@@ -93,7 +107,7 @@ ApplicationWindow {
Item {
id: contentArea
width: parent.width
height: parent.height - header.height - (footerLoader.active ? Theme.footerHeight : 0)
height: parent.height - header.height - (footerLoader.active ? ThemeManager.currentTheme.footerHeight : 0)
StarterPage {
anchors.fill: parent
+40
View File
@@ -0,0 +1,40 @@
pragma Singleton
import QtQuick
QtObject {
readonly property int sidebarWidth: 240
readonly property int headerHeight: 64
readonly property int footerHeight: 28
readonly property int animDurationOpen: 225
readonly property int animDurationClose: 195
property string currentThemeName: "dark"
readonly property var currentTheme: currentThemeName === "dark" ? DarkTheme : LightTheme
function setDarkTheme() {
currentThemeName = "dark"
if (appState) {
appState.themeName = "dark"
}
}
function setLightTheme() {
currentThemeName = "light"
if (appState) {
appState.themeName = "light"
}
}
function toggleTheme() {
currentThemeName = currentThemeName === "dark" ? "light" : "dark"
if (appState) {
appState.themeName = currentThemeName
}
}
onCurrentThemeNameChanged: {
// This ensures UI updates when theme changes
// The actual theme switching happens through the currentTheme property
}
}
+4 -4
View File
@@ -4,7 +4,7 @@ import Asteria
Rectangle {
id: overlay
color: Theme.overlayBg
color: ThemeManager.currentTheme.overlayBg
visible: bookController.errorMessage !== ""
MouseArea {
@@ -16,7 +16,7 @@ Rectangle {
anchors.centerIn: parent
width: Math.min(overlay.width * 0.7, 440)
height: dialogColumn.implicitHeight + 48
color: Theme.surface
color: ThemeManager.currentTheme.surface
radius: 8
Column {
@@ -35,7 +35,7 @@ Rectangle {
width: parent.width
text: bookController.errorMessage
font.pixelSize: 15
color: Theme.readingText
color: ThemeManager.currentTheme.readingText
horizontalAlignment: Text.AlignHCenter
wrapMode: Text.Wrap
}
@@ -53,7 +53,7 @@ Rectangle {
implicitWidth: 80
implicitHeight: 36
radius: 4
color: parent.hovered ? Qt.lighter(Theme.primaryAccent, 1.15) : Theme.primaryAccent
color: parent.hovered ? Qt.lighter(ThemeManager.currentTheme.primaryAccent, 1.15) : ThemeManager.currentTheme.primaryAccent
}
onClicked: bookController.clearError()
}
+7 -7
View File
@@ -3,13 +3,13 @@ import QtQuick.Controls
import Asteria
Rectangle {
height: Theme.footerHeight
color: Theme.footerBg
height: ThemeManager.currentTheme.footerHeight
color: ThemeManager.currentTheme.footerBg
Rectangle {
width: parent.width
height: 1
color: Theme.divider
color: ThemeManager.currentTheme.divider
anchors.top: parent.top
}
@@ -28,7 +28,7 @@ Rectangle {
text: "\ue5cb"
font.family: "Material Icons Outlined"
font.pixelSize: 16
color: prevBtn.enabled ? Theme.dimText : Theme.actionDisabled
color: prevBtn.enabled ? ThemeManager.currentTheme.dimText : ThemeManager.currentTheme.actionDisabled
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
@@ -45,7 +45,7 @@ Rectangle {
: ""
font.pixelSize: 11
font.bold: true
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
}
// Next chapter button
@@ -63,7 +63,7 @@ Rectangle {
text: "\ue5cc"
font.family: "Material Icons Outlined"
font.pixelSize: 16
color: nextBtn.enabled ? Theme.dimText : Theme.actionDisabled
color: nextBtn.enabled ? ThemeManager.currentTheme.dimText : ThemeManager.currentTheme.actionDisabled
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
@@ -81,6 +81,6 @@ Rectangle {
: ""
font.pixelSize: 11
font.bold: true
color: Theme.counterText
color: ThemeManager.currentTheme.counterText
}
}
+5 -5
View File
@@ -4,9 +4,9 @@ import Asteria
Rectangle {
id: header
height: barsVisible ? Theme.headerHeight : 0
height: barsVisible ? ThemeManager.currentTheme.headerHeight : 0
clip: true
color: Theme.surface
color: ThemeManager.currentTheme.surface
property bool sidebarOpen: false
property string currentPage: "starter"
@@ -28,7 +28,7 @@ Rectangle {
contentItem: Text {
text: "\u2630"
font.pixelSize: 24
color: Theme.buttonAccent
color: ThemeManager.currentTheme.buttonAccent
}
background: Item {}
onClicked: header.menuClicked()
@@ -44,7 +44,7 @@ Rectangle {
}
}
font.pixelSize: 16
color: Theme.headerText
color: ThemeManager.currentTheme.headerText
}
Row {
@@ -58,7 +58,7 @@ Rectangle {
anchors.verticalCenter: parent.verticalCenter
font.pixelSize: 16
font.bold: true
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
Timer {
running: true
+5 -5
View File
@@ -53,8 +53,8 @@ Rectangle {
}
gradient: Gradient {
GradientStop { position: 0.0; color: Theme.gradientTop }
GradientStop { position: 1.0; color: Theme.gradientBottom }
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
GradientStop { position: 1.0; color: ThemeManager.currentTheme.gradientBottom }
}
function updateContentWidth() {
@@ -81,7 +81,7 @@ Rectangle {
textFormat: Text.RichText
font.pixelSize: appState.fontSize
lineHeight: appState.lineSpacing
color: Theme.readingText
color: ThemeManager.currentTheme.readingText
text: model.content
}
@@ -101,11 +101,11 @@ Rectangle {
contentItem: Rectangle {
implicitWidth: 18
radius: 3
color: Theme.scrollbarThumb
color: ThemeManager.currentTheme.scrollbarThumb
}
background: Rectangle {
implicitWidth: 18
color: Theme.scrollbarTrack
color: ThemeManager.currentTheme.scrollbarTrack
}
}
+17 -17
View File
@@ -4,9 +4,9 @@ import Asteria
Rectangle {
id: sidebar
width: sidebarOpen ? Theme.sidebarWidth : 0
width: sidebarOpen ? ThemeManager.sidebarWidth : 0
clip: true
color: Theme.surface
color: ThemeManager.currentTheme.surface
property bool sidebarOpen: false
@@ -17,7 +17,7 @@ Rectangle {
Behavior on width {
NumberAnimation {
duration: sidebarOpen ? Theme.animDurationOpen : Theme.animDurationClose
duration: sidebarOpen ? ThemeManager.animDurationOpen : ThemeManager.animDurationClose
easing.type: Easing.OutQuad
}
}
@@ -25,25 +25,25 @@ Rectangle {
Text {
anchors.left: parent.left
anchors.leftMargin: 16
y: Theme.headerHeight / 2 - height / 2
y: ThemeManager.headerHeight / 2 - height / 2
text: "Asteria"
font.pixelSize: 18
font.bold: true
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
}
// Close button aligned with main header row
Button {
anchors.right: parent.right
anchors.rightMargin: 12
y: Theme.headerHeight / 2 - height / 2
y: ThemeManager.headerHeight / 2 - height / 2
z: 1
flat: true
contentItem: Text {
text: "\ue5cb"
font.family: "Material Icons Outlined"
font.pixelSize: 24
color: Theme.sidebarButtonText
color: DarkTheme.sidebarButtonText
}
background: Item {}
onClicked: sidebar.closeDrawer()
@@ -51,8 +51,8 @@ Rectangle {
Column {
id: navColumn
y: Theme.headerHeight
width: Theme.sidebarWidth
y: ThemeManager.headerHeight
width: ThemeManager.sidebarWidth
SideBarButton {
iconText: "\ue86d"
@@ -83,26 +83,26 @@ Rectangle {
Rectangle {
width: parent.width
height: 1
color: Theme.divider
color: ThemeManager.currentTheme.divider
}
SideBarButton {
iconText: "\ue3a9"
title: "Dark Theme"
onClicked: {} // Non-functional
onClicked: ThemeManager.setDarkTheme()
}
SideBarButton {
iconText: "\ue3a6"
title: "Light Theme"
onClicked: {} // Non-functional
onClicked: ThemeManager.setLightTheme()
}
// Divider
Rectangle {
width: parent.width
height: 1
color: Theme.divider
color: ThemeManager.currentTheme.divider
}
SideBarButton {
@@ -123,7 +123,7 @@ Rectangle {
id: tocDivider
width: parent.width
height: 1
color: Theme.divider
color: ThemeManager.currentTheme.divider
}
Text {
@@ -135,7 +135,7 @@ Rectangle {
text: "Contents"
font.pixelSize: 10
font.bold: true
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
font.letterSpacing: 1
}
@@ -162,8 +162,8 @@ Rectangle {
text: modelData.title
font.pixelSize: 13
color: readingModel.currentChapter === modelData.chapterIndex
? Theme.sidebarButtonText
: Theme.uiText
? ThemeManager.sidebarButtonText
: ThemeManager.uiText
elide: Text.ElideRight
wrapMode: Text.NoWrap
}
+3 -3
View File
@@ -6,7 +6,7 @@ Rectangle {
id: root
height: 48
width: parent ? parent.width : 240
color: mouseArea.containsMouse ? Qt.lighter(Theme.headerBg, 1.3) : "transparent"
color: mouseArea.containsMouse ? Qt.lighter(ThemeManager.currentTheme.headerBg, 1.3) : "transparent"
property string iconText: ""
property string title: ""
@@ -22,14 +22,14 @@ Rectangle {
text: root.iconText
font.family: "Material Icons Outlined"
font.pixelSize: 24
color: Theme.sidebarButtonText
color: ThemeManager.currentTheme.sidebarButtonText
}
Text {
anchors.verticalCenter: parent.verticalCenter
text: root.title
font.pixelSize: 14
color: Theme.sidebarButtonText
color: ThemeManager.currentTheme.sidebarButtonText
}
}
+7 -7
View File
@@ -3,14 +3,14 @@ import QtQuick.Controls
import Asteria
Rectangle {
color: Theme.backgroundDefault
color: ThemeManager.currentTheme.backgroundDefault
Text {
visible: appState.libraryBooks.length === 0
anchors.centerIn: parent
text: "No books read yet.\nOpen a book to start your library."
horizontalAlignment: Text.AlignHCenter
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
font.pixelSize: 14
lineHeight: 1.6
}
@@ -27,7 +27,7 @@ Rectangle {
id: bookRow
width: ListView.view.width
height: bookInfoCol.implicitHeight + 24
color: Theme.surface
color: ThemeManager.currentTheme.surface
radius: 4
Row {
@@ -47,7 +47,7 @@ Rectangle {
Text {
width: parent.width
text: modelData.title || "Unknown Title"
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
font.pixelSize: 14
font.bold: true
elide: Text.ElideRight
@@ -57,14 +57,14 @@ Rectangle {
visible: modelData.author !== ""
width: parent.width
text: modelData.author
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
font.pixelSize: 12
elide: Text.ElideRight
}
Text {
text: Math.round(modelData.progress * 100) + "%"
color: Theme.buttonAccent
color: ThemeManager.currentTheme.buttonAccent
font.pixelSize: 12
}
}
@@ -77,7 +77,7 @@ Rectangle {
text: "\ue872"
font.family: "Material Icons Outlined"
font.pixelSize: 20
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
+11 -11
View File
@@ -4,8 +4,8 @@ import Asteria
Rectangle {
gradient: Gradient {
GradientStop { position: 0.0; color: Theme.gradientTop }
GradientStop { position: 1.0; color: Theme.gradientBottom }
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
GradientStop { position: 1.0; color: ThemeManager.currentTheme.gradientBottom }
}
ScrollView {
@@ -22,7 +22,7 @@ Rectangle {
text: "Reading"
font.pixelSize: 13
font.weight: Font.Medium
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
leftPadding: 4
}
@@ -38,7 +38,7 @@ Rectangle {
id: labelH
text: "Horizontal margin"
font.pixelSize: 15
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
leftPadding: 4
anchors.verticalCenter: parent.verticalCenter
}
@@ -46,7 +46,7 @@ Rectangle {
id: valueH
text: appState.horizontalMargin + " px"
font.pixelSize: 13
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.verticalCenter: parent.verticalCenter
@@ -75,7 +75,7 @@ Rectangle {
id: labelV
text: "Vertical margin"
font.pixelSize: 15
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
leftPadding: 4
anchors.verticalCenter: parent.verticalCenter
}
@@ -83,7 +83,7 @@ Rectangle {
id: valueV
text: appState.verticalMargin + " px"
font.pixelSize: 13
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.verticalCenter: parent.verticalCenter
@@ -112,7 +112,7 @@ Rectangle {
id: labelF
text: "Font size"
font.pixelSize: 15
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
leftPadding: 4
anchors.verticalCenter: parent.verticalCenter
}
@@ -120,7 +120,7 @@ Rectangle {
id: valueF
text: appState.fontSize + " px"
font.pixelSize: 13
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.verticalCenter: parent.verticalCenter
@@ -149,7 +149,7 @@ Rectangle {
id: labelL
text: "Line spacing"
font.pixelSize: 15
color: Theme.uiText
color: ThemeManager.currentTheme.uiText
leftPadding: 4
anchors.verticalCenter: parent.verticalCenter
}
@@ -157,7 +157,7 @@ Rectangle {
id: valueL
text: appState.lineSpacing.toFixed(1) + "×"
font.pixelSize: 13
color: Theme.dimText
color: ThemeManager.currentTheme.dimText
anchors.right: parent.right
anchors.rightMargin: 4
anchors.verticalCenter: parent.verticalCenter
+2 -2
View File
@@ -3,8 +3,8 @@ import Asteria
Rectangle {
gradient: Gradient {
GradientStop { position: 0.0; color: Theme.gradientTop }
GradientStop { position: 1.0; color: Theme.gradientBottom }
GradientStop { position: 0.0; color: ThemeManager.currentTheme.gradientTop }
GradientStop { position: 1.0; color: ThemeManager.currentTheme.gradientBottom }
}
Image {
+36
View File
@@ -265,6 +265,7 @@ private slots:
QSignalSpy spyHeight(&state, &AppState::windowHeightChanged);
QSignalSpy spyX(&state, &AppState::windowXChanged);
QSignalSpy spyY(&state, &AppState::windowYChanged);
QSignalSpy spyTheme(&state, &AppState::themeNameChanged);
state.restore();
@@ -282,6 +283,41 @@ private slots:
QCOMPARE(spyHeight.count(), 0);
QCOMPARE(spyX.count(), 0);
QCOMPARE(spyY.count(), 0);
QCOMPARE(spyTheme.count(), 0);
}
void saveAndRestoreThemeName()
{
{
AppState state;
state.setThemeName(QStringLiteral("light"));
state.save();
}
AppState state2;
state2.restore();
QCOMPARE(state2.themeName(), QStringLiteral("light"));
}
void themeNameDefaultsToDark()
{
AppState state;
QCOMPARE(state.themeName(), QStringLiteral("dark"));
}
void themeNameSetterEmitsSignal()
{
AppState state;
QSignalSpy spy(&state, &AppState::themeNameChanged);
state.setThemeName(QStringLiteral("light"));
QCOMPARE(spy.count(), 1);
}
void themeNameSetterDoesNotEmitForSameValue()
{
AppState state;
QSignalSpy spy(&state, &AppState::themeNameChanged);
state.setThemeName(QStringLiteral("dark")); // Same as default
QCOMPARE(spy.count(), 0);
}
// Library tests