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.h 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. //==============================================================================
  21. /**
  22. A component that is positioned on either the left- or right-hand side of its parent,
  23. containing a header and some content. This sort of component is typically used for
  24. navigation and forms in mobile applications.
  25. When triggered with the showOrHide() method, the SidePanel will animate itself to its
  26. new position. This component also contains some logic to reactively resize and dismiss
  27. itself when the user drags it.
  28. @tags{GUI}
  29. */
  30. class SidePanel : public Component,
  31. private ComponentListener
  32. {
  33. public:
  34. //==============================================================================
  35. /** Creates a SidePanel component.
  36. @param title the text to use for the SidePanel's title bar
  37. @param width the width of the SidePanel
  38. @param positionOnLeft if true, the SidePanel will be positioned on the left of its parent component and
  39. if false, the SidePanel will be positioned on the right of its parent component
  40. @param contentComponent the component to add to this SidePanel - this content will take up the full
  41. size of the SidePanel, minus the height of the title bar. You can pass nullptr
  42. to this if you like and set the content component later using the setContent() method
  43. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when
  44. the SidePanel is deleted or when a different component is added. If false,
  45. the caller must manage the lifetime of the component
  46. */
  47. SidePanel (StringRef title, int width, bool positionOnLeft,
  48. Component* contentComponent = nullptr,
  49. bool deleteComponentWhenNoLongerNeeded = true);
  50. /** Destructor */
  51. ~SidePanel() override;
  52. //==============================================================================
  53. /** Sets the component that this SidePanel will contain.
  54. This will add the given component to this SidePanel and position it below the title bar.
  55. (Don't add or remove any child components directly using the normal
  56. Component::addChildComponent() methods).
  57. @param newContentComponent the component to add to this SidePanel, or nullptr to remove
  58. the current component.
  59. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when
  60. the SidePanel is deleted or when a different component is added. If false,
  61. the caller must manage the lifetime of the component
  62. @see getContent
  63. */
  64. void setContent (Component* newContentComponent,
  65. bool deleteComponentWhenNoLongerNeeded = true);
  66. /** Returns the component that's currently being used inside the SidePanel.
  67. @see setViewedComponent
  68. */
  69. Component* getContent() const noexcept { return contentComponent.get(); }
  70. /** Sets a custom component to be used for the title bar of this SidePanel, replacing
  71. the default. You can pass a nullptr to revert to the default title bar.
  72. @param titleBarComponentToUse the component to use as the title bar, or nullptr to use
  73. the default
  74. @param keepDismissButton if false the specified component will take up the full width of
  75. the title bar including the dismiss button but if true, the default
  76. dismiss button will be kept
  77. @param deleteComponentWhenNoLongerNeeded if true, the component will be deleted automatically when
  78. the SidePanel is deleted or when a different component is added. If false,
  79. the caller must manage the lifetime of the component
  80. @see getTitleBarComponent
  81. */
  82. void setTitleBarComponent (Component* titleBarComponentToUse,
  83. bool keepDismissButton,
  84. bool deleteComponentWhenNoLongerNeeded = true);
  85. /** Returns the component that is currently being used as the title bar of the SidePanel.
  86. @see setTitleBarComponent
  87. */
  88. Component* getTitleBarComponent() const noexcept { return titleBarComponent.get(); }
  89. /** Shows or hides the SidePanel.
  90. This will animate the SidePanel to either its full width or to be hidden on the
  91. left- or right-hand side of its parent component depending on the value of positionOnLeft
  92. that was passed to the constructor.
  93. @param show if true, this will show the SidePanel and if false the SidePanel will be hidden
  94. */
  95. void showOrHide (bool show);
  96. //==============================================================================
  97. /** Returns true if the SidePanel is currently showing. */
  98. bool isPanelShowing() const noexcept { return isShowing; }
  99. /** Returns true if the SidePanel is positioned on the left of its parent. */
  100. bool isPanelOnLeft() const noexcept { return isOnLeft; }
  101. /** Sets the width of the shadow that will be drawn on the side of the panel. */
  102. void setShadowWidth (int newWidth) noexcept { shadowWidth = newWidth; }
  103. /** Returns the width of the shadow that will be drawn on the side of the panel. */
  104. int getShadowWidth() const noexcept { return shadowWidth; }
  105. /** Sets the height of the title bar at the top of the SidePanel. */
  106. void setTitleBarHeight (int newHeight) noexcept { titleBarHeight = newHeight; }
  107. /** Returns the height of the title bar at the top of the SidePanel. */
  108. int getTitleBarHeight() const noexcept { return titleBarHeight; }
  109. /** Returns the text that is displayed in the title bar at the top of the SidePanel. */
  110. String getTitleText() const noexcept { return titleLabel.getText(); }
  111. //==============================================================================
  112. void moved() override;
  113. void resized() override;
  114. void paint (Graphics& g) override;
  115. void parentHierarchyChanged() override;
  116. void mouseDrag (const MouseEvent&) override;
  117. void mouseUp (const MouseEvent&) override;
  118. //==============================================================================
  119. /** This abstract base class is implemented by LookAndFeel classes to provide
  120. SidePanel drawing functionality.
  121. */
  122. struct JUCE_API LookAndFeelMethods
  123. {
  124. virtual ~LookAndFeelMethods() = default;
  125. virtual Font getSidePanelTitleFont (SidePanel&) = 0;
  126. virtual Justification getSidePanelTitleJustification (SidePanel&) = 0;
  127. virtual Path getSidePanelDismissButtonShape (SidePanel&) = 0;
  128. };
  129. /** A set of colour IDs to use to change the colour of various aspects of the SidePanel.
  130. These constants can be used either via the Component::setColour(), or LookAndFeel::setColour()
  131. methods.
  132. @see Component::setColour, Component::findColour, LookAndFeel::setColour, LookAndFeel::findColour
  133. */
  134. enum ColourIds
  135. {
  136. backgroundColour = 0x100f001,
  137. titleTextColour = 0x100f002,
  138. shadowBaseColour = 0x100f003,
  139. dismissButtonNormalColour = 0x100f004,
  140. dismissButtonOverColour = 0x100f005,
  141. dismissButtonDownColour = 0x100f006
  142. };
  143. //==============================================================================
  144. /** You can assign a lambda to this callback object and it will be called when the panel is moved. */
  145. std::function<void()> onPanelMove;
  146. /** You can assign a lambda to this callback object and it will be called when the panel is shown or hidden. */
  147. std::function<void (bool)> onPanelShowHide;
  148. private:
  149. //==============================================================================
  150. Component* parent = nullptr;
  151. OptionalScopedPointer<Component> contentComponent;
  152. OptionalScopedPointer<Component> titleBarComponent;
  153. Label titleLabel;
  154. ShapeButton dismissButton { "dismissButton", Colours::lightgrey, Colours::lightgrey, Colours::white };
  155. Rectangle<int> shadowArea;
  156. bool isOnLeft = false;
  157. bool isShowing = false;
  158. int panelWidth = 0;
  159. int shadowWidth = 15;
  160. int titleBarHeight = 40;
  161. Rectangle<int> startingBounds;
  162. bool shouldResize = false;
  163. int amountMoved = 0;
  164. bool shouldShowDismissButton = true;
  165. //==============================================================================
  166. void lookAndFeelChanged() override;
  167. void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;
  168. Rectangle<int> calculateBoundsInParent (Component&) const;
  169. void calculateAndRemoveShadowBounds (Rectangle<int>& bounds);
  170. bool isMouseEventInThisOrChildren (Component*);
  171. //==============================================================================
  172. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SidePanel)
  173. };
  174. } // namespace juce