Don't auto-close popups unless hitting the dimmer

To be able to have the virtual keyboard's input panel work during a
modal session we need to use a containment mask on the dimmer that lets
clicks into the input panel through. Such clicks should then not close
an auto-closing popup either.

Add a unit test that verifies that clicks pass through and don't
auto-close the popup.

Change-Id: If99e81220deea140a6e5d8971bb9d0a5467e2102
Done-with: Jarkko Koivikko <jarkko.koivikko@code-q.fi>
Pick-to: 6.2
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
This commit is contained in:
Volker Hilsheimer 2021-10-18 11:54:11 +02:00
parent 046d437da2
commit 974e2c668a
3 changed files with 147 additions and 1 deletions

View File

@ -309,7 +309,7 @@ bool QQuickPopupPrivate::tryClose(const QPointF &pos, QQuickPopup::ClosePolicy f
const bool onOutside = closePolicy & (flags & outsideFlags);
const bool onOutsideParent = closePolicy & (flags & outsideParentFlags);
if (onOutside || onOutsideParent) {
if (!contains(pos)) {
if (!contains(pos) && (!dimmer || dimmer->contains(dimmer->mapFromScene(pos)))) {
if (!onOutsideParent || !parentItem || !parentItem->contains(parentItem->mapFromScene(pos))) {
closeOrReject();
return true;
@ -742,6 +742,7 @@ static QQuickItem *createDimmer(QQmlComponent *component, QQuickPopup *popup, QQ
item->stackBefore(popup->popupItem());
item->setZ(popup->z());
// needed for the virtual keyboard to set a containment mask on the dimmer item
qCDebug(lcDimmer) << "dimmer" << item << "registered with" << parent;
parent->setProperty("_q_dimmerItem", QVariant::fromValue<QQuickItem*>(item));
if (popup->isModal()) {
item->setAcceptedMouseButtons(Qt::AllButtons);

View File

@ -0,0 +1,76 @@
/****************************************************************************
**
** Copyright (C) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
** * Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** * Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in
** the documentation and/or other materials provided with the
** distribution.
** * Neither the name of The Qt Company Ltd nor the names of its
** contributors may be used to endorse or promote products derived
** from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick
import QtQuick.Controls
ApplicationWindow {
id: window
width: 400
height: 400
title: "dimmerContainmentMask"
property alias modalPopup: modalPopup
property int clickCount: 0
MouseArea {
anchors.fill: parent
onClicked: ++clickCount;
}
Popup {
id: modalPopup
modal: true
x: 100
y: 100
width: 200
height: 200
}
}

View File

@ -111,6 +111,7 @@ private slots:
void centerInOverlayWithinStackViewItem();
void destroyDuringExitTransition();
void releaseAfterExitTransition();
void dimmerContainmentMask();
};
tst_QQuickPopup::tst_QQuickPopup()
@ -1624,6 +1625,74 @@ void tst_QQuickPopup::releaseAfterExitTransition()
QTRY_VERIFY(!popup->isOpened());
}
class ContainmentMask : public QObject
{
Q_OBJECT
public:
mutable bool called = false;
Q_INVOKABLE bool contains(const QPointF &point) const
{
called = true;
// let clicks at {1, 1} through the dimmer
return point != QPoint(1, 1);
}
};
/*
Test case for behavior we rely on in the virtual keyboard:
To prevent the virtual keyboard from being blocked by modal popups,
it sets a containment mask on the dimmer item, and lets clicks through
that hit the virtual keyboard.
*/
void tst_QQuickPopup::dimmerContainmentMask()
{
ContainmentMask containmentMask;
int expectedClickCount = 0;
QQuickApplicationHelper helper(this, "dimmerContainmentMask.qml");
QVERIFY2(helper.ready, helper.failureMessage());
QQuickWindow *window = helper.window;
window->show();
QCOMPARE(window->property("clickCount").toInt(), expectedClickCount);
QVERIFY(QTest::qWaitForWindowActive(window));
QQuickOverlay *overlay = QQuickOverlay::overlay(window);
QQuickPopup *modalPopup = window->property("modalPopup").value<QQuickPopup *>();
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(window->property("clickCount"), ++expectedClickCount);
modalPopup->open();
QTRY_VERIFY(modalPopup->isOpened());
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(window->property("clickCount"), expectedClickCount); // blocked by modal
QTRY_VERIFY(!modalPopup->isOpened()); // auto-close
modalPopup->open();
QTRY_VERIFY(modalPopup->isOpened());
QPointer<QQuickItem> dimmer = overlay->property("_q_dimmerItem").value<QQuickItem *>();
QVERIFY(dimmer);
dimmer->setContainmentMask(&containmentMask);
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QVERIFY(containmentMask.called);
QCOMPARE(window->property("clickCount"), ++expectedClickCount); // let through by containment mask
QVERIFY(modalPopup->isOpened()); // no auto-close
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(2, 2));
QCOMPARE(window->property("clickCount"), expectedClickCount); // blocked by modal
QTRY_VERIFY(!modalPopup->isOpened()); // auto-close
QTRY_VERIFY(!dimmer);
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(1, 1));
QCOMPARE(window->property("clickCount"), ++expectedClickCount); // no mask left behind
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, QPoint(2, 2));
QCOMPARE(window->property("clickCount"), ++expectedClickCount); // no mask left behind
}
QTEST_QUICKCONTROLS_MAIN(tst_QQuickPopup)
#include "tst_qquickpopup.moc"