diff --git a/qml/extensions/FavoriteExtension.qml b/qml/extensions/FavoriteExtension.qml index 1bd8566..cf6564f 100644 --- a/qml/extensions/FavoriteExtension.qml +++ b/qml/extensions/FavoriteExtension.qml @@ -118,7 +118,9 @@ UkuiMenuExtension { } } onClicked: { - if (mouse.button === Qt.LeftButton) { + if (mouse.button === Qt.RightButton) { + visualModel.model.openMenu(index) + } else if (mouse.button === Qt.LeftButton) { var data = {"id": model.id}; send(data); } diff --git a/src/appdata/app-data-manager.cpp b/src/appdata/app-data-manager.cpp index 59a1d9e..170da5d 100644 --- a/src/appdata/app-data-manager.cpp +++ b/src/appdata/app-data-manager.cpp @@ -44,6 +44,10 @@ private Q_SLOTS: void onAppUpdatedAll(const QStringList &infos); void onAppDeleted(QStringList infos); +public Q_SLOTS: + void fixToFavoriteSlot(const QString &path, const int &num); + void changedFavoriteOrderSlot(const QString &path, const int &num); + private: void updateFavoriteApps(); void removeApps(QStringList& appIdList, QStringList &removedIdList); @@ -201,6 +205,7 @@ void AppDataWorker::updateApps(const UkuiSearch::ApplicationInfoMap &infos, QLis if (infos.isEmpty()) { return; } + for (const QString &info : infos.keys()) { if (m_appManager->m_normalApps.contains(info)) { DataEntity &app = m_appManager->m_normalApps[info]; @@ -268,6 +273,20 @@ void AppDataWorker::onAppDeleted(QStringList infos) updateFavoriteApps(); } +void AppDataWorker::fixToFavoriteSlot(const QString &path, const int &num) +{ + if (num == 0) { + m_applicationInfo->setFavoritesOfApp(path, 0); + } else { + m_applicationInfo->setFavoritesOfApp(path, m_appManager->m_favoriteApps.length() + 1); + } +} + +void AppDataWorker::changedFavoriteOrderSlot(const QString &path, const int &num) +{ + m_applicationInfo->setFavoritesOfApp(path, num); +} + void AppDataWorker::removeApps(QStringList &appIdList, QStringList &removedIdList) { QMutexLocker locker(&m_appManager->m_mutex); @@ -296,6 +315,8 @@ AppDataManager::AppDataManager() connect(appDataWorker, &AppDataWorker::appDeleted, this, &AppDataManager::appDeleted); connect(appDataWorker, &AppDataWorker::appUpdated, this, &AppDataManager::appUpdated); connect(appDataWorker, &AppDataWorker::favoriteAppChanged, this, &AppDataManager::favoriteAppChanged); + connect(this, &AppDataManager::fixToFavoriteSignal, appDataWorker, &AppDataWorker::fixToFavoriteSlot); + connect(this, &AppDataManager::changedFavoriteOrderSignal, appDataWorker, &AppDataWorker::changedFavoriteOrderSlot); m_workerThread.start(); } diff --git a/src/appdata/app-data-manager.h b/src/appdata/app-data-manager.h index b5e2afc..6baa7ab 100644 --- a/src/appdata/app-data-manager.h +++ b/src/appdata/app-data-manager.h @@ -54,6 +54,8 @@ Q_SIGNALS: void appDeleted(QStringList idList); void appDataChanged(); void favoriteAppChanged(); + void fixToFavoriteSignal(const QString &path, const int &num); + void changedFavoriteOrderSignal(const QString &path, const int &num); private: AppDataManager(); diff --git a/src/extension/extensions/favorite-extension.cpp b/src/extension/extensions/favorite-extension.cpp index fdb244a..98d9e7f 100644 --- a/src/extension/extensions/favorite-extension.cpp +++ b/src/extension/extensions/favorite-extension.cpp @@ -23,9 +23,17 @@ #include #include #include +#include namespace UkuiMenu { +class FavoriteMenuProvider : public MenuProvider +{ +public: + int index() override { return 512; } + QList generateActions(QObject *parent, const RequestType &type, const QVariant &data) override; +}; + class FavoriteAppsModel : public QAbstractListModel { Q_OBJECT @@ -43,6 +51,7 @@ public: QVariant data(const QModelIndex &index, int role) const override; void setFavoriteAppsData(QVector &apps); + Q_INVOKABLE void openMenu(const int &index); public Q_SLOTS: void exchangedAppsOrder(int startIndex, int endIndex); @@ -55,6 +64,8 @@ FavoriteExtension::FavoriteExtension(QObject *parent) : MenuExtensionIFace(paren { qRegisterMetaType("FavoriteAppsModel*"); + MenuManager::instance()->registerMenuProvider(new FavoriteMenuProvider); + m_favoriteAppsModel = new FavoriteAppsModel(this); m_data.insert("favoriteAppsModel", QVariant::fromValue(m_favoriteAppsModel)); @@ -153,11 +164,49 @@ void FavoriteAppsModel::setFavoriteAppsData(QVector &apps) endResetModel(); } +void FavoriteAppsModel::openMenu(const int &index) +{ + if (index < 0 || index >= m_favoriteAppsData.size()) { + return; + } + + MenuManager::instance()->showMenu(m_favoriteAppsData.at(index)); +} + void FavoriteAppsModel::exchangedAppsOrder(int startIndex, int endIndex) { int endNum = m_favoriteAppsData.at(endIndex).favorite(); QString startId = m_favoriteAppsData.at(startIndex).id(); - m_appInfoTable->changeFavoriteAppPos(startId,endNum); + AppDataManager::instance()->changedFavoriteOrderSignal(startId, endNum); +} + +QList FavoriteMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data) +{ + if (!parent || (type != DataType)) { + return {}; + } + + DataEntity app = data.value(); + if (app.type() != DataType::Normal) { + return {}; + } + + QList list; + + if (app.favorite() == 0) { + list << new QAction(QObject::tr("Fix to favorite"), parent); + QObject::connect(list.last(), &QAction::triggered, parent, [app] { + Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 1); + }); + } else { + list << new QAction(QObject::tr("Remove from favorite"), parent); + QObject::connect(list.last(), &QAction::triggered, parent, [app] { + Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 0); + }); + } + + + return list; } } // UkuiMenu diff --git a/src/menu/menu-manager.cpp b/src/menu/menu-manager.cpp index 329ab71..b3093ad 100644 --- a/src/menu/menu-manager.cpp +++ b/src/menu/menu-manager.cpp @@ -96,9 +96,9 @@ void AppMenuProvider::addToPanelAction(QObject *parent, const QString &appId, QL return; } - QString actionName = isFixedOnTaskBar ? QObject::tr("Remove From Taskbar") : QObject::tr("Add To Taskbar"); + QString actionName = isFixedOnTaskBar ? QObject::tr("Remove from taskbar") : QObject::tr("Add to taskbar"); QString functionName = isFixedOnTaskBar ? "RemoveFromTaskbar" : "AddToTaskbar"; - list << new QAction(actionName); + list << new QAction(actionName, parent); QObject::connect(list.last(), &QAction::triggered, parent, [appId, functionName] { QDBusInterface iface("com.ukui.panel.desktop", "/", "com.ukui.panel.desktop", QDBusConnection::sessionBus()); @@ -119,7 +119,7 @@ void AppMenuProvider::addToPanelAction(QObject *parent, const QString &appId, QL void AppMenuProvider::addToDesktopAction(QObject *parent, const QString &appId, QList &list) { - list << new QAction(QObject::tr("Add to desktop shortcuts")); + list << new QAction(QObject::tr("Add to desktop shortcuts"), parent); QString desktopPath = QStandardPaths::writableLocation(QStandardPaths::DesktopLocation); QString path = QString(desktopPath + "/" + QFileInfo(appId).fileName()); @@ -144,7 +144,7 @@ void AppMenuProvider::addUninstall(QObject *parent, const QString &appId, QList< return appId.contains(appName); }); if (!isSystemApp) { - list << new QAction(QObject::tr("Uninstall")); //QIcon::fromTheme("edit-delete-symbolic") + list << new QAction(QObject::tr("Uninstall"), parent); //QIcon::fromTheme("edit-delete-symbolic") QObject::connect(list.last(), &QAction::triggered, parent, [appId] { AppManager::instance()->launchBinaryApp("kylin-uninstaller", appId); });