增加判断右键位置功能

This commit is contained in:
hewenfei 2023-05-06 09:20:34 +08:00 committed by He Sir
parent 07410beab5
commit 0098121472
15 changed files with 180 additions and 78 deletions

View File

@ -118,6 +118,7 @@ set(SOURCE_FILES
src/utils/app-page-header-utils.cpp src/utils/app-page-header-utils.h src/utils/app-page-header-utils.cpp src/utils/app-page-header-utils.h
src/utils/power-button.cpp src/utils/power-button.h src/utils/power-button.cpp src/utils/power-button.h
src/utils/app-manager.cpp src/utils/app-manager.h src/utils/app-manager.cpp src/utils/app-manager.h
src/menu/menu-provider.h
src/menu/menu-manager.cpp src/menu/menu-manager.h src/menu/menu-manager.cpp src/menu/menu-manager.h
src/items/theme-icon.h src/items/theme-icon.cpp src/items/theme-icon.h src/items/theme-icon.cpp
) )

View File

@ -85,7 +85,7 @@ Item {
Drag.hotSpot.y: height / 2 Drag.hotSpot.y: height / 2
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
appListView.model.openMenu(index); appListView.model.openMenu(index, MenuInfo.AppList);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {
@ -156,7 +156,7 @@ Item {
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
appListView.model.openMenu(index); appListView.model.openMenu(index, MenuInfo.AppList);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {

View File

@ -72,7 +72,7 @@ SwipeView {
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
menuManager.showMenu(id); menuManager.showMenu(id, MenuInfo.FolderPage);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {

View File

@ -212,7 +212,7 @@ RowLayout {
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
parent.parent.GridView.view.model.openMenu(index); modelManager.getAppModel().openMenu(index, MenuInfo.FullScreen);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {
@ -268,7 +268,7 @@ RowLayout {
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
parent.parent.GridView.view.model.openMenu(index); modelManager.getAppModel().openMenu(index, MenuInfo.FullScreen);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {
@ -362,6 +362,7 @@ RowLayout {
anchors.centerIn: parent anchors.centerIn: parent
hoverEnabled: true hoverEnabled: true
width: 170; height: width width: 170; height: width
acceptedButtons: Qt.LeftButton | Qt.RightButton
AppControls2.StyleBackground { AppControls2.StyleBackground {
anchors.fill: parent anchors.fill: parent
@ -379,9 +380,15 @@ RowLayout {
} }
} }
onClicked: { onClicked: function (mouse) {
//console.log("clicked", Object.keys(model)) if (mouse.button === Qt.RightButton) {
labelRepeater.model.openApp(labelAppsGridView.labelIndex, model.index); labelRepeater.model.openMenu(labelAppsGridView.labelIndex, model.index);
return;
}
if (mouse.button === Qt.LeftButton) {
labelRepeater.model.openApp(labelAppsGridView.labelIndex, model.index);
return;
}
} }
} }
} }

View File

@ -275,7 +275,7 @@ Loader {
} }
onClicked: { onClicked: {
if (mouse.button === Qt.RightButton) { if (mouse.button === Qt.RightButton) {
menuManager.showMenu(id); menuManager.showMenu(id, MenuInfo.FullScreenFolder);
return; return;
} }
if (mouse.button === Qt.LeftButton) { if (mouse.button === Qt.LeftButton) {
@ -305,4 +305,3 @@ Loader {
} }
} }

View File

@ -40,11 +40,12 @@ class FolderMenuProvider : public MenuProvider
public: public:
FolderMenuProvider(); FolderMenuProvider();
int index() override { return 256; } int index() override { return 256; }
QList<QAction *> generateActions(QObject *parent, const RequestType &type, const QVariant &data) override; bool isSupport(const RequestType &type) override;
QList<QAction *> generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location, const QString &locationId) override;
private: private:
void folderMenuForNormal(QObject *parent, const QString &appId, QList<QAction *> &list); static void folderMenuForNormal(QObject *parent, const QString &appId, QList<QAction *> &list);
void folderMenuForFolder(QObject *parent, const QString &folderId, QList<QAction *> &list); static void dissolveFolder(QObject *parent, const QString &folderId, QList<QAction *> &list);
static void renameFolder(QObject *parent, const QString &folderId, QList<QAction *> &list);
}; };
FolderMenuProvider::FolderMenuProvider() FolderMenuProvider::FolderMenuProvider()
@ -52,24 +53,52 @@ FolderMenuProvider::FolderMenuProvider()
} }
QList<QAction *> FolderMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data) bool FolderMenuProvider::isSupport(const MenuProvider::RequestType &type)
{ {
if (!parent || (type != DataType)) { return type == DataType;
}
QList<QAction *>
FolderMenuProvider::generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location,
const QString &locationId)
{
if (!parent) {
return {}; return {};
} }
DataEntity app = data.value<DataEntity>(); DataEntity app = data.value<DataEntity>();
QList<QAction *> list; QList<QAction *> list;
if (app.type() == DataType::Normal) {
// 应用菜单 switch (location) {
folderMenuForNormal(parent, app.id(), list); case MenuInfo::FullScreen: {
return list; if (app.type() == DataType::Folder) {
} dissolveFolder(parent, app.id(), list);
if (app.type() == DataType::Folder) { break;
//文件夹菜单 }
folderMenuForFolder(parent, app.id(), list); }
return list; case MenuInfo::AppList: {
if (app.type() == DataType::Folder) {
renameFolder(parent, app.id(), list);
dissolveFolder(parent, app.id(), list);
break;
}
if (app.type() != DataType::Normal || locationId != "all") {
break;
}
}
case MenuInfo::FullScreenFolder:
case MenuInfo::FolderPage: {
if (app.type() == DataType::Normal) {
folderMenuForNormal(parent, app.id(), list);
}
break;
}
case MenuInfo::Extension:
default:
break;
} }
return list; return list;
} }
@ -98,24 +127,19 @@ void FolderMenuProvider::folderMenuForNormal(QObject *parent, const QString &app
subMenu->addAction(newAct); subMenu->addAction(newAct);
QList<Folder> folders = AppFolderHelper::instance()->folderData(); QList<Folder> folders = AppFolderHelper::instance()->folderData();
for (const Folder &folder : folders) { for (const Folder &f : folders) {
QString name = QObject::tr("Add to \"%1\"").arg(folder.getName()); QString name = QObject::tr("Add to \"%1\"").arg(f.getName());
QAction* act = new QAction(name, subMenu); QAction* act = new QAction(name, subMenu);
QObject::connect(act, &QAction::triggered, parent, [appId, folder] { QObject::connect(act, &QAction::triggered, parent, [appId, f] {
AppFolderHelper::instance()->addAppToFolder(appId, folder.getId()); AppFolderHelper::instance()->addAppToFolder(appId, f.getId());
}); });
subMenu->addAction(act); subMenu->addAction(act);
} }
menu->addMenu(subMenu); menu->addMenu(subMenu);
} }
void FolderMenuProvider::folderMenuForFolder(QObject *parent, const QString &folderId, QList<QAction *> &list) void FolderMenuProvider::dissolveFolder(QObject *parent, const QString &folderId, QList<QAction *> &list)
{ {
list << new QAction(QObject::tr("Rename"), parent);
QObject::connect(list.last(), &QAction::triggered, parent, [folderId] {
ModelManager::instance()->getAppModel()->toRenameFolder(folderId);
});
list << new QAction(QObject::tr("Dissolve folder"), parent); list << new QAction(QObject::tr("Dissolve folder"), parent);
QObject::connect(list.last(), &QAction::triggered, parent, [folderId] { QObject::connect(list.last(), &QAction::triggered, parent, [folderId] {
AppFolderHelper::instance()->deleteFolder(folderId.toInt()); AppFolderHelper::instance()->deleteFolder(folderId.toInt());
@ -123,6 +147,14 @@ void FolderMenuProvider::folderMenuForFolder(QObject *parent, const QString &fol
}); });
} }
void FolderMenuProvider::renameFolder(QObject *parent, const QString &folderId, QList<QAction *> &list)
{
list << new QAction(QObject::tr("Rename"), parent);
QObject::connect(list.last(), &QAction::triggered, parent, [folderId] {
ModelManager::instance()->getAppModel()->toRenameFolder(folderId);
});
}
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()

View File

@ -31,7 +31,8 @@ class FavoriteMenuProvider : public MenuProvider
{ {
public: public:
int index() override { return 512; } int index() override { return 512; }
QList<QAction *> generateActions(QObject *parent, const RequestType &type, const QVariant &data) override; bool isSupport(const RequestType &type) override;
QList<QAction *> generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location, const QString &locationId) override;
}; };
class FavoriteAppsModel : public QAbstractListModel class FavoriteAppsModel : public QAbstractListModel
@ -170,7 +171,7 @@ void FavoriteAppsModel::openMenu(const int &index)
return; return;
} }
MenuManager::instance()->showMenu(m_favoriteAppsData.at(index)); MenuManager::instance()->showMenu(m_favoriteAppsData.at(index), MenuInfo::Extension, "favorite");
} }
void FavoriteAppsModel::exchangedAppsOrder(int startIndex, int endIndex) void FavoriteAppsModel::exchangedAppsOrder(int startIndex, int endIndex)
@ -180,9 +181,16 @@ void FavoriteAppsModel::exchangedAppsOrder(int startIndex, int endIndex)
AppDataManager::instance()->changedFavoriteOrderSignal(startId, endNum); AppDataManager::instance()->changedFavoriteOrderSignal(startId, endNum);
} }
QList<QAction *> FavoriteMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data) bool FavoriteMenuProvider::isSupport(const MenuProvider::RequestType &type)
{ {
if (!parent || (type != DataType)) { return type == DataType;
}
QList<QAction *>
FavoriteMenuProvider::generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location,
const QString &locationId)
{
if (!parent) {
return {}; return {};
} }
@ -193,19 +201,29 @@ QList<QAction *> FavoriteMenuProvider::generateActions(QObject *parent, const Me
QList<QAction *> list; QList<QAction *> list;
if (app.favorite() == 0) { switch (location) {
list << new QAction(QObject::tr("Fix to favorite"), parent); case MenuInfo::AppList:
QObject::connect(list.last(), &QAction::triggered, parent, [app] { case MenuInfo::FolderPage:
Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 1); case MenuInfo::Extension: {
}); if (app.favorite() == 0) {
} else { list << new QAction(QObject::tr("Fix to favorite"), parent);
list << new QAction(QObject::tr("Remove from favorite"), parent); QObject::connect(list.last(), &QAction::triggered, parent, [app] {
QObject::connect(list.last(), &QAction::triggered, parent, [app] { Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 1);
Q_EMIT AppDataManager::instance()->fixToFavoriteSignal(app.id(), 0); });
}); } else if (locationId == "favorite") {
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);
});
}
break;
}
case MenuInfo::FullScreen:
case MenuInfo::FullScreenFolder:
default:
break;
} }
return list; return list;
} }

