Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_MenuBarComponent.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. class MenuBarComponent::AccessibleItemComponent : public Component
  21. {
  22. public:
  23. AccessibleItemComponent (MenuBarComponent& comp, const String& menuItemName)
  24. : owner (comp),
  25. name (menuItemName)
  26. {
  27. setInterceptsMouseClicks (false, false);
  28. }
  29. const String& getName() const noexcept { return name; }
  30. private:
  31. std::unique_ptr<AccessibilityHandler> createAccessibilityHandler() override
  32. {
  33. class ComponentHandler : public AccessibilityHandler
  34. {
  35. public:
  36. explicit ComponentHandler (AccessibleItemComponent& item)
  37. : AccessibilityHandler (item,
  38. AccessibilityRole::menuItem,
  39. getAccessibilityActions (item)),
  40. itemComponent (item)
  41. {
  42. }
  43. String getTitle() const override { return itemComponent.name; }
  44. private:
  45. static AccessibilityActions getAccessibilityActions (AccessibleItemComponent& item)
  46. {
  47. auto showMenu = [&item] { item.owner.showMenu (item.owner.indexOfItemComponent (&item)); };
  48. return AccessibilityActions().addAction (AccessibilityActionType::focus,
  49. [&item] { item.owner.setItemUnderMouse (item.owner.indexOfItemComponent (&item)); })
  50. .addAction (AccessibilityActionType::press, showMenu)
  51. .addAction (AccessibilityActionType::showMenu, showMenu);
  52. }
  53. AccessibleItemComponent& itemComponent;
  54. };
  55. return std::make_unique<ComponentHandler> (*this);
  56. }
  57. MenuBarComponent& owner;
  58. const String name;
  59. };
  60. MenuBarComponent::MenuBarComponent (MenuBarModel* m)
  61. {
  62. setRepaintsOnMouseActivity (true);
  63. setWantsKeyboardFocus (false);
  64. setMouseClickGrabsKeyboardFocus (false);
  65. setModel (m);
  66. }
  67. MenuBarComponent::~MenuBarComponent()
  68. {
  69. setModel (nullptr);
  70. Desktop::getInstance().removeGlobalMouseListener (this);
  71. }
  72. MenuBarModel* MenuBarComponent::getModel() const noexcept
  73. {
  74. return model;
  75. }
  76. void MenuBarComponent::setModel (MenuBarModel* const newModel)
  77. {
  78. if (model != newModel)
  79. {
  80. if (model != nullptr)
  81. model->removeListener (this);
  82. model = newModel;
  83. if (model != nullptr)
  84. model->addListener (this);
  85. repaint();
  86. menuBarItemsChanged (nullptr);
  87. }
  88. }
  89. //==============================================================================
  90. void MenuBarComponent::paint (Graphics& g)
  91. {
  92. const auto isMouseOverBar = (currentPopupIndex >= 0 || itemUnderMouse >= 0 || isMouseOver());
  93. getLookAndFeel().drawMenuBarBackground (g, getWidth(), getHeight(), isMouseOverBar, *this);
  94. if (model == nullptr)
  95. return;
  96. for (size_t i = 0; i < itemComponents.size(); ++i)
  97. {
  98. const auto& itemComponent = itemComponents[i];
  99. const auto itemBounds = itemComponent->getBounds();
  100. Graphics::ScopedSaveState ss (g);
  101. g.setOrigin (itemBounds.getX(), 0);
  102. g.reduceClipRegion (0, 0, itemBounds.getWidth(), itemBounds.getHeight());
  103. getLookAndFeel().drawMenuBarItem (g,
  104. itemBounds.getWidth(),
  105. itemBounds.getHeight(),
  106. (int) i,
  107. itemComponent->getName(),
  108. (int) i == itemUnderMouse,
  109. (int) i == currentPopupIndex,
  110. isMouseOverBar,
  111. *this);
  112. }
  113. }
  114. void MenuBarComponent::resized()
  115. {
  116. int x = 0;
  117. for (size_t i = 0; i < itemComponents.size(); ++i)
  118. {
  119. auto& itemComponent = itemComponents[i];
  120. auto w = getLookAndFeel().getMenuBarItemWidth (*this, (int) i, itemComponent->getName());
  121. itemComponent->setBounds (x, 0, w, getHeight());
  122. x += w;
  123. }
  124. }
  125. int MenuBarComponent::getItemAt (Point<int> p)
  126. {
  127. for (size_t i = 0; i < itemComponents.size(); ++i)
  128. if (itemComponents[i]->getBounds().contains (p) && reallyContains (p, true))
  129. return (int) i;
  130. return -1;
  131. }
  132. void MenuBarComponent::repaintMenuItem (int index)
  133. {
  134. if (isPositiveAndBelow (index, (int) itemComponents.size()))
  135. {
  136. auto itemBounds = itemComponents[(size_t) index]->getBounds();
  137. repaint (itemBounds.getX() - 2,
  138. 0,
  139. itemBounds.getWidth() + 4,
  140. itemBounds.getHeight());
  141. }
  142. }
  143. void MenuBarComponent::setItemUnderMouse (int index)
  144. {
  145. if (itemUnderMouse == index)
  146. return;
  147. repaintMenuItem (itemUnderMouse);
  148. itemUnderMouse = index;
  149. repaintMenuItem (itemUnderMouse);
  150. if (isPositiveAndBelow (itemUnderMouse, (int) itemComponents.size()))
  151. if (auto* handler = itemComponents[(size_t) itemUnderMouse]->getAccessibilityHandler())
  152. handler->grabFocus();
  153. }
  154. void MenuBarComponent::setOpenItem (int index)
  155. {
  156. if (currentPopupIndex != index)
  157. {
  158. if (currentPopupIndex < 0 && index >= 0)
  159. model->handleMenuBarActivate (true);
  160. else if (currentPopupIndex >= 0 && index < 0)
  161. model->handleMenuBarActivate (false);
  162. repaintMenuItem (currentPopupIndex);
  163. currentPopupIndex = index;
  164. repaintMenuItem (currentPopupIndex);
  165. auto& desktop = Desktop::getInstance();
  166. if (index >= 0)
  167. desktop.addGlobalMouseListener (this);
  168. else
  169. desktop.removeGlobalMouseListener (this);
  170. }
  171. }
  172. void MenuBarComponent::updateItemUnderMouse (Point<int> p)
  173. {
  174. setItemUnderMouse (getItemAt (p));
  175. }
  176. void MenuBarComponent::showMenu (int index)
  177. {
  178. if (index != currentPopupIndex)
  179. {
  180. PopupMenu::dismissAllActiveMenus();
  181. menuBarItemsChanged (nullptr);
  182. setOpenItem (index);
  183. setItemUnderMouse (index);
  184. if (isPositiveAndBelow (index, (int) itemComponents.size()))
  185. {
  186. const auto& itemComponent = itemComponents[(size_t) index];
  187. auto m = model->getMenuForIndex (itemUnderMouse, itemComponent->getName());
  188. if (m.lookAndFeel == nullptr)
  189. m.setLookAndFeel (&getLookAndFeel());
  190. auto itemBounds = itemComponent->getBounds();
  191. const auto callback = [ref = SafePointer<MenuBarComponent> (this), index] (int result)
  192. {
  193. if (ref != nullptr)
  194. ref->menuDismissed (index, result);
  195. };
  196. m.showMenuAsync (PopupMenu::Options().withTargetComponent (this)
  197. .withTargetScreenArea (localAreaToGlobal (itemBounds))
  198. .withMinimumWidth (itemBounds.getWidth()),
  199. callback);
  200. }
  201. }
  202. }
  203. void MenuBarComponent::menuDismissed (int topLevelIndex, int itemId)
  204. {
  205. topLevelIndexClicked = topLevelIndex;
  206. postCommandMessage (itemId);
  207. }
  208. void MenuBarComponent::handleCommandMessage (int commandId)
  209. {
  210. updateItemUnderMouse (getMouseXYRelative());
  211. if (currentPopupIndex == topLevelIndexClicked)
  212. setOpenItem (-1);
  213. if (commandId != 0 && model != nullptr)
  214. model->menuItemSelected (commandId, topLevelIndexClicked);
  215. }
  216. //==============================================================================
  217. void MenuBarComponent::mouseEnter (const MouseEvent& e)
  218. {
  219. if (e.eventComponent == this)
  220. updateItemUnderMouse (e.getPosition());
  221. }
  222. void MenuBarComponent::mouseExit (const MouseEvent& e)
  223. {
  224. if (e.eventComponent == this)
  225. updateItemUnderMouse (e.getPosition());
  226. }
  227. void MenuBarComponent::mouseDown (const MouseEvent& e)
  228. {
  229. if (currentPopupIndex < 0)
  230. {
  231. updateItemUnderMouse (e.getEventRelativeTo (this).getPosition());
  232. currentPopupIndex = -2;
  233. showMenu (itemUnderMouse);
  234. }
  235. }
  236. void MenuBarComponent::mouseDrag (const MouseEvent& e)
  237. {
  238. const auto item = getItemAt (e.getEventRelativeTo (this).getPosition());
  239. if (item >= 0)
  240. showMenu (item);
  241. }
  242. void MenuBarComponent::mouseUp (const MouseEvent& e)
  243. {
  244. const auto e2 = e.getEventRelativeTo (this);
  245. updateItemUnderMouse (e2.getPosition());
  246. if (itemUnderMouse < 0 && getLocalBounds().contains (e2.x, e2.y))
  247. {
  248. setOpenItem (-1);
  249. PopupMenu::dismissAllActiveMenus();
  250. }
  251. }
  252. void MenuBarComponent::mouseMove (const MouseEvent& e)
  253. {
  254. const auto e2 = e.getEventRelativeTo (this);
  255. if (lastMousePos != e2.getPosition())
  256. {
  257. if (currentPopupIndex >= 0)
  258. {
  259. const auto item = getItemAt (e2.getPosition());
  260. if (item >= 0)
  261. showMenu (item);
  262. }
  263. else
  264. {
  265. updateItemUnderMouse (e2.getPosition());
  266. }
  267. lastMousePos = e2.getPosition();
  268. }
  269. }
  270. bool MenuBarComponent::keyPressed (const KeyPress& key)
  271. {
  272. const auto numMenus = (int) itemComponents.size();
  273. if (numMenus > 0)
  274. {
  275. const auto currentIndex = jlimit (0, numMenus - 1, currentPopupIndex);
  276. if (key.isKeyCode (KeyPress::leftKey))
  277. {
  278. showMenu ((currentIndex + numMenus - 1) % numMenus);
  279. return true;
  280. }
  281. if (key.isKeyCode (KeyPress::rightKey))
  282. {
  283. showMenu ((currentIndex + 1) % numMenus);
  284. return true;
  285. }
  286. }
  287. return false;
  288. }
  289. void MenuBarComponent::menuBarItemsChanged (MenuBarModel*)
  290. {
  291. StringArray newNames;
  292. if (model != nullptr)
  293. newNames = model->getMenuBarNames();
  294. auto itemsHaveChanged = [this, &newNames]
  295. {
  296. if ((int) itemComponents.size() != newNames.size())
  297. return true;
  298. for (size_t i = 0; i < itemComponents.size(); ++i)
  299. if (itemComponents[i]->getName() != newNames[(int) i])
  300. return true;
  301. return false;
  302. }();
  303. if (itemsHaveChanged)
  304. {
  305. updateItemComponents (newNames);
  306. repaint();
  307. resized();
  308. }
  309. }
  310. void MenuBarComponent::updateItemComponents (const StringArray& menuNames)
  311. {
  312. itemComponents.clear();
  313. for (const auto& name : menuNames)
  314. {
  315. itemComponents.push_back (std::make_unique<AccessibleItemComponent> (*this, name));
  316. addAndMakeVisible (*itemComponents.back());
  317. }
  318. }
  319. int MenuBarComponent::indexOfItemComponent (AccessibleItemComponent* itemComponent) const
  320. {
  321. const auto iter = std::find_if (itemComponents.cbegin(), itemComponents.cend(),
  322. [itemComponent] (const std::unique_ptr<AccessibleItemComponent>& c) { return c.get() == itemComponent; });
  323. if (iter != itemComponents.cend())
  324. return (int) std::distance (itemComponents.cbegin(), iter);
  325. jassertfalse;
  326. return -1;
  327. }
  328. void MenuBarComponent::menuCommandInvoked (MenuBarModel*, const ApplicationCommandTarget::InvocationInfo& info)
  329. {
  330. if (model == nullptr || (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) != 0)
  331. return;
  332. for (size_t i = 0; i < itemComponents.size(); ++i)
  333. {
  334. const auto menu = model->getMenuForIndex ((int) i, itemComponents[i]->getName());
  335. if (menu.containsCommandItem (info.commandID))
  336. {
  337. setItemUnderMouse ((int) i);
  338. startTimer (200);
  339. break;
  340. }
  341. }
  342. }
  343. void MenuBarComponent::timerCallback()
  344. {
  345. stopTimer();
  346. updateItemUnderMouse (getMouseXYRelative());
  347. }
  348. //==============================================================================
  349. std::unique_ptr<AccessibilityHandler> MenuBarComponent::createAccessibilityHandler()
  350. {
  351. struct MenuBarComponentAccessibilityHandler : public AccessibilityHandler
  352. {
  353. explicit MenuBarComponentAccessibilityHandler (MenuBarComponent& menuBarComponent)
  354. : AccessibilityHandler (menuBarComponent, AccessibilityRole::menuBar)
  355. {
  356. }
  357. AccessibleState getCurrentState() const override { return AccessibleState().withIgnored(); }
  358. };
  359. return std::make_unique<MenuBarComponentAccessibilityHandler> (*this);
  360. }
  361. } // namespace juce