mirror of
https://github.com/linuxdeepin/dde-dock.git
synced 2025-06-04 17:33:05 +00:00
feat(ContextMenu):change DBusMenu to QMenu
This commit is contained in:
parent
5f0ea1692c
commit
d6339d1183
@ -20,8 +20,6 @@
|
||||
*/
|
||||
|
||||
#include "dockitem.h"
|
||||
#include "dbus/dbusmenu.h"
|
||||
#include "dbus/dbusmenumanager.h"
|
||||
#include "components/hoverhighlighteffect.h"
|
||||
|
||||
#include <QMouseEvent>
|
||||
@ -33,15 +31,15 @@ DisplayMode DockItem::DockDisplayMode = DisplayMode::Efficient;
|
||||
QPointer<DockPopupWindow> DockItem::PopupWindow(nullptr);
|
||||
|
||||
DockItem::DockItem(QWidget *parent)
|
||||
: QWidget(parent),
|
||||
m_hover(false),
|
||||
m_popupShown(false),
|
||||
m_tapAndHold(false),
|
||||
m_draging(false),
|
||||
m_hoverEffect(new HoverHighlightEffect(this)),
|
||||
m_popupTipsDelayTimer(new QTimer(this)),
|
||||
m_popupAdjustDelayTimer(new QTimer(this)),
|
||||
m_menuManagerInter(new DBusMenuManager(this))
|
||||
: QWidget(parent)
|
||||
, m_hover(false)
|
||||
, m_popupShown(false)
|
||||
, m_tapAndHold(false)
|
||||
, m_draging(false)
|
||||
, m_hoverEffect(new HoverHighlightEffect(this))
|
||||
, m_popupTipsDelayTimer(new QTimer(this))
|
||||
, m_popupAdjustDelayTimer(new QTimer(this))
|
||||
|
||||
{
|
||||
if (PopupWindow.isNull()) {
|
||||
DockPopupWindow *arrowRectangle = new DockPopupWindow(nullptr);
|
||||
@ -64,11 +62,12 @@ DockItem::DockItem(QWidget *parent)
|
||||
|
||||
connect(m_popupTipsDelayTimer, &QTimer::timeout, this, &DockItem::showHoverTips);
|
||||
connect(m_popupAdjustDelayTimer, &QTimer::timeout, this, &DockItem::updatePopupPosition, Qt::QueuedConnection);
|
||||
connect(&m_contextMenu, &QMenu::triggered, this, &DockItem::menuActionClicked);
|
||||
|
||||
grabGesture(Qt::TapAndHoldGesture);
|
||||
|
||||
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
|
||||
setMaximumSize(50,50);
|
||||
setMaximumSize(50, 50);
|
||||
}
|
||||
|
||||
QSize DockItem::sizeHint() const
|
||||
@ -213,40 +212,36 @@ void DockItem::showContextMenu()
|
||||
if (menuJson.isEmpty())
|
||||
return;
|
||||
|
||||
QDBusPendingReply<QDBusObjectPath> result = m_menuManagerInter->RegisterMenu();
|
||||
|
||||
result.waitForFinished();
|
||||
if (result.isError()) {
|
||||
qWarning() << result.error();
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(menuJson.toLocal8Bit().data());
|
||||
if (jsonDocument.isNull())
|
||||
return;
|
||||
|
||||
QJsonObject jsonMenu = jsonDocument.object();
|
||||
|
||||
qDeleteAll(m_contextMenu.actions());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const QPoint p = popupMarkPoint();
|
||||
|
||||
QJsonObject menuObject;
|
||||
menuObject.insert("x", QJsonValue(p.x()));
|
||||
menuObject.insert("y", QJsonValue(p.y()));
|
||||
menuObject.insert("isDockMenu", QJsonValue(true));
|
||||
menuObject.insert("menuJsonContent", QJsonValue(menuJson));
|
||||
|
||||
switch (DockPosition) {
|
||||
case Top: menuObject.insert("direction", "top"); break;
|
||||
case Bottom: menuObject.insert("direction", "bottom"); break;
|
||||
case Left: menuObject.insert("direction", "left"); break;
|
||||
case Right: menuObject.insert("direction", "right"); break;
|
||||
}
|
||||
|
||||
const QDBusObjectPath path = result.argumentAt(0).value<QDBusObjectPath>();
|
||||
DBusMenu *menuInter = new DBusMenu(path.path(), this);
|
||||
|
||||
connect(menuInter, &DBusMenu::ItemInvoked, this, &DockItem::invokedMenuItem);
|
||||
connect(menuInter, &DBusMenu::ItemInvoked, menuInter, &DBusMenu::deleteLater);
|
||||
connect(menuInter, &DBusMenu::MenuUnregistered, this, &DockItem::onContextMenuAccepted, Qt::QueuedConnection);
|
||||
|
||||
menuInter->ShowMenu(QString(QJsonDocument(menuObject).toJson()));
|
||||
|
||||
hidePopup();
|
||||
emit requestWindowAutoHide(false);
|
||||
|
||||
m_contextMenu.exec(QCursor::pos());
|
||||
|
||||
onContextMenuAccepted();
|
||||
}
|
||||
|
||||
void DockItem::menuActionClicked(QAction *action)
|
||||
{
|
||||
invokedMenuItem(action->data().toString(), true);
|
||||
}
|
||||
|
||||
void DockItem::onContextMenuAccepted()
|
||||
|
@ -29,12 +29,12 @@
|
||||
#include <QFrame>
|
||||
#include <QPointer>
|
||||
#include <QGestureEvent>
|
||||
#include <QMenu>
|
||||
|
||||
#include <memory>
|
||||
|
||||
using namespace Dock;
|
||||
|
||||
class DBusMenuManager;
|
||||
class DockItem : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -102,12 +102,14 @@ protected slots:
|
||||
|
||||
private:
|
||||
void updatePopupPosition();
|
||||
void menuActionClicked(QAction *action);
|
||||
|
||||
protected:
|
||||
bool m_hover;
|
||||
bool m_popupShown;
|
||||
bool m_tapAndHold;
|
||||
bool m_draging;
|
||||
QMenu m_contextMenu;
|
||||
|
||||
QPointer<QWidget> m_lastPopupWidget;
|
||||
QPointer<HoverHighlightEffect> m_hoverEffect;
|
||||
@ -115,8 +117,6 @@ protected:
|
||||
QTimer *m_popupTipsDelayTimer;
|
||||
QTimer *m_popupAdjustDelayTimer;
|
||||
|
||||
DBusMenuManager *m_menuManagerInter;
|
||||
|
||||
static Position DockPosition;
|
||||
static DisplayMode DockDisplayMode;
|
||||
static QPointer<DockPopupWindow> PopupWindow;
|
||||
|
@ -20,7 +20,6 @@
|
||||
*/
|
||||
|
||||
#include "systemtrayitem.h"
|
||||
#include "dbus/dbusmenu.h"
|
||||
|
||||
#include <QProcess>
|
||||
#include <QDebug>
|
||||
@ -31,16 +30,15 @@
|
||||
Dock::Position SystemTrayItem::DockPosition = Dock::Position::Top;
|
||||
QPointer<DockPopupWindow> SystemTrayItem::PopupWindow = nullptr;
|
||||
|
||||
SystemTrayItem::SystemTrayItem(PluginsItemInterface * const pluginInter, const QString &itemKey, QWidget *parent)
|
||||
: AbstractTrayWidget(parent),
|
||||
m_popupShown(false),
|
||||
m_tapAndHold(false),
|
||||
m_pluginInter(pluginInter),
|
||||
m_menuManagerInter(new DBusMenuManager(this)),
|
||||
m_centralWidget(m_pluginInter->itemWidget(itemKey)),
|
||||
m_popupTipsDelayTimer(new QTimer(this)),
|
||||
m_popupAdjustDelayTimer(new QTimer(this)),
|
||||
m_itemKey(itemKey)
|
||||
SystemTrayItem::SystemTrayItem(PluginsItemInterface *const pluginInter, const QString &itemKey, QWidget *parent)
|
||||
: AbstractTrayWidget(parent)
|
||||
, m_popupShown(false)
|
||||
, m_tapAndHold(false)
|
||||
, m_pluginInter(pluginInter)
|
||||
, m_centralWidget(m_pluginInter->itemWidget(itemKey))
|
||||
, m_popupTipsDelayTimer(new QTimer(this))
|
||||
, m_popupAdjustDelayTimer(new QTimer(this))
|
||||
, m_itemKey(itemKey)
|
||||
{
|
||||
qDebug() << "load tray plugins item: " << m_pluginInter->pluginName() << itemKey << m_centralWidget;
|
||||
|
||||
@ -57,8 +55,7 @@ SystemTrayItem::SystemTrayItem(PluginsItemInterface * const pluginInter, const Q
|
||||
setAccessibleName(m_pluginInter->pluginName() + "-" + m_itemKey);
|
||||
setAttribute(Qt::WA_TranslucentBackground);
|
||||
|
||||
if (PopupWindow.isNull())
|
||||
{
|
||||
if (PopupWindow.isNull()) {
|
||||
DockPopupWindow *arrowRectangle = new DockPopupWindow(nullptr);
|
||||
arrowRectangle->setShadowBlurRadius(20);
|
||||
arrowRectangle->setRadius(6);
|
||||
@ -77,6 +74,7 @@ SystemTrayItem::SystemTrayItem(PluginsItemInterface * const pluginInter, const Q
|
||||
|
||||
connect(m_popupTipsDelayTimer, &QTimer::timeout, this, &SystemTrayItem::showHoverTips);
|
||||
connect(m_popupAdjustDelayTimer, &QTimer::timeout, this, &SystemTrayItem::updatePopupPosition, Qt::QueuedConnection);
|
||||
connect(&m_contextMenu, &QMenu::triggered, this, &SystemTrayItem::menuActionClicked);
|
||||
|
||||
grabGesture(Qt::TapAndHoldGesture);
|
||||
|
||||
@ -160,10 +158,8 @@ void SystemTrayItem::detachPluginWidget()
|
||||
|
||||
bool SystemTrayItem::event(QEvent *event)
|
||||
{
|
||||
if (m_popupShown)
|
||||
{
|
||||
switch (event->type())
|
||||
{
|
||||
if (m_popupShown) {
|
||||
switch (event->type()) {
|
||||
case QEvent::Paint:
|
||||
if (!m_popupAdjustDelayTimer->isActive())
|
||||
m_popupAdjustDelayTimer->start();
|
||||
@ -173,7 +169,7 @@ bool SystemTrayItem::event(QEvent *event)
|
||||
}
|
||||
|
||||
if (event->type() == QEvent::Gesture)
|
||||
gestureEvent(static_cast<QGestureEvent*>(event));
|
||||
gestureEvent(static_cast<QGestureEvent *>(event));
|
||||
|
||||
return AbstractTrayWidget::event(event);
|
||||
}
|
||||
@ -231,7 +227,7 @@ void SystemTrayItem::mouseReleaseEvent(QMouseEvent *event)
|
||||
return;
|
||||
}
|
||||
|
||||
if (checkAndResetTapHoldGestureState()&& event->source() == Qt::MouseEventSynthesizedByQt) {
|
||||
if (checkAndResetTapHoldGestureState() && event->source() == Qt::MouseEventSynthesizedByQt) {
|
||||
qDebug() << "SystemTray: tap and hold gesture detected, ignore the synthesized mouse release event";
|
||||
return;
|
||||
}
|
||||
@ -247,8 +243,9 @@ void SystemTrayItem::mouseReleaseEvent(QMouseEvent *event)
|
||||
AbstractTrayWidget::mouseReleaseEvent(event);
|
||||
}
|
||||
|
||||
void SystemTrayItem::showEvent(QShowEvent* event) {
|
||||
QTimer::singleShot(0, this, [=] {
|
||||
void SystemTrayItem::showEvent(QShowEvent *event)
|
||||
{
|
||||
QTimer::singleShot(0, this, [ = ] {
|
||||
onGSettingsChanged("enable");
|
||||
});
|
||||
|
||||
@ -321,7 +318,7 @@ void SystemTrayItem::popupWindowAccept()
|
||||
hidePopup();
|
||||
}
|
||||
|
||||
void SystemTrayItem::showPopupApplet(QWidget * const applet)
|
||||
void SystemTrayItem::showPopupApplet(QWidget *const applet)
|
||||
{
|
||||
// another model popup window already exists
|
||||
if (PopupWindow->model())
|
||||
@ -334,7 +331,7 @@ void SystemTrayItem::showPopupApplet(QWidget * const applet)
|
||||
showPopupWindow(applet, true);
|
||||
}
|
||||
|
||||
void SystemTrayItem::showPopupWindow(QWidget * const content, const bool model)
|
||||
void SystemTrayItem::showPopupWindow(QWidget *const content, const bool model)
|
||||
{
|
||||
m_popupShown = true;
|
||||
m_lastPopupWidget = content;
|
||||
@ -347,10 +344,9 @@ void SystemTrayItem::showPopupWindow(QWidget * const content, const bool model)
|
||||
if (lastContent)
|
||||
lastContent->setVisible(false);
|
||||
|
||||
switch (DockPosition)
|
||||
{
|
||||
switch (DockPosition) {
|
||||
case Dock::Position::Top: popup->setArrowDirection(DockPopupWindow::ArrowTop); break;
|
||||
case Dock::Position::Bottom:popup->setArrowDirection(DockPopupWindow::ArrowBottom); break;
|
||||
case Dock::Position::Bottom: popup->setArrowDirection(DockPopupWindow::ArrowBottom); break;
|
||||
case Dock::Position::Left: popup->setArrowDirection(DockPopupWindow::ArrowLeft); break;
|
||||
case Dock::Position::Right: popup->setArrowDirection(DockPopupWindow::ArrowRight); break;
|
||||
}
|
||||
@ -377,7 +373,7 @@ void SystemTrayItem::showHoverTips()
|
||||
if (!r.contains(QCursor::pos()))
|
||||
return;
|
||||
|
||||
QWidget * const content = trayTipsWidget();
|
||||
QWidget *const content = trayTipsWidget();
|
||||
if (!content)
|
||||
return;
|
||||
|
||||
@ -415,42 +411,37 @@ void SystemTrayItem::showContextMenu()
|
||||
if (menuJson.isEmpty())
|
||||
return;
|
||||
|
||||
QDBusPendingReply<QDBusObjectPath> result = m_menuManagerInter->RegisterMenu();
|
||||
|
||||
result.waitForFinished();
|
||||
if (result.isError())
|
||||
{
|
||||
qWarning() << result.error();
|
||||
QJsonDocument jsonDocument = QJsonDocument::fromJson(menuJson.toLocal8Bit().data());
|
||||
if (jsonDocument.isNull())
|
||||
return;
|
||||
|
||||
QJsonObject jsonMenu = jsonDocument.object();
|
||||
|
||||
qDeleteAll(m_contextMenu.actions());
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
const QPoint p = popupMarkPoint();
|
||||
|
||||
QJsonObject menuObject;
|
||||
menuObject.insert("x", QJsonValue(p.x()));
|
||||
menuObject.insert("y", QJsonValue(p.y()));
|
||||
menuObject.insert("isDockMenu", QJsonValue(true));
|
||||
menuObject.insert("menuJsonContent", QJsonValue(menuJson));
|
||||
|
||||
switch (DockPosition)
|
||||
{
|
||||
case Dock::Position::Top: menuObject.insert("direction", "top"); break;
|
||||
case Dock::Position::Bottom: menuObject.insert("direction", "bottom"); break;
|
||||
case Dock::Position::Left: menuObject.insert("direction", "left"); break;
|
||||
case Dock::Position::Right: menuObject.insert("direction", "right"); break;
|
||||
}
|
||||
|
||||
const QDBusObjectPath path = result.argumentAt(0).value<QDBusObjectPath>();
|
||||
DBusMenu *menuInter = new DBusMenu(path.path(), this);
|
||||
|
||||
connect(menuInter, &DBusMenu::ItemInvoked, this, &SystemTrayItem::invokedMenuItem);
|
||||
connect(menuInter, &DBusMenu::ItemInvoked, menuInter, &DBusMenu::deleteLater);
|
||||
connect(menuInter, &DBusMenu::MenuUnregistered, this, &SystemTrayItem::onContextMenuAccepted, Qt::QueuedConnection);
|
||||
|
||||
menuInter->ShowMenu(QString(QJsonDocument(menuObject).toJson()));
|
||||
|
||||
hidePopup();
|
||||
emit requestWindowAutoHide(false);
|
||||
|
||||
m_contextMenu.exec(QCursor::pos());
|
||||
|
||||
onContextMenuAccepted();
|
||||
}
|
||||
|
||||
void SystemTrayItem::menuActionClicked(QAction *action)
|
||||
{
|
||||
invokedMenuItem(action->data().toString(), true);
|
||||
}
|
||||
|
||||
void SystemTrayItem::onContextMenuAccepted()
|
||||
@ -473,7 +464,8 @@ void SystemTrayItem::updatePopupPosition()
|
||||
PopupWindow->show(p, PopupWindow->model());
|
||||
}
|
||||
|
||||
void SystemTrayItem::onGSettingsChanged(const QString &key) {
|
||||
void SystemTrayItem::onGSettingsChanged(const QString &key)
|
||||
{
|
||||
if (key != "enable") {
|
||||
return;
|
||||
}
|
||||
|
@ -25,10 +25,10 @@
|
||||
#include "constants.h"
|
||||
#include "abstracttraywidget.h"
|
||||
#include "util/dockpopupwindow.h"
|
||||
#include "dbus/dbusmenumanager.h"
|
||||
#include "pluginsiteminterface.h"
|
||||
|
||||
#include <QGestureEvent>
|
||||
#include <QMenu>
|
||||
|
||||
class QGSettings;
|
||||
class SystemTrayItem : public AbstractTrayWidget
|
||||
@ -93,13 +93,14 @@ private:
|
||||
void updatePopupPosition();
|
||||
void onGSettingsChanged(const QString &key);
|
||||
bool checkGSettingsControl() const;
|
||||
void menuActionClicked(QAction *action);
|
||||
|
||||
private:
|
||||
bool m_popupShown;
|
||||
bool m_tapAndHold;
|
||||
QMenu m_contextMenu;
|
||||
|
||||
PluginsItemInterface* m_pluginInter;
|
||||
DBusMenuManager *m_menuManagerInter;
|
||||
QWidget *m_centralWidget;
|
||||
|
||||
QTimer *m_popupTipsDelayTimer;
|
||||
|
Loading…
x
Reference in New Issue
Block a user