diff --git a/frame/window/quickpluginwindow.cpp b/frame/window/quickpluginwindow.cpp index 9fa95a0cb..31773af36 100644 --- a/frame/window/quickpluginwindow.cpp +++ b/frame/window/quickpluginwindow.cpp @@ -434,6 +434,7 @@ void QuickPluginWindow::onUpdatePlugin(PluginsItemInterface *itemInter, const Do QuickDockItem *quickDockItem = getDockItemByPlugin(itemInter); if (quickDockItem) { + quickDockItem->updateContextMenu(); updateDockItemSize(quickDockItem); quickDockItem->update(); } @@ -828,30 +829,12 @@ void QuickDockItem::mousePressEvent(QMouseEvent *event) if (event->button() != Qt::RightButton) return QWidget::mousePressEvent(event); - if (m_contextMenu->actions().isEmpty()) { - const QString menuJson = m_pluginItem->itemContextMenu(m_itemKey); - if (menuJson.isEmpty()) - return; - - QJsonDocument jsonDocument = QJsonDocument::fromJson(menuJson.toLocal8Bit().data()); - if (jsonDocument.isNull()) - return; - - QJsonObject jsonMenu = jsonDocument.object(); - - QJsonArray jsonMenuItems = jsonMenu.value("items").toArray(); - for (auto item : jsonMenuItems) { - QJsonObject itemObj = item.toObject(); - QAction *action = new QAction(itemObj.value("itemText").toString()); - action->setCheckable(itemObj.value("isCheckable").toBool()); - action->setChecked(itemObj.value("checked").toBool()); - action->setData(itemObj.value("itemId").toString()); - action->setEnabled(itemObj.value("isActive").toBool()); - m_contextMenu->addAction(action); - } - } + static std::once_flag contextMenuInitialized; + std::call_once(contextMenuInitialized, &QuickDockItem::updateContextMenu, this); - m_contextMenu->exec(QCursor::pos()); + if (!m_contextMenu->actions().isEmpty()) { + m_contextMenu->exec(QCursor::pos()); + } } void QuickDockItem::enterEvent(QEvent *event) @@ -1017,6 +1000,31 @@ int QuickDockItem::iconSize() const return 30; } +void QuickDockItem::updateContextMenu() +{ + m_contextMenu->clear(); + const QString menuJson = m_pluginItem->itemContextMenu(m_itemKey); + if (menuJson.isEmpty()) + return; + + QJsonDocument jsonDocument = QJsonDocument::fromJson(menuJson.toLocal8Bit().data()); + if (jsonDocument.isNull()) + return; + + QJsonObject jsonMenu = jsonDocument.object(); + + QJsonArray jsonMenuItems = jsonMenu.value("items").toArray(); + for (auto item : jsonMenuItems) { + QJsonObject itemObj = item.toObject(); + QAction *action = new QAction(itemObj.value("itemText").toString()); + action->setCheckable(itemObj.value("isCheckable").toBool()); + action->setChecked(itemObj.value("checked").toBool()); + action->setData(itemObj.value("itemId").toString()); + action->setEnabled(itemObj.value("isActive").toBool()); + m_contextMenu->addAction(action); + } +} + QPoint QuickDockItem::topleftPoint() const { QPoint p = this->pos(); diff --git a/frame/window/quickpluginwindow.h b/frame/window/quickpluginwindow.h index a910ca881..3d2903893 100644 --- a/frame/window/quickpluginwindow.h +++ b/frame/window/quickpluginwindow.h @@ -95,6 +95,7 @@ class QuickDockItem : public QWidget bool canInsert() const; bool canMove() const; void hideToolTip(); + void updateContextMenu(); QSize suitableSize() const;