[Solved, not perfect]How to auto-select an item after a popup menu appeared on screen?


#1

Not trigger an item, just auto-select it.


#2

I didn’t see anything in the API, but you could probably code your own by extending the PopupMenu class and adding some code borrowed from the MenuBarComponent class:

void MenuBarComponent::repaintMenuItem (int index)
{
    if (isPositiveAndBelow (index, xPositions.size()))
    {
        const int x1 = xPositions [index];
        const int x2 = xPositions [index + 1];

        repaint (x1 - 2, 0, x2 - x1 + 4, getHeight());
    }
}

void MenuBarComponent::setItemUnderMouse (const int index)
{
    if (itemUnderMouse != index)
    {
        repaintMenuItem (itemUnderMouse);
        itemUnderMouse = index;
        repaintMenuItem (itemUnderMouse);
    }
}

#3

Thanks, @matkatmusic

This approach is too ‘heavy’ and may not take effect I think…

I solved this issue, not perfect: force the mouse move to a position and over a menu item after the popup menu showed on screen. Here is the code:

menu.showMenuAsync (PopupMenu::Options().withTargetScreenArea (posOfMenu),
                    ModalCallbackFunction::forComponent (menuItemChosenCallback, this));

Desktop::getInstance().getMainMouseSource().setScreenPosition (posOfMenu.getPosition()
                                                               .translated (5, 35).toFloat());

#4

This approach works fine on Windows, but will flicker on macOS, it’s very annoying. Don’t know why…

Any better solution?