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

This commit is contained in:
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