View File

@ -41,7 +41,8 @@ class AppMenuProvider : public MenuProvider
public: public:
AppMenuProvider(); AppMenuProvider();
int index() override { return 1024; } int index() override { return 1024; }
QList<QAction *> generateActions(QObject *parent, const RequestType &type, const QVariant &data) override; bool isSupport(const RequestType &type) override;
QList<QAction *> generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location, const QString &locationId) override;
private: private:
void addToTop(QObject *parent, const QString &appId, const int &appTop, QList<QAction *> &list); void addToTop(QObject *parent, const QString &appId, const int &appTop, QList<QAction *> &list);
@ -62,10 +63,16 @@ AppMenuProvider::AppMenuProvider()
} }
} }
QList<QAction *> bool AppMenuProvider::isSupport(const MenuProvider::RequestType &type)
AppMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestType &type, const QVariant &data)
{ {
if (!parent || (type != DataType)) { return type == DataType;
}
QList<QAction *>
AppMenuProvider::generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location,
const QString &locationId)
{
if (!parent) {
return {}; return {};
} }
@ -78,17 +85,27 @@ AppMenuProvider::generateActions(QObject *parent, const MenuProvider::RequestTyp
QString appId = app.id(); QString appId = app.id();
int appTop = app.top(); int appTop = app.top();
//置顶 switch (location) {
addToTop(parent, appId, appTop ,list); case MenuInfo::AppList: {
//置顶
// 添加到任务栏 if (locationId == "all") {
addToPanelAction(parent, appId, list); addToTop(parent, appId, appTop, list);
}
//添加到桌面快捷方式 }
addToDesktopAction(parent, appId, list); case MenuInfo::Extension:
case MenuInfo::FolderPage:
// 卸载 case MenuInfo::FullScreen:
addUninstall(parent, appId, list); case MenuInfo::FullScreenFolder:
// 卸载
addToPanelAction(parent, appId, list);
// 添加到任务栏
addUninstall(parent, appId, list);
//添加到桌面快捷方式
addToDesktopAction(parent, appId, list);
break;
default:
break;
}
return list; return list;
} }
@ -213,26 +230,29 @@ void MenuManager::registerMenuProvider(MenuProvider *provider)
}); });
} }
void MenuManager::showMenu(const QString &appid, const QPoint &point) void MenuManager::showMenu(const QString &appid, const MenuInfo::Location location, const QString &lid, const QPoint &point)
{ {
DataEntity app; DataEntity app;
if (AppDataManager::instance()->getApp(appid, app)) { if (AppDataManager::instance()->getApp(appid, app)) {
showMenu(MenuProvider::DataType, QVariant::fromValue(app), point); showMenu(MenuProvider::DataType, QVariant::fromValue(app), location, lid, point);
} }
} }
void MenuManager::showMenu(const DataEntity &entity, const QPoint &point) void MenuManager::showMenu(const DataEntity &entity, const MenuInfo::Location location, const QString &lid,
const QPoint &point)
{ {
showMenu(MenuProvider::DataType, QVariant::fromValue(entity), point); showMenu(MenuProvider::DataType, QVariant::fromValue(entity), location, lid, point);
} }
void MenuManager::showMenu(const MenuProvider::RequestType &type, QVariant data, const QPoint &point) void MenuManager::showMenu(const MenuProvider::RequestType &type, QVariant data, const MenuInfo::Location location, const QString &lid, const QPoint &point)
{ {
QMenu menu; QMenu menu;
QList<QAction*> actions; QList<QAction*> actions;
for (const auto &provider : d->providers) { for (const auto &provider : d->providers) {
actions.append(provider->generateActions(&menu, type, data)); if (provider->isSupport(type)) {
actions.append(provider->generateActions(&menu, data, location, lid));
}
} }
if (actions.isEmpty() && menu.isEmpty()) { if (actions.isEmpty() && menu.isEmpty()) {

View File

@ -36,9 +36,9 @@ public:
static MenuManager *instance(); static MenuManager *instance();
~MenuManager() override; ~MenuManager() override;
void registerMenuProvider(MenuProvider *provider); void registerMenuProvider(MenuProvider *provider);
Q_INVOKABLE void showMenu(const QString &appid, const QPoint &point = QPoint()); Q_INVOKABLE void showMenu(const QString &appid, MenuInfo::Location, const QString& lid = QString(), const QPoint &point = QPoint());
void showMenu(const DataEntity &entity, const QPoint &point = QPoint()); void showMenu(const DataEntity &entity, MenuInfo::Location location, const QString& lid = QString(), const QPoint &point = QPoint());
void showMenu(const MenuProvider::RequestType &type, QVariant data, const QPoint &point = QPoint()); void showMenu(const MenuProvider::RequestType &type, QVariant data, MenuInfo::Location location, const QString& lid = QString(), const QPoint &point = QPoint());
private: private:
MenuManager(); MenuManager();

View File

@ -28,6 +28,20 @@
namespace UkuiMenu { namespace UkuiMenu {
class MenuInfo
{
Q_GADGET
public:
enum Location {
AppList = 0,
Extension,
FolderPage,
FullScreen,
FullScreenFolder,
};
Q_ENUM(Location)
};
class MenuProvider class MenuProvider
{ {
public: public:
@ -37,7 +51,8 @@ public:
}; };
virtual ~MenuProvider() = default; virtual ~MenuProvider() = default;
virtual int index() = 0; virtual int index() = 0;
virtual QList<QAction*> generateActions(QObject *parent, const RequestType &type, const QVariant &data) = 0; virtual bool isSupport(const RequestType &type) = 0;
virtual QList<QAction*> generateActions(QObject *parent, const QVariant &data, const MenuInfo::Location &location, const QString &locationId) = 0;
}; };
} // UkuiMenu } // UkuiMenu

View File

@ -177,4 +177,10 @@ inline void AppGroupModel::resetModel(QVector<LabelItem> &labels)
d->labelIndex = QVector<int>(d->labels.size(), -1); d->labelIndex = QVector<int>(d->labels.size(), -1);
} }
void AppGroupModel::openMenu(int labelIndex, int appIndex)
{
int index = d->labelIndex.at(labelIndex);
d->appModel->openMenu(++index + appIndex, MenuInfo::FullScreen);
}
} // UkuiMenu } // UkuiMenu

