diff --git a/plugins/platformtheme/appearance-monitor.cpp b/plugins/platformtheme/appearance-monitor.cpp index 45cfb8234322ce06c950f351ffcf5c7402a56872..55e1194c7f9d9862eea60cfd6ee8b8a5b4283706 100644 --- a/plugins/platformtheme/appearance-monitor.cpp +++ b/plugins/platformtheme/appearance-monitor.cpp @@ -12,78 +12,46 @@ * Author: liuxinhao */ -#include "plugins/platformtheme/appearance-monitor.h" -#include -#include +#include "appearance-monitor.h" +#include +#include #include #include +#include +#include #include -#include -#include -#include "plugins/platformtheme/appearance_proxy.h" -#include "plugins/platformtheme/display_proxy.h" -#include "appearance-monitor.h" - -#define APPEARANCE_DBUS_NAME "com.kylinsec.Kiran.SessionDaemon.Appearance" -#define APPEARANCE_DBUS_OBJECT_PATH "/com/kylinsec/Kiran/SessionDaemon/Appearance" - -#define DISPLAY_DBUS_NAME "com.kylinsec.Kiran.SessionDaemon.Display" -#define DISPLAY_DBUS_OBJECT_PATH "/com/kylinsec/Kiran/SessionDaemon/Display" +#include namespace Kiran { namespace Platformtheme -{ -AppearanceMonitor::AppearanceMonitor(QObject *parent) - : QObject(parent), - m_appearanceServiceWatcher(nullptr), - m_displayServiceWatcher(nullptr) { - m_polishCursorTimer.setInterval(500); - m_polishCursorTimer.setSingleShot(true); - connect(&m_polishCursorTimer, &QTimer::timeout, this, &AppearanceMonitor::handleCursorThemeChanged); +// TODO:后面改为include "xsettings-i.h" +#define XSETTINGS_SCHEMA_ID "com.kylinsec.kiran.xsettings" +#define XSETTINGS_SCHEMA_GTK_FONT_NAME "gtkFontName" +#define XSETTINGS_SCHEMA_NET_ICON_THEME_NAME "netIconThemeName" +#define XSETTINGS_SCHEMA_NET_THEME_NAME "netThemeName" +#define XSETTINGS_SCHEMA_GTK_CURSOR_THEME_NAME "gtkCursorThemeName" +#define XSETTINGS_SCHEMA_WINDOW_SCALING_FACTOR "windowScalingFactor" - m_appearanceIface = new AppearanceProxy(APPEARANCE_DBUS_NAME, - APPEARANCE_DBUS_OBJECT_PATH, - QDBusConnection::sessionBus(), - this); - if (QDBusConnection::sessionBus().interface()->isServiceRegistered(APPEARANCE_DBUS_NAME)) - { - loadAppearance(); - } - else - { - m_appearanceServiceWatcher = new QDBusServiceWatcher(APPEARANCE_DBUS_NAME, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration, this); - connect(m_appearanceServiceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &AppearanceMonitor::loadAppearance); - qDebug() << "kiran session daemon appearance service isn't registered!"; - } - - connect(m_appearanceIface, &AppearanceProxy::FontChanged, - this, &AppearanceMonitor::handleFontSettingChanged); - connect(m_appearanceIface, &AppearanceProxy::ThemeChanged, - this, &AppearanceMonitor::handleThemeSettingChanged); +#define MARCO_SCHEMA_ID "org.mate.Marco.general" +#define MARCO_SCHAME_KEY_TITLEBAR_FONT "titlebarFont" - m_displayIface = new DisplayProxy("com.kylinsec.Kiran.SessionDaemon.Display", - "/com/kylinsec/Kiran/SessionDaemon/Display", - QDBusConnection::sessionBus(), - this); - if (QDBusConnection::sessionBus().interface()->isServiceRegistered("com.kylinsec.Kiran.SessionDaemon.Display")) - { - loadScalingFactor(); - } - else - { - m_displayServiceWatcher = new QDBusServiceWatcher(DISPLAY_DBUS_NAME, QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForRegistration, this); - connect(m_displayServiceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &AppearanceMonitor::loadScalingFactor); - - qDebug() << "kiran session daemon display service isn't registered!"; - } - - connect(m_displayIface, &DisplayProxy::window_scaling_factorChanged, - this, &AppearanceMonitor::handleWindowScaleFactorChanged); - m_polishCursorTimer.setInterval(500); - m_polishCursorTimer.setSingleShot(true); +AppearanceMonitor::AppearanceMonitor(QObject *parent) : QObject(parent) +{ + m_polishCursorTimer = new QTimer(this); + m_polishCursorTimer->setInterval(500); + m_polishCursorTimer->setSingleShot(true); + m_xsettingsSettings = new QGSettings(XSETTINGS_SCHEMA_ID, "", this); + m_marcoSettings = new QGSettings(MARCO_SCHEMA_ID, "", this); + + loadAppearance(); + loadScalingFactor(); + + connect(m_polishCursorTimer, &QTimer::timeout, this, &AppearanceMonitor::handleCursorThemeChanged); + connect(m_xsettingsSettings, &QGSettings::changed, this, &AppearanceMonitor::processXSettingsSettingChanged); + connect(m_marcoSettings, &QGSettings::changed, this, &AppearanceMonitor::processMarcoSettingChanged); } AppearanceMonitor *AppearanceMonitor::instance() @@ -107,36 +75,20 @@ AppearanceMonitor::~AppearanceMonitor() { } -void AppearanceMonitor::handleFontSettingChanged(int type, const QString &fontValue) +QFont AppearanceMonitor::appFont() const { - QString fontName; - int fontSize; - if (!parseFontValue(fontValue, fontName, fontSize)) - { - return; - } - - switch (type) - { - case APPEARANCE_FONT_TYPE_APPLICATION: - m_appFontSize = fontSize; - m_appFontName = fontName; - emit appFontChanged(appFont()); - break; - case APPEARANCE_FONT_TYPE_WINDOW_TITLE: - m_titleBarFontSize = fontSize; - m_titleBarFontName = fontName; - emit titleBarFontChanged(titleBarFont()); - return; - default: - break; - } + QFont font = QFont(QString()); + font.setFamily(m_appFontName); + font.setPointSize(m_appFontSize); + return font; } -void AppearanceMonitor::handleWindowScaleFactorChanged(int scaleFactor) +QFont AppearanceMonitor::titleBarFont() const { - m_scaleFactor = scaleFactor; - emit scaleFactorChanged(m_scaleFactor); + QFont font = QFont(QString()); + font.setFamily(m_titleBarFontName); + font.setPointSize(m_titleBarFontSize); + return font; } bool AppearanceMonitor::parseFontValue(const QString &font, QString &fontName, int &fontSize) @@ -179,151 +131,145 @@ bool AppearanceMonitor::parseFontValue(const QString &font, QString &fontName, i void AppearanceMonitor::loadAppearance() { - // application font - QString tempFontName; - int tempFontSize; - QString fontValue = m_appearanceIface->GetFont(APPEARANCE_FONT_TYPE_APPLICATION); - if (parseFontValue(fontValue, tempFontName, tempFontSize)) - { - m_appFontName = tempFontName; - m_appFontSize = tempFontSize; - qDebug("application font: %s %d", m_appFontName.toStdString().c_str(), m_appFontSize); + updateAppFont(); + updateWindowFont(); + updateIconTheme(); + updateWindowTheme(); + updateCursorTheme(); +} - emit appFontChanged(appFont()); - } - else +void AppearanceMonitor::loadScalingFactor() +{ + auto windowScaleFactor = m_xsettingsSettings->get(XSETTINGS_SCHEMA_WINDOW_SCALING_FACTOR).toInt(); + + if (m_scaleFactor != windowScaleFactor) { - qWarning() << "appearance monitor: parse application font failed!"; + m_scaleFactor = windowScaleFactor; + emit scaleFactorChanged(m_scaleFactor); } +} - // window titlebar font - fontValue = m_appearanceIface->GetFont(APPEARANCE_FONT_TYPE_WINDOW_TITLE); - if (parseFontValue(fontValue, tempFontName, tempFontSize)) - { - m_titleBarFontName = tempFontName; - m_titleBarFontSize = tempFontSize; - qDebug("title bar font: %s %d", m_titleBarFontName.toStdString().c_str(), m_titleBarFontSize); +void AppearanceMonitor::updateAppFont() +{ + QString fontName; + int fontSize = 10; - emit titleBarFontChanged(titleBarFont()); - } - else + auto fontValue = m_xsettingsSettings->get(XSETTINGS_SCHEMA_GTK_FONT_NAME).toString(); + if (!parseFontValue(fontValue, fontName, fontSize)) { - qDebug() << "parse titlebar font failed!"; + qWarning() << "Parse application font" << fontValue << "failed!"; + return; } - // icon theme - auto themeReply = m_appearanceIface->GetTheme(APPEARANCE_THEME_TYPE_ICON); - themeReply.waitForFinished(); - if (!themeReply.isError()) - { - m_iconTheme = themeReply.value(); - qDebug("icon theme: %s", m_iconTheme.toStdString().c_str()); + qDebug() << "Application font is" << fontName << ", size is" << fontSize; - emit iconThemeChanged(m_iconTheme); - } - else + if (m_appFontName != fontName || m_appFontSize != fontSize) { - qDebug() << "get icon theme failed," << themeReply.error(); + m_appFontName = fontName; + m_appFontSize = fontSize; + emit appFontChanged(appFont()); } +} - // gtk theme - themeReply = m_appearanceIface->GetTheme(APPEARANCE_THEME_TYPE_GTK); - themeReply.waitForFinished(); - if (!themeReply.isError()) - { - QString gtkThemeName = themeReply.value(); +void AppearanceMonitor::updateWindowFont() +{ + QString fontName; + int fontSize = 11; - if (gtkThemeName.contains("dark", Qt::CaseInsensitive)) - m_gtkThemeName = "kiran-dark"; - else - m_gtkThemeName = "kiran"; + // TODO:暂时先直接调用marco的gsettings,后面kiran-cc-daemon提供marco和kwin的兼容接口后再更换 + auto fontValue = m_marcoSettings->get(MARCO_SCHAME_KEY_TITLEBAR_FONT).toString(); + if (!parseFontValue(fontValue, fontName, fontSize)) + { + qWarning() << "Parse titlebar font failed!"; + } - qDebug("gtk theme: %s", m_gtkThemeName.toStdString().c_str()); + qDebug() << "Titlebar font is" << fontName << ", size is" << fontSize; - emit gtkThemeChanged(m_gtkThemeName); - } - else + if (m_titleBarFontName != fontName || m_titleBarFontSize != fontSize) { - qDebug() << "get gtk theme failed," << themeReply.error(); + m_titleBarFontName = fontName; + m_titleBarFontSize = fontSize; + emit titleBarFontChanged(titleBarFont()); } - - m_polishCursorTimer.start(); } -void AppearanceMonitor::loadScalingFactor() +void AppearanceMonitor::updateIconTheme() { - handleWindowScaleFactorChanged(m_displayIface->window_scaling_factor()); -} + auto iconTheme = m_xsettingsSettings->get(XSETTINGS_SCHEMA_NET_ICON_THEME_NAME).toString(); + qDebug() << "Icon theme is" << iconTheme; -QFont AppearanceMonitor::appFont() const -{ - QFont font = QFont(QString()); - font.setFamily(m_appFontName); - font.setPointSize(m_appFontSize); - return font; + if (iconTheme != m_iconTheme) + { + m_iconTheme = iconTheme; + emit iconThemeChanged(m_iconTheme); + } } -int AppearanceMonitor::scaleFactor() const +void AppearanceMonitor::updateWindowTheme() { - return m_scaleFactor; + QString gtkThemeName = m_xsettingsSettings->get(XSETTINGS_SCHEMA_NET_THEME_NAME).toString(); + QString windowThemeName; + if (gtkThemeName.contains("dark", Qt::CaseInsensitive)) + windowThemeName = "kiran-dark"; + else + windowThemeName = "kiran"; + + qDebug() << "Window theme is" << windowThemeName; + + if (windowThemeName != m_windowThemeName) + { + m_windowThemeName = windowThemeName; + emit gtkThemeChanged(m_windowThemeName); + } } -QFont AppearanceMonitor::titleBarFont() const +void AppearanceMonitor::updateCursorTheme() { - QFont font = QFont(QString()); - font.setFamily(m_titleBarFontName); - font.setPointSize(m_titleBarFontSize); - return font; + // 延迟通知,让QXcbCursor更新主题 + // 若未变化光标,qt5.15之前都需要合入修复补丁 + m_polishCursorTimer->start(); } -QString AppearanceMonitor::iconTheme() const +void AppearanceMonitor::handleCursorThemeChanged() { - return m_iconTheme; + emit cursorThemeChanged(); } -void AppearanceMonitor::handleThemeSettingChanged(int type, const QString &themeName) +void AppearanceMonitor::processXSettingsSettingChanged(const QString &key) { - if (type == APPEARANCE_THEME_TYPE_ICON) + qDebug() << "Xsettings gsettings key" << key << "is changed."; + + if (key == XSETTINGS_SCHEMA_GTK_FONT_NAME) { - m_iconTheme = themeName; - emit iconThemeChanged(m_iconTheme); + updateAppFont(); } - else if (type == APPEARANCE_THEME_TYPE_GTK) + else if (key == XSETTINGS_SCHEMA_NET_ICON_THEME_NAME) { - QString gtkTheme; - - if (themeName.contains("dark", Qt::CaseInsensitive)) - { - gtkTheme = "kiran-dark"; - } - else - { - gtkTheme = "kiran"; - } - - if (gtkTheme != m_gtkThemeName) - { - qDebug() << "gtk theme changed:" << themeName; - m_gtkThemeName = gtkTheme; - emit gtkThemeChanged(m_gtkThemeName); - } + updateIconTheme(); + } + else if (key == XSETTINGS_SCHEMA_NET_THEME_NAME) + { + updateWindowTheme(); + } + else if (key == XSETTINGS_SCHEMA_GTK_CURSOR_THEME_NAME) + { + updateCursorTheme(); } - else if (type == APPEARANCE_THEME_TYPE_CURSOR) + else if (key == XSETTINGS_SCHEMA_WINDOW_SCALING_FACTOR) { - // 延迟通知,让QXcbCursor更新主题 - // 若未变化光标,qt5.15之前都需要合入修复补丁 - m_polishCursorTimer.start(); + loadScalingFactor(); } } -void AppearanceMonitor::handleCursorThemeChanged() +void AppearanceMonitor::processMarcoSettingChanged(const QString &key) { - emit cursorThemeChanged(); -} + qDebug() << "Marco gsettings key" << key << "is changed."; -QString AppearanceMonitor::gtkTheme() const -{ - return m_gtkThemeName; + if (key == MARCO_SCHAME_KEY_TITLEBAR_FONT) + { + updateWindowFont(); + } } + } // namespace Platformtheme } // namespace Kiran diff --git a/plugins/platformtheme/appearance-monitor.h b/plugins/platformtheme/appearance-monitor.h index 0c14a5d101ae4b5c8cb57a61a769d0d855cc80fa..39ea1a1ed7eabdd31ae68e0fbdb76c9e5093b7e3 100644 --- a/plugins/platformtheme/appearance-monitor.h +++ b/plugins/platformtheme/appearance-monitor.h @@ -16,11 +16,8 @@ #include #include -#include -class DisplayProxy; -class AppearanceProxy; -class QDBusServiceWatcher; +class QGSettings; namespace Kiran { @@ -29,6 +26,7 @@ namespace Platformtheme class AppearanceMonitor : public QObject { Q_OBJECT + private: explicit AppearanceMonitor(QObject* parent = nullptr); @@ -38,9 +36,9 @@ public: QFont appFont() const; QFont titleBarFont() const; - QString iconTheme() const; - QString gtkTheme() const; - int scaleFactor() const; + QString iconTheme() const { return m_iconTheme; }; + QString gtkTheme() const { return m_windowThemeName; }; + int scaleFactor() const { return m_scaleFactor; }; signals: void appFontChanged(QFont font); @@ -54,12 +52,17 @@ private: static bool parseFontValue(const QString& font, QString& fontName, int& fontSize); void loadAppearance(); void loadScalingFactor(); - + + void updateAppFont(); + void updateWindowFont(); + void updateIconTheme(); + void updateWindowTheme(); + void updateCursorTheme(); + private slots: - void handleFontSettingChanged(int type, const QString& fontValue); - void handleWindowScaleFactorChanged(int scaleFactor); - void handleThemeSettingChanged(int type, const QString& themeName); void handleCursorThemeChanged(); + void processXSettingsSettingChanged(const QString& key); + void processMarcoSettingChanged(const QString& key); private: QString m_appFontName = "Noto Sans CJK"; @@ -71,15 +74,12 @@ private: int m_scaleFactor = 0; QString m_iconTheme = "hicolor"; - QString m_gtkThemeName = "kiran"; - - QTimer m_polishCursorTimer; + QString m_windowThemeName = "kiran"; - DisplayProxy* m_displayIface; - QDBusServiceWatcher* m_displayServiceWatcher; + QTimer* m_polishCursorTimer; - AppearanceProxy* m_appearanceIface; - QDBusServiceWatcher* m_appearanceServiceWatcher; + QGSettings* m_xsettingsSettings; + QGSettings* m_marcoSettings; }; } // namespace Platformtheme } // namespace Kiran \ No newline at end of file