From b6bb2f4882101508025ab993d44fb66d8642ca94 Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 20 May 2021 17:51:54 +0100 Subject: [PATCH] Accessibility: Improved PopupMenu focus handling when opening and added support for submenus --- .../juce_gui_basics/menus/juce_PopupMenu.cpp | 47 ++++++++++++++----- 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp index 2e7efa3abe..67b77c8061 100644 --- a/modules/juce_gui_basics/menus/juce_PopupMenu.cpp +++ b/modules/juce_gui_basics/menus/juce_PopupMenu.cpp @@ -233,13 +233,18 @@ private: .addAction (AccessibilityActionType::toggle, std::move (onToggle)); if (hasActiveSubMenu (item.item)) - actions.addAction (AccessibilityActionType::showMenu, [&item] - { - item.parentWindow.showSubMenuFor (&item); + { + auto showSubMenu = [&item] + { + item.parentWindow.showSubMenuFor (&item); - if (auto* subMenu = item.parentWindow.activeSubMenu.get()) - subMenu->setCurrentlyHighlightedChild (subMenu->items.getFirst()); - }); + if (auto* subMenu = item.parentWindow.activeSubMenu.get()) + subMenu->setCurrentlyHighlightedChild (subMenu->items.getFirst()); + }; + + actions.addAction (AccessibilityActionType::press, showSubMenu); + actions.addAction (AccessibilityActionType::showMenu, showSubMenu); + } return actions; } @@ -519,6 +524,24 @@ struct MenuWindow : public Component float getDesktopScaleFactor() const override { return scaleFactor * Desktop::getInstance().getGlobalScaleFactor(); } + void visibilityChanged() override + { + if (! isShowing()) + return; + + auto* accessibleFocus = [this] + { + if (currentChild != nullptr) + if (auto* childHandler = currentChild->getAccessibilityHandler()) + return childHandler; + + return getAccessibilityHandler(); + }(); + + if (accessibleFocus != nullptr) + accessibleFocus->grabFocus(); + } + //============================================================================== bool keyPressed (const KeyPress& key) override { @@ -1100,9 +1123,6 @@ struct MenuWindow : public Component void setCurrentlyHighlightedChild (ItemComponent* child) { - if (currentChild == child) - return; - if (currentChild != nullptr) currentChild->setHighlighted (false); @@ -1217,8 +1237,14 @@ struct MenuWindow : public Component AccessibilityActions().addAction (AccessibilityActionType::focus, [this] { if (currentChild != nullptr) + { if (auto* handler = currentChild->getAccessibilityHandler()) handler->grabFocus(); + } + else + { + selectNextItem (MenuSelectionDirection::forwards); + } })); } @@ -2028,9 +2054,6 @@ int PopupMenu::showWithOptionalCallback (const Options& options, window->toFront (false); // need to do this after making it modal, or it could // be stuck behind other comps that are already modal.. - if (auto* handler = window->getAccessibilityHandler()) - handler->grabFocus(); - #if JUCE_MODAL_LOOPS_PERMITTED if (userCallback == nullptr && canBeModal) return window->runModalLoop();