The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

118 lines
3.9KB

  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::detail
  19. {
  20. struct FocusHelpers
  21. {
  22. FocusHelpers() = delete;
  23. static int getOrder (const Component* c)
  24. {
  25. auto order = c->getExplicitFocusOrder();
  26. return order > 0 ? order : std::numeric_limits<int>::max();
  27. }
  28. template <typename FocusContainerFn>
  29. static void findAllComponents (Component* parent,
  30. std::vector<Component*>& components,
  31. FocusContainerFn isFocusContainer)
  32. {
  33. if (parent == nullptr || parent->getNumChildComponents() == 0)
  34. return;
  35. std::vector<Component*> localComponents;
  36. for (auto* c : parent->getChildren())
  37. if (c->isVisible() && c->isEnabled())
  38. localComponents.push_back (c);
  39. const auto compareComponents = [&] (const Component* a, const Component* b)
  40. {
  41. const auto getComponentOrderAttributes = [] (const Component* c)
  42. {
  43. return std::make_tuple (getOrder (c),
  44. c->isAlwaysOnTop() ? 0 : 1,
  45. c->getY(),
  46. c->getX());
  47. };
  48. return getComponentOrderAttributes (a) < getComponentOrderAttributes (b);
  49. };
  50. // This will sort so that they are ordered in terms of explicit focus,
  51. // always on top, left-to-right, and then top-to-bottom.
  52. std::stable_sort (localComponents.begin(), localComponents.end(), compareComponents);
  53. for (auto* c : localComponents)
  54. {
  55. components.push_back (c);
  56. if (! (c->*isFocusContainer)())
  57. findAllComponents (c, components, isFocusContainer);
  58. }
  59. }
  60. enum class NavigationDirection { forwards, backwards };
  61. template <typename FocusContainerFn>
  62. static Component* navigateFocus (Component* current,
  63. Component* focusContainer,
  64. NavigationDirection direction,
  65. FocusContainerFn isFocusContainer)
  66. {
  67. if (focusContainer != nullptr)
  68. {
  69. std::vector<Component*> components;
  70. findAllComponents (focusContainer, components, isFocusContainer);
  71. const auto iter = std::find (components.cbegin(), components.cend(), current);
  72. if (iter == components.cend())
  73. return nullptr;
  74. switch (direction)
  75. {
  76. case NavigationDirection::forwards:
  77. if (iter != std::prev (components.cend()))
  78. return *std::next (iter);
  79. break;
  80. case NavigationDirection::backwards:
  81. if (iter != components.cbegin())
  82. return *std::prev (iter);
  83. break;
  84. }
  85. }
  86. return nullptr;
  87. }
  88. };
  89. } // namespace juce::detail