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.

271 lines
8.4KB

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