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_SidePanel.cpp 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. SidePanel::SidePanel (StringRef title, int width, bool positionOnLeft,
  21. Component* contentToDisplay, bool deleteComponentWhenNoLongerNeeded)
  22. : titleLabel ("titleLabel", title),
  23. isOnLeft (positionOnLeft),
  24. panelWidth (width)
  25. {
  26. lookAndFeelChanged();
  27. addAndMakeVisible (titleLabel);
  28. dismissButton.onClick = [this] { showOrHide (false); };
  29. addAndMakeVisible (dismissButton);
  30. auto& desktop = Desktop::getInstance();
  31. desktop.addGlobalMouseListener (this);
  32. desktop.getAnimator().addChangeListener (this);
  33. if (contentToDisplay != nullptr)
  34. setContent (contentToDisplay, deleteComponentWhenNoLongerNeeded);
  35. setOpaque (false);
  36. setVisible (false);
  37. setAlwaysOnTop (true);
  38. }
  39. SidePanel::~SidePanel()
  40. {
  41. auto& desktop = Desktop::getInstance();
  42. desktop.removeGlobalMouseListener (this);
  43. desktop.getAnimator().removeChangeListener (this);
  44. if (parent != nullptr)
  45. parent->removeComponentListener (this);
  46. }
  47. void SidePanel::setContent (Component* newContent, bool deleteComponentWhenNoLongerNeeded)
  48. {
  49. if (contentComponent.get() != newContent)
  50. {
  51. if (deleteComponentWhenNoLongerNeeded)
  52. contentComponent.setOwned (newContent);
  53. else
  54. contentComponent.setNonOwned (newContent);
  55. addAndMakeVisible (contentComponent);
  56. resized();
  57. }
  58. }
  59. void SidePanel::setTitleBarComponent (Component* titleBarComponentToUse,
  60. bool keepDismissButton,
  61. bool deleteComponentWhenNoLongerNeeded)
  62. {
  63. if (titleBarComponent.get() != titleBarComponentToUse)
  64. {
  65. if (deleteComponentWhenNoLongerNeeded)
  66. titleBarComponent.setOwned (titleBarComponentToUse);
  67. else
  68. titleBarComponent.setNonOwned (titleBarComponentToUse);
  69. addAndMakeVisible (titleBarComponent);
  70. resized();
  71. }
  72. shouldShowDismissButton = keepDismissButton;
  73. }
  74. void SidePanel::showOrHide (bool show)
  75. {
  76. if (parent != nullptr)
  77. {
  78. isShowing = show;
  79. Desktop::getInstance().getAnimator().animateComponent (this, calculateBoundsInParent (*parent),
  80. 1.0f, 250, true, 1.0, 0.0);
  81. if (isShowing && ! isVisible())
  82. setVisible (true);
  83. }
  84. }
  85. void SidePanel::moved()
  86. {
  87. if (onPanelMove != nullptr)
  88. onPanelMove();
  89. }
  90. void SidePanel::resized()
  91. {
  92. auto bounds = getLocalBounds();
  93. calculateAndRemoveShadowBounds (bounds);
  94. auto titleBounds = bounds.removeFromTop (titleBarHeight);
  95. if (titleBarComponent != nullptr)
  96. {
  97. if (shouldShowDismissButton)
  98. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  99. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  100. titleBarComponent->setBounds (titleBounds);
  101. }
  102. else
  103. {
  104. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  105. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  106. titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40)
  107. : titleBounds.withTrimmedLeft (40));
  108. }
  109. if (contentComponent != nullptr)
  110. contentComponent->setBounds (bounds);
  111. }
  112. void SidePanel::paint (Graphics& g)
  113. {
  114. auto& lf = getLookAndFeel();
  115. auto bgColour = lf.findColour (SidePanel::backgroundColour);
  116. auto shadowColour = lf.findColour (SidePanel::shadowBaseColour);
  117. g.setGradientFill (ColourGradient (shadowColour.withAlpha (0.7f), (isOnLeft ? shadowArea.getTopLeft()
  118. : shadowArea.getTopRight()).toFloat(),
  119. shadowColour.withAlpha (0.0f), (isOnLeft ? shadowArea.getTopRight()
  120. : shadowArea.getTopLeft()).toFloat(), false));
  121. g.fillRect (shadowArea);
  122. g.excludeClipRegion (shadowArea);
  123. g.fillAll (bgColour);
  124. }
  125. void SidePanel::parentHierarchyChanged()
  126. {
  127. auto* newParent = getParentComponent();
  128. if ((newParent != nullptr) && (parent != newParent))
  129. {
  130. if (parent != nullptr)
  131. parent->removeComponentListener (this);
  132. parent = newParent;
  133. parent->addComponentListener (this);
  134. }
  135. }
  136. void SidePanel::mouseDrag (const MouseEvent& e)
  137. {
  138. if (shouldResize)
  139. {
  140. Point<int> convertedPoint;
  141. if (getParentComponent() == nullptr)
  142. convertedPoint = e.eventComponent->localPointToGlobal (e.getPosition());
  143. else
  144. convertedPoint = getParentComponent()->getLocalPoint (e.eventComponent, e.getPosition());
  145. auto currentMouseDragX = convertedPoint.x;
  146. if (isOnLeft)
  147. {
  148. amountMoved = startingBounds.getRight() - currentMouseDragX;
  149. setBounds (getBounds().withX (startingBounds.getX() - jmax (amountMoved, 0)));
  150. }
  151. else
  152. {
  153. amountMoved = currentMouseDragX - startingBounds.getX();
  154. setBounds (getBounds().withX (startingBounds.getX() + jmax (amountMoved, 0)));
  155. }
  156. }
  157. else if (isShowing)
  158. {
  159. auto relativeMouseDownPosition = getLocalPoint (e.eventComponent, e.getMouseDownPosition());
  160. auto relativeMouseDragPosition = getLocalPoint (e.eventComponent, e.getPosition());
  161. if (! getLocalBounds().contains (relativeMouseDownPosition)
  162. && getLocalBounds().contains (relativeMouseDragPosition))
  163. {
  164. shouldResize = true;
  165. startingBounds = getBounds();
  166. }
  167. }
  168. }
  169. void SidePanel::mouseUp (const MouseEvent&)
  170. {
  171. if (shouldResize)
  172. {
  173. showOrHide (amountMoved < (panelWidth / 2));
  174. amountMoved = 0;
  175. shouldResize = false;
  176. }
  177. }
  178. //==============================================================================
  179. void SidePanel::lookAndFeelChanged()
  180. {
  181. auto& lf = getLookAndFeel();
  182. dismissButton.setShape (lf.getSidePanelDismissButtonShape (*this), false, true, false);
  183. dismissButton.setColours (lf.findColour (SidePanel::dismissButtonNormalColour),
  184. lf.findColour (SidePanel::dismissButtonOverColour),
  185. lf.findColour (SidePanel::dismissButtonDownColour));
  186. titleLabel.setFont (lf.getSidePanelTitleFont (*this));
  187. titleLabel.setColour (Label::textColourId, findColour (SidePanel::titleTextColour));
  188. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (*this));
  189. }
  190. void SidePanel::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  191. {
  192. ignoreUnused (wasMoved);
  193. if (wasResized && (&component == parent))
  194. setBounds (calculateBoundsInParent (component));
  195. }
  196. void SidePanel::changeListenerCallback (ChangeBroadcaster*)
  197. {
  198. if (! Desktop::getInstance().getAnimator().isAnimating (this))
  199. {
  200. if (onPanelShowHide != nullptr)
  201. onPanelShowHide (isShowing);
  202. if (isVisible() && ! isShowing)
  203. setVisible (false);
  204. }
  205. }
  206. Rectangle<int> SidePanel::calculateBoundsInParent (Component& parentComp) const
  207. {
  208. auto parentBounds = parentComp.getLocalBounds();
  209. if (isOnLeft)
  210. {
  211. return isShowing ? parentBounds.removeFromLeft (panelWidth)
  212. : parentBounds.withX (parentBounds.getX() - panelWidth).withWidth (panelWidth);
  213. }
  214. return isShowing ? parentBounds.removeFromRight (panelWidth)
  215. : parentBounds.withX (parentBounds.getRight()).withWidth (panelWidth);
  216. }
  217. void SidePanel::calculateAndRemoveShadowBounds (Rectangle<int>& bounds)
  218. {
  219. shadowArea = isOnLeft ? bounds.removeFromRight (shadowWidth)
  220. : bounds.removeFromLeft (shadowWidth);
  221. }
  222. bool SidePanel::isMouseEventInThisOrChildren (Component* eventComponent)
  223. {
  224. if (eventComponent == this)
  225. return true;
  226. for (auto& child : getChildren())
  227. if (eventComponent == child)
  228. return true;
  229. return false;
  230. }
  231. //==============================================================================
  232. std::unique_ptr<AccessibilityHandler> SidePanel::createAccessibilityHandler()
  233. {
  234. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
  235. }
  236. } // namespace juce