// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause #include #include #include #include #include #include #include #include // The chat list's resize handle, dragged with real pointer events. // // Two things are only observable under a real drag, and both were reported as // "the width jumps to an extreme on a millimetre of movement and cannot be // changed again until I restart": the grab must come back, or a stale // translation pins the column, and the handle must not be parked over the // list's flickable, which is exactly where the seam is. // // The scene is a reduction of Main.qml's columns: a scrollable page, a seam, a // handle. // plain Qt6::Quick test. static const char *sceneQml = R"QML( import QtQuick import QtQuick.Controls as QQC2 QQC2.ApplicationWindow { id: root width: 1200 height: 700 visible: true // false is the layout that works: the handle is a child of the window, laid // over the seam, and never inside the page the list scrolls in. property bool handleInsidePage: false // The width-at-grab is a property of the handle, and it has to be read back // from there. Addressing it on the handler instead is accepted by QML with // nothing but a warning, reads back as undefined, and turns the whole drag // into NaN. property bool widthReadFromTheHandler: false // Main.qml's range, and the narrow end of it is the interesting one: it used // to be seventeen grid units, a width chosen so that a name has somewhere to // go, and now it is an avatar and the small padding either side of it. The // two are named apart from the sum so that the relationship - the minimum is // the row's own padding plus the row's own avatar - stays visible here, and // so that changing one of them in ContactDelegate.qml breaks this loudly. readonly property real narrowPadding: 4 readonly property real avatarSize: 40 readonly property real minWidth: narrowPadding * 2 + avatarSize readonly property real maxWidth: 468 property real columnWidth: 360 function clampedWidth() { return Math.max(root.minWidth, Math.min(root.maxWidth, root.columnWidth)) } // Main.qml's setChatListColumnWidth, guard and all: a width that is not a // number is refused rather than clamped, because Math.min and Math.max both // pass NaN through and Qt then refuses to apply NaN to an item's geometry - // leaving the old width in place with the drag still writing NaN over it, // which is a column that cannot be moved again until a restart. function setColumnWidth(w) { if (!Number.isFinite(w)) return root.columnWidth = Math.max(root.minWidth, Math.min(root.maxWidth, w)) } Item { id: column width: root.clampedWidth() height: root.height // The page the conversations are listed in, and the ScrollView that // fills it - the thing whose scrollbar sits under the seam. Item { id: page objectName: "page" anchors.fill: parent QQC2.ScrollView { id: listScroll anchors.fill: parent contentWidth: availableWidth Column { width: listScroll.availableWidth Repeater { model: 40 Item { width: 10; height: 24 } } } } } Item { id: handle objectName: "handle" // The one line under test: inside the page, it sits on the // ScrollView's scrollbar; outside, it sits beside it. parent: root.handleInsidePage ? page : root.contentItem z: 500 x: parent === page ? page.width - width / 2 : page.width - width / 2 y: 0 width: 12 height: parent === page ? page.height : root.height property real widthAtGrab: 0 Rectangle { anchors.horizontalCenter: parent.horizontalCenter width: 1 height: parent.height color: "red" } HoverHandler { cursorShape: Qt.SizeHorCursor } DragHandler { id: resizeDrag objectName: "resizeDrag" target: null cursorShape: Qt.SizeHorCursor yAxis.enabled: false grabPermissions: PointerHandler.CanTakeOverFromAnything onActiveChanged: { if (!active) return if (root.widthReadFromTheHandler) resizeDrag.widthAtGrab = root.clampedWidth() else handle.widthAtGrab = root.clampedWidth() } onTranslationChanged: { if (!active) return const base = root.widthReadFromTheHandler ? resizeDrag.widthAtGrab : handle.widthAtGrab root.setColumnWidth(base + translation.x) } } } } // What the assertions ask about, readable from C++. property bool dragActive: resizeDrag.active property real dragTranslation: resizeDrag.translation.x } )QML"; class ChatListHandleDragTest : public QObject { Q_OBJECT static void settle(int rounds = 6) { for (int i = 0; i < rounds; ++i) QCoreApplication::processEvents(QEventLoop::AllEvents, 20); } QTemporaryDir *m_dir = nullptr; QQmlApplicationEngine *m_view = nullptr; QObject *m_scene = nullptr; QObject *scene() const { return m_scene; } QQuickWindow *window() const { return qobject_cast(scene()); } QQuickItem *item(const char *name) const { return scene()->findChild(QString::fromLatin1(name)); } // The seam's centre in window coordinates: the point a user aims at. static QPoint handleCentre(QQuickItem *handle) { return handle->mapToScene(QPointF(handle->width() / 2, handle->height() / 2)).toPoint(); } double columnWidth() const { return item("page")->width(); } bool handlerStuck() const { return scene()->property("dragActive").toBool(); } void drag(int dx) { auto *handle = item("handle"); const QPoint start = handleCentre(handle); QTest::mousePress(window(), Qt::LeftButton, Qt::NoModifier, start); settle(); // In steps, so this is a drag and not one click that happened to land // elsewhere: a real hand reports more than one position. for (int step = 1; step <= 4; ++step) { QTest::mouseMove(window(), start + QPoint(dx * step / 4, 0)); settle(); } QTest::mouseRelease(window(), Qt::LeftButton, Qt::NoModifier, start + QPoint(dx, 0)); settle(); } bool start(bool handleInsidePage) { delete m_view; m_view = new QQmlApplicationEngine; QQmlComponent c(m_view, QUrl::fromLocalFile(m_dir->filePath(QStringLiteral("scene.qml")))); if (c.isError()) { for (const QQmlError &e : c.errors()) qWarning("%s", qPrintable(e.toString())); delete m_view; m_view = nullptr; return false; } m_scene = c.create(); if (!m_scene) { for (const QQmlError &e : c.errors()) qWarning("%s", qPrintable(e.toString())); delete m_view; m_view = nullptr; m_scene = nullptr; return false; } m_scene->setProperty("handleInsidePage", handleInsidePage); m_scene->setParent(m_view); auto *window = qobject_cast(m_scene); window->resize(1200, 700); window->show(); settle(); return window->isVisible(); } private Q_SLOTS: void initTestCase() { m_dir = new QTemporaryDir; QVERIFY(m_dir->isValid()); QFile qml(m_dir->filePath(QStringLiteral("scene.qml"))); QVERIFY(qml.open(QIODevice::WriteOnly)); qml.write(sceneQml); qml.close(); } void cleanupTestCase() { delete m_view; delete m_dir; } // "I move it a millimetre and it goes off to some extreme." The column has // to follow the handle, and a drag of n pixels has to move it by about n and // by nothing else. The extreme this used to reach was not a limit but NaN: // a width Qt refuses to apply, so the column kept the width it had while the // drag went on writing nonsense over it, and stopped responding for good. void test_small_drag_moves_the_column_and_nothing_else() { QVERIFY(start(false)); const double before = columnWidth(); // 20px and not 2: a DragHandler does not become active until the pointer // has travelled about ten, so anything shorter is a click that never // starts a drag, and testing that would be testing Qt rather than this. drag(20); const double after = columnWidth(); QVERIFY2(after > before && after - before <= 24.0, qPrintable(QStringLiteral("a 20px drag moved the column from %1 to %2").arg(before).arg(after))); QVERIFY2(after < 468.0, "the column went to the maximum"); } void test_drag_releases_the_grab() { QVERIFY(start(false)); drag(30); QVERIFY2(!handlerStuck(), "the DragHandler is still active after the release"); } // "And I cannot change it again": a second drag has to work, and from // wherever the first one left the column. void test_a_second_drag_still_works() { QVERIFY(start(false)); drag(30); const double afterFirst = columnWidth(); drag(-30); const double afterSecond = columnWidth(); QVERIFY2(afterSecond < afterFirst, qPrintable(QStringLiteral("second drag did nothing: %1 then %2").arg(afterFirst).arg(afterSecond))); } // Several drags alternating, which is what a user does while settling on a // width. A stuck handler shows up here even when the single drags pass. void test_repeated_drags_keep_working() { QVERIFY(start(false)); const double start_ = columnWidth(); drag(20); QVERIFY2(!handlerStuck(), "stuck after the first drag"); drag(-20); QVERIFY2(!handlerStuck(), "stuck after the second drag"); drag(20); QVERIFY2(!handlerStuck(), "stuck after the third drag"); drag(-20); // A handler that never let go has been feeding one growing translation // into the width, so the column ends up pinned at a limit rather than // back where it started. const double end = columnWidth(); QVERIFY2(qAbs(end - start_) < 4.0, qPrintable(QStringLiteral("four cancelling drags left the column at %1, started at %2").arg(end).arg(start_))); } // The handle on the seam, which in the real page is the right edge of a // ScrollView. Re-measuring after a resize is what caught it going stale, so // this guards the position as well as the drag. void test_handle_stays_on_the_seam_when_the_window_moves() { QVERIFY(start(false)); auto *page = item("page"); auto *handle = item("handle"); // Across the fold the page row moves its pages. A position measured by // a call the binding cannot see is captured mid-slide and never fixed. for (int w : {1200, 1600, 700, 500, 900, 640, 1300}) { window()->resize(w, 700); settle(); const double seam = page->width(); const double handleCentreX = handle->mapToScene(QPointF(handle->width() / 2, 0)).x(); QVERIFY2(qAbs(handleCentreX - seam) < 1.5, qPrintable(QStringLiteral("at window width %1 the handle is at %2, the seam at %3").arg(w).arg(handleCentreX).arg(seam))); } } // The trap this suite was written for. widthAtGrab belongs to the handle, and // QML lets you address it on the handler as well - it warns, and then hands // back undefined, so the drag measures from nothing, arrives as NaN, and // leaves a column that cannot be moved again. Kept as a failing case so the // way it fails is on the record: the width does not move, and no error // escapes to anywhere a user would see it. void test_reading_the_grab_width_from_the_handler_freezes_the_column() { QVERIFY(start(false)); m_scene->setProperty("widthReadFromTheHandler", true); settle(); drag(30); QVERIFY2(qAbs(columnWidth() - 360) < 1.0, "a drag measured from an unreadable width should not move the column - " "if this passes, the guard in setChatListColumnWidth is no longer the " "thing keeping the column usable"); // And the point of the guard: the column is still movable afterwards. m_scene->setProperty("widthReadFromTheHandler", false); drag(30); QVERIFY(columnWidth() > 360); } // The same drag, with the handle parented to the page instead of the window. // This is the layout that put the handle on top of the ScrollView, and it is // kept as a failing case on purpose: it is what the bug report describes, // and a test that stops covering it stops being able to catch a return. void test_parented_to_the_page_it_loses_the_grab() { QVERIFY(start(true)); drag(30); QVERIFY2(!handlerStuck(), "a handle inside the scrollable page does not give the grab back"); } // Below the old seventeen grid units the column is a strip of avatars, and the // handle has to be able to reach it: a minimum that stops short of it is a // narrow mode that cannot be arrived at. void test_the_handle_reaches_the_narrow_end() { QVERIFY(start(false)); const double minimum = scene()->property("minWidth").toDouble(); QVERIFY2(minimum < 306.0, "the minimum is still the old name-sized width"); // Far past the end of the range, which is what a drag to the window edge // is: the column must stop at the minimum rather than keep going. drag(-3000); const double end = columnWidth(); QVERIFY2(qAbs(end - minimum) < 1.0, qPrintable(QStringLiteral("dragged 3000px left, the column is at %1 and the minimum is %2").arg(end).arg(minimum))); QVERIFY2(end > 0, "the column was dragged to nothing"); } // And the same drag coming back: a column at the minimum that cannot be // widened is the bug this whole suite was written for, in a new place. void test_a_column_at_the_narrow_end_can_be_widened_again() { QVERIFY(start(false)); drag(-3000); const double minimum = columnWidth(); drag(120); const double after = columnWidth(); QVERIFY2(after > minimum + 100.0, qPrintable(QStringLiteral("a 120px drag from the minimum left the column at %1, it was %2").arg(after).arg(minimum))); QVERIFY2(!handlerStuck(), "the DragHandler is still active after releasing at the minimum"); } // Landing exactly on the minimum by accident has to be as usable as // overshooting it: a drag that stops a pixel short and one that flies past // both leave the column at the same width, and that width has to come back. void test_widening_off_the_narrow_end_needs_no_second_grab() { QVERIFY(start(false)); drag(-3000); drag(-3000); QVERIFY2(!handlerStuck(), "stuck after a second drag at the minimum"); drag(60); QVERIFY2(columnWidth() > scene()->property("minWidth").toDouble() + 50.0, "the column would not come back off the minimum"); } }; QTEST_MAIN(ChatListHandleDragTest) #include "ChatListHandleDragTest.moc"