diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp index 3d3aea6..ae8355b 100644 --- a/src/settings/settings.cpp +++ b/src/settings/settings.cpp @@ -26,45 +26,40 @@ namespace UkuiMenu { -StyleGSetting::StyleGSetting(QObject *parent) : QObject(parent) +GlobalSetting *GlobalSetting::instance() { - init(); + static GlobalSetting setting(nullptr); + return &setting; } -QVariant StyleGSetting::get(StyleGSetting::Key key) +GlobalSetting::GlobalSetting(QObject *parent) : QObject(parent) +{ + initStyleSetting(); +} + +void GlobalSetting::initStyleSetting() +{ + m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT); + if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) { + QGSettings *settings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this); + + QStringList keys = settings->keys(); + if (keys.contains(UKUI_STYLE_NAME_KEY)) { + m_cache.insert(StyleName, settings->get(UKUI_STYLE_NAME_KEY)); + } + + connect(settings, &QGSettings::changed, this, [this, settings] (const QString &key) { + if (key == UKUI_STYLE_NAME_KEY) { + m_cache.insert(StyleName, settings->get(key)); + Q_EMIT styleChanged(StyleName); + } + }); + } +} + +QVariant GlobalSetting::get(const GlobalSetting::Key& key) { return m_cache.value(key); } -void StyleGSetting::init() -{ - m_cache.insert(StyleName, UKUI_STYLE_VALUE_LIGHT); - - if (QGSettings::isSchemaInstalled(UKUI_STYLE_SCHEMA)) { - m_GSettings = new QGSettings(UKUI_STYLE_SCHEMA, {}, this); - - QStringList keys = m_GSettings->keys(); - if (keys.contains(UKUI_STYLE_NAME_KEY)) { - valueChangedSlot(UKUI_STYLE_NAME_KEY); - } - - connect(m_GSettings, &QGSettings::changed, - this, &StyleGSetting::valueChangedSlot); - } -} - -void StyleGSetting::valueChangedSlot(const QString &key) -{ - if (key == UKUI_STYLE_NAME_KEY) { - m_cache.insert(StyleName, m_GSettings->get(key).toString()); - Q_EMIT valueChanged(StyleName); - } -} - -StyleGSetting *StyleGSetting::instance() -{ - static StyleGSetting setting(nullptr); - return &setting; -} - } // UkuiMenu diff --git a/src/settings/settings.h b/src/settings/settings.h index 28fdaaf..7e39f0e 100644 --- a/src/settings/settings.h +++ b/src/settings/settings.h @@ -29,7 +29,7 @@ namespace UkuiMenu { -class StyleGSetting : public QObject +class GlobalSetting : public QObject { Q_OBJECT public: @@ -39,27 +39,22 @@ public: }; Q_ENUM(Key) - static StyleGSetting *instance(); - StyleGSetting() = delete; - StyleGSetting(const StyleGSetting& obj) = delete; - StyleGSetting(const StyleGSetting&& obj) = delete; + static GlobalSetting *instance(); + GlobalSetting() = delete; + GlobalSetting(const GlobalSetting& obj) = delete; + GlobalSetting(const GlobalSetting&& obj) = delete; -public Q_SLOTS: - QVariant get(StyleGSetting::Key key); + QVariant get(const GlobalSetting::Key& key); Q_SIGNALS: - void valueChanged(const StyleGSetting::Key& key); - -private Q_SLOTS: - void valueChangedSlot(const QString& key); + void styleChanged(const GlobalSetting::Key& key); private: - explicit StyleGSetting(QObject *parent = nullptr); - void init(); + explicit GlobalSetting(QObject *parent = nullptr); + void initStyleSetting(); private: - QGSettings *m_GSettings{nullptr}; - QMap m_cache; + QMap m_cache; }; } // UkuiMenu diff --git a/src/uiconfig/color-helper.cpp b/src/uiconfig/color-helper.cpp index 7ca7da5..60aa560 100644 --- a/src/uiconfig/color-helper.cpp +++ b/src/uiconfig/color-helper.cpp @@ -65,8 +65,8 @@ QColor ColorHelper::colorWithAlpha(ColorRole::Role role, qreal alpha) void ColorHelper::initStyleMonitor() { updateStyle(); - connect(StyleGSetting::instance(), &StyleGSetting::valueChanged, this , [this] (const StyleGSetting::Key& key) { - if (key & StyleGSetting::StyleName) { + connect(GlobalSetting::instance(), &GlobalSetting::styleChanged, this , [this] (const GlobalSetting::Key& key) { + if (key & GlobalSetting::StyleName) { updateStyle(); } }); @@ -76,7 +76,7 @@ void ColorHelper::initStyleMonitor() void ColorHelper::updateStyle() { - QString styleName = StyleGSetting::instance()->get(StyleGSetting::StyleName).toString(); + QString styleName = GlobalSetting::instance()->get(GlobalSetting::StyleName).toString(); m_isDarkStyle = (styleName != UKUI_STYLE_VALUE_LIGHT); Q_EMIT colorChanged(); diff --git a/translations/ukui-menu_bo_CN.ts b/translations/ukui-menu_bo_CN.ts index a389879..352d4e0 100644 --- a/translations/ukui-menu_bo_CN.ts +++ b/translations/ukui-menu_bo_CN.ts @@ -1,13 +1,6 @@ - - QObject - - i am a text. - - - main diff --git a/translations/ukui-menu_zh_CN.ts b/translations/ukui-menu_zh_CN.ts index 19709d8..a93db89 100644 --- a/translations/ukui-menu_zh_CN.ts +++ b/translations/ukui-menu_zh_CN.ts @@ -1,13 +1,6 @@ - - QObject - - i am a text. - - - main