From 6be19a4576ff0b045a5cd83fd0919416797d2e09 Mon Sep 17 00:00:00 2001 From: kateleet Date: Fri, 26 Nov 2021 21:17:48 +0800 Subject: [PATCH] Add fontconfig --- CMakeLists.txt | 2 +- settings-daemon/CMakeLists.txt | 1 + settings-daemon/application.cpp | 2 +- settings-daemon/language/language.cpp | 10 +++ settings-daemon/theme/thememanager.cpp | 91 ++++++++++++++++++++++++++ settings-daemon/theme/thememanager.h | 3 + 6 files changed, 107 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e74e78..3b69bf6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake;${CMAKE_CURRENT_SOURCE_DIR}/cmake") -set(QT Core Gui Widgets Quick QuickControls2 DBus X11Extras LinguistTools) +set(QT Core Gui Widgets Quick QuickControls2 DBus Xml X11Extras LinguistTools) find_package(Qt5 REQUIRED ${QT}) # find_package(FishUI REQUIRED) find_package(PkgConfig REQUIRED) diff --git a/settings-daemon/CMakeLists.txt b/settings-daemon/CMakeLists.txt index de35122..47c71c7 100644 --- a/settings-daemon/CMakeLists.txt +++ b/settings-daemon/CMakeLists.txt @@ -66,6 +66,7 @@ target_link_libraries(${TARGET} Qt5::Widgets Qt5::DBus Qt5::X11Extras + Qt5::Xml ${X11_LIBRARIES} X11::X11 X11::Xi diff --git a/settings-daemon/application.cpp b/settings-daemon/application.cpp index 0a76195..d7bb86f 100644 --- a/settings-daemon/application.cpp +++ b/settings-daemon/application.cpp @@ -31,7 +31,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) - , m_themeManager(new ThemeManager(this)) + , m_themeManager(ThemeManager::self()) , m_brightnessManager(new BrightnessManager(this)) , m_upowerManager(new UPowerManager(this)) , m_language(new Language(this)) diff --git a/settings-daemon/language/language.cpp b/settings-daemon/language/language.cpp index a6fdbaf..019c331 100644 --- a/settings-daemon/language/language.cpp +++ b/settings-daemon/language/language.cpp @@ -19,8 +19,10 @@ #include "language.h" #include "languageadaptor.h" +#include "theme/thememanager.h" #include +#include Language::Language(QObject *parent) : QObject(parent) @@ -46,6 +48,14 @@ void Language::setLanguage(const QString &code) return; } + QSettings settings(QStringLiteral("cutefishos"), QStringLiteral("theme")); + // 中文语言下更换字体 + if (code == "zh_CN") { + settings.setValue("Font", "Noto Sans CJK SC"); + } else if (code.contains("en_")) { + settings.setValue("Font", "Noto Sans"); + } + m_settings->setValue("language", code); emit languageChanged(); diff --git a/settings-daemon/theme/thememanager.cpp b/settings-daemon/theme/thememanager.cpp index 68604ba..4987e38 100644 --- a/settings-daemon/theme/thememanager.cpp +++ b/settings-daemon/theme/thememanager.cpp @@ -20,6 +20,8 @@ #include "thememanager.h" #include "themeadaptor.h" +#include +#include #include #include #include @@ -40,6 +42,12 @@ static QString gtk3SettingsIniPath() return QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1String("/gtk-3.0/settings.ini"); } +ThemeManager *ThemeManager::self() +{ + static ThemeManager t; + return &t; +} + ThemeManager::ThemeManager(QObject *parent) : QObject(parent) , m_settings(new QSettings(QStringLiteral("cutefishos"), QStringLiteral("theme"))) @@ -70,6 +78,9 @@ ThemeManager::ThemeManager(QObject *parent) // Start the DE and need to update the settings again. initGtkConfig(); + + // 登陆后更新 fontconfig + updateFontConfig(); } bool ThemeManager::isDarkMode() @@ -163,6 +174,7 @@ void ThemeManager::setSystemFont(const QString &fontFamily) { m_settings->setValue(s_systemFontName, fontFamily); updateGtkFont(); + updateFontConfig(); emit systemFontChanged(); } @@ -175,6 +187,8 @@ QString ThemeManager::systemFixedFont() void ThemeManager::setSystemFixedFont(const QString &fontFamily) { m_settings->setValue(s_systemFixedFontName, fontFamily); + + updateFontConfig(); } qreal ThemeManager::systemFontPointSize() @@ -351,6 +365,83 @@ void ThemeManager::updateGtkIconTheme() settings.sync(); } +void ThemeManager::updateFontConfig() +{ + const QString &fimilyFont = systemFont(); + const QString &fixedFont = systemFixedFont(); + + QString content = QString("" + "" + "" + "" + "" + "serif" + "" + "" + "%1" + "%2" + "" + "" + "" + "" + "sans-serif" + "" + "" + "%3" + "%4" + "" + "" + "" + "" + "monospace" + "" + "" + "%5" + "%6" + "%7" + "" + "" + "" + "rgb" + "" + "" + ).arg(fimilyFont).arg(fimilyFont) + .arg(fimilyFont).arg(fimilyFont) + .arg(fixedFont).arg(fixedFont) + .arg(fimilyFont); + + QString targetPath(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QLatin1Char('/') + QLatin1String("fontconfig")); + + if (!QDir(targetPath).exists()) { + QDir(targetPath).mkpath(targetPath); + } + + targetPath += "/conf.d"; + + if (!QDir(targetPath).exists()) { + QDir(targetPath).mkpath(targetPath); + } + + QString xmlOut; + QXmlStreamReader reader(content); + QXmlStreamWriter writer(&xmlOut); + writer.setAutoFormatting(true); + + while (!reader.atEnd()) { + reader.readNext(); + if (!reader.isWhitespace()) { + writer.writeCurrentToken(reader); + } + } + + QFile file(targetPath + "/99-cutefish.conf"); + if (file.open(QIODevice::WriteOnly)) { + QTextStream s(&file); + s << xmlOut.toLatin1(); + file.close(); + } +} + QString ThemeManager::iconTheme() const { return m_iconTheme; diff --git a/settings-daemon/theme/thememanager.h b/settings-daemon/theme/thememanager.h index 4d373da..b975967 100644 --- a/settings-daemon/theme/thememanager.h +++ b/settings-daemon/theme/thememanager.h @@ -41,6 +41,8 @@ class ThemeManager : public QObject Q_PROPERTY(QString iconTheme READ iconTheme WRITE setIconTheme NOTIFY iconThemeChanged) public: + static ThemeManager *self(); + ThemeManager(QObject *parent = nullptr); bool isDarkMode(); @@ -103,6 +105,7 @@ private: void updateGtkFont(); void updateGtkDarkTheme(); void updateGtkIconTheme(); + void updateFontConfig(); QSettings *m_settings;