forked from github_mirror/framelesshelper
Simplify code
Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
parent
23d53fef87
commit
86dcad3f3a
|
@ -25,6 +25,7 @@
|
|||
#include "framelesshelper.h"
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
|
||||
#include "utilities.h"
|
||||
#include <QtCore/qdebug.h>
|
||||
#include <QtCore/qcoreevent.h>
|
||||
#include <QtGui/qevent.h>
|
||||
|
@ -164,55 +165,15 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
|
|||
}
|
||||
return Qt::CursorShape::ArrowCursor;
|
||||
};
|
||||
const auto isInSpecificObjects = [](const QPointF &mousePos,
|
||||
const QObjectList &objects) -> bool {
|
||||
if (objects.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (auto &&obj : qAsConst(objects)) {
|
||||
if (!obj) {
|
||||
continue;
|
||||
}
|
||||
if (!obj->isWidgetType() && !obj->inherits("QQuickItem")) {
|
||||
qWarning() << obj << "is not a QWidget or QQuickItem!";
|
||||
continue;
|
||||
}
|
||||
if (!obj->property("visible").toBool()) {
|
||||
qDebug() << "Skipping invisible object" << obj;
|
||||
continue;
|
||||
}
|
||||
const auto mapOriginPointToWindow = [](const QObject *obj) -> QPointF {
|
||||
Q_ASSERT(obj);
|
||||
if (!obj) {
|
||||
return {};
|
||||
}
|
||||
QPointF point = {obj->property("x").toReal(), obj->property("y").toReal()};
|
||||
for (QObject *parent = obj->parent(); parent; parent = parent->parent()) {
|
||||
point += {parent->property("x").toReal(), parent->property("y").toReal()};
|
||||
if (parent->isWindowType()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return point;
|
||||
};
|
||||
const QPointF originPoint = mapOriginPointToWindow(obj);
|
||||
const qreal width = obj->property("width").toReal();
|
||||
const qreal height = obj->property("height").toReal();
|
||||
if (QRectF(originPoint.x(), originPoint.y(), width, height).contains(mousePos)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const auto isInTitlebarArea = [this, &isInSpecificObjects](const QPointF &globalPoint,
|
||||
const auto isInTitlebarArea = [this](const QPointF &globalPoint,
|
||||
const QPointF &point,
|
||||
const QWindow *window) -> bool {
|
||||
Q_ASSERT(window);
|
||||
return (point.y() <= m_titleBarHeight)
|
||||
&& !isInSpecificObjects(globalPoint, getIgnoreObjects(window));
|
||||
&& !Utilities::isMouseInSpecificObjects(globalPoint, getIgnoreObjects(window));
|
||||
};
|
||||
const auto moveOrResize =
|
||||
[this, &getWindowEdges, &isInTitlebarArea, &isInSpecificObjects](const QPointF &globalPoint,
|
||||
[this, &getWindowEdges, &isInTitlebarArea](const QPointF &globalPoint,
|
||||
const QPointF &point,
|
||||
QWindow *window) {
|
||||
Q_ASSERT(window);
|
||||
|
@ -227,7 +188,7 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
|
|||
}
|
||||
} else {
|
||||
if ((window->windowState() == Qt::WindowState::WindowNoState)
|
||||
&& !isInSpecificObjects(globalPoint, getIgnoreObjects(window))
|
||||
&& !Utilities::isMouseInSpecificObjects(globalPoint, getIgnoreObjects(window))
|
||||
&& getResizable(window)) {
|
||||
if (!window->startSystemResize(edges)) {
|
||||
// ### FIXME: TO BE IMPLEMENTED!
|
||||
|
|
|
@ -542,53 +542,12 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
|
|||
break;
|
||||
}
|
||||
|
||||
const auto isInSpecificObjects =
|
||||
[](const QPointF &mousePos, const QObjectList &objects, const qreal dpr) -> bool {
|
||||
if (objects.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
for (auto &&object : qAsConst(objects)) {
|
||||
if (!object) {
|
||||
continue;
|
||||
}
|
||||
if (!object->isWidgetType() && !object->inherits("QQuickItem")) {
|
||||
qWarning() << object << "is not a QWidget or QQuickItem!";
|
||||
continue;
|
||||
}
|
||||
if (!object->property("visible").toBool()) {
|
||||
qDebug() << "Skipping invisible object" << object;
|
||||
continue;
|
||||
}
|
||||
const auto mapOriginPointToWindow = [](const QObject *obj) -> QPointF {
|
||||
Q_ASSERT(obj);
|
||||
if (!obj) {
|
||||
return {};
|
||||
}
|
||||
QPointF point = {obj->property("x").toReal(), obj->property("y").toReal()};
|
||||
for (QObject *parent = obj->parent(); parent; parent = parent->parent()) {
|
||||
point += {parent->property("x").toReal(), parent->property("y").toReal()};
|
||||
if (parent->isWindowType()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return point;
|
||||
};
|
||||
const QPointF originPoint = mapOriginPointToWindow(object);
|
||||
const qreal width = object->property("width").toReal();
|
||||
const qreal height = object->property("height").toReal();
|
||||
if (QRectF(originPoint.x() * dpr, originPoint.y() * dpr, width * dpr, height * dpr)
|
||||
.contains(mousePos)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
};
|
||||
const qreal dpr = window->devicePixelRatio();
|
||||
const QPointF globalMouse = QCursor::pos(window->screen()) * dpr;
|
||||
POINT winLocalMouse = {qRound(globalMouse.x()), qRound(globalMouse.y())};
|
||||
ScreenToClient(msg->hwnd, &winLocalMouse);
|
||||
const QPointF localMouse = {static_cast<qreal>(winLocalMouse.x), static_cast<qreal>(winLocalMouse.y)};
|
||||
const bool isInIgnoreObjects = isInSpecificObjects(globalMouse, getIgnoredObjects(window), dpr);
|
||||
const bool isInIgnoreObjects = Utilities::isMouseInSpecificObjects(globalMouse, getIgnoredObjects(window), dpr);
|
||||
const int bh = getSystemMetric(window, Utilities::SystemMetric::BorderHeight, true);
|
||||
const int tbh = getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true);
|
||||
const bool isTitleBar = (localMouse.y() <= tbh) && !isInIgnoreObjects;
|
||||
|
|
|
@ -421,3 +421,51 @@ bool Utilities::isWindowFixedSize(const QWindow *window)
|
|||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Utilities::isMouseInSpecificObjects(const QPointF &mousePos, const QObjectList &objects, const qreal dpr)
|
||||
{
|
||||
if (mousePos.isNull()) {
|
||||
qWarning() << "Mouse position point is null.";
|
||||
return false;
|
||||
}
|
||||
if (objects.isEmpty()) {
|
||||
qWarning() << "Object list is empty.";
|
||||
return false;
|
||||
}
|
||||
for (auto &&object : qAsConst(objects)) {
|
||||
if (!object) {
|
||||
qWarning() << "Object pointer is null.";
|
||||
continue;
|
||||
}
|
||||
if (!object->isWidgetType() && !object->inherits("QQuickItem")) {
|
||||
qWarning() << object << "is not a QWidget or QQuickItem!";
|
||||
continue;
|
||||
}
|
||||
if (!object->property("visible").toBool()) {
|
||||
qDebug() << "Skipping invisible object" << object;
|
||||
continue;
|
||||
}
|
||||
const auto mapOriginPointToWindow = [](const QObject *obj) -> QPointF {
|
||||
Q_ASSERT(obj);
|
||||
if (!obj) {
|
||||
return {};
|
||||
}
|
||||
QPointF point = {obj->property("x").toReal(), obj->property("y").toReal()};
|
||||
for (QObject *parent = obj->parent(); parent; parent = parent->parent()) {
|
||||
point += {parent->property("x").toReal(), parent->property("y").toReal()};
|
||||
if (parent->isWindowType()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return point;
|
||||
};
|
||||
const QPointF originPoint = mapOriginPointToWindow(object);
|
||||
const qreal width = object->property("width").toReal();
|
||||
const qreal height = object->property("height").toReal();
|
||||
const QRectF rect = {originPoint.x() * dpr, originPoint.y() * dpr, width * dpr, height * dpr};
|
||||
if (rect.contains(mousePos)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -75,6 +75,8 @@ FRAMELESSHELPER_EXPORT bool shouldUseNativeTitleBar();
|
|||
|
||||
FRAMELESSHELPER_EXPORT bool isWindowFixedSize(const QWindow *window);
|
||||
|
||||
FRAMELESSHELPER_EXPORT bool isMouseInSpecificObjects(const QPointF &mousePos, const QObjectList &objects, const qreal dpr = 1.0);
|
||||
|
||||
#ifdef Q_OS_WINDOWS
|
||||
// Windows specific
|
||||
FRAMELESSHELPER_EXPORT bool isWin7OrGreater();
|
||||
|
|
Loading…
Reference in New Issue