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_Button.cpp 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. struct Button::CallbackHelper : public Timer,
  21. public ApplicationCommandManagerListener,
  22. public Value::Listener,
  23. public KeyListener
  24. {
  25. CallbackHelper (Button& b) : button (b) {}
  26. void timerCallback() override
  27. {
  28. button.repeatTimerCallback();
  29. }
  30. bool keyStateChanged (bool, Component*) override
  31. {
  32. return button.keyStateChangedCallback();
  33. }
  34. void valueChanged (Value& value) override
  35. {
  36. if (value.refersToSameSourceAs (button.isOn))
  37. button.setToggleState (button.isOn.getValue(), dontSendNotification, sendNotification);
  38. }
  39. bool keyPressed (const KeyPress&, Component*) override
  40. {
  41. // returning true will avoid forwarding events for keys that we're using as shortcuts
  42. return button.isShortcutPressed();
  43. }
  44. void applicationCommandInvoked (const ApplicationCommandTarget::InvocationInfo& info) override
  45. {
  46. if (info.commandID == button.commandID
  47. && (info.commandFlags & ApplicationCommandInfo::dontTriggerVisualFeedback) == 0)
  48. button.flashButtonState();
  49. }
  50. void applicationCommandListChanged() override
  51. {
  52. button.applicationCommandListChangeCallback();
  53. }
  54. Button& button;
  55. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CallbackHelper)
  56. };
  57. //==============================================================================
  58. Button::Button (const String& name) : Component (name), text (name)
  59. {
  60. callbackHelper.reset (new CallbackHelper (*this));
  61. setWantsKeyboardFocus (true);
  62. isOn.addListener (callbackHelper.get());
  63. }
  64. Button::~Button()
  65. {
  66. clearShortcuts();
  67. if (commandManagerToUse != nullptr)
  68. commandManagerToUse->removeListener (callbackHelper.get());
  69. isOn.removeListener (callbackHelper.get());
  70. callbackHelper.reset();
  71. }
  72. //==============================================================================
  73. void Button::setButtonText (const String& newText)
  74. {
  75. if (text != newText)
  76. {
  77. text = newText;
  78. repaint();
  79. }
  80. }
  81. void Button::setTooltip (const String& newTooltip)
  82. {
  83. SettableTooltipClient::setTooltip (newTooltip);
  84. generateTooltip = false;
  85. }
  86. void Button::updateAutomaticTooltip (const ApplicationCommandInfo& info)
  87. {
  88. if (generateTooltip && commandManagerToUse != nullptr)
  89. {
  90. auto tt = info.description.isNotEmpty() ? info.description
  91. : info.shortName;
  92. for (auto& kp : commandManagerToUse->getKeyMappings()->getKeyPressesAssignedToCommand (commandID))
  93. {
  94. auto key = kp.getTextDescription();
  95. tt << " [";
  96. if (key.length() == 1)
  97. tt << TRANS("shortcut") << ": '" << key << "']";
  98. else
  99. tt << key << ']';
  100. }
  101. SettableTooltipClient::setTooltip (tt);
  102. }
  103. }
  104. void Button::setConnectedEdges (int newFlags)
  105. {
  106. if (connectedEdgeFlags != newFlags)
  107. {
  108. connectedEdgeFlags = newFlags;
  109. repaint();
  110. }
  111. }
  112. //==============================================================================
  113. void Button::checkToggleableState (bool wasToggleable)
  114. {
  115. if (isToggleable() != wasToggleable)
  116. invalidateAccessibilityHandler();
  117. }
  118. void Button::setToggleable (bool isNowToggleable)
  119. {
  120. const auto wasToggleable = isToggleable();
  121. canBeToggled = isNowToggleable;
  122. checkToggleableState (wasToggleable);
  123. }
  124. void Button::setToggleState (bool shouldBeOn, NotificationType notification)
  125. {
  126. setToggleState (shouldBeOn, notification, notification);
  127. }
  128. void Button::setToggleState (bool shouldBeOn, NotificationType clickNotification, NotificationType stateNotification)
  129. {
  130. if (shouldBeOn != lastToggleState)
  131. {
  132. WeakReference<Component> deletionWatcher (this);
  133. if (shouldBeOn)
  134. {
  135. turnOffOtherButtonsInGroup (clickNotification, stateNotification);
  136. if (deletionWatcher == nullptr)
  137. return;
  138. }
  139. // This test is done so that if the value is void rather than explicitly set to
  140. // false, the value won't be changed unless the required value is true.
  141. if (getToggleState() != shouldBeOn)
  142. {
  143. isOn = shouldBeOn;
  144. if (deletionWatcher == nullptr)
  145. return;
  146. }
  147. lastToggleState = shouldBeOn;
  148. repaint();
  149. if (clickNotification != dontSendNotification)
  150. {
  151. // async callbacks aren't possible here
  152. jassert (clickNotification != sendNotificationAsync);
  153. sendClickMessage (ModifierKeys::currentModifiers);
  154. if (deletionWatcher == nullptr)
  155. return;
  156. }
  157. if (stateNotification != dontSendNotification)
  158. sendStateMessage();
  159. else
  160. buttonStateChanged();
  161. if (auto* handler = getAccessibilityHandler())
  162. handler->notifyAccessibilityEvent (AccessibilityEvent::valueChanged);
  163. }
  164. }
  165. void Button::setToggleState (bool shouldBeOn, bool sendChange)
  166. {
  167. setToggleState (shouldBeOn, sendChange ? sendNotification : dontSendNotification);
  168. }
  169. void Button::setClickingTogglesState (bool shouldToggle) noexcept
  170. {
  171. const auto wasToggleable = isToggleable();
  172. clickTogglesState = shouldToggle;
  173. checkToggleableState (wasToggleable);
  174. // if you've got clickTogglesState turned on, you shouldn't also connect the button
  175. // up to be a command invoker. Instead, your command handler must flip the state of whatever
  176. // it is that this button represents, and the button will update its state to reflect this
  177. // in the applicationCommandListChanged() method.
  178. jassert (commandManagerToUse == nullptr || ! clickTogglesState);
  179. }
  180. void Button::setRadioGroupId (int newGroupId, NotificationType notification)
  181. {
  182. if (radioGroupId != newGroupId)
  183. {
  184. radioGroupId = newGroupId;
  185. if (lastToggleState)
  186. turnOffOtherButtonsInGroup (notification, notification);
  187. setToggleable (true);
  188. invalidateAccessibilityHandler();
  189. }
  190. }
  191. void Button::turnOffOtherButtonsInGroup (NotificationType clickNotification, NotificationType stateNotification)
  192. {
  193. if (auto* p = getParentComponent())
  194. {
  195. if (radioGroupId != 0)
  196. {
  197. WeakReference<Component> deletionWatcher (this);
  198. for (auto* c : p->getChildren())
  199. {
  200. if (c != this)
  201. {
  202. if (auto b = dynamic_cast<Button*> (c))
  203. {
  204. if (b->getRadioGroupId() == radioGroupId)
  205. {
  206. b->setToggleState (false, clickNotification, stateNotification);
  207. if (deletionWatcher == nullptr)
  208. return;
  209. }
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }
  216. //==============================================================================
  217. void Button::enablementChanged()
  218. {
  219. updateState();
  220. repaint();
  221. }
  222. Button::ButtonState Button::updateState()
  223. {
  224. return updateState (isMouseOver (true), isMouseButtonDown());
  225. }
  226. Button::ButtonState Button::updateState (bool over, bool down)
  227. {
  228. ButtonState newState = buttonNormal;
  229. if (isEnabled() && isVisible() && ! isCurrentlyBlockedByAnotherModalComponent())
  230. {
  231. if ((down && (over || (triggerOnMouseDown && buttonState == buttonDown))) || isKeyDown)
  232. newState = buttonDown;
  233. else if (over)
  234. newState = buttonOver;
  235. }
  236. setState (newState);
  237. return newState;
  238. }
  239. void Button::setState (ButtonState newState)
  240. {
  241. if (buttonState != newState)
  242. {
  243. buttonState = newState;
  244. repaint();
  245. if (buttonState == buttonDown)
  246. {
  247. buttonPressTime = Time::getApproximateMillisecondCounter();
  248. lastRepeatTime = 0;
  249. }
  250. sendStateMessage();
  251. }
  252. }
  253. bool Button::isDown() const noexcept { return buttonState == buttonDown; }
  254. bool Button::isOver() const noexcept { return buttonState != buttonNormal; }
  255. void Button::buttonStateChanged() {}
  256. uint32 Button::getMillisecondsSinceButtonDown() const noexcept
  257. {
  258. auto now = Time::getApproximateMillisecondCounter();
  259. return now > buttonPressTime ? now - buttonPressTime : 0;
  260. }
  261. void Button::setTriggeredOnMouseDown (bool isTriggeredOnMouseDown) noexcept
  262. {
  263. triggerOnMouseDown = isTriggeredOnMouseDown;
  264. }
  265. bool Button::getTriggeredOnMouseDown() const noexcept
  266. {
  267. return triggerOnMouseDown;
  268. }
  269. //==============================================================================
  270. void Button::clicked()
  271. {
  272. }
  273. void Button::clicked (const ModifierKeys&)
  274. {
  275. clicked();
  276. }
  277. enum { clickMessageId = 0x2f3f4f99 };
  278. void Button::triggerClick()
  279. {
  280. postCommandMessage (clickMessageId);
  281. }
  282. void Button::internalClickCallback (const ModifierKeys& modifiers)
  283. {
  284. if (clickTogglesState)
  285. {
  286. const bool shouldBeOn = (radioGroupId != 0 || ! lastToggleState);
  287. if (shouldBeOn != getToggleState())
  288. {
  289. setToggleState (shouldBeOn, sendNotification);
  290. return;
  291. }
  292. }
  293. sendClickMessage (modifiers);
  294. }
  295. void Button::flashButtonState()
  296. {
  297. if (isEnabled())
  298. {
  299. needsToRelease = true;
  300. setState (buttonDown);
  301. callbackHelper->startTimer (100);
  302. }
  303. }
  304. void Button::handleCommandMessage (int commandId)
  305. {
  306. if (commandId == clickMessageId)
  307. {
  308. if (isEnabled())
  309. {
  310. flashButtonState();
  311. internalClickCallback (ModifierKeys::currentModifiers);
  312. }
  313. }
  314. else
  315. {
  316. Component::handleCommandMessage (commandId);
  317. }
  318. }
  319. //==============================================================================
  320. void Button::addListener (Listener* l) { buttonListeners.add (l); }
  321. void Button::removeListener (Listener* l) { buttonListeners.remove (l); }
  322. void Button::sendClickMessage (const ModifierKeys& modifiers)
  323. {
  324. Component::BailOutChecker checker (this);
  325. if (commandManagerToUse != nullptr && commandID != 0)
  326. {
  327. ApplicationCommandTarget::InvocationInfo info (commandID);
  328. info.invocationMethod = ApplicationCommandTarget::InvocationInfo::fromButton;
  329. info.originatingComponent = this;
  330. commandManagerToUse->invoke (info, true);
  331. }
  332. clicked (modifiers);
  333. if (checker.shouldBailOut())
  334. return;
  335. buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonClicked (this); });
  336. if (checker.shouldBailOut())
  337. return;
  338. if (onClick != nullptr)
  339. onClick();
  340. }
  341. void Button::sendStateMessage()
  342. {
  343. Component::BailOutChecker checker (this);
  344. buttonStateChanged();
  345. if (checker.shouldBailOut())
  346. return;
  347. buttonListeners.callChecked (checker, [this] (Listener& l) { l.buttonStateChanged (this); });
  348. if (checker.shouldBailOut())
  349. return;
  350. if (onStateChange != nullptr)
  351. onStateChange();
  352. }
  353. //==============================================================================
  354. void Button::paint (Graphics& g)
  355. {
  356. if (needsToRelease && isEnabled())
  357. {
  358. needsToRelease = false;
  359. needsRepainting = true;
  360. }
  361. paintButton (g, isOver(), isDown());
  362. lastStatePainted = buttonState;
  363. }
  364. //==============================================================================
  365. void Button::mouseEnter (const MouseEvent&) { updateState (true, false); }
  366. void Button::mouseExit (const MouseEvent&) { updateState (false, false); }
  367. void Button::mouseDown (const MouseEvent& e)
  368. {
  369. updateState (true, true);
  370. if (isDown())
  371. {
  372. if (autoRepeatDelay >= 0)
  373. callbackHelper->startTimer (autoRepeatDelay);
  374. if (triggerOnMouseDown)
  375. internalClickCallback (e.mods);
  376. }
  377. }
  378. void Button::mouseUp (const MouseEvent& e)
  379. {
  380. const auto wasDown = isDown();
  381. const auto wasOver = isOver();
  382. updateState (isMouseSourceOver (e), false);
  383. if (wasDown && wasOver && ! triggerOnMouseDown)
  384. {
  385. if (lastStatePainted != buttonDown)
  386. flashButtonState();
  387. WeakReference<Component> deletionWatcher (this);
  388. internalClickCallback (e.mods);
  389. if (deletionWatcher != nullptr)
  390. updateState (isMouseSourceOver (e), false);
  391. }
  392. }
  393. void Button::mouseDrag (const MouseEvent& e)
  394. {
  395. auto oldState = buttonState;
  396. updateState (isMouseSourceOver (e), true);
  397. if (autoRepeatDelay >= 0 && buttonState != oldState && isDown())
  398. callbackHelper->startTimer (autoRepeatSpeed);
  399. }
  400. bool Button::isMouseSourceOver (const MouseEvent& e)
  401. {
  402. if (e.source.isTouch() || e.source.isPen())
  403. return getLocalBounds().toFloat().contains (e.position);
  404. return isMouseOver();
  405. }
  406. void Button::focusGained (FocusChangeType)
  407. {
  408. updateState();
  409. repaint();
  410. }
  411. void Button::focusLost (FocusChangeType)
  412. {
  413. updateState();
  414. repaint();
  415. }
  416. void Button::visibilityChanged()
  417. {
  418. needsToRelease = false;
  419. updateState();
  420. }
  421. void Button::parentHierarchyChanged()
  422. {
  423. auto* newKeySource = shortcuts.isEmpty() ? nullptr : getTopLevelComponent();
  424. if (newKeySource != keySource.get())
  425. {
  426. if (keySource != nullptr)
  427. keySource->removeKeyListener (callbackHelper.get());
  428. keySource = newKeySource;
  429. if (keySource != nullptr)
  430. keySource->addKeyListener (callbackHelper.get());
  431. }
  432. }
  433. //==============================================================================
  434. void Button::setCommandToTrigger (ApplicationCommandManager* newCommandManager,
  435. CommandID newCommandID, bool generateTip)
  436. {
  437. commandID = newCommandID;
  438. generateTooltip = generateTip;
  439. if (commandManagerToUse != newCommandManager)
  440. {
  441. if (commandManagerToUse != nullptr)
  442. commandManagerToUse->removeListener (callbackHelper.get());
  443. commandManagerToUse = newCommandManager;
  444. if (commandManagerToUse != nullptr)
  445. commandManagerToUse->addListener (callbackHelper.get());
  446. // if you've got clickTogglesState turned on, you shouldn't also connect the button
  447. // up to be a command invoker. Instead, your command handler must flip the state of whatever
  448. // it is that this button represents, and the button will update its state to reflect this
  449. // in the applicationCommandListChanged() method.
  450. jassert (commandManagerToUse == nullptr || ! clickTogglesState);
  451. }
  452. if (commandManagerToUse != nullptr)
  453. applicationCommandListChangeCallback();
  454. else
  455. setEnabled (true);
  456. }
  457. void Button::applicationCommandListChangeCallback()
  458. {
  459. if (commandManagerToUse != nullptr)
  460. {
  461. ApplicationCommandInfo info (0);
  462. if (commandManagerToUse->getTargetForCommand (commandID, info) != nullptr)
  463. {
  464. updateAutomaticTooltip (info);
  465. setEnabled ((info.flags & ApplicationCommandInfo::isDisabled) == 0);
  466. setToggleState ((info.flags & ApplicationCommandInfo::isTicked) != 0, dontSendNotification);
  467. }
  468. else
  469. {
  470. setEnabled (false);
  471. }
  472. }
  473. }
  474. //==============================================================================
  475. void Button::addShortcut (const KeyPress& key)
  476. {
  477. if (key.isValid())
  478. {
  479. jassert (! isRegisteredForShortcut (key)); // already registered!
  480. shortcuts.add (key);
  481. parentHierarchyChanged();
  482. }
  483. }
  484. void Button::clearShortcuts()
  485. {
  486. shortcuts.clear();
  487. parentHierarchyChanged();
  488. }
  489. bool Button::isShortcutPressed() const
  490. {
  491. if (isShowing() && ! isCurrentlyBlockedByAnotherModalComponent())
  492. for (auto& s : shortcuts)
  493. if (s.isCurrentlyDown())
  494. return true;
  495. return false;
  496. }
  497. bool Button::isRegisteredForShortcut (const KeyPress& key) const
  498. {
  499. for (auto& s : shortcuts)
  500. if (key == s)
  501. return true;
  502. return false;
  503. }
  504. bool Button::keyStateChangedCallback()
  505. {
  506. if (! isEnabled())
  507. return false;
  508. const bool wasDown = isKeyDown;
  509. isKeyDown = isShortcutPressed();
  510. if (autoRepeatDelay >= 0 && (isKeyDown && ! wasDown))
  511. callbackHelper->startTimer (autoRepeatDelay);
  512. updateState();
  513. if (isEnabled() && wasDown && ! isKeyDown)
  514. {
  515. internalClickCallback (ModifierKeys::currentModifiers);
  516. // (return immediately - this button may now have been deleted)
  517. return true;
  518. }
  519. return wasDown || isKeyDown;
  520. }
  521. bool Button::keyPressed (const KeyPress& key)
  522. {
  523. if (isEnabled() && key.isKeyCode (KeyPress::returnKey))
  524. {
  525. triggerClick();
  526. return true;
  527. }
  528. return false;
  529. }
  530. //==============================================================================
  531. void Button::setRepeatSpeed (int initialDelayMillisecs,
  532. int repeatMillisecs,
  533. int minimumDelayInMillisecs) noexcept
  534. {
  535. autoRepeatDelay = initialDelayMillisecs;
  536. autoRepeatSpeed = repeatMillisecs;
  537. autoRepeatMinimumDelay = jmin (autoRepeatSpeed, minimumDelayInMillisecs);
  538. }
  539. void Button::repeatTimerCallback()
  540. {
  541. if (needsRepainting)
  542. {
  543. callbackHelper->stopTimer();
  544. updateState();
  545. needsRepainting = false;
  546. }
  547. else if (autoRepeatSpeed > 0 && (isKeyDown || (updateState() == buttonDown)))
  548. {
  549. auto repeatSpeed = autoRepeatSpeed;
  550. if (autoRepeatMinimumDelay >= 0)
  551. {
  552. auto timeHeldDown = jmin (1.0, getMillisecondsSinceButtonDown() / 4000.0);
  553. timeHeldDown *= timeHeldDown;
  554. repeatSpeed = repeatSpeed + (int) (timeHeldDown * (autoRepeatMinimumDelay - repeatSpeed));
  555. }
  556. repeatSpeed = jmax (1, repeatSpeed);
  557. auto now = Time::getMillisecondCounter();
  558. // if we've been blocked from repeating often enough, speed up the repeat timer to compensate..
  559. if (lastRepeatTime != 0 && (int) (now - lastRepeatTime) > repeatSpeed * 2)
  560. repeatSpeed = jmax (1, repeatSpeed / 2);
  561. lastRepeatTime = now;
  562. callbackHelper->startTimer (repeatSpeed);
  563. internalClickCallback (ModifierKeys::currentModifiers);
  564. }
  565. else if (! needsToRelease)
  566. {
  567. callbackHelper->stopTimer();
  568. }
  569. }
  570. //==============================================================================
  571. class ButtonAccessibilityHandler : public AccessibilityHandler
  572. {
  573. public:
  574. explicit ButtonAccessibilityHandler (Button& buttonToWrap, AccessibilityRole roleIn)
  575. : AccessibilityHandler (buttonToWrap,
  576. isRadioButton (buttonToWrap) ? AccessibilityRole::radioButton : roleIn,
  577. getAccessibilityActions (buttonToWrap),
  578. getAccessibilityInterfaces (buttonToWrap)),
  579. button (buttonToWrap)
  580. {
  581. }
  582. AccessibleState getCurrentState() const override
  583. {
  584. auto state = AccessibilityHandler::getCurrentState();
  585. if (button.isToggleable())
  586. {
  587. state = state.withCheckable();
  588. if (button.getToggleState())
  589. state = state.withChecked();
  590. }
  591. return state;
  592. }
  593. String getTitle() const override
  594. {
  595. auto title = AccessibilityHandler::getTitle();
  596. if (title.isEmpty())
  597. return button.getButtonText();
  598. return title;
  599. }
  600. String getHelp() const override { return button.getTooltip(); }
  601. private:
  602. class ButtonValueInterface : public AccessibilityTextValueInterface
  603. {
  604. public:
  605. explicit ButtonValueInterface (Button& buttonToWrap)
  606. : button (buttonToWrap)
  607. {
  608. }
  609. bool isReadOnly() const override { return true; }
  610. String getCurrentValueAsString() const override { return button.getToggleState() ? "On" : "Off"; }
  611. void setValueAsString (const String&) override {}
  612. private:
  613. Button& button;
  614. //==============================================================================
  615. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonValueInterface)
  616. };
  617. static bool isRadioButton (const Button& button) noexcept
  618. {
  619. return button.getRadioGroupId() != 0;
  620. }
  621. static AccessibilityActions getAccessibilityActions (Button& button)
  622. {
  623. auto actions = AccessibilityActions().addAction (AccessibilityActionType::press,
  624. [&button] { button.triggerClick(); });
  625. if (button.isToggleable())
  626. actions = actions.addAction (AccessibilityActionType::toggle,
  627. [&button] { button.setToggleState (! button.getToggleState(), sendNotification); });
  628. return actions;
  629. }
  630. static Interfaces getAccessibilityInterfaces (Button& button)
  631. {
  632. if (button.isToggleable())
  633. return { std::make_unique<ButtonValueInterface> (button) };
  634. return {};
  635. }
  636. Button& button;
  637. //==============================================================================
  638. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ButtonAccessibilityHandler)
  639. };
  640. std::unique_ptr<AccessibilityHandler> Button::createAccessibilityHandler()
  641. {
  642. return std::make_unique<ButtonAccessibilityHandler> (*this, AccessibilityRole::button);
  643. }
  644. } // namespace juce