aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickmenu.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-06-06 15:58:36 +0200
committerJ-P Nurmi <jpnurmi@qt.io>2017-06-07 09:56:27 +0000
commit0d59bff8929299ce5e2d9ec957055854a59c6cd6 (patch)
tree34e0c56e2af911bab3e5098e022af2d79d6eae8b /src/quicktemplates2/qquickmenu.cpp
parentf37397ba3125e1ab5dc940270d90844a6010224f (diff)
QQuickMenu: update the highlighted item on mouse hover
This is the expected behavior on desktop. NOTE: It must be possible to mix mouse hover highlighting, and keyboard navigation in the way that keyboard navigation must seamlessly continue from the item that was previously mouse highlighted. Furthermore, there should be only one item highlighted at a time. In order to be able to respect keyboard navigation while another item is hovered and vice versa, the visual highlight should be bound to MenuItem::highlighted instead of Control::activeFocus or Control::hovered. [ChangeLog][Controls][MenuItem] Menu has been fixed to highlight its items while key navigating and mouse hovering to ensure seamless item highlight between mouse hover and key navigation. In order to provide appropriate highlighting that works for key navigation and mouse hover, styles should bind their visual highlight to MenuItem::highlighted instead of Control::activeFocus or Control::hovered. Change-Id: I70cad5a5b441f2616d1ce2166e97974dc1ae063f Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickmenu.cpp')
-rw-r--r--src/quicktemplates2/qquickmenu.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/quicktemplates2/qquickmenu.cpp b/src/quicktemplates2/qquickmenu.cpp
index 4d1cfc52..1cea712f 100644
--- a/src/quicktemplates2/qquickmenu.cpp
+++ b/src/quicktemplates2/qquickmenu.cpp
@@ -186,6 +186,7 @@ void QQuickMenuPrivate::insertItem(int index, QQuickItem *item)
QObjectPrivate::connect(menuItem, &QQuickMenuItem::pressed, this, &QQuickMenuPrivate::onItemPressed);
QObject::connect(menuItem, &QQuickMenuItem::triggered, q, &QQuickPopup::close);
QObjectPrivate::connect(menuItem, &QQuickItem::activeFocusChanged, this, &QQuickMenuPrivate::onItemActiveFocusChanged);
+ QObjectPrivate::connect(menuItem, &QQuickControl::hoveredChanged, this, &QQuickMenuPrivate::onItemHovered);
}
}
@@ -209,6 +210,7 @@ void QQuickMenuPrivate::removeItem(int index, QQuickItem *item)
QObjectPrivate::disconnect(menuItem, &QQuickMenuItem::pressed, this, &QQuickMenuPrivate::onItemPressed);
QObject::disconnect(menuItem, &QQuickMenuItem::triggered, q, &QQuickPopup::close);
QObjectPrivate::disconnect(menuItem, &QQuickItem::activeFocusChanged, this, &QQuickMenuPrivate::onItemActiveFocusChanged);
+ QObjectPrivate::disconnect(menuItem, &QQuickControl::hoveredChanged, this, &QQuickMenuPrivate::onItemHovered);
}
}
@@ -305,6 +307,18 @@ void QQuickMenuPrivate::onItemPressed()
item->forceActiveFocus();
}
+void QQuickMenuPrivate::onItemHovered()
+{
+ Q_Q(QQuickMenu);
+ QQuickAbstractButton *button = qobject_cast<QQuickAbstractButton *>(q->sender());
+ if (!button || !button->isHovered() || QQuickAbstractButtonPrivate::get(button)->touchId != -1)
+ return;
+
+ int index = contentModel->indexOf(button, nullptr);
+ if (index != -1)
+ setCurrentIndex(index);
+}
+
void QQuickMenuPrivate::onItemActiveFocusChanged()
{
Q_Q(QQuickMenu);
@@ -327,6 +341,16 @@ int QQuickMenuPrivate::currentIndex() const
void QQuickMenuPrivate::setCurrentIndex(int index)
{
contentItem->setProperty("currentIndex", index);
+
+ QQuickMenuItem *newCurrentItem = contentItem->property("currentItem").value<QQuickMenuItem *>();
+
+ if (currentItem != newCurrentItem) {
+ if (currentItem)
+ currentItem->setHighlighted(false);
+ if (newCurrentItem)
+ newCurrentItem->setHighlighted(true);
+ currentItem = newCurrentItem;
+ }
}
void QQuickMenuPrivate::contentData_append(QQmlListProperty<QObject> *prop, QObject *obj)