mirror of
https://gitee.com/spark-store-project/spark-store
synced 2025-09-23 03:22:20 +08:00
parent
1a98dae4f8
commit
0db7fad640
171
src/backend/ThemeChecker.cpp
Normal file → Executable file
171
src/backend/ThemeChecker.cpp
Normal file → Executable file
@ -1,44 +1,147 @@
|
|||||||
#include "ThemeChecker.h"
|
#include "ThemeChecker.h"
|
||||||
#include <QTimer>
|
|
||||||
#include <QProcess>
|
#include <QDBusConnection>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
|
|
||||||
ThemeChecker::ThemeChecker(QObject *parent) : QObject(parent), lastColorSchema(-1) {
|
Q_GLOBAL_STATIC(ThemeChecker, m_instance)
|
||||||
timer = new QTimer(this);
|
|
||||||
connect(timer, SIGNAL(timeout()), this, SLOT(checkThemeChange()));
|
constexpr char kFreedesktopPortalServiceName[] = "org.freedesktop.portal.Desktop";
|
||||||
timer->start(1000);
|
constexpr char kFreedesktopPortalServicePath[] = "/org/freedesktop/portal/desktop";
|
||||||
|
constexpr char kFreedesktopPortalSettingsInterface[] = "org.freedesktop.portal.Settings";
|
||||||
|
|
||||||
|
ThemeChecker::ThemeChecker(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
, m_paletteType(Dtk::Gui::DGuiApplicationHelper::instance()->paletteType())
|
||||||
|
{
|
||||||
|
m_interface = new QDBusInterface(kFreedesktopPortalServiceName,
|
||||||
|
kFreedesktopPortalServicePath,
|
||||||
|
kFreedesktopPortalSettingsInterface,
|
||||||
|
QDBusConnection::sessionBus(),
|
||||||
|
this);
|
||||||
|
|
||||||
|
initThemeType();
|
||||||
|
initConnections();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ThemeChecker::checkThemeChange() {
|
ThemeChecker *ThemeChecker::instance()
|
||||||
|
{
|
||||||
|
return m_instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ThemeChecker::useDarkTheme()
|
||||||
QProcess process;
|
{
|
||||||
QStringList parameters;
|
if (m_paletteType == Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType) {
|
||||||
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'";
|
return m_themeType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType;
|
||||||
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 {
|
} else {
|
||||||
qWarning() << "DBus调用未能完成";
|
return m_paletteType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ThemeChecker::initThemeType()
|
||||||
|
{
|
||||||
|
QVariantList args { "org.freedesktop.appearance", "color-scheme" };
|
||||||
|
QDBusMessage msg = m_interface->callWithArgumentList(QDBus::Block, "Read", args);
|
||||||
|
if (msg.type() != QDBusMessage::MessageType::ReplyMessage || msg.arguments().size() < 1) {
|
||||||
|
qWarning().noquote() << "Init color-scheme from D-Bus failed:" << msg.errorName() << msg.errorMessage();
|
||||||
|
m_themeType = Dtk::Gui::DGuiApplicationHelper::toColorType(Dtk::Gui::DGuiApplicationHelper::fetchPalette(Dtk::Gui::DGuiApplicationHelper::instance()->applicationTheme()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
quint32 colorScheme = qvariant_cast<QDBusVariant>(qvariant_cast<QDBusVariant>(msg.arguments().first()).variant()).variant().toUInt();
|
||||||
|
switch (colorScheme) {
|
||||||
|
case 1:
|
||||||
|
m_themeType = Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
m_themeType = Dtk::Gui::DGuiApplicationHelper::ColorType::LightType;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qInfo().noquote() << QString("color-scheme: %1, fetching themeType according to QPalette::Window").arg(colorScheme);
|
||||||
|
m_themeType = Dtk::Gui::DGuiApplicationHelper::toColorType(Dtk::Gui::DGuiApplicationHelper::fetchPalette(Dtk::Gui::DGuiApplicationHelper::instance()->applicationTheme()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_paletteType == Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType) {
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::instance()->setApplicationPalette(Dtk::Gui::DGuiApplicationHelper::standardPalette(m_themeType));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThemeChecker::initConnections()
|
||||||
|
{
|
||||||
|
QDBusConnection::sessionBus().connect(kFreedesktopPortalServiceName,
|
||||||
|
kFreedesktopPortalServicePath,
|
||||||
|
kFreedesktopPortalSettingsInterface,
|
||||||
|
"SettingChanged",
|
||||||
|
this,
|
||||||
|
SLOT(slotSettingChanged(const QString &, const QString &, const QDBusVariant &)));
|
||||||
|
|
||||||
|
connect(Dtk::Gui::DGuiApplicationHelper::instance()->applicationTheme(), &Dtk::Gui::DPlatformTheme::themeNameChanged, this, &ThemeChecker::slotThemeNameChanged);
|
||||||
|
connect(Dtk::Gui::DGuiApplicationHelper::instance(), &Dtk::Gui::DGuiApplicationHelper::paletteTypeChanged, this, &ThemeChecker::slotPaletteTypeChanged);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThemeChecker::slotSettingChanged(const QString &_namespace, const QString &key, const QDBusVariant &variant)
|
||||||
|
{
|
||||||
|
if (_namespace != "org.freedesktop.appearance" || key != "color-scheme") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::ColorType colorType = Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType;
|
||||||
|
|
||||||
|
quint32 colorScheme = variant.variant().toUInt();
|
||||||
|
switch (colorScheme) {
|
||||||
|
case 1:
|
||||||
|
colorType = Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
colorType = Dtk::Gui::DGuiApplicationHelper::ColorType::LightType;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
qInfo().noquote() << QString("color-scheme: %1, fetching themeType according to QPalette::Window").arg(colorScheme);
|
||||||
|
colorType = Dtk::Gui::DGuiApplicationHelper::toColorType(Dtk::Gui::DGuiApplicationHelper::fetchPalette(Dtk::Gui::DGuiApplicationHelper::instance()->applicationTheme()));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_paletteType != Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType) {
|
||||||
|
m_themeType = colorType;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_themeType != colorType) {
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::instance()->setApplicationPalette(Dtk::Gui::DGuiApplicationHelper::standardPalette(colorType));
|
||||||
|
emit themeChanged(colorType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType);
|
||||||
|
m_themeType = colorType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThemeChecker::slotThemeNameChanged(const QByteArray &theme)
|
||||||
|
{
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::ColorType themeType = Dtk::Gui::DGuiApplicationHelper::ColorType::LightType;
|
||||||
|
if (theme.endsWith("dark")) {
|
||||||
|
themeType = Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_paletteType != Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType) {
|
||||||
|
m_themeType = themeType;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (m_themeType != themeType) {
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::instance()->setApplicationPalette(Dtk::Gui::DGuiApplicationHelper::standardPalette(themeType));
|
||||||
|
emit themeChanged(themeType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType);
|
||||||
|
m_themeType = themeType;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ThemeChecker::slotPaletteTypeChanged(Dtk::Gui::DGuiApplicationHelper::ColorType paletteType)
|
||||||
|
{
|
||||||
|
m_paletteType = paletteType;
|
||||||
|
|
||||||
|
if (m_paletteType != Dtk::Gui::DGuiApplicationHelper::ColorType::UnknownType) {
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::instance()->setApplicationPalette(Dtk::Gui::DGuiApplicationHelper::standardPalette(m_paletteType));
|
||||||
|
emit themeChanged(m_paletteType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::instance()->setApplicationPalette(Dtk::Gui::DGuiApplicationHelper::standardPalette(m_themeType));
|
||||||
|
emit themeChanged(m_themeType == Dtk::Gui::DGuiApplicationHelper::ColorType::DarkType);
|
||||||
|
}
|
||||||
|
26
src/backend/ThemeChecker.h
Normal file → Executable file
26
src/backend/ThemeChecker.h
Normal file → Executable file
@ -2,25 +2,39 @@
|
|||||||
#define THEMECHECKER_H
|
#define THEMECHECKER_H
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QTimer>
|
#include <QDBusVariant>
|
||||||
|
#include <QDBusInterface>
|
||||||
|
|
||||||
|
#include <DPlatformTheme>
|
||||||
|
#include <DGuiApplicationHelper>
|
||||||
|
|
||||||
class ThemeChecker : public QObject
|
class ThemeChecker : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ThemeChecker(QObject *parent = nullptr);
|
explicit ThemeChecker(QObject *parent = nullptr);
|
||||||
|
static ThemeChecker *instance();
|
||||||
|
|
||||||
|
bool useDarkTheme();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void initThemeType();
|
||||||
|
void initConnections();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void themeChanged(bool isDark);
|
void themeChanged(bool isDark);
|
||||||
|
|
||||||
public slots:
|
private slots:
|
||||||
void checkThemeChange();
|
void slotSettingChanged(const QString &_namespace, const QString &key, const QDBusVariant &variant);
|
||||||
|
void slotThemeNameChanged(const QByteArray &theme);
|
||||||
|
void slotPaletteTypeChanged(Dtk::Gui::DGuiApplicationHelper::ColorType paletteType);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int lastColorSchema;
|
QDBusInterface *m_interface = nullptr;
|
||||||
QTimer *timer;
|
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::ColorType m_paletteType;
|
||||||
|
Dtk::Gui::DGuiApplicationHelper::ColorType m_themeType;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // THEMECHECKER_H
|
#endif // THEMECHECKER_H
|
||||||
|
37
src/mainwindow-dtk.cpp
Normal file → Executable file
37
src/mainwindow-dtk.cpp
Normal file → Executable file
@ -41,8 +41,6 @@ MainWindow::MainWindow(QWidget *parent)
|
|||||||
initTmpDir();
|
initTmpDir();
|
||||||
|
|
||||||
ui->appintopage->setDownloadWidget(downloadlistwidget);
|
ui->appintopage->setDownloadWidget(downloadlistwidget);
|
||||||
|
|
||||||
emit DGuiApplicationHelper::instance()->themeTypeChanged(DGuiApplicationHelper::instance()->themeType());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
MainWindow::~MainWindow()
|
MainWindow::~MainWindow()
|
||||||
@ -135,13 +133,14 @@ void MainWindow::initUI()
|
|||||||
updateUi(0);
|
updateUi(0);
|
||||||
|
|
||||||
initTrayIcon();
|
initTrayIcon();
|
||||||
|
|
||||||
|
refreshTheme(ThemeChecker::instance()->useDarkTheme());
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::initTitleBar()
|
void MainWindow::initTitleBar()
|
||||||
{
|
{
|
||||||
ui->titlebar->setIcon(QIcon::fromTheme("spark-store"));
|
ui->titlebar->setIcon(QIcon::fromTheme("spark-store"));
|
||||||
ui->titlebar->setBackgroundTransparent(true);
|
ui->titlebar->setBackgroundTransparent(true);
|
||||||
ui->titlebar->setSwitchThemeMenuVisible(false); // 去除 dtk 标题栏主题切换菜单
|
|
||||||
|
|
||||||
// 初始化标题栏控件
|
// 初始化标题栏控件
|
||||||
DLabel *title = new DLabel(ui->titlebar);
|
DLabel *title = new DLabel(ui->titlebar);
|
||||||
@ -320,40 +319,10 @@ void MainWindow::refreshTheme(bool isDarkMode)
|
|||||||
ui->settingspage->setTheme(isDarkMode);
|
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); //当前已经是浅色模式
|
|
||||||
|
|
||||||
// 检查 isDark 为 true 时, isUserSetDark 是否为 true
|
|
||||||
// 检查 isDark 为 false , isUserSetWhite 是否为 ture
|
|
||||||
qDebug() << isUserSetDark << isUserSetWhite << isDark;
|
|
||||||
|
|
||||||
|
|
||||||
if ((isUserSetDark != isDark) || (isUserSetWhite == !isDark)) {
|
|
||||||
// 否则,根据传入的 isDark 值设置
|
|
||||||
refreshTheme(isDark);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void MainWindow::initConnections()
|
void MainWindow::initConnections()
|
||||||
{
|
{
|
||||||
|
|
||||||
// 主题切换
|
// 主题切换
|
||||||
connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, [=](DGuiApplicationHelper::ColorType themeType)
|
connect(ThemeChecker::instance(), &ThemeChecker::themeChanged, this, &MainWindow::refreshTheme);
|
||||||
{
|
|
||||||
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, [=]()
|
||||||
|
4
src/mainwindow-dtk.h
Normal file → Executable file
4
src/mainwindow-dtk.h
Normal file → Executable file
@ -30,8 +30,7 @@ public:
|
|||||||
void openUrl(const QString &url);
|
void openUrl(const QString &url);
|
||||||
|
|
||||||
bool isCloseWindowAnimation();
|
bool isCloseWindowAnimation();
|
||||||
void refreshTheme(bool isDarkMode);
|
Q_INVOKABLE void refreshTheme(bool isDarkMode);
|
||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent *event) override;
|
void closeEvent(QCloseEvent *event) override;
|
||||||
@ -50,7 +49,6 @@ 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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user