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.

469 lines
14KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. struct ConcertinaPanel::PanelSizes
  21. {
  22. struct Panel
  23. {
  24. Panel() = default;
  25. Panel (int sz, int mn, int mx) noexcept
  26. : size (sz), minSize (mn), maxSize (mx) {}
  27. int setSize (int newSize) noexcept
  28. {
  29. jassert (minSize <= maxSize);
  30. auto oldSize = size;
  31. size = jlimit (minSize, maxSize, newSize);
  32. return size - oldSize;
  33. }
  34. int expand (int amount) noexcept
  35. {
  36. amount = jmin (amount, maxSize - size);
  37. size += amount;
  38. return amount;
  39. }
  40. int reduce (int amount) noexcept
  41. {
  42. amount = jmin (amount, size - minSize);
  43. size -= amount;
  44. return amount;
  45. }
  46. bool canExpand() const noexcept { return size < maxSize; }
  47. bool isMinimised() const noexcept { return size <= minSize; }
  48. int size, minSize, maxSize;
  49. };
  50. Array<Panel> sizes;
  51. Panel& get (int index) noexcept { return sizes.getReference (index); }
  52. const Panel& get (int index) const noexcept { return sizes.getReference (index); }
  53. PanelSizes withMovedPanel (int index, int targetPosition, int totalSpace) const
  54. {
  55. auto num = sizes.size();
  56. totalSpace = jmax (totalSpace, getMinimumSize (0, num));
  57. targetPosition = jmax (targetPosition, totalSpace - getMaximumSize (index, num));
  58. PanelSizes newSizes (*this);
  59. newSizes.stretchRange (0, index, targetPosition - newSizes.getTotalSize (0, index), stretchLast);
  60. newSizes.stretchRange (index, num, totalSpace - newSizes.getTotalSize (0, index) - newSizes.getTotalSize (index, num), stretchFirst);
  61. return newSizes;
  62. }
  63. PanelSizes fittedInto (int totalSpace) const
  64. {
  65. auto newSizes (*this);
  66. auto num = newSizes.sizes.size();
  67. totalSpace = jmax (totalSpace, getMinimumSize (0, num));
  68. newSizes.stretchRange (0, num, totalSpace - newSizes.getTotalSize (0, num), stretchAll);
  69. return newSizes;
  70. }
  71. PanelSizes withResizedPanel (int index, int panelHeight, int totalSpace) const
  72. {
  73. PanelSizes newSizes (*this);
  74. if (totalSpace <= 0)
  75. {
  76. newSizes.get(index).size = panelHeight;
  77. }
  78. else
  79. {
  80. auto num = sizes.size();
  81. auto minSize = getMinimumSize (0, num);
  82. totalSpace = jmax (totalSpace, minSize);
  83. newSizes.get(index).setSize (panelHeight);
  84. newSizes.stretchRange (0, index, totalSpace - newSizes.getTotalSize (0, num), stretchLast);
  85. newSizes.stretchRange (index, num, totalSpace - newSizes.getTotalSize (0, num), stretchLast);
  86. newSizes = newSizes.fittedInto (totalSpace);
  87. }
  88. return newSizes;
  89. }
  90. private:
  91. enum ExpandMode
  92. {
  93. stretchAll,
  94. stretchFirst,
  95. stretchLast
  96. };
  97. void growRangeFirst (int start, int end, int spaceDiff) noexcept
  98. {
  99. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  100. for (int i = start; i < end && spaceDiff > 0; ++i)
  101. spaceDiff -= get (i).expand (spaceDiff);
  102. }
  103. void growRangeLast (int start, int end, int spaceDiff) noexcept
  104. {
  105. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  106. for (int i = end; --i >= start && spaceDiff > 0;)
  107. spaceDiff -= get (i).expand (spaceDiff);
  108. }
  109. void growRangeAll (int start, int end, int spaceDiff) noexcept
  110. {
  111. Array<Panel*> expandableItems;
  112. for (int i = start; i < end; ++i)
  113. if (get(i).canExpand() && ! get(i).isMinimised())
  114. expandableItems.add (& get(i));
  115. for (int attempts = 4; --attempts >= 0 && spaceDiff > 0;)
  116. for (int i = expandableItems.size(); --i >= 0 && spaceDiff > 0;)
  117. spaceDiff -= expandableItems.getUnchecked(i)->expand (spaceDiff / (i + 1));
  118. growRangeLast (start, end, spaceDiff);
  119. }
  120. void shrinkRangeFirst (int start, int end, int spaceDiff) noexcept
  121. {
  122. for (int i = start; i < end && spaceDiff > 0; ++i)
  123. spaceDiff -= get(i).reduce (spaceDiff);
  124. }
  125. void shrinkRangeLast (int start, int end, int spaceDiff) noexcept
  126. {
  127. for (int i = end; --i >= start && spaceDiff > 0;)
  128. spaceDiff -= get(i).reduce (spaceDiff);
  129. }
  130. void stretchRange (int start, int end, int amountToAdd, ExpandMode expandMode) noexcept
  131. {
  132. if (end > start)
  133. {
  134. if (amountToAdd > 0)
  135. {
  136. if (expandMode == stretchAll) growRangeAll (start, end, amountToAdd);
  137. else if (expandMode == stretchFirst) growRangeFirst (start, end, amountToAdd);
  138. else if (expandMode == stretchLast) growRangeLast (start, end, amountToAdd);
  139. }
  140. else
  141. {
  142. if (expandMode == stretchFirst) shrinkRangeFirst (start, end, -amountToAdd);
  143. else shrinkRangeLast (start, end, -amountToAdd);
  144. }
  145. }
  146. }
  147. int getTotalSize (int start, int end) const noexcept
  148. {
  149. int tot = 0;
  150. while (start < end) tot += get (start++).size;
  151. return tot;
  152. }
  153. int getMinimumSize (int start, int end) const noexcept
  154. {
  155. int tot = 0;
  156. while (start < end) tot += get (start++).minSize;
  157. return tot;
  158. }
  159. int getMaximumSize (int start, int end) const noexcept
  160. {
  161. int tot = 0;
  162. while (start < end)
  163. {
  164. auto mx = get (start++).maxSize;
  165. if (mx > 0x100000)
  166. return mx;
  167. tot += mx;
  168. }
  169. return tot;
  170. }
  171. };
  172. //==============================================================================
  173. class ConcertinaPanel::PanelHolder : public Component
  174. {
  175. public:
  176. PanelHolder (Component* comp, bool takeOwnership)
  177. : component (comp, takeOwnership)
  178. {
  179. setRepaintsOnMouseActivity (true);
  180. setWantsKeyboardFocus (false);
  181. addAndMakeVisible (comp);
  182. }
  183. void paint (Graphics& g) override
  184. {
  185. if (customHeaderComponent == nullptr)
  186. {
  187. const Rectangle<int> area (getWidth(), getHeaderSize());
  188. g.reduceClipRegion (area);
  189. getLookAndFeel().drawConcertinaPanelHeader (g, area, isMouseOver(), isMouseButtonDown(),
  190. getPanel(), *component);
  191. }
  192. }
  193. void resized() override
  194. {
  195. auto bounds = getLocalBounds();
  196. auto headerBounds = bounds.removeFromTop (getHeaderSize());
  197. if (customHeaderComponent != nullptr)
  198. customHeaderComponent->setBounds (headerBounds);
  199. component->setBounds (bounds);
  200. }
  201. void mouseDown (const MouseEvent&) override
  202. {
  203. mouseDownY = getY();
  204. dragStartSizes = getPanel().getFittedSizes();
  205. }
  206. void mouseDrag (const MouseEvent& e) override
  207. {
  208. if (e.mouseWasDraggedSinceMouseDown())
  209. {
  210. auto& panel = getPanel();
  211. panel.setLayout (dragStartSizes.withMovedPanel (panel.holders.indexOf (this),
  212. mouseDownY + e.getDistanceFromDragStartY(),
  213. panel.getHeight()), false);
  214. }
  215. }
  216. void mouseDoubleClick (const MouseEvent&) override
  217. {
  218. getPanel().panelHeaderDoubleClicked (component);
  219. }
  220. void setCustomHeaderComponent (Component* headerComponent, bool shouldTakeOwnership)
  221. {
  222. customHeaderComponent.set (headerComponent, shouldTakeOwnership);
  223. if (headerComponent != nullptr)
  224. {
  225. addAndMakeVisible (customHeaderComponent);
  226. customHeaderComponent->addMouseListener (this, false);
  227. }
  228. }
  229. OptionalScopedPointer<Component> component;
  230. private:
  231. PanelSizes dragStartSizes;
  232. int mouseDownY;
  233. OptionalScopedPointer<Component> customHeaderComponent;
  234. int getHeaderSize() const noexcept
  235. {
  236. ConcertinaPanel& panel = getPanel();
  237. auto ourIndex = panel.holders.indexOf (this);
  238. return panel.currentSizes->get(ourIndex).minSize;
  239. }
  240. ConcertinaPanel& getPanel() const
  241. {
  242. auto panel = dynamic_cast<ConcertinaPanel*> (getParentComponent());
  243. jassert (panel != nullptr);
  244. return *panel;
  245. }
  246. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PanelHolder)
  247. };
  248. //==============================================================================
  249. ConcertinaPanel::ConcertinaPanel()
  250. : currentSizes (new PanelSizes()),
  251. headerHeight (20)
  252. {
  253. }
  254. ConcertinaPanel::~ConcertinaPanel() {}
  255. int ConcertinaPanel::getNumPanels() const noexcept
  256. {
  257. return holders.size();
  258. }
  259. Component* ConcertinaPanel::getPanel (int index) const noexcept
  260. {
  261. if (PanelHolder* h = holders[index])
  262. return h->component;
  263. return nullptr;
  264. }
  265. void ConcertinaPanel::addPanel (int insertIndex, Component* component, bool takeOwnership)
  266. {
  267. jassert (component != nullptr); // can't use a null pointer here!
  268. jassert (indexOfComp (component) < 0); // You can't add the same component more than once!
  269. auto holder = new PanelHolder (component, takeOwnership);
  270. holders.insert (insertIndex, holder);
  271. currentSizes->sizes.insert (insertIndex, PanelSizes::Panel (headerHeight, headerHeight, std::numeric_limits<int>::max()));
  272. addAndMakeVisible (holder);
  273. resized();
  274. }
  275. void ConcertinaPanel::removePanel (Component* component)
  276. {
  277. auto index = indexOfComp (component);
  278. if (index >= 0)
  279. {
  280. currentSizes->sizes.remove (index);
  281. holders.remove (index);
  282. resized();
  283. }
  284. }
  285. bool ConcertinaPanel::setPanelSize (Component* panelComponent, int height, bool animate)
  286. {
  287. auto index = indexOfComp (panelComponent);
  288. jassert (index >= 0); // The specified component doesn't seem to have been added!
  289. height += currentSizes->get(index).minSize;
  290. auto oldSize = currentSizes->get(index).size;
  291. setLayout (currentSizes->withResizedPanel (index, height, getHeight()), animate);
  292. return oldSize != currentSizes->get(index).size;
  293. }
  294. bool ConcertinaPanel::expandPanelFully (Component* component, bool animate)
  295. {
  296. return setPanelSize (component, getHeight(), animate);
  297. }
  298. void ConcertinaPanel::setMaximumPanelSize (Component* component, int maximumSize)
  299. {
  300. auto index = indexOfComp (component);
  301. jassert (index >= 0); // The specified component doesn't seem to have been added!
  302. if (index >= 0)
  303. {
  304. currentSizes->get(index).maxSize = currentSizes->get(index).minSize + maximumSize;
  305. resized();
  306. }
  307. }
  308. void ConcertinaPanel::setPanelHeaderSize (Component* component, int headerSize)
  309. {
  310. auto index = indexOfComp (component);
  311. jassert (index >= 0); // The specified component doesn't seem to have been added!
  312. if (index >= 0)
  313. {
  314. auto oldMin = currentSizes->get (index).minSize;
  315. currentSizes->get (index).minSize = headerSize;
  316. currentSizes->get (index).size += headerSize - oldMin;
  317. resized();
  318. }
  319. }
  320. void ConcertinaPanel::setCustomPanelHeader (Component* component, Component* customComponent, bool takeOwnership)
  321. {
  322. OptionalScopedPointer<Component> optional (customComponent, takeOwnership);
  323. auto index = indexOfComp (component);
  324. jassert (index >= 0); // The specified component doesn't seem to have been added!
  325. if (index >= 0)
  326. holders.getUnchecked (index)->setCustomHeaderComponent (optional.release(), takeOwnership);
  327. }
  328. void ConcertinaPanel::resized()
  329. {
  330. applyLayout (getFittedSizes(), false);
  331. }
  332. int ConcertinaPanel::indexOfComp (Component* comp) const noexcept
  333. {
  334. for (int i = 0; i < holders.size(); ++i)
  335. if (holders.getUnchecked(i)->component == comp)
  336. return i;
  337. return -1;
  338. }
  339. ConcertinaPanel::PanelSizes ConcertinaPanel::getFittedSizes() const
  340. {
  341. return currentSizes->fittedInto (getHeight());
  342. }
  343. void ConcertinaPanel::applyLayout (const PanelSizes& sizes, bool animate)
  344. {
  345. if (! animate)
  346. animator.cancelAllAnimations (false);
  347. const int animationDuration = 150;
  348. auto w = getWidth();
  349. int y = 0;
  350. for (int i = 0; i < holders.size(); ++i)
  351. {
  352. PanelHolder& p = *holders.getUnchecked (i);
  353. auto h = sizes.get (i).size;
  354. const Rectangle<int> pos (0, y, w, h);
  355. if (animate)
  356. animator.animateComponent (&p, pos, 1.0f, animationDuration, false, 1.0, 1.0);
  357. else
  358. p.setBounds (pos);
  359. y += h;
  360. }
  361. }
  362. void ConcertinaPanel::setLayout (const PanelSizes& sizes, bool animate)
  363. {
  364. *currentSizes = sizes;
  365. applyLayout (getFittedSizes(), animate);
  366. }
  367. void ConcertinaPanel::panelHeaderDoubleClicked (Component* component)
  368. {
  369. if (! expandPanelFully (component, true))
  370. setPanelSize (component, 0, true);
  371. }
  372. //==============================================================================
  373. std::unique_ptr<AccessibilityHandler> ConcertinaPanel::createAccessibilityHandler()
  374. {
  375. return std::make_unique<AccessibilityHandler> (*this, AccessibilityRole::group);
  376. }
  377. } // namespace juce