View File

@ -40,6 +40,7 @@ public:
bool containLabel(); bool containLabel();
Q_INVOKABLE void openMenu(int labelIndex, int appIndex);
Q_INVOKABLE void openApp(int labelIndex, int appIndex); Q_INVOKABLE void openApp(int labelIndex, int appIndex);
Q_INVOKABLE int getLabelIndex(const QString &labelId); Q_INVOKABLE int getLabelIndex(const QString &labelId);

View File

@ -187,13 +187,13 @@ void AppModel::insertData(QVector<DataEntity> &data, int index)
Q_EMIT endInsertRows(); Q_EMIT endInsertRows();
} }
void AppModel::openMenu(const int &index) void AppModel::openMenu(const int &index, MenuInfo::Location location)
{ {
if (index < 0 || index >= m_apps.size()) { if (index < 0 || index >= m_apps.size()) {
return; return;
} }
MenuManager::instance()->showMenu(m_apps.at(index)); MenuManager::instance()->showMenu(m_apps.at(index), location, DataProviderManager::instance()->activatedProvider());
} }
void AppModel::renameFolder(const QString &index, const QString &folderName) void AppModel::renameFolder(const QString &index, const QString &folderName)

View File

@ -24,6 +24,7 @@
#include "commons.h" #include "commons.h"
#include "data-provider-manager.h" #include "data-provider-manager.h"
#include "menu-provider.h"
namespace UkuiMenu { namespace UkuiMenu {
@ -41,7 +42,7 @@ public:
Q_INVOKABLE int getLabelIndex(const QString &id); Q_INVOKABLE int getLabelIndex(const QString &id);
Q_INVOKABLE QVariantList folderApps(const QString &folderName); Q_INVOKABLE QVariantList folderApps(const QString &folderName);
Q_INVOKABLE void appClicked(const int &index); Q_INVOKABLE void appClicked(const int &index);
Q_INVOKABLE void openMenu(const int &index); Q_INVOKABLE void openMenu(const int &index, MenuInfo::Location location);
Q_INVOKABLE void renameFolder(const QString &index, const QString &folderName); Q_INVOKABLE void renameFolder(const QString &index, const QString &folderName);
Q_INVOKABLE void addAppsToFolder(const QString &appIdA, const QString &appIdB, const QString &folderName); Q_INVOKABLE void addAppsToFolder(const QString &appIdA, const QString &appIdB, const QString &folderName);
Q_INVOKABLE void addAppToFolder(const QString &appId, const QString &folderId); Q_INVOKABLE void addAppToFolder(const QString &appId, const QString &folderId);

View File

@ -66,8 +66,10 @@ void UkuiMenuApplication::registerQmlTypes()
// commons // commons
qRegisterMetaType<UkuiMenu::DataType::Type>("DataType::Type"); qRegisterMetaType<UkuiMenu::DataType::Type>("DataType::Type");
qRegisterMetaType<UkuiMenu::DataEntity>("DataEntity"); qRegisterMetaType<UkuiMenu::DataEntity>("DataEntity");
qRegisterMetaType<UkuiMenu::MenuInfo::Location>("MenuInfo::Location");
qmlRegisterUncreatableType<Display>(uri, versionMajor, versionMinor, "Display", "Use enums only."); qmlRegisterUncreatableType<Display>(uri, versionMajor, versionMinor, "Display", "Use enums only.");
qmlRegisterUncreatableType<UkuiMenu::DataType>(uri, versionMajor, versionMinor, "DataType", "Use enums only"); qmlRegisterUncreatableType<UkuiMenu::DataType>(uri, versionMajor, versionMinor, "DataType", "Use enums only");
qmlRegisterUncreatableType<UkuiMenu::MenuInfo>(uri, versionMajor, versionMinor, "MenuInfo", "Use enums only.");
// qmlRegisterUncreatableType<UkuiMenu::DataEntity>(uri, versionMajor, versionMinor, "DataEntity", "unknown"); // qmlRegisterUncreatableType<UkuiMenu::DataEntity>(uri, versionMajor, versionMinor, "DataEntity", "unknown");
// vis colors // vis colors