Skip to content

Commit

Permalink
Fix #19987 Menu Navigation not working with Left/Right arrows
Browse files Browse the repository at this point in the history
Add a force close upon using Right Arrow on a non-submenu item
Use signals when appropriate to invoke openPrevMenu/openNextMenu
Add hasSiblingMenus property to allow context menus using the StyledMenu class to be closed using left/right arrow key
  • Loading branch information
NinjaNas committed Aug 19, 2024
1 parent 831b789 commit d0b8a77
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/appshell/qml/platform/AppMenuBar.qml
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,20 @@ ListView {
id: menuLoader

property string menuId: ""
property bool hasSiblingMenus: true

onHandleMenuItem: function(itemId) {
Qt.callLater(appMenuModel.handleMenuItem, itemId)
}

onOpenPrevMenu: {
appMenuModel.openPrevMenu()
}

onOpenNextMenu: {
appMenuModel.openNextMenu()
}

onOpened: {
appMenuModel.openedMenuId = menuLoader.menuId
}
Expand Down
13 changes: 12 additions & 1 deletion src/appshell/view/navigableappmenumodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,18 @@ void NavigableAppMenuModel::handleMenuItem(const QString& itemId)
AppMenuModel::handleMenuItem(itemId);
}

void NavigableAppMenuModel::openPrevMenu()
{
navigate(Qt::Key_Left);
activateHighlightedMenu();
}

void NavigableAppMenuModel::openNextMenu()
{
navigate(Qt::Key_Right);
activateHighlightedMenu();
}

void NavigableAppMenuModel::openMenu(const QString& menuId, bool byHover)
{
bool navigationStarted = isNavigationStarted();
Expand Down Expand Up @@ -343,7 +355,6 @@ bool NavigableAppMenuModel::processEventForAppMenu(QEvent* event)
default:
break;
}

return false;
}

Expand Down
2 changes: 2 additions & 0 deletions src/appshell/view/navigableappmenumodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class NavigableAppMenuModel : public AppMenuModel
Q_INVOKABLE void load() override;
Q_INVOKABLE void handleMenuItem(const QString& itemId) override;
Q_INVOKABLE void openMenu(const QString& menuId, bool byHover);
Q_INVOKABLE void openPrevMenu();
Q_INVOKABLE void openNextMenu();

bool isNavigationStarted() const;
bool isMenuOpened() const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,14 @@ Loader {
id: loader

signal handleMenuItem(string itemId)
signal openPrevMenu()
signal openNextMenu()
signal opened()
signal closed(bool force)

property alias menu: loader.item
property var menuAnchorItem: null
property bool hasSiblingMenus: root.hasSiblingMenus

property alias isMenuOpened: loader.active

Expand Down Expand Up @@ -62,6 +65,14 @@ Loader {
Qt.callLater(loader.handleMenuItem, itemId)
}

onOpenPrevMenu: {
Qt.callLater(loader.openPrevMenu)
}

onOpenNextMenu: {
Qt.callLater(loader.openNextMenu)
}

onClosed: function(force) {
Qt.callLater(prv.unloadMenu, force)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@ MenuView {
property alias model: view.model

property int preferredAlign: Qt.AlignRight // Left, HCenter, Right
property bool hasSiblingMenus: loader.hasSiblingMenus

signal handleMenuItem(string itemId)
signal openPrevMenu()
signal openNextMenu()

property alias width: content.width
property alias height: content.height
Expand Down Expand Up @@ -138,6 +141,10 @@ MenuView {
case NavigationEvent.Right:
var selectedItem = prv.selectedItem()
if (!Boolean(selectedItem) || !selectedItem.hasSubMenu) {
if (root.hasSiblingMenus) {
root.close(true)
root.openNextMenu()
}
return
}

Expand All @@ -160,6 +167,10 @@ MenuView {
}

root.close()

if(root.hasSiblingMenus) {
root.openPrevMenu()
}
break
case NavigationEvent.Up:
case NavigationEvent.Down:
Expand All @@ -180,6 +191,7 @@ MenuView {
var menuLoaderComponent = Qt.createComponent("../StyledMenuLoader.qml");
root.subMenuLoader = menuLoaderComponent.createObject(root)
root.subMenuLoader.menuAnchorItem = root.anchorItem
root.subMenuLoader.hasSiblingMenus = root.hasSiblingMenus

root.subMenuLoader.handleMenuItem.connect(function(itemId) {
Qt.callLater(root.handleMenuItem, itemId)
Expand All @@ -195,6 +207,7 @@ MenuView {

if (force) {
root.close(true)
root.openNextMenu()
}
})
}
Expand Down

0 comments on commit d0b8a77

Please sign in to comment.