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.

124 lines
3.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. namespace KeyboardFocusHelpers
  16. {
  17. static int getOrder (const Component* c)
  18. {
  19. auto order = c->getExplicitFocusOrder();
  20. return order > 0 ? order : (std::numeric_limits<int>::max() / 2);
  21. }
  22. static void findAllFocusableComponents (Component* parent, Array<Component*>& comps)
  23. {
  24. if (parent->getNumChildComponents() != 0)
  25. {
  26. Array<Component*> localComps;
  27. for (auto* c : parent->getChildren())
  28. if (c->isVisible() && c->isEnabled())
  29. localComps.add (c);
  30. // This will sort so that they are ordered in terms of left-to-right
  31. // and then top-to-bottom.
  32. std::stable_sort (localComps.begin(), localComps.end(),
  33. [] (const Component* a, const Component* b)
  34. {
  35. auto explicitOrder1 = getOrder (a);
  36. auto explicitOrder2 = getOrder (b);
  37. if (explicitOrder1 != explicitOrder2)
  38. return explicitOrder1 < explicitOrder2;
  39. if (a->getY() != b->getY())
  40. return a->getY() < b->getY();
  41. return a->getX() < b->getX();
  42. });
  43. for (auto* c : localComps)
  44. {
  45. if (c->getWantsKeyboardFocus())
  46. comps.add (c);
  47. if (! c->isFocusContainer())
  48. findAllFocusableComponents (c, comps);
  49. }
  50. }
  51. }
  52. static Component* findFocusContainer (Component* c)
  53. {
  54. c = c->getParentComponent();
  55. if (c != nullptr)
  56. while (c->getParentComponent() != nullptr && ! c->isFocusContainer())
  57. c = c->getParentComponent();
  58. return c;
  59. }
  60. static Component* getIncrementedComponent (Component* current, int delta)
  61. {
  62. if (auto* focusContainer = findFocusContainer (current))
  63. {
  64. Array<Component*> comps;
  65. KeyboardFocusHelpers::findAllFocusableComponents (focusContainer, comps);
  66. if (! comps.isEmpty())
  67. {
  68. auto index = comps.indexOf (current);
  69. return comps [(index + comps.size() + delta) % comps.size()];
  70. }
  71. }
  72. return nullptr;
  73. }
  74. }
  75. //==============================================================================
  76. KeyboardFocusTraverser::KeyboardFocusTraverser() {}
  77. KeyboardFocusTraverser::~KeyboardFocusTraverser() {}
  78. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  79. {
  80. jassert (current != nullptr);
  81. return KeyboardFocusHelpers::getIncrementedComponent (current, 1);
  82. }
  83. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  84. {
  85. jassert (current != nullptr);
  86. return KeyboardFocusHelpers::getIncrementedComponent (current, -1);
  87. }
  88. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  89. {
  90. Array<Component*> comps;
  91. if (parentComponent != nullptr)
  92. KeyboardFocusHelpers::findAllFocusableComponents (parentComponent, comps);
  93. return comps.getFirst();
  94. }
  95. } // namespace juce