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
|
2017-09-18 14:33:44 +08:00
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
|
#include "dockpopupwindow.h"
|
2021-11-05 21:29:32 +08:00
|
|
|
|
#include "imageutil.h"
|
2021-08-06 17:23:05 +08:00
|
|
|
|
#include "utils.h"
|
2022-11-17 10:49:48 +08:00
|
|
|
|
#include "dbusutil.h"
|
2023-05-11 10:46:31 +08:00
|
|
|
|
#include "dockscreen.h"
|
|
|
|
|
#include "displaymanager.h"
|
2016-07-15 11:00:55 +08:00
|
|
|
|
|
2016-08-30 15:11:09 +08:00
|
|
|
|
#include <QScreen>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QDesktopWidget>
|
2020-03-13 12:59:02 +08:00
|
|
|
|
#include <QAccessible>
|
|
|
|
|
#include <QAccessibleEvent>
|
2021-11-05 21:29:32 +08:00
|
|
|
|
#include <QCursor>
|
|
|
|
|
#include <QGSettings>
|
2016-08-30 15:11:09 +08:00
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
|
DWIDGET_USE_NAMESPACE
|
|
|
|
|
|
2023-05-11 10:46:31 +08:00
|
|
|
|
#define DOCK_SCREEN DockScreen::instance()
|
|
|
|
|
#define DIS_INS DisplayManager::instance()
|
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
|
DockPopupWindow::DockPopupWindow(QWidget *parent)
|
2023-04-07 14:43:09 +08:00
|
|
|
|
: DBlurEffectWidget(parent)
|
2022-11-17 10:49:48 +08:00
|
|
|
|
, m_model(false)
|
|
|
|
|
, m_eventMonitor(new XEventMonitor(xEventMonitorService, xEventMonitorPath, QDBusConnection::sessionBus(), this))
|
|
|
|
|
, m_enableMouseRelease(true)
|
2022-12-15 19:58:43 +08:00
|
|
|
|
, m_extendWidget(nullptr)
|
2023-04-07 14:43:09 +08:00
|
|
|
|
, m_lastWidget(nullptr)
|
2016-07-15 11:00:55 +08:00
|
|
|
|
{
|
2023-04-07 14:43:09 +08:00
|
|
|
|
setContentsMargins(0, 0, 0, 0);
|
2017-05-15 10:52:59 +08:00
|
|
|
|
m_wmHelper = DWindowManagerHelper::instance();
|
|
|
|
|
|
2023-04-07 14:43:09 +08:00
|
|
|
|
setWindowFlags(Qt::ToolTip | Qt::WindowStaysOnTopHint);
|
2021-08-06 17:23:05 +08:00
|
|
|
|
if (Utils::IS_WAYLAND_DISPLAY) {
|
|
|
|
|
setAttribute(Qt::WA_NativeWindow);
|
2022-02-17 09:55:08 +08:00
|
|
|
|
windowHandle()->setProperty("_d_dwayland_window-type", "override");
|
|
|
|
|
} else {
|
2021-08-06 17:23:05 +08:00
|
|
|
|
setAttribute(Qt::WA_InputMethodEnabled, false);
|
|
|
|
|
}
|
2016-08-11 15:32:48 +08:00
|
|
|
|
|
2022-11-17 10:49:48 +08:00
|
|
|
|
connect(m_eventMonitor, &XEventMonitor::ButtonPress, this, &DockPopupWindow::onButtonPress);
|
|
|
|
|
|
|
|
|
|
if (Utils::IS_WAYLAND_DISPLAY)
|
|
|
|
|
QDBusConnection::sessionBus().connect("com.deepin.dde.lockFront", "/com/deepin/dde/lockFront", "com.deepin.dde.lockFront", "Visible", "b", this, SLOT(hide()));
|
2016-07-15 11:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
|
DockPopupWindow::~DockPopupWindow()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
|
bool DockPopupWindow::model() const
|
|
|
|
|
{
|
2018-03-15 11:35:10 +08:00
|
|
|
|
return isVisible() && m_model;
|
2016-07-15 11:00:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 14:43:09 +08:00
|
|
|
|
QWidget *DockPopupWindow::getContent()
|
|
|
|
|
{
|
|
|
|
|
return m_lastWidget;
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
|
void DockPopupWindow::setContent(QWidget *content)
|
|
|
|
|
{
|
2023-04-07 14:43:09 +08:00
|
|
|
|
if (m_lastWidget)
|
|
|
|
|
m_lastWidget->removeEventFilter(this);
|
2016-07-27 14:08:23 +08:00
|
|
|
|
content->installEventFilter(this);
|
|
|
|
|
|
2020-03-13 12:59:02 +08:00
|
|
|
|
QAccessibleEvent event(this, QAccessible::NameChanged);
|
|
|
|
|
QAccessible::updateAccessibility(&event);
|
|
|
|
|
|
2020-07-16 06:28:02 +00:00
|
|
|
|
if (!content->objectName().trimmed().isEmpty())
|
|
|
|
|
setAccessibleName(content->objectName() + "-popup");
|
2016-07-28 10:59:21 +08:00
|
|
|
|
|
2023-04-07 14:43:09 +08:00
|
|
|
|
m_lastWidget = content;
|
|
|
|
|
content->setParent(this);
|
|
|
|
|
content->show();
|
|
|
|
|
resize(content->sizeHint());
|
2016-07-27 14:08:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 19:58:43 +08:00
|
|
|
|
void DockPopupWindow::setExtendWidget(QWidget *widget)
|
|
|
|
|
{
|
|
|
|
|
m_extendWidget = widget;
|
2023-01-12 10:58:43 +08:00
|
|
|
|
connect(widget, &QWidget::destroyed, this, [ this ] { m_extendWidget = nullptr; }, Qt::UniqueConnection);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 14:43:09 +08:00
|
|
|
|
void DockPopupWindow::setPosition(Dock::Position position)
|
|
|
|
|
{
|
|
|
|
|
m_position = position;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QWidget *DockPopupWindow::extendWidget() const
|
2023-01-12 10:58:43 +08:00
|
|
|
|
{
|
|
|
|
|
return m_extendWidget;
|
2022-12-15 19:58:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-15 11:00:55 +08:00
|
|
|
|
void DockPopupWindow::show(const QPoint &pos, const bool model)
|
|
|
|
|
{
|
|
|
|
|
m_model = model;
|
2017-03-06 09:45:38 +08:00
|
|
|
|
show(pos.x(), pos.y());
|
2016-07-18 10:09:26 +08:00
|
|
|
|
|
2022-11-17 10:49:48 +08:00
|
|
|
|
if (!m_eventKey.isEmpty()) {
|
|
|
|
|
m_eventMonitor->UnregisterArea(m_eventKey);
|
|
|
|
|
m_eventKey.clear();
|
2019-05-30 14:01:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_model) {
|
2022-11-17 10:49:48 +08:00
|
|
|
|
m_eventKey = m_eventMonitor->RegisterFullScreen();
|
2018-04-26 20:05:54 +08:00
|
|
|
|
}
|
2022-11-17 10:49:48 +08:00
|
|
|
|
|
2022-01-05 15:52:40 +08:00
|
|
|
|
blockButtonRelease();
|
2016-07-18 10:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-03-06 09:45:38 +08:00
|
|
|
|
void DockPopupWindow::show(const int x, const int y)
|
|
|
|
|
{
|
2023-04-07 14:43:09 +08:00
|
|
|
|
QPoint displayPoint;
|
2017-03-24 15:55:33 +08:00
|
|
|
|
m_lastPoint = QPoint(x, y);
|
2023-04-07 14:43:09 +08:00
|
|
|
|
switch (m_position) {
|
|
|
|
|
case Dock::Position::Left:
|
|
|
|
|
displayPoint = m_lastPoint + QPoint(0, -m_lastWidget->height() / 2);
|
|
|
|
|
break;
|
|
|
|
|
case Dock::Position::Right:
|
|
|
|
|
displayPoint = m_lastPoint + QPoint(-m_lastWidget->width(), -m_lastWidget->height() / 2);
|
|
|
|
|
break;
|
|
|
|
|
case Dock::Position::Top:
|
|
|
|
|
displayPoint = m_lastPoint + QPoint(-m_lastWidget->width() / 2, 0);
|
|
|
|
|
break;
|
|
|
|
|
case Dock::Position::Bottom:
|
|
|
|
|
displayPoint = m_lastPoint + QPoint(-m_lastWidget->width() / 2, -m_lastWidget->height());
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-01-05 15:52:40 +08:00
|
|
|
|
blockButtonRelease();
|
2023-05-11 10:46:31 +08:00
|
|
|
|
QScreen *screen = DIS_INS->screen(DOCK_SCREEN->current());
|
|
|
|
|
if (!screen)
|
|
|
|
|
return;
|
|
|
|
|
QRect screenRect = screen->geometry();
|
2023-05-11 13:24:05 +08:00
|
|
|
|
if (getContent()->width() <= screenRect.width()) {
|
2023-05-11 10:46:31 +08:00
|
|
|
|
displayPoint.setX(qMax(screenRect.x(), displayPoint.x()));
|
|
|
|
|
displayPoint.setX(qMin(screenRect.x() + screenRect.width() - getContent()->width(), displayPoint.x()));
|
|
|
|
|
}
|
2023-04-07 14:43:09 +08:00
|
|
|
|
move(displayPoint);
|
|
|
|
|
resize(m_lastWidget->size());
|
|
|
|
|
DBlurEffectWidget::show();
|
2023-05-24 15:42:17 +08:00
|
|
|
|
activateWindow();
|
2017-03-06 09:45:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 15:52:40 +08:00
|
|
|
|
void DockPopupWindow::blockButtonRelease()
|
|
|
|
|
{
|
|
|
|
|
// 短暂的不处理鼠标release事件,防止出现刚显示又被隐藏的情况
|
|
|
|
|
m_enableMouseRelease = false;
|
|
|
|
|
QTimer::singleShot(10, this, [this] {
|
|
|
|
|
m_enableMouseRelease = true;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-19 15:08:16 +08:00
|
|
|
|
void DockPopupWindow::hide()
|
|
|
|
|
{
|
2022-11-17 10:49:48 +08:00
|
|
|
|
if (!m_eventKey.isEmpty()) {
|
|
|
|
|
m_eventMonitor->UnregisterArea(m_eventKey);
|
|
|
|
|
m_eventKey.clear();
|
|
|
|
|
}
|
2016-07-19 15:08:16 +08:00
|
|
|
|
|
2023-04-07 14:43:09 +08:00
|
|
|
|
DBlurEffectWidget::hide();
|
2016-07-19 15:08:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-05 14:33:57 +08:00
|
|
|
|
void DockPopupWindow::showEvent(QShowEvent *e)
|
|
|
|
|
{
|
2023-04-07 14:43:09 +08:00
|
|
|
|
DBlurEffectWidget::showEvent(e);
|
2021-12-31 15:09:12 +08:00
|
|
|
|
if (Utils::IS_WAYLAND_DISPLAY) {
|
|
|
|
|
Utils::updateCursor(this);
|
|
|
|
|
}
|
2016-09-05 14:33:57 +08:00
|
|
|
|
|
2017-12-15 16:47:16 +08:00
|
|
|
|
QTimer::singleShot(1, this, &DockPopupWindow::ensureRaised);
|
2016-09-05 14:33:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 19:58:43 +08:00
|
|
|
|
void DockPopupWindow::hideEvent(QHideEvent *event)
|
|
|
|
|
{
|
|
|
|
|
m_extendWidget = nullptr;
|
2023-04-07 14:43:09 +08:00
|
|
|
|
Dtk::Widget::DBlurEffectWidget::hideEvent(event);
|
2022-12-15 19:58:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-01 10:55:19 +08:00
|
|
|
|
void DockPopupWindow::enterEvent(QEvent *e)
|
|
|
|
|
{
|
2023-04-07 14:43:09 +08:00
|
|
|
|
DBlurEffectWidget::enterEvent(e);
|
2021-12-31 15:09:12 +08:00
|
|
|
|
if (Utils::IS_WAYLAND_DISPLAY) {
|
|
|
|
|
Utils::updateCursor(this);
|
|
|
|
|
}
|
2016-09-01 10:55:19 +08:00
|
|
|
|
|
2017-12-15 16:47:16 +08:00
|
|
|
|
QTimer::singleShot(1, this, &DockPopupWindow::ensureRaised);
|
2016-09-01 10:55:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
|
bool DockPopupWindow::eventFilter(QObject *o, QEvent *e)
|
|
|
|
|
{
|
2023-04-03 11:26:16 +08:00
|
|
|
|
if (o != getContent())
|
2016-07-27 14:08:23 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
2023-04-03 11:26:16 +08:00
|
|
|
|
switch(e->type()) {
|
|
|
|
|
case QEvent::Resize: {
|
|
|
|
|
// FIXME: ensure position move after global mouse release event
|
|
|
|
|
if (isVisible()) {
|
|
|
|
|
QTimer::singleShot(10, this, [=] {
|
|
|
|
|
// NOTE(sbw): double check is necessary, in this time, the popup maybe already hided.
|
|
|
|
|
if (isVisible())
|
|
|
|
|
show(m_lastPoint, m_model);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-09-08 14:14:45 +08:00
|
|
|
|
case QEvent::WindowDeactivate:
|
2023-04-03 11:26:16 +08:00
|
|
|
|
case QEvent::Hide: {
|
|
|
|
|
this->hide();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2018-03-27 17:39:29 +08:00
|
|
|
|
}
|
2016-07-27 14:49:29 +08:00
|
|
|
|
|
2016-07-27 14:08:23 +08:00
|
|
|
|
return false;
|
2016-07-18 10:09:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-12-15 16:47:16 +08:00
|
|
|
|
void DockPopupWindow::ensureRaised()
|
|
|
|
|
{
|
|
|
|
|
if (isVisible())
|
|
|
|
|
raise();
|
|
|
|
|
}
|
2022-11-17 10:49:48 +08:00
|
|
|
|
|
|
|
|
|
void DockPopupWindow::onButtonPress(int type, int x, int y, const QString &key)
|
|
|
|
|
{
|
2022-01-05 15:52:40 +08:00
|
|
|
|
if (!m_enableMouseRelease)
|
|
|
|
|
return;
|
2023-05-11 10:46:31 +08:00
|
|
|
|
QScreen *screen = DIS_INS->screen(DOCK_SCREEN->current());
|
|
|
|
|
if (!screen)
|
|
|
|
|
return;
|
|
|
|
|
QRect screenRect = screen->geometry();
|
2023-04-11 13:04:25 +08:00
|
|
|
|
QRect popupRect(((pos() - screenRect.topLeft()) * qApp->devicePixelRatio() + screenRect.topLeft()), size() * qApp->devicePixelRatio());
|
|
|
|
|
if (popupRect.contains(QPoint(x, y)))
|
2019-05-30 14:01:37 +08:00
|
|
|
|
return;
|
|
|
|
|
|
2022-12-15 19:58:43 +08:00
|
|
|
|
if (m_extendWidget) {
|
|
|
|
|
// 计算额外添加的区域,如果鼠标的点击点在额外的区域内,也无需隐藏
|
|
|
|
|
QPoint extendPoint = m_extendWidget->mapToGlobal(QPoint(0, 0));
|
2023-04-11 13:04:25 +08:00
|
|
|
|
QRect extendRect(((extendPoint - screenRect.topLeft()) * qApp->devicePixelRatio() + screenRect.topLeft()), m_extendWidget->size() * qApp->devicePixelRatio());
|
2022-12-15 19:58:43 +08:00
|
|
|
|
if (extendRect.contains(QPoint(x, y)))
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-07-18 10:09:26 +08:00
|
|
|
|
|
2023-05-05 13:39:44 +08:00
|
|
|
|
// if there is something focus on widget, return
|
|
|
|
|
if (auto focus = qApp->focusWidget()) {
|
|
|
|
|
auto className = QString(focus->metaObject()->className());
|
2023-11-21 11:36:43 +08:00
|
|
|
|
//qDebug() << "Find focused widget, focus className is" << className;
|
2023-05-05 13:39:44 +08:00
|
|
|
|
if (className == "QLineEdit") {
|
|
|
|
|
qDebug() << "PopupWindow window will not be hidden";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-18 10:09:26 +08:00
|
|
|
|
emit accept();
|
2022-11-17 10:49:48 +08:00
|
|
|
|
hide();
|
|
|
|
|
}
|
2016-07-18 10:09:26 +08:00
|
|
|
|
|
2023-01-05 17:03:02 +08:00
|
|
|
|
PopupSwitchWidget::PopupSwitchWidget(QWidget *parent)
|
|
|
|
|
: QWidget(parent)
|
|
|
|
|
, m_containerLayout(new QVBoxLayout(this))
|
|
|
|
|
, m_topWidget(nullptr)
|
|
|
|
|
{
|
|
|
|
|
m_containerLayout->setContentsMargins(0, 0, 0, 0);
|
|
|
|
|
m_containerLayout->setSpacing(0);
|
2016-07-15 11:00:55 +08:00
|
|
|
|
}
|
2017-05-15 10:52:59 +08:00
|
|
|
|
|
2023-01-05 17:03:02 +08:00
|
|
|
|
PopupSwitchWidget::~PopupSwitchWidget()
|
2017-05-15 10:52:59 +08:00
|
|
|
|
{
|
|
|
|
|
}
|
2017-12-15 16:47:16 +08:00
|
|
|
|
|
2023-01-05 17:03:02 +08:00
|
|
|
|
void PopupSwitchWidget::pushWidget(QWidget *widget)
|
2017-12-15 16:47:16 +08:00
|
|
|
|
{
|
2023-01-05 17:03:02 +08:00
|
|
|
|
// 首先将界面其他的窗体移除
|
|
|
|
|
for (int i = m_containerLayout->count() - 1; i >= 0; i--) {
|
|
|
|
|
QLayoutItem *item = m_containerLayout->itemAt(i);
|
|
|
|
|
item->widget()->removeEventFilter(this);
|
|
|
|
|
item->widget()->hide();
|
|
|
|
|
m_containerLayout->removeItem(item);
|
2022-05-05 11:22:54 +08:00
|
|
|
|
}
|
2023-01-05 17:03:02 +08:00
|
|
|
|
m_topWidget = widget;
|
|
|
|
|
setFixedSize(widget->size());
|
|
|
|
|
widget->installEventFilter(this);
|
|
|
|
|
m_containerLayout->addWidget(widget);
|
|
|
|
|
widget->show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool PopupSwitchWidget::eventFilter(QObject *watched, QEvent *event)
|
|
|
|
|
{
|
|
|
|
|
if (watched == m_topWidget && event->type() == QEvent::Resize) {
|
|
|
|
|
setFixedSize(m_topWidget->size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return QWidget::eventFilter(watched, event);
|
2017-12-15 16:47:16 +08:00
|
|
|
|
}
|