|
| 1 | +// SPDX-FileCopyrightText: 2018 - 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | + |
| 5 | +#include "dockpopupwindow.h" |
| 6 | +#include <QApplication> |
| 7 | +#include <QTimer> |
| 8 | +#include <QAccessibleEvent> |
| 9 | + |
| 10 | +DWIDGET_USE_NAMESPACE |
| 11 | + |
| 12 | +DockPopupWindow::DockPopupWindow(QWidget *parent) |
| 13 | + : DArrowRectangle(ArrowBottom, FloatWidget, parent) |
| 14 | + , m_model(false) |
| 15 | + , m_regionInter(new DRegionMonitor(this)) |
| 16 | + , m_enableMouseRelease(true) |
| 17 | +{ |
| 18 | + setMargin(0); |
| 19 | + m_wmHelper = DWindowManagerHelper::instance(); |
| 20 | + compositeChanged(); |
| 21 | + setWindowFlags(Qt::X11BypassWindowManagerHint | Qt::WindowStaysOnTopHint); |
| 22 | + setShadowBlurRadius(20); |
| 23 | + setRadius(6); |
| 24 | + setShadowYOffset(2); |
| 25 | + setShadowXOffset(0); |
| 26 | + setArrowWidth(18); |
| 27 | + setArrowHeight(10); |
| 28 | + connect(m_wmHelper, &DWindowManagerHelper::hasCompositeChanged, this, &DockPopupWindow::compositeChanged); |
| 29 | + connect(m_regionInter, &DRegionMonitor::buttonRelease, this, &DockPopupWindow::onGlobMouseRelease); |
| 30 | +} |
| 31 | + |
| 32 | +DockPopupWindow::~DockPopupWindow() |
| 33 | +{ |
| 34 | + QWidget *content = getContent(); |
| 35 | + if (content) { |
| 36 | + content->removeEventFilter(this); |
| 37 | + content->setParent(nullptr); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +bool DockPopupWindow::model() const |
| 42 | +{ |
| 43 | + return isVisible() && m_model; |
| 44 | +} |
| 45 | + |
| 46 | +void DockPopupWindow::setContent(QWidget *content) |
| 47 | +{ |
| 48 | + QWidget *lastWidget = getContent(); |
| 49 | + if (lastWidget) |
| 50 | + lastWidget->removeEventFilter(this); |
| 51 | + content->installEventFilter(this); |
| 52 | + QAccessibleEvent event(this, QAccessible::NameChanged); |
| 53 | + QAccessible::updateAccessibility(&event); |
| 54 | + if (!content->objectName().trimmed().isEmpty()) |
| 55 | + setAccessibleName(content->objectName() + "-popup"); |
| 56 | + DArrowRectangle::setContent(content); |
| 57 | +} |
| 58 | + |
| 59 | +void DockPopupWindow::show(const QPoint &pos, const bool model) |
| 60 | +{ |
| 61 | + m_model = model; |
| 62 | + m_lastPoint = pos; |
| 63 | + show(pos.x(), pos.y()); |
| 64 | + if (m_regionInter->registered()) { |
| 65 | + m_regionInter->unregisterRegion(); |
| 66 | + } |
| 67 | + if (m_model) { |
| 68 | + m_regionInter->registerRegion(); |
| 69 | + } |
| 70 | + blockButtonRelease(); |
| 71 | +} |
| 72 | + |
| 73 | +void DockPopupWindow::show(const int x, const int y) |
| 74 | +{ |
| 75 | + m_lastPoint = QPoint(x, y); |
| 76 | + blockButtonRelease(); |
| 77 | + DArrowRectangle::show(x, y); |
| 78 | +} |
| 79 | + |
| 80 | +void DockPopupWindow::blockButtonRelease() |
| 81 | +{ |
| 82 | + // 短暂的不处理鼠标release事件,防止出现刚显示又被隐藏的情况 |
| 83 | + m_enableMouseRelease = false; |
| 84 | + QTimer::singleShot(10, this, [this] { |
| 85 | + m_enableMouseRelease = true; |
| 86 | + }); |
| 87 | +} |
| 88 | +void DockPopupWindow::hide() |
| 89 | +{ |
| 90 | + if (m_regionInter->registered()) |
| 91 | + m_regionInter->unregisterRegion(); |
| 92 | + DArrowRectangle::hide(); |
| 93 | +} |
| 94 | + |
| 95 | +void DockPopupWindow::showEvent(QShowEvent *e) |
| 96 | +{ |
| 97 | + DArrowRectangle::showEvent(e); |
| 98 | + QTimer::singleShot(1, this, &DockPopupWindow::ensureRaised); |
| 99 | +} |
| 100 | + |
| 101 | +void DockPopupWindow::enterEvent(QEnterEvent *e) |
| 102 | +{ |
| 103 | + DArrowRectangle::enterEvent(e); |
| 104 | + QTimer::singleShot(1, this, &DockPopupWindow::ensureRaised); |
| 105 | +} |
| 106 | + |
| 107 | +bool DockPopupWindow::eventFilter(QObject *o, QEvent *e) |
| 108 | +{ |
| 109 | + if (o != getContent() || e->type() != QEvent::Resize) |
| 110 | + return false; |
| 111 | + // FIXME: ensure position move after global mouse release event |
| 112 | + if (isVisible()) { |
| 113 | + QTimer::singleShot(10, this, [=] { |
| 114 | + // NOTE(sbw): double check is necessary, in this time, the popup maybe already hided. |
| 115 | + if (isVisible()) |
| 116 | + show(m_lastPoint, m_model); |
| 117 | + }); |
| 118 | + } |
| 119 | + return false; |
| 120 | +} |
| 121 | + |
| 122 | +void DockPopupWindow::onGlobMouseRelease(const QPoint &mousePos, const int flag) |
| 123 | +{ |
| 124 | + Q_ASSERT(m_model); |
| 125 | + if (!m_enableMouseRelease) |
| 126 | + return; |
| 127 | + if ((flag != DRegionMonitor::WatchedFlags::Button_Left) && (flag != DRegionMonitor::WatchedFlags::Button_Right)) { |
| 128 | + return; |
| 129 | + } |
| 130 | + const QRect rect = QRect(pos(), size()); |
| 131 | + if (rect.contains(mousePos)) |
| 132 | + return; |
| 133 | + emit accept(); |
| 134 | + m_regionInter->unregisterRegion(); |
| 135 | +} |
| 136 | + |
| 137 | +void DockPopupWindow::compositeChanged() |
| 138 | +{ |
| 139 | + setBackgroundColor(QColor(235, 235, 235, 80)); |
| 140 | + setBorderColor(QColor(235, 235, 235, 80)); |
| 141 | +} |
| 142 | + |
| 143 | +void DockPopupWindow::ensureRaised() |
| 144 | +{ |
| 145 | + if (isVisible()) { |
| 146 | + QWidget *content = getContent(); |
| 147 | + if (!content || !content->isVisible()) { |
| 148 | + this->setVisible(false); |
| 149 | + } else { |
| 150 | + raise(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments