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.

295 lines
9.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. if (onPanelShowHide != nullptr)
  84. onPanelShowHide (isShowing);
  85. }
  86. }
  87. void SidePanel::moved()
  88. {
  89. if (onPanelMove != nullptr)
  90. onPanelMove();
  91. }
  92. void SidePanel::resized()
  93. {
  94. auto bounds = getLocalBounds();
  95. calculateAndRemoveShadowBounds (bounds);
  96. auto titleBounds = bounds.removeFromTop (titleBarHeight);
  97. if (titleBarComponent != nullptr)
  98. {
  99. if (shouldShowDismissButton)
  100. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  101. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  102. titleBarComponent->setBounds (titleBounds);
  103. }
  104. else
  105. {
  106. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  107. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  108. titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40)
  109. : titleBounds.withTrimmedLeft (40));
  110. }
  111. if (contentComponent != nullptr)
  112. contentComponent->setBounds (bounds);
  113. }
  114. void SidePanel::paint (Graphics& g)
  115. {
  116. auto& lf = getLookAndFeel();
  117. auto bgColour = lf.findColour (SidePanel::backgroundColour);
  118. auto shadowColour = lf.findColour (SidePanel::shadowBaseColour);
  119. g.setGradientFill (ColourGradient (shadowColour.withAlpha (0.7f), (isOnLeft ? shadowArea.getTopLeft()
  120. : shadowArea.getTopRight()).toFloat(),
  121. shadowColour.withAlpha (0.0f), (isOnLeft ? shadowArea.getTopRight()
  122. : shadowArea.getTopLeft()).toFloat(), false));
  123. g.fillRect (shadowArea);
  124. g.excludeClipRegion (shadowArea);
  125. g.fillAll (bgColour);
  126. }
  127. void SidePanel::parentHierarchyChanged()
  128. {
  129. auto* newParent = getParentComponent();
  130. if ((newParent != nullptr) && (parent != newParent))
  131. {
  132. if (parent != nullptr)
  133. parent->removeComponentListener (this);
  134. parent = newParent;
  135. parent->addComponentListener (this);
  136. }
  137. }
  138. void SidePanel::mouseDrag (const MouseEvent& e)
  139. {
  140. if (shouldResize)
  141. {
  142. Point<int> convertedPoint;
  143. if (getParentComponent() == nullptr)
  144. convertedPoint = e.eventComponent->localPointToGlobal (e.getPosition());
  145. else
  146. convertedPoint = getParentComponent()->getLocalPoint (e.eventComponent, e.getPosition());
  147. auto currentMouseDragX = convertedPoint.x;
  148. if (isOnLeft)
  149. {
  150. amountMoved = startingBounds.getRight() - currentMouseDragX;
  151. setBounds (getBounds().withX (startingBounds.getX() - jmax (amountMoved, 0)));
  152. }
  153. else
  154. {
  155. amountMoved = currentMouseDragX - startingBounds.getX();
  156. setBounds (getBounds().withX (startingBounds.getX() + jmax (amountMoved, 0)));
  157. }
  158. }
  159. else if (isShowing)
  160. {
  161. auto relativeMouseDownPosition = getLocalPoint (e.eventComponent, e.getMouseDownPosition());
  162. auto relativeMouseDragPosition = getLocalPoint (e.eventComponent, e.getPosition());
  163. if (! getLocalBounds().contains (relativeMouseDownPosition)
  164. && getLocalBounds().contains (relativeMouseDragPosition))
  165. {
  166. shouldResize = true;
  167. startingBounds = getBounds();
  168. }
  169. }
  170. }
  171. void SidePanel::mouseUp (const MouseEvent&)
  172. {
  173. if (shouldResize)
  174. {
  175. showOrHide (amountMoved < (panelWidth / 2));
  176. amountMoved = 0;
  177. shouldResize = false;
  178. }
  179. }
  180. //==============================================================================
  181. void SidePanel::lookAndFeelChanged()
  182. {
  183. auto& lf = getLookAndFeel();
  184. dismissButton.setShape (lf.getSidePanelDismissButtonShape (*this), false, true, false);
  185. dismissButton.setColours (lf.findColour (SidePanel::dismissButtonNormalColour),
  186. lf.findColour (SidePanel::dismissButtonOverColour),
  187. lf.findColour (SidePanel::dismissButtonDownColour));
  188. titleLabel.setFont (lf.getSidePanelTitleFont (*this));
  189. titleLabel.setColour (Label::textColourId, findColour (SidePanel::titleTextColour));
  190. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (*this));
  191. }
  192. void SidePanel::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  193. {
  194. ignoreUnused (wasMoved);
  195. if (wasResized && (&component == parent))
  196. setBounds (calculateBoundsInParent (component));
  197. }
  198. void SidePanel::changeListenerCallback (ChangeBroadcaster*)
  199. {
  200. if (isVisible() && ! isShowing && ! Desktop::getInstance().getAnimator().isAnimating (this))
  201. setVisible (false);
  202. }
  203. Rectangle<int> SidePanel::calculateBoundsInParent (Component& parentComp) const
  204. {
  205. auto parentBounds = parentComp.getLocalBounds();
  206. if (isOnLeft)
  207. {
  208. return isShowing ? parentBounds.removeFromLeft (panelWidth)
  209. : parentBounds.withX (parentBounds.getX() - panelWidth).withWidth (panelWidth);
  210. }
  211. return isShowing ? parentBounds.removeFromRight (panelWidth)
  212. : parentBounds.withX (parentBounds.getRight()).withWidth (panelWidth);
  213. }
  214. void SidePanel::calculateAndRemoveShadowBounds (Rectangle<int>& bounds)
  215. {
  216. shadowArea = isOnLeft ? bounds.removeFromRight (shadowWidth)
  217. : bounds.removeFromLeft (shadowWidth);
  218. }
  219. bool SidePanel::isMouseEventInThisOrChildren (Component* eventComponent)
  220. {
  221. if (eventComponent == this)
  222. return true;
  223. for (auto& child : getChildren())
  224. if (eventComponent == child)
  225. return true;
  226. return false;
  227. }
  228. } // namespace juce