Skip to content

Commit

Permalink
Fix musescore#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 Nov 30, 2023
1 parent 3976041 commit b92d9e7
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
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: {
Qt.callLater(appMenuModel.handleMenuItem, itemId)
}

onOpenPrevMenu: {
appMenuModel.openPrevMenu()
}

onOpenNextMenu: {
appMenuModel.openNextMenu()
}

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

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

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

void NavigableAppMenuModel::openMenu(const QString& menuId, bool byHover)
{
bool navigationStarted = isNavigationStarted();
Expand Down Expand Up @@ -330,7 +344,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: loader.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 @@ -35,6 +35,8 @@ MenuView {
property int preferredAlign: Qt.AlignRight // Left, HCenter, Right

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

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

Expand All @@ -159,7 +165,10 @@ MenuView {
root.navigationParentControl.requestActive()
}

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

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

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

0 comments on commit b92d9e7

Please sign in to comment.