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.

232 lines
10KB

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