From f79dbd8f062b396769ec9a8925df82d3a65267f8 Mon Sep 17 00:00:00 2001 From: cutefishd Date: Sun, 4 Apr 2021 17:34:21 +0800 Subject: [PATCH] Use TopLevelMenu instead of DockMenu --- CMakeLists.txt | 1 - qml/AppItem.qml | 3 +- qml/DockMenu.qml | 43 --------------- resources.qrc | 1 - src/main.cpp | 3 -- src/popupwindow.cpp | 129 -------------------------------------------- src/popupwindow.h | 47 ---------------- 7 files changed, 2 insertions(+), 225 deletions(-) delete mode 100644 qml/DockMenu.qml delete mode 100644 src/popupwindow.cpp delete mode 100644 src/popupwindow.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6d01149..985cbbd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,7 +28,6 @@ set(SRCS src/trashmanager.cpp src/utils.cpp src/xwindowinterface.cpp - src/popupwindow.cpp src/fakewindow.cpp ) diff --git a/qml/AppItem.qml b/qml/AppItem.qml index 47c99c0..e98f251 100644 --- a/qml/AppItem.qml +++ b/qml/AppItem.qml @@ -1,6 +1,7 @@ import QtQuick 2.12 import QtQuick.Controls 2.12 import Cutefish.Dock 1.0 +import MeuiKit 1.0 as Meui DockItem { id: appItem @@ -36,7 +37,7 @@ DockItem { updateGeometry() } - DockMenu { + Meui.TopLevelMenu { id: contextMenu MenuItem { diff --git a/qml/DockMenu.qml b/qml/DockMenu.qml deleted file mode 100644 index c1c594d..0000000 --- a/qml/DockMenu.qml +++ /dev/null @@ -1,43 +0,0 @@ -import QtQuick 2.12 -import QtQuick.Layouts 1.12 -import QtQuick.Controls 2.12 -import Cutefish.Dock 1.0 -import MeuiKit 1.0 as Meui - -PopupWindow { - id: control - - default property alias content : _mainLayout.data - - Rectangle { - id: _background - anchors.fill: parent - opacity: 0.6 - color: Meui.Theme.backgroundColor - radius: Meui.Theme.mediumRadius - - Meui.WindowShadow { - view: control - geometry: Qt.rect(control.x, control.y, control.width, control.height) - radius: _background.radius - } - - Meui.WindowBlur { - view: control - geometry: Qt.rect(control.x, control.y, control.width, control.height) - windowRadius: _background.radius - enabled: true - } - } - - ColumnLayout { - id: _mainLayout - anchors.fill: parent - anchors.topMargin: 8 - anchors.bottomMargin: 8 - } - - function open() { - control.show() - } -} diff --git a/resources.qrc b/resources.qrc index ba29ef4..530e34b 100644 --- a/resources.qrc +++ b/resources.qrc @@ -96,6 +96,5 @@ svg/light/dark-mode.svg svg/dark/dark-mode.svg svg/dark/bluetooth-symbolic.svg - qml/DockMenu.qml diff --git a/src/main.cpp b/src/main.cpp index 8a6ff38..854190c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,15 +26,12 @@ #include "applicationmodel.h" #include "mainwindow.h" -#include "popupwindow.h" - int main(int argc, char *argv[]) { QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication app(argc, argv); qmlRegisterType("Cutefish.Dock", 1, 0, "DockSettings"); - qmlRegisterType("Cutefish.Dock", 1, 0, "PopupWindow"); QString qmFilePath = QString("%1/%2.qm").arg("/usr/share/cutefish-dock/translations/").arg(QLocale::system().name()); if (QFile::exists(qmFilePath)) { diff --git a/src/popupwindow.cpp b/src/popupwindow.cpp deleted file mode 100644 index d1aeba8..0000000 --- a/src/popupwindow.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include "popupwindow.h" -#include -#include -#include -#include - -PopupWindow::PopupWindow(QQuickWindow *parent) - : QQuickWindow(parent) - , m_parentItem(0) - , m_contentItem(0) - , m_mouseMoved(false) - , m_dismissed(false) -{ - setFlags(Qt::Popup); - setColor(Qt::transparent); - connect(qApp, SIGNAL(applicationStateChanged(Qt::ApplicationState)), - this, SLOT(applicationStateChanged(Qt::ApplicationState))); -} - -void PopupWindow::applicationStateChanged(Qt::ApplicationState state) -{ - if (state != Qt::ApplicationActive) - dismissPopup(); -} - -void PopupWindow::show() -{ - QPoint pos = QCursor::pos(); - int w = m_contentItem->implicitWidth(); - int h = m_contentItem->implicitHeight() + 16; - int posx = pos.x(); - int posy = pos.y(); - - QWindow *pw = transientParent(); - if (!pw && parentItem()) - pw = parentItem()->window(); - if (!pw) - pw = this; - - QRect g = pw->screen()->availableGeometry(); - - if (posx + w > g.right()) { - if (qobject_cast(transientParent())) { - // reposition submenu window on the parent menu's left side - int submenuOverlap = pw->x() + pw->width() - posx; - posx -= pw->width() + w - 2 * submenuOverlap; - } else { - posx = g.right() - w; - } - } else { - posx = qMax(posx, g.left()); - } - - posy = qBound(g.top(), posy, g.bottom() - h); - - setGeometry(posx, posy, w, h); - - QQuickWindow::show(); - setMouseGrabEnabled(true); - setKeyboardGrabEnabled(true); -} - -void PopupWindow::setParentItem(QQuickItem *item) -{ - m_parentItem = item; - if (m_parentItem) - setTransientParent(m_parentItem->window()); -} - -void PopupWindow::setPopupContentItem(QQuickItem *contentItem) -{ - if (!contentItem) - return; - - contentItem->setParentItem(this->contentItem()); - m_contentItem = contentItem; -} - -void PopupWindow::dismissPopup() -{ - m_dismissed = true; - emit popupDismissed(); - hide(); -} - -void PopupWindow::mouseMoveEvent(QMouseEvent *e) -{ - QQuickWindow::mouseMoveEvent(e); -} - -void PopupWindow::mousePressEvent(QMouseEvent *e) -{ - QRect rect = QRect(QPoint(), size()); - if (rect.contains(e->pos())) { - QQuickWindow::mousePressEvent(e); - } else { - dismissPopup(); - } -} - -void PopupWindow::mouseReleaseEvent(QMouseEvent *e) -{ - QQuickWindow::mouseReleaseEvent(e); - dismissPopup(); -} - -bool PopupWindow::event(QEvent *event) -{ - //QTBUG-45079 - //This is a workaround for popup menu not being closed when using touch input. - //Currently mouse synthesized events are not created for touch events which are - //outside the qquickwindow. - - if (event->type() == QEvent::TouchBegin && !qobject_cast(transientParent())) { - QRect rect = QRect(QPoint(), size()); - QTouchEvent *touch = static_cast(event); - QTouchEvent::TouchPoint point = touch->touchPoints().first(); - if ((point.state() == Qt::TouchPointPressed) && !rect.contains(point.pos().toPoint())) { - //first default handling - bool result = QQuickWindow::event(event); - //now specific broken case - if (!m_dismissed) - dismissPopup(); - return result; - } - } - - return QQuickWindow::event(event); -} diff --git a/src/popupwindow.h b/src/popupwindow.h deleted file mode 100644 index 6f2cf54..0000000 --- a/src/popupwindow.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef TOPLEVELMENU_H -#define TOPLEVELMENU_H - -#include -#include - -class PopupWindow : public QQuickWindow -{ - Q_OBJECT - Q_PROPERTY(QQuickItem *popupContentItem READ popupContentItem WRITE setPopupContentItem) - Q_CLASSINFO("DefaultProperty", "popupContentItem") - Q_PROPERTY(QQuickItem *parentItem READ parentItem WRITE setParentItem) - -public: - PopupWindow(QQuickWindow *parent = nullptr); - - QQuickItem *popupContentItem() const { return m_contentItem; } - void setPopupContentItem(QQuickItem *popupContentItem); - - QQuickItem *parentItem() const { return m_parentItem; } - virtual void setParentItem(QQuickItem *); - -public slots: - void show(); - void dismissPopup(); - -signals: - void popupDismissed(); - void geometryChanged(); - -protected: - void mousePressEvent(QMouseEvent *) override; - void mouseReleaseEvent(QMouseEvent *) override; - void mouseMoveEvent(QMouseEvent *) override; - bool event(QEvent *) override; - -protected slots: - void applicationStateChanged(Qt::ApplicationState state); - -private: - QQuickItem *m_parentItem; - QPointer m_contentItem; - bool m_mouseMoved; - bool m_dismissed; -}; - -#endif // POPUPWINDOW_H