feat: 使用dbus支持kde的暗色模式

This commit is contained in:
uniartisan 2023-10-24 19:25:32 +08:00
parent c00d62c010
commit 8d4874d553
5 changed files with 153 additions and 48 deletions

View File

@ -0,0 +1,44 @@
#include "ThemeChecker.h"
#include <QTimer>
#include <QProcess>
#include <QDebug>
ThemeChecker::ThemeChecker(QObject *parent) : QObject(parent), lastColorSchema(-1) {
timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(checkThemeChange()));
timer->start(1000);
}
void ThemeChecker::checkThemeChange() {
QProcess process;
QStringList parameters;
parameters << "-c" << "dbus-send --session --print-reply=literal --reply-timeout=1000 --dest=org.freedesktop.portal.Desktop /org/freedesktop/portal/desktop org.freedesktop.portal.Settings.Read string:'org.freedesktop.appearance' string:'color-scheme'";
process.start("/bin/sh", parameters);
if (process.waitForFinished(-1)) {
QString errorOutput = QString(process.readAllStandardError()).trimmed();
if (!errorOutput.isEmpty()) {
qWarning() << "检测到DBus错误:" << errorOutput;
return;
}
QString output = QString(process.readAll()).trimmed();
int systemColorSchema = output.right(1).toInt();
if(systemColorSchema != lastColorSchema) {
lastColorSchema = systemColorSchema;
qDebug() << "主题已更改,新的主题是:" << systemColorSchema;
if(systemColorSchema == 1){
emit themeChanged(true);
}
else if(systemColorSchema == 0 || systemColorSchema == 2){
emit themeChanged(false);
}
}
} else {
qWarning() << "DBus调用未能完成";
}
}

View File

@ -0,0 +1,26 @@
#ifndef THEMECHECKER_H
#define THEMECHECKER_H
#include <QObject>
#include <QTimer>
class ThemeChecker : public QObject
{
Q_OBJECT
public:
explicit ThemeChecker(QObject *parent = nullptr);
signals:
void themeChanged(bool isDark);
public slots:
void checkThemeChange();
private:
int lastColorSchema;
QTimer *timer;
};
#endif // THEMECHECKER_H

View File

@ -16,6 +16,8 @@
#include <QtConcurrent> #include <QtConcurrent>
#include <unistd.h> #include <unistd.h>
#include <backend/ThemeChecker.h>
#define AppPageApplist 0 #define AppPageApplist 0
#define AppPageSearchlist 1 #define AppPageSearchlist 1
#define AppPageAppdetail 2 #define AppPageAppdetail 2
@ -267,12 +269,10 @@ void MainWindow::initTrayIcon()
trayIcon->show(); trayIcon->show();
} }
void MainWindow::initConnections() void MainWindow::refreshTheme(bool isDarkMode)
{ {
// 主题切换 // 使用isDarkMode变量来判断是否是深色模式
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [=](DGuiApplicationHelper::ColorType themeType) if (isDarkMode) {
{
if (themeType == DGuiApplicationHelper::DarkType) {
//深色模式 //深色模式
setMaskColor(QColor("#2a2b2b")); setMaskColor(QColor("#2a2b2b"));
backButton->setIcon(QIcon(":/icon/dark/back.svg")); backButton->setIcon(QIcon(":/icon/dark/back.svg"));
@ -314,10 +314,40 @@ void MainWindow::initConnections()
} }
} }
ui->pushButton_14->setStyleSheet(ui->pushButton_4->styleSheet()); ui->pushButton_14->setStyleSheet(ui->pushButton_4->styleSheet());
ui->applistpage->setTheme(themeType == DGuiApplicationHelper::DarkType); ui->applistpage->setTheme(isDarkMode);
ui->applistpage_1->setTheme(themeType == DGuiApplicationHelper::DarkType); ui->applistpage_1->setTheme(isDarkMode);
ui->appintopage->setTheme(themeType == DGuiApplicationHelper::DarkType); ui->appintopage->setTheme(isDarkMode);
ui->settingspage->setTheme(themeType == DGuiApplicationHelper::DarkType); }); ui->settingspage->setTheme(isDarkMode);
}
void MainWindow::onThemeChanged(bool isDark) {
DGuiApplicationHelper::ColorType currentTheme = DGuiApplicationHelper::instance()->themeType();
// 检查是否用户手动设置为暗色模式
bool isUserSetDark = (currentTheme == DGuiApplicationHelper::DarkType);
bool isUserSetWhite = !(currentTheme == DGuiApplicationHelper::LightType);
if (!isUserSetDark && !isUserSetWhite) {
// 否则,根据传入的 isDark 值设置
refreshTheme(isDark);
}
}
void MainWindow::initConnections()
{
// 主题切换
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [=](DGuiApplicationHelper::ColorType themeType)
{
bool isDarkMode = (themeType == DGuiApplicationHelper::ColorType::DarkType);
refreshTheme(isDarkMode);
});
ThemeChecker *themeChecker = new ThemeChecker(this);
connect(themeChecker, SIGNAL(themeChanged(bool)), this, SLOT(onThemeChanged(bool)));
// appintopage按下下载按钮时标题栏下载列表按钮抖动 // appintopage按下下载按钮时标题栏下载列表按钮抖动
connect(ui->appintopage, &AppIntoPage::clickedDownloadBtn, [=]() connect(ui->appintopage, &AppIntoPage::clickedDownloadBtn, [=]()

View File

@ -30,6 +30,8 @@ public:
void openUrl(const QString &url); void openUrl(const QString &url);
bool isCloseWindowAnimation(); bool isCloseWindowAnimation();
void refreshTheme(bool isDarkMode);
protected: protected:
void closeEvent(QCloseEvent *event) override; void closeEvent(QCloseEvent *event) override;
@ -48,6 +50,7 @@ private:
public slots: public slots:
void notify(QObject *receiver, QEvent *event); void notify(QObject *receiver, QEvent *event);
void onThemeChanged(bool isDark);
private slots: private slots:
//接受来自dbus的url //接受来自dbus的url

View File

@ -35,6 +35,7 @@ PKGCONFIG += dtkcore dtkgui dtkwidget
SOURCES += \ SOURCES += \
backend/DataCollectorAndUploader.cpp \ backend/DataCollectorAndUploader.cpp \
backend/ThemeChecker.cpp \
backend/downloadworker.cpp \ backend/downloadworker.cpp \
backend/image_show.cpp \ backend/image_show.cpp \
backend/sparkapi.cpp \ backend/sparkapi.cpp \
@ -60,6 +61,7 @@ SOURCES += \
HEADERS += \ HEADERS += \
backend/DataCollectorAndUploader.h \ backend/DataCollectorAndUploader.h \
backend/ThemeChecker.h \
backend/downloadworker.h \ backend/downloadworker.h \
backend/image_show.h \ backend/image_show.h \
backend/sparkapi.h \ backend/sparkapi.h \