diff --git a/CMakeLists.txt b/CMakeLists.txt index aa7465c..3059431 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -75,6 +75,7 @@ set(SOURCE_FILES set(QRC_FILES qml/qml.qrc res/res.qrc) # desktop file set(DESKTOP_FILE data/ukui-menu.desktop) +set(GSETTING_FILE data/org.ukui.menu.settings.gschema.xml) # data files #set(DATA_FILES data/xxx) @@ -113,3 +114,4 @@ install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "/usr/bin") install(FILES ${QM_FILES} DESTINATION "${UKUI_MENU_TRANSLATION_DIR}") # 安装desktop文件 install(FILES ${DESKTOP_FILE} DESTINATION "/etc/xdg/autostart") +install(FILES ${GSETTING_FILE} DESTINATION "/usr/share/glib-2.0/schemas") diff --git a/data/org.ukui.menu.settings.gschema.xml b/data/org.ukui.menu.settings.gschema.xml new file mode 100644 index 0000000..5b23445 --- /dev/null +++ b/data/org.ukui.menu.settings.gschema.xml @@ -0,0 +1,10 @@ + + + + + 300 + animation duration + Control the duration of animation + + + diff --git a/src/settings/settings.cpp b/src/settings/settings.cpp index ae8355b..bca78eb 100644 --- a/src/settings/settings.cpp +++ b/src/settings/settings.cpp @@ -21,6 +21,8 @@ #include #include +#define UKUI_MENU_SCHEMA "org.ukui.menu.settings" + #define UKUI_STYLE_SCHEMA "org.ukui.style" #define UKUI_STYLE_NAME_KEY "styleName" @@ -62,4 +64,59 @@ QVariant GlobalSetting::get(const GlobalSetting::Key& key) return m_cache.value(key); } +MenuSetting *MenuSetting::instance() +{ + static MenuSetting setting(nullptr); + return &setting; +} + +MenuSetting::MenuSetting(QObject *parent) : QObject(parent) +{ + initSettings(); +} + +void MenuSetting::initSettings() +{ + m_cache.insert(UKUI_MENU_ANIMATION_DURATION, {300}); + + QByteArray id{UKUI_MENU_SCHEMA}; + if (QGSettings::isSchemaInstalled(id)) { + m_gSettings = new QGSettings(id, "/", this); + + if (!m_gSettings) { + qWarning() << "Unable to initialize settings."; + return; + } + + for (const auto &key: m_gSettings->keys()) { + m_cache.insert(key, m_gSettings->get(key)); + } + + connect(m_gSettings, &QGSettings::changed, this, &MenuSetting::updateValue); + } +} + +QVariant MenuSetting::get(const QString &key) +{ + return m_cache.value(key, {}); +} + +bool MenuSetting::set(const QString &key, const QVariant &value) +{ + if (m_gSettings) { + m_gSettings->set(key, value); +// m_cache.insert(key,value); +// Q_EMIT changed(key); + return true; + } + + return false; +} + +void MenuSetting::updateValue(const QString &key) +{ + m_cache.insert(key, m_gSettings->get(key)); + Q_EMIT changed(key); +} + } // UkuiMenu diff --git a/src/settings/settings.h b/src/settings/settings.h index 7e39f0e..2e2bfd2 100644 --- a/src/settings/settings.h +++ b/src/settings/settings.h @@ -27,6 +27,9 @@ #define UKUI_STYLE_VALUE_LIGHT "ukui-light" #define UKUI_STYLE_VALUE_DEFAULT "ukui-default" +// ukui-menu settings +#define UKUI_MENU_ANIMATION_DURATION "animationDuration" + namespace UkuiMenu { class GlobalSetting : public QObject @@ -57,6 +60,33 @@ private: QMap m_cache; }; +class MenuSetting : public QObject +{ + Q_OBJECT +public: + static MenuSetting *instance(); + MenuSetting() = delete; + MenuSetting(const MenuSetting& obj) = delete; + MenuSetting(const MenuSetting&& obj) = delete; + + Q_INVOKABLE QVariant get(const QString& key); + Q_INVOKABLE bool set(const QString& key, const QVariant& value); + +Q_SIGNALS: + void changed(const QString& key); + +private Q_SLOTS: + void updateValue(const QString& key); + +private: + explicit MenuSetting(QObject *parent = nullptr); + void initSettings(); + +private: + QMap m_cache; + QGSettings *m_gSettings{nullptr}; +}; + } // UkuiMenu #endif //UKUI_MENU_SETTINGS_H diff --git a/src/ukui-menu-application.cpp b/src/ukui-menu-application.cpp index c0356e0..9f487d4 100644 --- a/src/ukui-menu-application.cpp +++ b/src/ukui-menu-application.cpp @@ -17,15 +17,16 @@ */ #include "ukui-menu-application.h" +#include "settings.h" +#include "commons.h" #include #include +#include #include using namespace UkuiMenu; -static UkuiMenuApplication* menuApplication = nullptr; - UkuiMenuApplication::UkuiMenuApplication(MenuMessageProcessor *processor) : QObject(nullptr) { registerQmlTypes(); @@ -41,16 +42,21 @@ void UkuiMenuApplication::startUkuiMenu() void UkuiMenuApplication::registerQmlTypes() { + const char *uri = "org.ukui.menu.core"; + int versionMajor = 1, versionMinor = 0; + CommonsModule::defineCommonsMod(uri, versionMajor, versionMinor); } void UkuiMenuApplication::initQmlEngine() { + const QUrl url(QStringLiteral("qrc:/qml/main.qml")); m_applicationEngine = new QQmlApplicationEngine(this); m_applicationEngine->addImportPath("qrc:/qml"); - const QUrl url(QStringLiteral("qrc:/qml/main.qml")); + m_applicationEngine->rootContext()->setContextProperty("menuSetting", MenuSetting::instance()); + QObject::connect(m_applicationEngine, &QQmlApplicationEngine::objectCreated, this, [url](QObject *obj, const QUrl &objUrl) { if (!obj && url == objUrl) @@ -88,7 +94,7 @@ void MenuMessageProcessor::processMessage(quint32 instanceId, const QByteArray & QStringList options = QString(message).split(' '); QCommandLineParser parser; - QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu")); + QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu.")); QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu.")); parser.addOption(showUkuiMenu); @@ -120,7 +126,7 @@ bool MenuMessageProcessor::preprocessMessage(const QStringList& message) QCommandLineParser parser; - QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu")); + QCommandLineOption showUkuiMenu({"s", "show"}, QObject::tr("Show ukui-menu.")); QCommandLineOption quitUkuiMenu({"q", "quit"}, QObject::tr("Quit ukui-menu.")); QCommandLineOption helpOption = parser.addHelpOption(); QCommandLineOption versionOption = parser.addVersionOption();