From 634688f433407b4bff89611195aa02f07a9ad516 Mon Sep 17 00:00:00 2001 From: gjq Date: Tue, 28 Mar 2023 09:38:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E5=A4=B9?= =?UTF-8?q?=E5=8F=B3=E9=94=AE=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qml/AppControls2/FolderItem.qml | 47 ++++++++----- src/appdata/app-folder-helper.cpp | 112 ++++++++++++++++++++++++++++++ src/appdata/app-folder-helper.h | 1 + src/menu/menu-manager.cpp | 2 +- 4 files changed, 144 insertions(+), 18 deletions(-) diff --git a/qml/AppControls2/FolderItem.qml b/qml/AppControls2/FolderItem.qml index 3952343..8274c88 100644 --- a/qml/AppControls2/FolderItem.qml +++ b/qml/AppControls2/FolderItem.qml @@ -1,24 +1,37 @@ -import QtQuick 2.0 +import QtQuick 2.12 import QtQuick.Layouts 1.12 +import QtQuick.Controls 2.5 +import org.ukui.menu.core 1.0 -Item { - RowLayout { +StyleBackground { + radius: 4 + useStyleTransparent: false + alpha: control.containsPress ? 0.82 : control.containsMouse ? 0.55 : 0.00 + ToolTip.visible: content.textTruncated && control.containsMouse + ToolTip.text: name + + IconLabel { + id: content anchors.fill: parent - Image { - Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter - Layout.preferredWidth: 32 - Layout.preferredHeight: 32 - source: icon - } + iconHeight: 32; iconWidth: iconHeight + appName: name; appIcon: icon + display: Display.TextBesideIcon + spacing: 12 + } + MouseArea { + id: control + anchors.fill: parent + hoverEnabled: true + acceptedButtons: Qt.LeftButton | Qt.RightButton - Text { - Layout.fillWidth: true - Layout.fillHeight: true - Layout.leftMargin: 5 - - horizontalAlignment: Qt.AlignLeft - verticalAlignment: Qt.AlignVCenter - text: name + onClicked: { + if (mouse.button === Qt.RightButton) { + appListView.model.openMenu(index); + return + } + if (mouse.button === Qt.LeftButton) { + appListView.model.appClicked(index); + } } } } diff --git a/src/appdata/app-folder-helper.cpp b/src/appdata/app-folder-helper.cpp index d0f5aa6..37fb058 100644 --- a/src/appdata/app-folder-helper.cpp +++ b/src/appdata/app-folder-helper.cpp @@ -17,6 +17,7 @@ */ #include "app-folder-helper.h" +#include "menu-manager.h" #include #include @@ -24,12 +25,101 @@ #include #include #include +#include #define FOLDER_FILE_PATH ".config/ukui-menu/" #define FOLDER_FILE_NAME "folder.json" namespace UkuiMenu { +class FolderMenuProvider : public MenuProvider +{ +public: + FolderMenuProvider(); + int index() override { return 256; } + QList generateActions(QObject *parent, const RequestType &type, const QVariant &data) override; + +private: + void folderMenuForNormal(QObject *parent, const QString &appId, QList &list); + void folderMenuForFolder(QObject *parent, const QString &folderId, QList &list); +}; + +FolderMenuProvider::FolderMenuProvider() +{ + +} + +QList FolderMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data) +{ + if (!parent || (type != DataType)) { + return {}; + } + + DataEntity app = data.value(); + QList list; + if (app.type() == DataType::Normal) { + // 应用菜单 + folderMenuForNormal(parent, app.id(), list); + return list; + } + if (app.type() == DataType::Folder) { + //文件夹菜单 + folderMenuForFolder(parent, app.id(), list); + return list; + } + return list; +} + +void FolderMenuProvider::folderMenuForNormal(QObject *parent, const QString &appId, QList &list) +{ + Folder folder; + if (AppFolderHelper::instance()->searchFolderByAppName(appId, folder)) { + //从当前文件夹移除 + list << new QAction(QObject::tr("Remove from folder"), parent); + QObject::connect(list.last(), &QAction::triggered, parent, [appId, folder] { + AppFolderHelper::instance()->removeAppFromFolder(appId, folder.getId()); + AppFolderHelper::instance()->forceSync(); + }); + return; + } + + QMenu *menu = static_cast(parent); + //添加到应用组 + QMenu *subMenu = new QMenu(QObject::tr("Add to folder"), menu); + + QAction* newAct = new QAction(QObject::tr("Add to new folder"), subMenu); + QObject::connect(newAct, &QAction::triggered, parent, [appId] { + AppFolderHelper::instance()->addAppToNewFolder(appId, ""); + AppFolderHelper::instance()->forceSync(); + }); + subMenu->addAction(newAct); + + QList folders = AppFolderHelper::instance()->folderData(); + for (const Folder &folder : folders) { + QString name = QObject::tr("Add to %1").arg(folder.getName()); + QAction* act = new QAction(name, subMenu); + QObject::connect(act, &QAction::triggered, parent, [appId, folder] { + AppFolderHelper::instance()->addAppToFolder(appId, folder.getId()); + }); + subMenu->addAction(act); + } + menu->addMenu(subMenu); +} + +void FolderMenuProvider::folderMenuForFolder(QObject *parent, const QString &folderId, QList &list) +{ +// list << new QAction(QObject::tr("Rename"), parent); +// QObject::connect(list.last(), &QAction::triggered, parent, [folderId] { +// AppFolderHelper::instance()->renameFolder(folderId.toInt(), "new name"); +// }); + + list << new QAction(QObject::tr("Dissolve folder"), parent); + QObject::connect(list.last(), &QAction::triggered, parent, [folderId] { + AppFolderHelper::instance()->deleteFolder(folderId.toInt()); + AppFolderHelper::instance()->forceSync(); + }); +} + QString AppFolderHelper::s_folderConfigFile = QDir::homePath() + "/" + FOLDER_FILE_PATH + FOLDER_FILE_NAME; AppFolderHelper *AppFolderHelper::instance() @@ -41,6 +131,8 @@ AppFolderHelper *AppFolderHelper::instance() AppFolderHelper::AppFolderHelper() { qRegisterMetaType("Folder"); + MenuManager::instance()->registerMenuProvider(new FolderMenuProvider); + if (!QFile::exists(s_folderConfigFile)) { QDir dir; QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH); @@ -90,6 +182,8 @@ void AppFolderHelper::addAppToFolder(const QString &appId, const int &folderId) folder.apps.append(appId); } + forceSync(); + Q_EMIT folderDataChanged(); } @@ -133,6 +227,10 @@ void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folde folder.apps.removeOne(appId); } + if (m_folders[folderId].getApps().isEmpty()) { + deleteFolder(folderId); + } + Q_EMIT folderDataChanged(); } @@ -144,12 +242,26 @@ bool AppFolderHelper::deleteFolder(const int& folderId) } if (m_folders.remove(folderId)) { + Q_EMIT folderDataChanged(); return true; } return false; } +void AppFolderHelper::renameFolder(const int &folderId, const QString &folderName) +{ + { + QMutexLocker locker(&m_mutex); + if (!m_folders.contains(folderId)) { + return; + } + m_folders[folderId].name = folderName; + } + + Q_EMIT folderDataChanged(); +} + QList AppFolderHelper::folderData() { QMutexLocker locker(&m_mutex); diff --git a/src/appdata/app-folder-helper.h b/src/appdata/app-folder-helper.h index bc0a7b5..d147b84 100644 --- a/src/appdata/app-folder-helper.h +++ b/src/appdata/app-folder-helper.h @@ -70,6 +70,7 @@ public: void addAppToNewFolder(const QString& appId, const QString& folderName); void removeAppFromFolder(const QString& appId, const int& folderId); bool deleteFolder(const int& folderId); + void renameFolder(const int& folderId, const QString& folderName); void forceSync(); diff --git a/src/menu/menu-manager.cpp b/src/menu/menu-manager.cpp index e159d4a..596a613 100644 --- a/src/menu/menu-manager.cpp +++ b/src/menu/menu-manager.cpp @@ -192,7 +192,7 @@ void MenuManager::showMenu(const MenuProvider::RequestType &type, const QVariant actions.append(provider->generateActions(&menu, type, data)); } - if (actions.isEmpty()) { + if (actions.isEmpty() && menu.isEmpty()) { return; }