forked from openkylin/ukui-menu
添加文件夹右键菜单
This commit is contained in:
parent
6467f0ef89
commit
634688f433
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
*/
|
||||
|
||||
#include "app-folder-helper.h"
|
||||
#include "menu-manager.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonArray>
|
||||
|
@ -24,12 +25,101 @@
|
|||
#include <QDebug>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QMenu>
|
||||
|
||||
#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<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;
|
||||
|
||||
AppFolderHelper *AppFolderHelper::instance()
|
||||
|
@ -41,6 +131,8 @@ AppFolderHelper *AppFolderHelper::instance()
|
|||
AppFolderHelper::AppFolderHelper()
|
||||
{
|
||||
qRegisterMetaType<Folder>("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<Folder> AppFolderHelper::folderData()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue