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.

238 lines
7.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace juce
  20. {
  21. SidePanel::SidePanel (StringRef title, int width, bool positionOnLeft,
  22. Component* contentToDisplay, bool deleteComponentWhenNoLongerNeeded)
  23. : titleLabel ("titleLabel", title),
  24. isOnLeft (positionOnLeft),
  25. panelWidth (width)
  26. {
  27. lookAndFeelChanged();
  28. addAndMakeVisible (titleLabel);
  29. dismissButton.onClick = [this] { showOrHide (false); };
  30. addAndMakeVisible (dismissButton);
  31. Desktop::getInstance().addGlobalMouseListener (this);
  32. if (contentToDisplay != nullptr)
  33. setContent (contentToDisplay, deleteComponentWhenNoLongerNeeded);
  34. setOpaque (false);
  35. }
  36. SidePanel::~SidePanel()
  37. {
  38. if (parent != nullptr)
  39. parent->removeComponentListener (this);
  40. }
  41. void SidePanel::setContent (Component* newContent, bool deleteComponentWhenNoLongerNeeded)
  42. {
  43. if (contentComponent.get() != newContent)
  44. {
  45. if (deleteComponentWhenNoLongerNeeded)
  46. contentComponent.setOwned (newContent);
  47. else
  48. contentComponent.setNonOwned (newContent);
  49. addAndMakeVisible (contentComponent);
  50. resized();
  51. }
  52. }
  53. void SidePanel::showOrHide (bool show)
  54. {
  55. if (parent != nullptr)
  56. {
  57. isShowing = show;
  58. Desktop::getInstance().getAnimator().animateComponent (this, calculateBoundsInParent (*parent),
  59. 1.0f, 250, true, 1.0, 0.0);
  60. }
  61. }
  62. void SidePanel::resized()
  63. {
  64. auto bounds = getLocalBounds();
  65. calculateAndRemoveShadowBounds (bounds);
  66. auto titleBounds = bounds.removeFromTop (titleBarHeight);
  67. dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
  68. : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
  69. titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40)
  70. : titleBounds.withTrimmedLeft (40));
  71. if (contentComponent != nullptr)
  72. contentComponent->setBounds (bounds);
  73. }
  74. void SidePanel::paint (Graphics& g)
  75. {
  76. auto& lf = getLookAndFeel();
  77. auto bgColour = lf.findColour (SidePanel::backgroundColour);
  78. auto shadowColour = lf.findColour (SidePanel::shadowBaseColour);
  79. g.setGradientFill (ColourGradient (shadowColour.withAlpha (0.7f), (isOnLeft ? shadowArea.getTopLeft()
  80. : shadowArea.getTopRight()).toFloat(),
  81. shadowColour.withAlpha (0.0f), (isOnLeft ? shadowArea.getTopRight()
  82. : shadowArea.getTopLeft()).toFloat(), false));
  83. g.fillRect (shadowArea);
  84. g.excludeClipRegion (shadowArea);
  85. g.fillAll (bgColour);
  86. }
  87. void SidePanel::parentHierarchyChanged()
  88. {
  89. auto* newParent = getParentComponent();
  90. if ((newParent != nullptr) && (parent != newParent))
  91. {
  92. if (parent != nullptr)
  93. parent->removeComponentListener (this);
  94. parent = newParent;
  95. parent->addComponentListener (this);
  96. }
  97. }
  98. void SidePanel::mouseDrag (const MouseEvent& e)
  99. {
  100. if (shouldResize)
  101. {
  102. Point<int> convertedPoint;
  103. if (getParentComponent() == nullptr)
  104. convertedPoint = e.eventComponent->localPointToGlobal (e.getPosition());
  105. else
  106. convertedPoint = getParentComponent()->getLocalPoint (e.eventComponent, e.getPosition());
  107. auto currentMouseDragX = convertedPoint.x;
  108. if (isOnLeft)
  109. {
  110. amountMoved = startingBounds.getRight() - currentMouseDragX;
  111. setBounds (getBounds().withX (startingBounds.getX() - jmax (amountMoved, 0)));
  112. }
  113. else
  114. {
  115. amountMoved = currentMouseDragX - startingBounds.getX();
  116. setBounds (getBounds().withX (startingBounds.getX() + jmax (amountMoved, 0)));
  117. }
  118. }
  119. else if (isShowing)
  120. {
  121. auto relativeMouseDownPosition = getLocalPoint (e.eventComponent, e.getMouseDownPosition());
  122. auto relativeMouseDragPosition = getLocalPoint (e.eventComponent, e.getPosition());
  123. if (! getLocalBounds().contains (relativeMouseDownPosition)
  124. && getLocalBounds().contains (relativeMouseDragPosition))
  125. {
  126. shouldResize = true;
  127. startingBounds = getBounds();
  128. }
  129. }
  130. }
  131. void SidePanel::mouseUp (const MouseEvent&)
  132. {
  133. if (shouldResize)
  134. {
  135. showOrHide (amountMoved < (panelWidth / 2));
  136. amountMoved = 0;
  137. shouldResize = false;
  138. }
  139. }
  140. //==========================================================================
  141. void SidePanel::lookAndFeelChanged()
  142. {
  143. auto& lf = getLookAndFeel();
  144. dismissButton.setShape (lf.getSidePanelDismissButtonShape (*this), false, true, false);
  145. dismissButton.setColours (lf.findColour (SidePanel::dismissButtonNormalColour),
  146. lf.findColour (SidePanel::dismissButtonOverColour),
  147. lf.findColour (SidePanel::dismissButtonDownColour));
  148. titleLabel.setFont (lf.getSidePanelTitleFont (*this));
  149. titleLabel.setColour (Label::textColourId, findColour (SidePanel::titleTextColour));
  150. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (*this));
  151. }
  152. void SidePanel::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
  153. {
  154. ignoreUnused (wasMoved);
  155. if (wasResized && (&component == parent))
  156. setBounds (calculateBoundsInParent (component));
  157. }
  158. Rectangle<int> SidePanel::calculateBoundsInParent (Component& parentComp) const
  159. {
  160. auto parentBounds = parentComp.getBounds();
  161. if (isOnLeft)
  162. {
  163. return isShowing ? parentBounds.removeFromLeft (panelWidth)
  164. : parentBounds.withX (parentBounds.getX() - panelWidth).withWidth (panelWidth);
  165. }
  166. return isShowing ? parentBounds.removeFromRight (panelWidth)
  167. : parentBounds.withX (parentBounds.getRight()).withWidth (panelWidth);
  168. }
  169. void SidePanel::calculateAndRemoveShadowBounds (Rectangle<int>& bounds)
  170. {
  171. shadowArea = isOnLeft ? bounds.removeFromRight (shadowWidth)
  172. : bounds.removeFromLeft (shadowWidth);
  173. }
  174. bool SidePanel::isMouseEventInThisOrChildren (Component* eventComponent)
  175. {
  176. if (eventComponent == this)
  177. return true;
  178. for (auto& child : getChildren())
  179. if (eventComponent == child)
  180. return true;
  181. return false;
  182. }
  183. } // namespace juce