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.

297 lines
9.2KB

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