fix HiDPI position calculate error

This commit is contained in:
Altair Wei 2021-09-21 15:42:11 +08:00
parent 3a3e39c0c0
commit 989de57f33
3 changed files with 56 additions and 37 deletions

View File

@ -3,6 +3,7 @@
int main(int argc, char *argv[])
{
#if 1
QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
@ -11,7 +12,7 @@ int main(int argc, char *argv[])
QGuiApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
#endif
#endif
#endif
QApplication application(argc, argv);
FLWindow win;

View File

@ -318,6 +318,9 @@ void FramelessHelper::updateHoverStates(const QPoint& pos)
void FramelessHelper::startMove(const QPoint &globalPos)
{
#ifdef Q_OS_LINUX
// On HiDPI screen, X11 ButtonRelease is likely to trigger
// a QEvent::MouseMove, so we reset m_clickedFrameSection in advance.
m_clickedFrameSection = Qt::NoSection;
Utilities::sendX11ButtonReleaseEvent(m_window, globalPos);
Utilities::startX11Moving(m_window, globalPos);
#endif
@ -326,6 +329,9 @@ void FramelessHelper::startMove(const QPoint &globalPos)
void FramelessHelper::startResize(const QPoint &globalPos, Qt::WindowFrameSection frameSection)
{
#ifdef Q_OS_LINUX
// On HiDPI screen, X11 ButtonRelease is likely to trigger
// a QEvent::MouseMove, so we reset m_clickedFrameSection in advance.
m_clickedFrameSection = Qt::NoSection;
Utilities::sendX11ButtonReleaseEvent(m_window, globalPos);
Utilities::startX11Resizing(m_window, globalPos, frameSection);
#endif
@ -375,17 +381,20 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
&& isInTitlebarArea(ev->pos())) {
// Start system move
startMove(ev->globalPos());
ev->accept();
filterOut = true;
} else if (isClickResizeHandler() && isHoverResizeHandler()) {
// Start system resize
startResize(ev->globalPos(), m_hoveredFrameSection);
ev->accept();
filterOut = true;
}
// This case takes into account that the mouse moves outside the window boundary
QRect windowRect(0, 0, windowSize().width(), windowSize().height());
if (m_clickedFrameSection != Qt::NoSection && !windowRect.contains(ev->pos())) {
if (isClickResizeHandler() && !windowRect.contains(ev->pos())) {
startResize(ev->globalPos(), m_clickedFrameSection);
ev->accept();
filterOut = true;
}

View File

@ -26,6 +26,7 @@
#include <QtCore/qvariant.h>
#include <QtCore/qdebug.h>
#include <QtGui/qscreen.h>
#include <QtX11Extras/qx11info_x11.h>
#include <X11/Xlib.h>
@ -159,15 +160,19 @@ void Utilities::sendX11ButtonReleaseEvent(QWindow *w, const QPoint &globalPos)
memset(&xevent, 0, sizeof(XEvent));
xevent.type = ButtonRelease;
xevent.xbutton.button = Button1;
xevent.xbutton.time = CurrentTime;
xevent.xbutton.button = 0;
xevent.xbutton.same_screen = True;
xevent.xbutton.send_event = True;
xevent.xbutton.window = w->winId();
xevent.xbutton.x = pos.x();
xevent.xbutton.y = pos.y();
xevent.xbutton.x_root = globalPos.x();
xevent.xbutton.y_root = globalPos.y();
xevent.xbutton.root = QX11Info::appRootWindow(screen);
xevent.xbutton.x = pos.x() * w->screen()->devicePixelRatio();
xevent.xbutton.y = pos.y() * w->screen()->devicePixelRatio();
xevent.xbutton.x_root = globalPos.x() * w->screen()->devicePixelRatio();
xevent.xbutton.y_root = globalPos.y() * w->screen()->devicePixelRatio();
xevent.xbutton.display = display;
if (XSendEvent(display, w->winId(), False, ButtonReleaseMask, &xevent) == 0)
if (XSendEvent(display, w->winId(), True, ButtonReleaseMask, &xevent) == 0)
qWarning() << "Failed to send ButtonRelease event.";
XFlush(display);
}
@ -178,23 +183,27 @@ void Utilities::sendX11MoveResizeEvent(QWindow *w, const QPoint &globalPos, int
const auto winId = w->winId();
const auto screen = QX11Info::appScreen();
XUngrabPointer(display, CurrentTime);
XEvent xev;
const Atom netMoveResize = XInternAtom(display, "_NET_WM_MOVERESIZE", false);
memset(&xev, 0x00, sizeof(xev));
const Atom netMoveResize = XInternAtom(display, "_NET_WM_MOVERESIZE", False);
xev.xclient.type = ClientMessage;
xev.xclient.message_type = netMoveResize;
xev.xclient.serial = 0;
xev.xclient.display = display;
xev.xclient.send_event = True;
xev.xclient.window = winId;
xev.xclient.format = 32;
xev.xclient.data.l[0] = globalPos.x();
xev.xclient.data.l[1] = globalPos.y();
xev.xclient.data.l[0] = globalPos.x() * w->screen()->devicePixelRatio();
xev.xclient.data.l[1] = globalPos.y() * w->screen()->devicePixelRatio();
xev.xclient.data.l[2] = section;
xev.xclient.data.l[3] = Button1;
xev.xclient.data.l[4] = 1;
XUngrabPointer(display, QX11Info::appTime());
xev.xclient.data.l[4] = 0;
if(XSendEvent(display, QX11Info::appRootWindow(screen),
false, SubstructureRedirectMask | SubstructureNotifyMask, &xev) == 0)
False, SubstructureRedirectMask | SubstructureNotifyMask, &xev) == 0)
qWarning("Failed to send Move or Resize event.");
XFlush(display);
}