添加文件夹右键菜单

This commit is contained in:
gjq 2023-03-28 09:38:39 +08:00 committed by hewenfei
parent 6467f0ef89
commit 634688f433
4 changed files with 144 additions and 18 deletions

View File

@ -1,24 +1,37 @@
import QtQuick 2.0 import QtQuick 2.12
import QtQuick.Layouts 1.12 import QtQuick.Layouts 1.12
import QtQuick.Controls 2.5
import org.ukui.menu.core 1.0
Item { StyleBackground {
RowLayout { 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 anchors.fill: parent
Image { iconHeight: 32; iconWidth: iconHeight
Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter appName: name; appIcon: icon
Layout.preferredWidth: 32 display: Display.TextBesideIcon
Layout.preferredHeight: 32 spacing: 12
source: icon
} }
MouseArea {
id: control
anchors.fill: parent
hoverEnabled: true
acceptedButtons: Qt.LeftButton | Qt.RightButton
Text { onClicked: {
Layout.fillWidth: true if (mouse.button === Qt.RightButton) {
Layout.fillHeight: true appListView.model.openMenu(index);
Layout.leftMargin: 5 return
}
horizontalAlignment: Qt.AlignLeft if (mouse.button === Qt.LeftButton) {
verticalAlignment: Qt.AlignVCenter appListView.model.appClicked(index);
text: name }
} }
} }
} }

View File

@ -17,6 +17,7 @@
*/ */
#include "app-folder-helper.h" #include "app-folder-helper.h"
#include "menu-manager.h"
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonArray> #include <QJsonArray>
@ -24,12 +25,101 @@
#include <QDebug> #include <QDebug>
#include <QFile> #include <QFile>
#include <QDir> #include <QDir>
#include <QMenu>
#define FOLDER_FILE_PATH ".config/ukui-menu/" #define FOLDER_FILE_PATH ".config/ukui-menu/"
#define FOLDER_FILE_NAME "folder.json" #define FOLDER_FILE_NAME "folder.json"
namespace UkuiMenu { namespace UkuiMenu {
class FolderMenuProvider : public MenuProvider
{
public:
FolderMenuProvider();
int index() override { return 256; }
QList<QAction *> generateActions(QObject *parent, const RequestType &type, const QVariant &data) override;
private:
void folderMenuForNormal(QObject *parent, const QString &appId, QList<QAction *> &list);
void folderMenuForFolder(QObject *parent, const QString &folderId, QList<QAction *> &list);
};
FolderMenuProvider::FolderMenuProvider()
{
}
QList<QAction *> FolderMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data)
{
if (!parent || (type != DataType)) {
return {};
}
DataEntity app = data.value<DataEntity>();
QList<QAction *> 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<QAction *> &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<QMenu*>(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<Folder> 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<QAction *> &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; QString AppFolderHelper::s_folderConfigFile = QDir::homePath() + "/" + FOLDER_FILE_PATH + FOLDER_FILE_NAME;
AppFolderHelper *AppFolderHelper::instance() AppFolderHelper *AppFolderHelper::instance()
@ -41,6 +131,8 @@ AppFolderHelper *AppFolderHelper::instance()
AppFolderHelper::AppFolderHelper() AppFolderHelper::AppFolderHelper()
{ {
qRegisterMetaType<Folder>("Folder"); qRegisterMetaType<Folder>("Folder");
MenuManager::instance()->registerMenuProvider(new FolderMenuProvider);
if (!QFile::exists(s_folderConfigFile)) { if (!QFile::exists(s_folderConfigFile)) {
QDir dir; QDir dir;
QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH); QString folderConfigDir(QDir::homePath() + "/" + FOLDER_FILE_PATH);
@ -90,6 +182,8 @@ void AppFolderHelper::addAppToFolder(const QString &appId, const int &folderId)
folder.apps.append(appId); folder.apps.append(appId);
} }
forceSync();
Q_EMIT folderDataChanged(); Q_EMIT folderDataChanged();
} }
@ -133,6 +227,10 @@ void AppFolderHelper::removeAppFromFolder(const QString &appId, const int &folde
folder.apps.removeOne(appId); folder.apps.removeOne(appId);
} }
if (m_folders[folderId].getApps().isEmpty()) {
deleteFolder(folderId);
}
Q_EMIT folderDataChanged(); Q_EMIT folderDataChanged();
} }
@ -144,12 +242,26 @@ bool AppFolderHelper::deleteFolder(const int& folderId)
} }
if (m_folders.remove(folderId)) { if (m_folders.remove(folderId)) {
Q_EMIT folderDataChanged();
return true; return true;
} }
return false; 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<Folder> AppFolderHelper::folderData() QList<Folder> AppFolderHelper::folderData()
{ {
QMutexLocker locker(&m_mutex); QMutexLocker locker(&m_mutex);

View File

@ -70,6 +70,7 @@ public:
void addAppToNewFolder(const QString& appId, const QString& folderName); void addAppToNewFolder(const QString& appId, const QString& folderName);
void removeAppFromFolder(const QString& appId, const int& folderId); void removeAppFromFolder(const QString& appId, const int& folderId);
bool deleteFolder(const int& folderId); bool deleteFolder(const int& folderId);
void renameFolder(const int& folderId, const QString& folderName);
void forceSync(); void forceSync();

View File

@ -192,7 +192,7 @@ void MenuManager::showMenu(const MenuProvider::RequestType &type, const QVariant
actions.append(provider->generateActions(&menu, type, data)); actions.append(provider->generateActions(&menu, type, data));
} }
if (actions.isEmpty()) { if (actions.isEmpty() && menu.isEmpty()) {
return; return;
} }