Menu: fix disabled sub-menu items being highlighted
When a menu item with a sub-menu was triggered by key or mouse, it would open the sub-menu with the first menu item highlighted. This doesn't make sense for disabled menu items, so this patch makes it find the first enabled item. Change-Id: I9df1c750749e5a77b027b6f476b8ae1f5ea035bd Fixes: QTBUG-69540 Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
parent
32f05c5f5f
commit
763b51d494
|
@ -468,10 +468,12 @@ void QQuickMenuPrivate::onItemTriggered()
|
|||
if (!item)
|
||||
return;
|
||||
|
||||
if (QQuickMenu *subMenu = item->subMenu())
|
||||
subMenu->popup(subMenu->itemAt(0));
|
||||
else
|
||||
if (QQuickMenu *subMenu = item->subMenu()) {
|
||||
auto subMenuPrivate = QQuickMenuPrivate::get(subMenu);
|
||||
subMenu->popup(subMenuPrivate->firstEnabledMenuItem());
|
||||
} else {
|
||||
q->dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
void QQuickMenuPrivate::onItemActiveFocusChanged()
|
||||
|
@ -621,6 +623,22 @@ bool QQuickMenuPrivate::activatePreviousItem()
|
|||
return false;
|
||||
}
|
||||
|
||||
QQuickMenuItem *QQuickMenuPrivate::firstEnabledMenuItem() const
|
||||
{
|
||||
for (int i = 0; i < contentModel->count(); ++i) {
|
||||
QQuickItem *item = itemAt(i);
|
||||
if (!item || !item->isEnabled())
|
||||
continue;
|
||||
|
||||
QQuickMenuItem *menuItem = qobject_cast<QQuickMenuItem *>(item);
|
||||
if (!menuItem)
|
||||
continue;
|
||||
|
||||
return menuItem;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObject *obj)
|
||||
{
|
||||
QQuickMenu *q = qobject_cast<QQuickMenu *>(prop->object);
|
||||
|
@ -1419,7 +1437,8 @@ void QQuickMenu::keyPressEvent(QKeyEvent *event)
|
|||
}
|
||||
} else {
|
||||
if (QQuickMenu *subMenu = d->currentSubMenu()) {
|
||||
subMenu->popup(subMenu->itemAt(0));
|
||||
auto subMenuPrivate = QQuickMenuPrivate::get(subMenu);
|
||||
subMenu->popup(subMenuPrivate->firstEnabledMenuItem());
|
||||
event->accept();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -115,6 +115,8 @@ public:
|
|||
bool activateNextItem();
|
||||
bool activatePreviousItem();
|
||||
|
||||
QQuickMenuItem *firstEnabledMenuItem() const;
|
||||
|
||||
static void contentData_append(QQmlListProperty<QObject> *prop, QObject *obj);
|
||||
static int contentData_count(QQmlListProperty<QObject> *prop);
|
||||
static QObject *contentData_at(QQmlListProperty<QObject> *prop, int index);
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
/****************************************************************************
|
||||
**
|
||||
** Copyright (C) 2019 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 2.12
|
||||
import QtQuick.Controls 2.12
|
||||
|
||||
ApplicationWindow {
|
||||
width: 600
|
||||
height: 400
|
||||
|
||||
property alias mainMenu: mainMenu
|
||||
property alias subMenu: subMenu
|
||||
|
||||
Menu {
|
||||
id: mainMenu
|
||||
title: "Menu"
|
||||
|
||||
Menu {
|
||||
id: subMenu
|
||||
title: "Sub Menu"
|
||||
MenuItem {
|
||||
id: subMenuItem1
|
||||
text: "Sub Menu Item 1"
|
||||
enabled: false
|
||||
}
|
||||
MenuItem {
|
||||
id: subMenuItem2
|
||||
text: "Sub Menu Item 2"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -79,8 +79,12 @@ private slots:
|
|||
void removeTakeItem();
|
||||
void subMenuMouse_data();
|
||||
void subMenuMouse();
|
||||
void subMenuDisabledMouse_data();
|
||||
void subMenuDisabledMouse();
|
||||
void subMenuKeyboard_data();
|
||||
void subMenuKeyboard();
|
||||
void subMenuDisabledKeyboard_data();
|
||||
void subMenuDisabledKeyboard();
|
||||
void subMenuPosition_data();
|
||||
void subMenuPosition();
|
||||
void addRemoveSubMenus();
|
||||
|
@ -997,6 +1001,64 @@ void tst_QQuickMenu::subMenuMouse()
|
|||
QVERIFY(!subSubMenu1->isVisible());
|
||||
}
|
||||
|
||||
void tst_QQuickMenu::subMenuDisabledMouse_data()
|
||||
{
|
||||
subMenuMouse_data();
|
||||
}
|
||||
|
||||
// QTBUG-69540
|
||||
void tst_QQuickMenu::subMenuDisabledMouse()
|
||||
{
|
||||
if ((QGuiApplication::platformName() == QLatin1String("offscreen"))
|
||||
|| (QGuiApplication::platformName() == QLatin1String("minimal")))
|
||||
QSKIP("Mouse hovering not functional on offscreen/minimal platforms");
|
||||
|
||||
QFETCH(bool, cascade);
|
||||
|
||||
QQuickApplicationHelper helper(this, QLatin1String("subMenuDisabled.qml"));
|
||||
QQuickApplicationWindow *window = helper.appWindow;
|
||||
window->show();
|
||||
QVERIFY(QTest::qWaitForWindowActive(window));
|
||||
centerOnScreen(window);
|
||||
moveMouseAway(window);
|
||||
|
||||
QQuickMenu *mainMenu = window->property("mainMenu").value<QQuickMenu *>();
|
||||
QVERIFY(mainMenu);
|
||||
mainMenu->setCascade(cascade);
|
||||
QCOMPARE(mainMenu->cascade(), cascade);
|
||||
|
||||
QQuickMenuItem *menuItem1 = qobject_cast<QQuickMenuItem *>(mainMenu->itemAt(0));
|
||||
QVERIFY(menuItem1);
|
||||
|
||||
QQuickMenu *subMenu = window->property("subMenu").value<QQuickMenu *>();
|
||||
QVERIFY(subMenu);
|
||||
|
||||
mainMenu->open();
|
||||
QVERIFY(mainMenu->isVisible());
|
||||
QVERIFY(!menuItem1->isHighlighted());
|
||||
QVERIFY(!subMenu->isVisible());
|
||||
|
||||
// Open the sub-menu with a mouse click.
|
||||
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, menuItem1->mapToScene(QPoint(1, 1)).toPoint());
|
||||
QCOMPARE(mainMenu->isVisible(), cascade);
|
||||
QVERIFY(subMenu->isVisible());
|
||||
QVERIFY(menuItem1->isHighlighted());
|
||||
// Now the sub-menu is open. The current behavior is that the first menu item
|
||||
// in the new menu is highlighted; make sure that we choose the next item if
|
||||
// the first is disabled.
|
||||
QQuickMenuItem *subMenuItem1 = qobject_cast<QQuickMenuItem *>(subMenu->itemAt(0));
|
||||
QVERIFY(subMenuItem1);
|
||||
QQuickMenuItem *subMenuItem2 = qobject_cast<QQuickMenuItem *>(subMenu->itemAt(1));
|
||||
QVERIFY(subMenuItem2);
|
||||
QVERIFY(!subMenuItem1->isHighlighted());
|
||||
QVERIFY(subMenuItem2->isHighlighted());
|
||||
|
||||
// Close all menus by clicking on the item that isn't disabled.
|
||||
QTest::mouseClick(window, Qt::LeftButton, Qt::NoModifier, subMenuItem2->mapToScene(QPoint(1, 1)).toPoint());
|
||||
QVERIFY(!mainMenu->isVisible());
|
||||
QVERIFY(!subMenu->isVisible());
|
||||
}
|
||||
|
||||
void tst_QQuickMenu::subMenuKeyboard_data()
|
||||
{
|
||||
QTest::addColumn<bool>("cascade");
|
||||
|
@ -1121,6 +1183,67 @@ void tst_QQuickMenu::subMenuKeyboard()
|
|||
QVERIFY(!subSubMenu1->isVisible());
|
||||
}
|
||||
|
||||
void tst_QQuickMenu::subMenuDisabledKeyboard_data()
|
||||
{
|
||||
subMenuKeyboard_data();
|
||||
}
|
||||
|
||||
// QTBUG-69540
|
||||
void tst_QQuickMenu::subMenuDisabledKeyboard()
|
||||
{
|
||||
QFETCH(bool, cascade);
|
||||
QFETCH(bool, mirrored);
|
||||
|
||||
QQuickApplicationHelper helper(this, QLatin1String("subMenuDisabled.qml"));
|
||||
QQuickApplicationWindow *window = helper.appWindow;
|
||||
window->show();
|
||||
QVERIFY(QTest::qWaitForWindowActive(window));
|
||||
centerOnScreen(window);
|
||||
moveMouseAway(window);
|
||||
|
||||
if (mirrored)
|
||||
window->setLocale(QLocale("ar_EG"));
|
||||
|
||||
QQuickMenu *mainMenu = window->property("mainMenu").value<QQuickMenu *>();
|
||||
QVERIFY(mainMenu);
|
||||
mainMenu->setCascade(cascade);
|
||||
QCOMPARE(mainMenu->cascade(), cascade);
|
||||
|
||||
QQuickMenuItem *menuItem1 = qobject_cast<QQuickMenuItem *>(mainMenu->itemAt(0));
|
||||
QVERIFY(menuItem1);
|
||||
|
||||
QQuickMenu *subMenu = window->property("subMenu").value<QQuickMenu *>();
|
||||
QVERIFY(subMenu);
|
||||
|
||||
mainMenu->open();
|
||||
QVERIFY(mainMenu->isVisible());
|
||||
QVERIFY(!menuItem1->isHighlighted());
|
||||
QVERIFY(!subMenu->isVisible());
|
||||
|
||||
// Highlight the top-level menu item.
|
||||
QTest::keyClick(window, Qt::Key_Down);
|
||||
QVERIFY(menuItem1->isHighlighted());
|
||||
|
||||
QQuickMenuItem *subMenuItem1 = qobject_cast<QQuickMenuItem *>(subMenu->itemAt(0));
|
||||
QVERIFY(subMenuItem1);
|
||||
QQuickMenuItem *subMenuItem2 = qobject_cast<QQuickMenuItem *>(subMenu->itemAt(1));
|
||||
QVERIFY(subMenuItem2);
|
||||
|
||||
// Open the sub-menu.
|
||||
QTest::keyClick(window, mirrored ? Qt::Key_Left : Qt::Key_Right);
|
||||
// The first sub-menu item is disabled, so it should highlight the second one.
|
||||
QVERIFY(!subMenuItem1->isHighlighted());
|
||||
QVERIFY(subMenuItem2->isHighlighted());
|
||||
|
||||
// Close the menus with escape.
|
||||
QTest::keyClick(window, Qt::Key_Escape);
|
||||
QCOMPARE(mainMenu->isVisible(), cascade);
|
||||
QVERIFY(!subMenu->isVisible());
|
||||
QTest::keyClick(window, Qt::Key_Escape);
|
||||
QVERIFY(!mainMenu->isVisible());
|
||||
QVERIFY(!subMenu->isVisible());
|
||||
}
|
||||
|
||||
void tst_QQuickMenu::subMenuPosition_data()
|
||||
{
|
||||
QTest::addColumn<bool>("cascade");
|
||||
|
|
Loading…
Reference in New Issue