diff --git a/extension/recent-file/qml/RecentFileExtension.qml b/extension/recent-file/qml/RecentFileExtension.qml index 786aa89..87a3a7e 100644 --- a/extension/recent-file/qml/RecentFileExtension.qml +++ b/extension/recent-file/qml/RecentFileExtension.qml @@ -43,6 +43,13 @@ UkuiMenuExtension { } } + onClicked: { + var data = { + "action": "closeMenu" + } + send(data); + } + AppControls2.StyleBackground { anchors.top: parent.top width: parent.width; height: 1 @@ -157,7 +164,7 @@ UkuiMenuExtension { "url": model.uri, "index": model.index } - data["action"] = mouse.button === Qt.RightButton ? "right" : "" + data["action"] = mouse.button === Qt.RightButton ? "right" : "openFile" send(data) } } diff --git a/extension/recent-file/recent-file-extension.cpp b/extension/recent-file/recent-file-extension.cpp index bc28e79..c998cdd 100644 --- a/extension/recent-file/recent-file-extension.cpp +++ b/extension/recent-file/recent-file-extension.cpp @@ -350,14 +350,20 @@ QVariantMap RecentFileExtension::data() void RecentFileExtension::receive(QVariantMap data) { - QString path = data.value("url").toString(); if (data.value("action").toString() == "right") { + QString path = data.value("url").toString(); int index = data.value("index").toInt(); creatMenu(path, index); return; + + } else if (m_contextMenu) { + m_contextMenu.data()->close(); } - openFile(path); + if (data.value("action").toString() == "openFile") { + QString path = data.value("url").toString(); + openFile(path); + } } void RecentFileExtension::openFile(const QString &fileUrl) @@ -378,29 +384,35 @@ void RecentFileExtension::openFile(const QString &fileUrl) void RecentFileExtension::creatMenu(const QString &path, const int &index) { - QMenu menu; + if (m_contextMenu) { + m_contextMenu.data()->close(); + return; + } - QAction open(QIcon::fromTheme("document-open-symbolic"), tr("Open")); - QAction remove(QIcon::fromTheme("edit-clear-symbolic"), tr("Remove from list")); - QAction clear(QIcon::fromTheme("edit-delete-symbolic"), tr("Clear list")); - QAction directory(tr("Open the directory where the file is located")); + QMenu *menu = new QMenu; + menu->setAttribute(Qt::WA_DeleteOnClose); - connect(&open, &QAction::triggered, this, [this, path]() { + QAction *open = new QAction(QIcon::fromTheme("document-open-symbolic"), tr("Open"), menu); + QAction *remove = new QAction(QIcon::fromTheme("edit-clear-symbolic"), tr("Remove from list"), menu); + QAction *clear = new QAction(QIcon::fromTheme("edit-delete-symbolic"), tr("Clear list"), menu); + QAction *directory = new QAction(tr("Open the directory where the file is located"), menu); + + connect(open, &QAction::triggered, this, [this, path]() { openFile(path); }); - connect(&remove, &QAction::triggered, this, [this, index]() { + connect(remove, &QAction::triggered, this, [this, index]() { GVFSRecentFileData::removeRecentFileByInfoId(m_recentFilesModel->getInfoId(index)); }); - connect(&clear, &QAction::triggered, this, [this]() { + connect(clear, &QAction::triggered, this, [this]() { QStringList infoIdList = m_recentFilesModel->getAllInfoId(); for (const QString &infoId : infoIdList) { GVFSRecentFileData::removeRecentFileByInfoId(infoId); } }); - connect(&directory, &QAction::triggered, this, [this, path]() { + connect(directory, &QAction::triggered, this, [this, path]() { QDBusMessage message = QDBusMessage::createMethodCall(FREEDESKTOP_FILEMANAGER_NAME, FREEDESKTOP_FILEMANAGER_PATH, FREEDESKTOP_FILEMANAGER_INTERFACE, "ShowFolders"); @@ -408,13 +420,16 @@ void RecentFileExtension::creatMenu(const QString &path, const int &index) QDBusConnection::sessionBus().asyncCall(message); }); - menu.addAction(&open); - menu.addSeparator(); - menu.addAction(&remove); - menu.addAction(&clear); - menu.addSeparator(); - menu.addAction(&directory); - menu.exec(QCursor::pos()); + menu->addAction(open); + menu->addSeparator(); + menu->addAction(remove); + menu->addAction(clear); + menu->addSeparator(); + menu->addAction(directory); + + m_contextMenu = menu; + + menu->popup(QCursor::pos()); } RecentFilesModel::RecentFilesModel(QObject *parent) : QAbstractListModel(parent) diff --git a/extension/recent-file/recent-file-extension.h b/extension/recent-file/recent-file-extension.h index 633c4f8..75683f6 100644 --- a/extension/recent-file/recent-file-extension.h +++ b/extension/recent-file/recent-file-extension.h @@ -19,6 +19,9 @@ #ifndef UKUI_MENU_RECENT_FILE_EXTENSION_H #define UKUI_MENU_RECENT_FILE_EXTENSION_H +#include +#include + class QThread; #include "menu-extension-iface.h" @@ -53,6 +56,7 @@ public: void receive(QVariantMap data) override; private: + QPointer m_contextMenu; QVector m_recentFile; QVariantMap m_data; RecentFilesModel *m_recentFilesModel = nullptr; diff --git a/src/menu/menu-manager.cpp b/src/menu/menu-manager.cpp old mode 100644 new mode 100755 index e3fa060..31e6123 --- a/src/menu/menu-manager.cpp +++ b/src/menu/menu-manager.cpp @@ -34,6 +34,8 @@ #include #include #include +#include +#include using namespace UkuiMenu; @@ -192,6 +194,8 @@ MenuManager *MenuManager::instance() class MenuManagerPrivate { public: + QPointer contextMenu; + QWindow *mainWindow {nullptr}; QList providers; }; @@ -238,21 +242,46 @@ void MenuManager::showMenu(const DataEntity &entity, const MenuInfo::Location lo void MenuManager::showMenu(const MenuProvider::RequestType &type, QVariant data, const MenuInfo::Location location, const QString &lid, const QPoint &point) { - QMenu menu; - - QList actions; - for (const auto &provider : d->providers) { - if (provider->isSupport(type)) { - actions.append(provider->generateActions(&menu, data, location, lid)); - } - } - - if (actions.isEmpty() && menu.isEmpty()) { + if (closeMenu()) { return; } - menu.addActions(actions); - menu.exec(checkPoint(point)); + QMenu *menu = new QMenu; + QList actions; + for (const auto &provider : d->providers) { + if (provider->isSupport(type)) { + actions.append(provider->generateActions(menu, data, location, lid)); + } + } + + if (actions.isEmpty() && menu->isEmpty()) { + delete menu; + return; + } + + d->contextMenu = menu; + menu->setAttribute(Qt::WA_DeleteOnClose); + if (menu->winId()) { + menu->windowHandle()->setTransientParent(d->mainWindow); + } + + menu->addActions(actions); + menu->popup(checkPoint(point)); +} + +void MenuManager::setMainWindow(QWindow *mainWindow) +{ + d->mainWindow = mainWindow; +} + +bool MenuManager::closeMenu() +{ + if (d->contextMenu) { + d->contextMenu.data()->close(); + return true; + } + + return false; } inline QPoint MenuManager::checkPoint(const QPoint &rawPoint) diff --git a/src/menu/menu-manager.h b/src/menu/menu-manager.h index 2a11552..cdf2e58 100644 --- a/src/menu/menu-manager.h +++ b/src/menu/menu-manager.h @@ -25,6 +25,7 @@ #include "data-entity.h" #include "menu-provider.h" +class QWindow; class MenuManagerPrivate; namespace UkuiMenu { @@ -39,6 +40,8 @@ public: Q_INVOKABLE void showMenu(const QString &appid, MenuInfo::Location, const QString& lid = QString(), 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, MenuInfo::Location location, const QString& lid = QString(), const QPoint &point = QPoint()); + void setMainWindow(QWindow *mainWindow); + bool closeMenu(); private: MenuManager(); diff --git a/src/utils/app-page-header-utils.cpp b/src/utils/app-page-header-utils.cpp index 0a6d86d..6737d9f 100644 --- a/src/utils/app-page-header-utils.cpp +++ b/src/utils/app-page-header-utils.cpp @@ -24,6 +24,7 @@ #include #include #include +#include namespace UkuiMenu { @@ -54,6 +55,7 @@ private: private: int m_currentIndex = 0; + QPointer m_menu; QString m_currentId; QVector m_providers; QHash m_roleNames; @@ -148,27 +150,34 @@ QString ProviderModel::currentProviderIcon() void ProviderModel::openMenu(int offset, int mouseX, int mouseY) { - QMenu menu; + if (m_menu) { + m_menu.data()->close(); + return; + } + + QMenu *menu = new QMenu; + menu->setAttribute(Qt::WA_DeleteOnClose); if (m_providers.isEmpty()) { return; } for (const ProviderInfo &info : m_providers) { - QAction *action = new QAction(info.name, &menu); + QAction *action = new QAction(info.name, menu); action->setCheckable(true); - connect(action, &QAction::triggered, &menu, [info]() { + connect(action, &QAction::triggered, menu, [info]() { DataProviderManager::instance()->activateProvider(info.id); }); - menu.addAction(action); + menu->addAction(action); } - menu.actions().at(m_currentIndex)->setChecked(true); - int x = mouseX - menu.sizeHint().width(); + menu->actions().at(m_currentIndex)->setChecked(true); + int x = mouseX - menu->sizeHint().width(); int y = mouseY + offset; - menu.exec(QPoint(x, y)); - Q_EMIT menuClosed(); + m_menu = menu; + connect(menu, &QMenu::aboutToHide, this, &ProviderModel::menuClosed); + menu->popup(QPoint(x, y)); } void ProviderModel::reactivateProvider() diff --git a/src/windows/menu-main-window.cpp b/src/windows/menu-main-window.cpp index 2bb8d60..36acd83 100644 --- a/src/windows/menu-main-window.cpp +++ b/src/windows/menu-main-window.cpp @@ -18,6 +18,7 @@ #include "menu-main-window.h" #include "settings.h" +#include "menu-manager.h" #include #include @@ -416,6 +417,8 @@ void MenuWindow::init() }); updateGeometry(); + + MenuManager::instance()->setMainWindow(this); } void MenuWindow::updateGeometry() @@ -515,6 +518,10 @@ bool MenuWindow::event(QEvent *event) if (event->type() == QEvent::UpdateRequest) { WindowHelper::removeHeaderBar(this); } + if (event->type() == QEvent::MouseButtonPress) { + MenuManager::instance()->closeMenu(); + } + return QQuickView::event(event); }