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.

179 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. //==============================================================================
  16. struct OutlineWindowComponent : public Component
  17. {
  18. OutlineWindowComponent (Component* c, FocusOutline::OutlineWindowProperties& p)
  19. : target (c), props (p)
  20. {
  21. setVisible (true);
  22. setInterceptsMouseClicks (false, false);
  23. if (target->isOnDesktop())
  24. {
  25. setSize (1, 1);
  26. addToDesktop (ComponentPeer::windowIgnoresMouseClicks
  27. | ComponentPeer::windowIsTemporary
  28. | ComponentPeer::windowIgnoresKeyPresses);
  29. }
  30. else if (auto* parent = target->getParentComponent())
  31. {
  32. auto targetIndex = parent->getIndexOfChildComponent (target);
  33. parent->addChildComponent (this, targetIndex + 1);
  34. }
  35. }
  36. void paint (Graphics& g) override
  37. {
  38. if (target != nullptr)
  39. props.drawOutline (g, getWidth(), getHeight());
  40. }
  41. void resized() override
  42. {
  43. repaint();
  44. }
  45. float getDesktopScaleFactor() const override
  46. {
  47. return target != nullptr ? target->getDesktopScaleFactor()
  48. : Component::getDesktopScaleFactor();
  49. }
  50. private:
  51. WeakReference<Component> target;
  52. FocusOutline::OutlineWindowProperties& props;
  53. JUCE_DECLARE_NON_COPYABLE (OutlineWindowComponent)
  54. };
  55. //==============================================================================
  56. FocusOutline::FocusOutline (std::unique_ptr<OutlineWindowProperties> props)
  57. : properties (std::move (props))
  58. {
  59. }
  60. FocusOutline::~FocusOutline()
  61. {
  62. if (owner != nullptr)
  63. owner->removeComponentListener (this);
  64. if (lastParentComp != nullptr)
  65. lastParentComp->removeComponentListener (this);
  66. }
  67. void FocusOutline::setOwner (Component* componentToFollow)
  68. {
  69. if (componentToFollow != owner)
  70. {
  71. if (owner != nullptr)
  72. owner->removeComponentListener (this);
  73. owner = componentToFollow;
  74. if (owner != nullptr)
  75. owner->addComponentListener (this);
  76. updateParent();
  77. updateOutlineWindow();
  78. }
  79. }
  80. void FocusOutline::componentMovedOrResized (Component& c, bool, bool)
  81. {
  82. if (owner == &c)
  83. updateOutlineWindow();
  84. }
  85. void FocusOutline::componentBroughtToFront (Component& c)
  86. {
  87. if (owner == &c)
  88. updateOutlineWindow();
  89. }
  90. void FocusOutline::componentParentHierarchyChanged (Component& c)
  91. {
  92. if (owner == &c)
  93. {
  94. updateParent();
  95. updateOutlineWindow();
  96. }
  97. }
  98. void FocusOutline::componentVisibilityChanged (Component& c)
  99. {
  100. if (owner == &c)
  101. updateOutlineWindow();
  102. }
  103. void FocusOutline::updateParent()
  104. {
  105. lastParentComp = (owner != nullptr ? owner->getParentComponent()
  106. : nullptr);
  107. }
  108. void FocusOutline::updateOutlineWindow()
  109. {
  110. if (reentrant)
  111. return;
  112. const ScopedValueSetter<bool> setter (reentrant, true);
  113. if (owner == nullptr)
  114. {
  115. outlineWindow = nullptr;
  116. return;
  117. }
  118. if (owner->isShowing()
  119. && owner->getWidth() > 0 && owner->getHeight() > 0)
  120. {
  121. if (outlineWindow == nullptr)
  122. outlineWindow = std::make_unique<OutlineWindowComponent> (owner, *properties);
  123. WeakReference<Component> deletionChecker (outlineWindow.get());
  124. outlineWindow->setAlwaysOnTop (owner->isAlwaysOnTop());
  125. if (deletionChecker == nullptr)
  126. return;
  127. const auto windowBounds = [this]
  128. {
  129. const auto bounds = properties->getOutlineBounds (*owner);
  130. if (lastParentComp != nullptr)
  131. return lastParentComp->getLocalArea (nullptr, bounds);
  132. return bounds;
  133. }();
  134. outlineWindow->setBounds (windowBounds);
  135. }
  136. else
  137. {
  138. outlineWindow = nullptr;
  139. }
  140. }
  141. } // namespace juce