2023-02-16 13:51:55 +08:00
|
|
|
// Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd.
|
|
|
|
// SPDX-FileCopyrightText: 2018 - 2023 UnionTech Software Technology Co., Ltd.
|
2022-09-06 11:36:55 +08:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2018-02-07 11:52:47 +08:00
|
|
|
|
2019-09-06 14:44:07 +08:00
|
|
|
#include "constants.h"
|
2017-12-28 19:23:50 +08:00
|
|
|
#include "abstracttraywidget.h"
|
|
|
|
|
|
|
|
#include <xcb/xproto.h>
|
|
|
|
#include <QMouseEvent>
|
2018-10-16 14:33:06 +08:00
|
|
|
#include <QDebug>
|
2017-12-28 19:23:50 +08:00
|
|
|
|
2018-10-24 18:09:44 +08:00
|
|
|
AbstractTrayWidget::AbstractTrayWidget(QWidget *parent, Qt::WindowFlags f)
|
2019-09-06 14:44:07 +08:00
|
|
|
: QWidget(parent, f)
|
|
|
|
, m_handleMouseReleaseTimer(new QTimer(this))
|
2021-12-01 12:22:31 +08:00
|
|
|
, m_ownerPID(0)
|
2017-12-28 19:23:50 +08:00
|
|
|
{
|
2018-12-13 11:43:48 +08:00
|
|
|
m_handleMouseReleaseTimer->setSingleShot(true);
|
2021-12-27 09:38:42 +08:00
|
|
|
m_handleMouseReleaseTimer->setInterval(10);
|
2018-12-13 11:43:48 +08:00
|
|
|
|
|
|
|
connect(m_handleMouseReleaseTimer, &QTimer::timeout, this, &AbstractTrayWidget::handleMouseRelease);
|
2017-12-28 19:23:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AbstractTrayWidget::~AbstractTrayWidget()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-10-16 14:33:06 +08:00
|
|
|
void AbstractTrayWidget::mousePressEvent(QMouseEvent *event)
|
|
|
|
{
|
2019-01-14 17:48:43 +08:00
|
|
|
// call QWidget::mousePressEvent means to show dock-context-menu
|
|
|
|
// when right button of mouse is pressed immediately in fashion mode
|
|
|
|
|
|
|
|
// here we hide the right button press event when it is click in the special area
|
2020-01-14 16:49:31 +08:00
|
|
|
if (event->button() == Qt::RightButton && perfectIconRect().contains(event->pos(), true)) {
|
2018-12-11 16:41:11 +08:00
|
|
|
event->accept();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QWidget::mousePressEvent(event);
|
2018-10-16 14:33:06 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 19:23:50 +08:00
|
|
|
void AbstractTrayWidget::mouseReleaseEvent(QMouseEvent *e)
|
|
|
|
{
|
2018-12-13 11:43:48 +08:00
|
|
|
//e->accept();
|
|
|
|
|
|
|
|
// 由于 XWindowTrayWidget 中对 发送鼠标事件到X窗口的函数, 如 sendClick/sendHoverEvent 中
|
|
|
|
// 使用了 setX11PassMouseEvent, 而每次调用 setX11PassMouseEvent 时都会导致产生 mousePress 和 mouseRelease 事件
|
|
|
|
// 因此如果直接在这里处理事件会导致一些问题, 所以使用 Timer 来延迟处理 100 毫秒内的最后一个事件
|
2020-08-10 19:45:23 +08:00
|
|
|
m_lastMouseReleaseData.first = e->pos();
|
|
|
|
m_lastMouseReleaseData.second = e->button();
|
|
|
|
|
|
|
|
m_handleMouseReleaseTimer->start();
|
2018-12-17 14:38:09 +08:00
|
|
|
|
|
|
|
QWidget::mouseReleaseEvent(e);
|
2018-12-13 11:43:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-06 14:44:07 +08:00
|
|
|
void AbstractTrayWidget::handleMouseRelease()
|
|
|
|
{
|
2018-12-13 11:43:48 +08:00
|
|
|
Q_ASSERT(sender() == m_handleMouseReleaseTimer);
|
|
|
|
|
|
|
|
// do not dealwith all mouse event of SystemTray, class SystemTrayItem will handle it
|
|
|
|
if (trayTyep() == SystemTray)
|
2017-12-28 19:23:50 +08:00
|
|
|
return;
|
|
|
|
|
2018-12-13 11:43:48 +08:00
|
|
|
const QPoint point(m_lastMouseReleaseData.first - rect().center());
|
|
|
|
if (point.manhattanLength() > 24)
|
|
|
|
return;
|
2017-12-28 19:23:50 +08:00
|
|
|
|
|
|
|
QPoint globalPos = QCursor::pos();
|
|
|
|
uint8_t buttonIndex = XCB_BUTTON_INDEX_1;
|
|
|
|
|
2018-12-13 11:43:48 +08:00
|
|
|
switch (m_lastMouseReleaseData.second) {
|
2017-12-28 19:23:50 +08:00
|
|
|
case Qt:: MiddleButton:
|
|
|
|
buttonIndex = XCB_BUTTON_INDEX_2;
|
|
|
|
break;
|
|
|
|
case Qt::RightButton:
|
|
|
|
buttonIndex = XCB_BUTTON_INDEX_3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendClick(buttonIndex, globalPos.x(), globalPos.y());
|
2018-10-16 14:33:06 +08:00
|
|
|
|
|
|
|
// left mouse button clicked
|
|
|
|
if (buttonIndex == XCB_BUTTON_INDEX_1) {
|
|
|
|
Q_EMIT clicked();
|
|
|
|
}
|
2017-12-28 19:23:50 +08:00
|
|
|
}
|
2019-01-14 17:48:43 +08:00
|
|
|
|
|
|
|
const QRect AbstractTrayWidget::perfectIconRect() const
|
|
|
|
{
|
|
|
|
const QRect itemRect = rect();
|
2020-01-14 16:49:31 +08:00
|
|
|
const int iconSize = std::min(itemRect.width(), itemRect.height());
|
2019-01-14 17:48:43 +08:00
|
|
|
|
|
|
|
QRect iconRect;
|
|
|
|
iconRect.setWidth(iconSize);
|
|
|
|
iconRect.setHeight(iconSize);
|
|
|
|
iconRect.moveTopLeft(itemRect.center() - iconRect.center());
|
|
|
|
|
|
|
|
return iconRect;
|
|
|
|
}
|
2019-09-06 14:44:07 +08:00
|
|
|
|
|
|
|
void AbstractTrayWidget::resizeEvent(QResizeEvent *event)
|
|
|
|
{
|
|
|
|
QWidget::resizeEvent(event);
|
|
|
|
|
|
|
|
const Dock::Position position = qApp->property(PROP_POSITION).value<Dock::Position>();
|
|
|
|
// 保持横纵比
|
|
|
|
if (position == Dock::Bottom || position == Dock::Top) {
|
|
|
|
setMaximumWidth(height());
|
|
|
|
setMaximumHeight(QWIDGETSIZE_MAX);
|
|
|
|
} else {
|
|
|
|
setMaximumHeight(width());
|
|
|
|
setMaximumWidth(QWIDGETSIZE_MAX);
|
|
|
|
}
|
|
|
|
}
|
2021-11-29 15:46:12 +08:00
|
|
|
|
|
|
|
uint AbstractTrayWidget::getOwnerPID()
|
|
|
|
{
|
|
|
|
return this->m_ownerPID;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractTrayWidget::setOwnerPID(uint PID)
|
|
|
|
{
|
|
|
|
this->m_ownerPID = PID;
|
|
|
|
}
|