LMMS
Loading...
Searching...
No Matches
juce_SidePanel.cpp
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
6
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
9
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
12
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
15
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
18
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
21 DISCLAIMED.
22
23 ==============================================================================
24*/
25
26namespace juce
27{
28
29SidePanel::SidePanel (StringRef title, int width, bool positionOnLeft,
30 Component* contentToDisplay, bool deleteComponentWhenNoLongerNeeded)
31 : titleLabel ("titleLabel", title),
32 isOnLeft (positionOnLeft),
34{
36
38
39 dismissButton.onClick = [this] { showOrHide (false); };
41
42 auto& desktop = Desktop::getInstance();
43
44 desktop.addGlobalMouseListener (this);
45 desktop.getAnimator().addChangeListener (this);
46
47 if (contentToDisplay != nullptr)
48 setContent (contentToDisplay, deleteComponentWhenNoLongerNeeded);
49
50 setOpaque (false);
51 setVisible (false);
52 setAlwaysOnTop (true);
53}
54
56{
57 auto& desktop = Desktop::getInstance();
58
59 desktop.removeGlobalMouseListener (this);
60 desktop.getAnimator().removeChangeListener (this);
61
62 if (parent != nullptr)
63 parent->removeComponentListener (this);
64}
65
66void SidePanel::setContent (Component* newContent, bool deleteComponentWhenNoLongerNeeded)
67{
68 if (contentComponent.get() != newContent)
69 {
70 if (deleteComponentWhenNoLongerNeeded)
71 contentComponent.setOwned (newContent);
72 else
73 contentComponent.setNonOwned (newContent);
74
76
77 resized();
78 }
79}
80
81void SidePanel::setTitleBarComponent (Component* titleBarComponentToUse,
82 bool keepDismissButton,
83 bool deleteComponentWhenNoLongerNeeded)
84{
85 if (titleBarComponent.get() != titleBarComponentToUse)
86 {
87 if (deleteComponentWhenNoLongerNeeded)
88 titleBarComponent.setOwned (titleBarComponentToUse);
89 else
90 titleBarComponent.setNonOwned (titleBarComponentToUse);
91
93
94 resized();
95 }
96
97 shouldShowDismissButton = keepDismissButton;
98}
99
100void SidePanel::showOrHide (bool show)
101{
102 if (parent != nullptr)
103 {
104 isShowing = show;
105
106 Desktop::getInstance().getAnimator().animateComponent (this, calculateBoundsInParent (*parent),
107 1.0f, 250, true, 1.0, 0.0);
108
109 if (isShowing && ! isVisible())
110 setVisible (true);
111 }
112}
113
115{
116 if (onPanelMove != nullptr)
117 onPanelMove();
118}
119
121{
122 auto bounds = getLocalBounds();
123
125
126 auto titleBounds = bounds.removeFromTop (titleBarHeight);
127
128 if (titleBarComponent != nullptr)
129 {
131 dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
132 : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
133
134 titleBarComponent->setBounds (titleBounds);
135 }
136 else
137 {
138 dismissButton.setBounds (isOnLeft ? titleBounds.removeFromRight (30).withTrimmedRight (10)
139 : titleBounds.removeFromLeft (30).withTrimmedLeft (10));
140
141 titleLabel.setBounds (isOnLeft ? titleBounds.withTrimmedRight (40)
142 : titleBounds.withTrimmedLeft (40));
143 }
144
145 if (contentComponent != nullptr)
146 contentComponent->setBounds (bounds);
147}
148
150{
151 auto& lf = getLookAndFeel();
152
153 auto bgColour = lf.findColour (SidePanel::backgroundColour);
154 auto shadowColour = lf.findColour (SidePanel::shadowBaseColour);
155
156 g.setGradientFill (ColourGradient (shadowColour.withAlpha (0.7f), (isOnLeft ? shadowArea.getTopLeft()
157 : shadowArea.getTopRight()).toFloat(),
158 shadowColour.withAlpha (0.0f), (isOnLeft ? shadowArea.getTopRight()
159 : shadowArea.getTopLeft()).toFloat(), false));
160 g.fillRect (shadowArea);
161
162 g.excludeClipRegion (shadowArea);
163 g.fillAll (bgColour);
164}
165
167{
168 auto* newParent = getParentComponent();
169
170 if ((newParent != nullptr) && (parent != newParent))
171 {
172 if (parent != nullptr)
173 parent->removeComponentListener (this);
174
175 parent = newParent;
176 parent->addComponentListener (this);
177 }
178}
179
181{
182 if (shouldResize)
183 {
184 Point<int> convertedPoint;
185
186 if (getParentComponent() == nullptr)
187 convertedPoint = e.eventComponent->localPointToGlobal (e.getPosition());
188 else
189 convertedPoint = getParentComponent()->getLocalPoint (e.eventComponent, e.getPosition());
190
191 auto currentMouseDragX = convertedPoint.x;
192
193 if (isOnLeft)
194 {
195 amountMoved = startingBounds.getRight() - currentMouseDragX;
196 setBounds (getBounds().withX (startingBounds.getX() - jmax (amountMoved, 0)));
197 }
198 else
199 {
200 amountMoved = currentMouseDragX - startingBounds.getX();
201 setBounds (getBounds().withX (startingBounds.getX() + jmax (amountMoved, 0)));
202 }
203 }
204 else if (isShowing)
205 {
206 auto relativeMouseDownPosition = getLocalPoint (e.eventComponent, e.getMouseDownPosition());
207 auto relativeMouseDragPosition = getLocalPoint (e.eventComponent, e.getPosition());
208
209 if (! getLocalBounds().contains (relativeMouseDownPosition)
210 && getLocalBounds().contains (relativeMouseDragPosition))
211 {
212 shouldResize = true;
214 }
215 }
216}
217
219{
220 if (shouldResize)
221 {
223
224 amountMoved = 0;
225 shouldResize = false;
226 }
227}
228
229//==============================================================================
231{
232 auto& lf = getLookAndFeel();
233
234 dismissButton.setShape (lf.getSidePanelDismissButtonShape (*this), false, true, false);
235
236 dismissButton.setColours (lf.findColour (SidePanel::dismissButtonNormalColour),
238 lf.findColour (SidePanel::dismissButtonDownColour));
239
240 titleLabel.setFont (lf.getSidePanelTitleFont (*this));
242 titleLabel.setJustificationType (lf.getSidePanelTitleJustification (*this));
243}
244
245void SidePanel::componentMovedOrResized (Component& component, bool wasMoved, bool wasResized)
246{
247 ignoreUnused (wasMoved);
248
249 if (wasResized && (&component == parent))
251}
252
254{
255 if (! Desktop::getInstance().getAnimator().isAnimating (this))
256 {
257 if (onPanelShowHide != nullptr)
259
260 if (isVisible() && ! isShowing)
261 setVisible (false);
262 }
263}
264
266{
267 auto parentBounds = parentComp.getLocalBounds();
268
269 if (isOnLeft)
270 {
271 return isShowing ? parentBounds.removeFromLeft (panelWidth)
272 : parentBounds.withX (parentBounds.getX() - panelWidth).withWidth (panelWidth);
273 }
274
275 return isShowing ? parentBounds.removeFromRight (panelWidth)
276 : parentBounds.withX (parentBounds.getRight()).withWidth (panelWidth);
277}
278
284
286{
287 if (eventComponent == this)
288 return true;
289
290 for (auto& child : getChildren())
291 if (eventComponent == child)
292 return true;
293
294 return false;
295}
296
297//==============================================================================
298std::unique_ptr<AccessibilityHandler> SidePanel::createAccessibilityHandler()
299{
300 return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
301}
302
303} // namespace juce
Definition juce_ChangeBroadcaster.h:35
Definition juce_ColourGradient.h:38
bool contains(Point< int > localPoint)
Definition juce_Component.cpp:1434
bool isVisible() const noexcept
Definition juce_Component.h:122
Component * getParentComponent() const noexcept
Definition juce_Component.h:804
Point< int > getLocalPoint(const Component *sourceComponent, Point< int > pointRelativeToSourceComponent) const
Definition juce_Component.cpp:1136
void addAndMakeVisible(Component *child, int zOrder=-1)
Definition juce_Component.cpp:1554
void setAlwaysOnTop(bool shouldStayOnTop)
Definition juce_Component.cpp:1074
void setOpaque(bool shouldBeOpaque)
Definition juce_Component.cpp:829
Rectangle< int > getBounds() const noexcept
Definition juce_Component.h:304
Component() noexcept
Definition juce_Component.cpp:517
void setBounds(int x, int y, int width, int height)
Definition juce_Component.cpp:1147
Colour findColour(int colourID, bool inheritFromParent=false) const
Definition juce_Component.cpp:2219
LookAndFeel & getLookAndFeel() const noexcept
Definition juce_Component.cpp:2173
Rectangle< int > getLocalBounds() const noexcept
Definition juce_Component.cpp:2283
const Array< Component * > & getChildren() const noexcept
Definition juce_Component.h:685
virtual void setVisible(bool shouldBeVisible)
Definition juce_Component.cpp:575
static Desktop &JUCE_CALLTYPE getInstance()
Definition juce_Desktop.cpp:50
Definition juce_GraphicsContext.h:45
@ textColourId
Definition juce_Label.h:107
Definition juce_MouseEvent.h:39
Definition juce_Point.h:42
ValueType x
Definition juce_Point.h:246
Definition juce_Rectangle.h:67
Rectangle removeFromRight(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:542
Rectangle removeFromLeft(ValueType amountToRemove) noexcept
Definition juce_Rectangle.h:526
void setTitleBarComponent(Component *titleBarComponentToUse, bool keepDismissButton, bool deleteComponentWhenNoLongerNeeded=true)
Definition juce_SidePanel.cpp:81
void showOrHide(bool show)
Definition juce_SidePanel.cpp:100
std::function< void()> onPanelMove
Definition juce_SidePanel.h:180
int panelWidth
Definition juce_SidePanel.h:213
OptionalScopedPointer< Component > titleBarComponent
Definition juce_SidePanel.h:203
void calculateAndRemoveShadowBounds(Rectangle< int > &bounds)
Definition juce_SidePanel.cpp:279
Rectangle< int > shadowArea
Definition juce_SidePanel.h:208
OptionalScopedPointer< Component > contentComponent
Definition juce_SidePanel.h:202
int titleBarHeight
Definition juce_SidePanel.h:215
void setContent(Component *newContentComponent, bool deleteComponentWhenNoLongerNeeded=true)
Definition juce_SidePanel.cpp:66
bool shouldResize
Definition juce_SidePanel.h:218
int shadowWidth
Definition juce_SidePanel.h:214
void resized() override
Definition juce_SidePanel.cpp:120
void componentMovedOrResized(Component &, bool wasMoved, bool wasResized) override
Definition juce_SidePanel.cpp:245
void lookAndFeelChanged() override
Definition juce_SidePanel.cpp:230
Rectangle< int > calculateBoundsInParent(Component &) const
Definition juce_SidePanel.cpp:265
@ titleTextColour
Definition juce_SidePanel.h:171
@ dismissButtonOverColour
Definition juce_SidePanel.h:174
@ dismissButtonDownColour
Definition juce_SidePanel.h:175
@ shadowBaseColour
Definition juce_SidePanel.h:172
@ backgroundColour
Definition juce_SidePanel.h:170
@ dismissButtonNormalColour
Definition juce_SidePanel.h:173
void paint(Graphics &g) override
Definition juce_SidePanel.cpp:149
Rectangle< int > startingBounds
Definition juce_SidePanel.h:217
ShapeButton dismissButton
Definition juce_SidePanel.h:206
Component * parent
Definition juce_SidePanel.h:201
void changeListenerCallback(ChangeBroadcaster *) override
Definition juce_SidePanel.cpp:253
int amountMoved
Definition juce_SidePanel.h:219
void parentHierarchyChanged() override
Definition juce_SidePanel.cpp:166
void mouseUp(const MouseEvent &) override
Definition juce_SidePanel.cpp:218
void mouseDrag(const MouseEvent &) override
Definition juce_SidePanel.cpp:180
bool isShowing
Definition juce_SidePanel.h:211
std::unique_ptr< AccessibilityHandler > createAccessibilityHandler() override
Definition juce_SidePanel.cpp:298
~SidePanel() override
Definition juce_SidePanel.cpp:55
Label titleLabel
Definition juce_SidePanel.h:205
std::function< void(bool)> onPanelShowHide
Definition juce_SidePanel.h:183
bool isOnLeft
Definition juce_SidePanel.h:210
void moved() override
Definition juce_SidePanel.cpp:114
bool isMouseEventInThisOrChildren(Component *)
Definition juce_SidePanel.cpp:285
bool shouldShowDismissButton
Definition juce_SidePanel.h:221
SidePanel(StringRef title, int width, bool positionOnLeft, Component *contentComponent=nullptr, bool deleteComponentWhenNoLongerNeeded=true)
Definition juce_SidePanel.cpp:29
Definition juce_StringRef.h:62
* e
Definition inflate.c:1404
int g
Definition inflate.c:1573
static const char * title
Definition pugl.h:1747
static int width
Definition pugl.h:1593
Definition carla_juce.cpp:31
constexpr Type jmax(Type a, Type b)
Definition juce_MathsFunctions.h:94
void ignoreUnused(Types &&...) noexcept
Definition juce_MathsFunctions.h:333
@ group
Definition juce_AccessibilityRole.h:61