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.

461 lines
16KB

  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. #include "MainComponent.h"
  19. //==============================================================================
  20. struct SidePanelHeader : public Component
  21. {
  22. SidePanelHeader (MainComponent& o)
  23. : owner (o)
  24. {
  25. setOpaque (true);
  26. static const unsigned char homeIconPathData[]
  27. = { 110,109,0,0,64,65,0,0,64,64,98,0,0,64,65,0,0,64,64,74,12,186,64,164,112,5,65,24,217,22,64,70,182,
  28. 51,65,98,35,219,9,64,240,167,54,65,0,0,0,64,170,241,58,65,0,0,0,64,0,0,64,65,98,0,0,0,64,22,217,
  29. 72,65,166,155,28,64,0,0,80,65,0,0,64,64,0,0,80,65,108,0,0,160,64,0,0,80,65,108,0,0,160,64,0,0,
  30. 160,65,98,0,0,160,64,139,108,164,65,211,77,174,64,0,0,168,65,0,0,192,64,0,0,168,65,108,0,0,16,65,
  31. 0,0,168,65,98,22,217,24,65,0,0,168,65,0,0,32,65,127,106,164,65,0,0,32,65,0,0,160,65,108,0,0,32,65,0,0,128,
  32. 65,108,0,0,96,65,0,0,128,65,108,0,0,96,65,0,0,160,65,98,0,0,96,65,127,106,164,65,233,38,103,65,0,0,168,
  33. 65,0,0,112,65,0,0,168,65,108,0,0,144,65,0,0,168,65,98,139,108,148,65,0,0,168,65,0,0,152,65,139,108,164,
  34. 65,0,0,152,65,0,0,160,65,108,0,0,152,65,0,0,80,65,108,0,0,168,65,0,0,80,65,98,139,108,172,65,0,0,80,65,0,
  35. 0,176,65,23,217,72,65,0,0,176,65,0,0,64,65,98,0,0,176,65,170,241,58,65,156,196,174,65,240,167,54,65,158,239,
  36. 172,65,70,182,51,65,98,213,120,145,65,164,112,5,65,0,0,64,65,0,0,64,64,0,0,64,65,0,0,64,64,99,
  37. 101,0,0 };
  38. static const unsigned char settingsIconPathData[]
  39. = { 110,109,202,111,210,64,243,226,61,64,108,0,0,224,64,0,0,0,0,108,0,0,48,65,0,0,0,0,108,27,200,54,65,243,
  40. 226,61,64,98,91,248,63,65,174,170,76,64,95,130,72,65,231,138,96,64,46,46,80,65,180,163,120,64,108,42,
  41. 181,124,65,20,38,49,64,108,149,90,142,65,246,108,199,64,108,68,249,118,65,2,85,1,65,98,112,166,119,65,
  42. 201,31,6,65,0,0,120,65,111,5,11,65,0,0,120,65,0,0,16,65,98,0,0,120,65,145,250,20,65,108,166,119,65,55,
  43. 224,25,65,72,249,118,65,254,170,30,65,108,151,90,142,65,133,73,60,65,108,46,181,124,65,123,182,115,65,
  44. 108,50,46,80,65,18,215,97,65,98,99,130,72,65,70,221,103,65,96,248,63,65,83,213,108,65,32,200,54,65,66,
  45. 135,112,65,108,0,0,48,65,0,0,144,65,108,0,0,224,64,0,0,144,65,108,202,111,210,64,67,135,112,65,98,74,
  46. 15,192,64,84,213,108,65,65,251,174,64,70,221,103,65,164,163,159,64,19,215,97,65,108,92,43,13,64,123,182,
  47. 115,65,108,187,181,82,62,133,73,60,65,108,244,26,36,64,254,170,30,65,98,64,102,33,64,55,224,25,5,0,0,32,
  48. 64,145,250,20,65,0,0,32,64,0,0,16,65,98,0,0,32,64,111,5,11,65,64,102,33,64,201,31,6,65,244,26,36,64,2,85,
  49. 1,65,108,187,181,82,62,246,108,199,64,108,92,43,13,64,20,38,49,64,108,164,163,159,64,180,163,120,64,98,65,
  50. 251,174,64,231,138,96,64,74,15,192,64,175,170,76,64,202,111,210,64,243,226,61,64,99,109,0,0,16,65,0,0,64,
  51. 65,98,121,130,42,65,0,0,64,65,0,0,64,65,121,130,42,65,0,0,64,65,0,0,16,65,98,0,0,64,65,13,251,234,64,121,
  52. 130,42,65,0,0,192,64,0,0,16,65,0,0,192,64,98,13,251,234,64,0,0,192,64,0,0,192,64,13,251,234,64,0,0,192,64,
  53. 0,0,16,65,98,0,0,192,64,121,130,42,65,13,251,234,64,0,0,64,65,0,0,16,65,0,0,64,65,99,101,0,0 };
  54. Path p;
  55. p.loadPathFromData (homeIconPathData, sizeof (homeIconPathData));
  56. homeButton.setShape (p, true, true, false);
  57. p.clear();
  58. p.loadPathFromData (settingsIconPathData, sizeof (settingsIconPathData));
  59. settingsButton.setShape (p, true, true, false);
  60. titleLabel.setText (owner.getSidePanel().getTitleText(), NotificationType::dontSendNotification);
  61. addAndMakeVisible (titleLabel);
  62. homeButton.onClick = [this] { owner.homeButtonClicked(); };
  63. addAndMakeVisible (homeButton);
  64. addAndMakeVisible (settingsButton);
  65. settingsButton.onClick = [this] { owner.settingsButtonClicked(); };
  66. lookAndFeelChanged();
  67. }
  68. void paint (Graphics& g) override
  69. {
  70. g.fillAll (owner.getSidePanel().findColour (SidePanel::backgroundColour));
  71. }
  72. void resized() override
  73. {
  74. auto bounds = getLocalBounds();
  75. auto buttonWidth = owner.getSidePanel().getTitleBarHeight();
  76. bounds.removeFromLeft (10);
  77. homeButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  78. settingsButton.setBounds (bounds.removeFromLeft (buttonWidth).reduced (7));
  79. bounds.removeFromLeft (10);
  80. bounds.removeFromRight (10);
  81. titleLabel.setBounds (bounds);
  82. }
  83. void lookAndFeelChanged() override
  84. {
  85. auto& sidePanel = owner.getSidePanel();
  86. auto& lf = sidePanel.getLookAndFeel();
  87. titleLabel.setFont (lf.getSidePanelTitleFont (sidePanel));
  88. titleLabel.setJustificationType (lf.getSidePanelTitleJustification (sidePanel));
  89. titleLabel.setColour (Label::textColourId, owner.findColour (SidePanel::titleTextColour));
  90. auto normal = sidePanel.findColour (SidePanel::dismissButtonNormalColour);
  91. auto over = sidePanel.findColour (SidePanel::dismissButtonOverColour);
  92. auto down = sidePanel.findColour (SidePanel::dismissButtonDownColour);
  93. homeButton.setColours (normal, over, down);
  94. settingsButton.setColours (normal, over, down);
  95. }
  96. MainComponent& owner;
  97. Label titleLabel;
  98. ShapeButton homeButton { "Home", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack },
  99. settingsButton { "Settings", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  100. };
  101. //==============================================================================
  102. class DemoList : public Component,
  103. public ListBoxModel
  104. {
  105. public:
  106. DemoList (DemoContentComponent& holder)
  107. : demoHolder (holder)
  108. {
  109. addAndMakeVisible (demos);
  110. demos.setModel (this);
  111. demos.setRowHeight (40);
  112. }
  113. void resized() override
  114. {
  115. demos.setBounds (getLocalBounds());
  116. }
  117. //==============================================================================
  118. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  119. {
  120. Rectangle<int> bounds (0, 0, width, height);
  121. auto textColour = findColour (Label::textColourId);
  122. g.setColour (textColour.withAlpha (0.4f));
  123. if (rowNumber == 0)
  124. g.fillRect (bounds.removeFromTop (2).reduced (7, 0));
  125. g.fillRect (bounds.removeFromBottom (2).reduced (7, 0));
  126. if (rowIsSelected)
  127. {
  128. g.setColour (findColour (TextEditor::highlightColourId).withAlpha (0.4f));
  129. g.fillRect (bounds);
  130. textColour = findColour (TextEditor::highlightedTextColourId);
  131. }
  132. g.setColour (textColour);
  133. g.drawFittedText (getNameForRow (rowNumber), bounds, Justification::centred, 1);
  134. }
  135. int getNumRows() override
  136. {
  137. return (int) (selectedCategory.isEmpty() ? JUCEDemos::getCategories().size()
  138. : JUCEDemos::getCategory (selectedCategory).demos.size());
  139. }
  140. String getNameForRow (int rowNumber) override
  141. {
  142. if (selectedCategory.isEmpty())
  143. {
  144. if (isPositiveAndBelow (rowNumber, JUCEDemos::getCategories().size()))
  145. return JUCEDemos::getCategories()[(size_t) rowNumber].name;
  146. }
  147. else
  148. {
  149. auto& category = JUCEDemos::getCategory (selectedCategory);
  150. if (isPositiveAndBelow (rowNumber, category.demos.size()))
  151. return category.demos[(size_t) rowNumber].demoFile.getFileName();
  152. }
  153. return {};
  154. }
  155. void returnKeyPressed (int row) override { selectRow (row); }
  156. void listBoxItemClicked (int row, const MouseEvent&) override { selectRow (row); }
  157. //==============================================================================
  158. void showCategory (const String& categoryName) noexcept
  159. {
  160. selectedCategory = categoryName;
  161. demos.deselectAllRows();
  162. demos.setHeaderComponent (categoryName.isEmpty() ? nullptr
  163. : std::make_unique<CategoryListHeaderComponent> (*this));
  164. demos.updateContent();
  165. }
  166. private:
  167. //==============================================================================
  168. class CategoryListHeaderComponent : public Button
  169. {
  170. public:
  171. explicit CategoryListHeaderComponent (DemoList& o)
  172. : Button ({}),
  173. owner (o)
  174. {
  175. setTitle ("Previous");
  176. setSize (0, 30);
  177. }
  178. void paintButton (Graphics& g, bool, bool) override
  179. {
  180. g.setColour (findColour (Label::textColourId));
  181. g.drawFittedText ("<", getLocalBounds().reduced (20, 0), Justification::centredLeft, 1);
  182. }
  183. void clicked() override
  184. {
  185. owner.showCategory ({});
  186. }
  187. using Button::clicked;
  188. private:
  189. DemoList& owner;
  190. };
  191. //==============================================================================
  192. void selectRow (int row)
  193. {
  194. if (row < 0)
  195. return;
  196. if (selectedCategory.isEmpty())
  197. showCategory (JUCEDemos::getCategories()[(size_t) row].name);
  198. else
  199. demoHolder.setDemo (selectedCategory, row);
  200. if (demos.isShowing())
  201. selectFirstRow();
  202. }
  203. void selectFirstRow()
  204. {
  205. if (auto* handler = demos.getAccessibilityHandler())
  206. {
  207. for (auto* child : handler->getChildren())
  208. {
  209. if (child->getRole() == AccessibilityRole::listItem)
  210. {
  211. child->grabFocus();
  212. break;
  213. }
  214. }
  215. }
  216. }
  217. //==============================================================================
  218. String selectedCategory;
  219. DemoContentComponent& demoHolder;
  220. ListBox demos;
  221. };
  222. //==============================================================================
  223. MainComponent::MainComponent()
  224. {
  225. contentComponent.reset (new DemoContentComponent (*this, [this] (bool isHeavyweight)
  226. {
  227. demosPanel.showOrHide (false);
  228. if (isHeavyweight)
  229. {
  230. #if JUCE_MAC && USE_COREGRAPHICS_RENDERING
  231. setRenderingEngine (1);
  232. #else
  233. setRenderingEngine (0);
  234. #endif
  235. }
  236. isShowingHeavyweightDemo = isHeavyweight;
  237. resized();
  238. }));
  239. demosPanel.setContent (new DemoList (*contentComponent));
  240. demosPanel.setTitleBarComponent (new SidePanelHeader (*this), true);
  241. addAndMakeVisible (contentComponent.get());
  242. addAndMakeVisible (showDemosButton);
  243. addAndMakeVisible (demosPanel);
  244. demosPanel.setTitle ("Demos");
  245. demosPanel.setFocusContainerType (FocusContainerType::focusContainer);
  246. showDemosButton.onClick = [this] { demosPanel.showOrHide (true); };
  247. demosPanel.onPanelMove = [this]
  248. {
  249. sidePanelWidth = jmax (0, demosPanel.getRight());
  250. if (isShowingHeavyweightDemo)
  251. resized();
  252. };
  253. demosPanel.onPanelShowHide = [this] (bool isShowing)
  254. {
  255. if (isShowing)
  256. {
  257. sidePanelWidth = jmax (0, demosPanel.getWidth());
  258. if (isShowingHeavyweightDemo)
  259. resized();
  260. if (auto* handler = demosPanel.getAccessibilityHandler())
  261. handler->grabFocus();
  262. }
  263. else
  264. {
  265. sidePanelWidth = 0;
  266. if (isShowingHeavyweightDemo)
  267. Timer::callAfterDelay (250, [this] { resized(); });
  268. }
  269. };
  270. contentComponent->showHomeScreen();
  271. setOpaque (true);
  272. setSize (800, 800);
  273. }
  274. MainComponent::~MainComponent()
  275. {
  276. contentComponent->clearCurrentDemo();
  277. }
  278. //==============================================================================
  279. void MainComponent::paint (Graphics& g)
  280. {
  281. g.fillAll (findColour (ResizableWindow::backgroundColourId));
  282. }
  283. void MainComponent::resized()
  284. {
  285. auto safeBounds = [this]
  286. {
  287. auto bounds = getLocalBounds();
  288. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (getScreenBounds()))
  289. return display->safeAreaInsets.subtractedFrom (bounds);
  290. return bounds;
  291. }();
  292. showDemosButton.setBounds (safeBounds.getX(), safeBounds.getY(), 150, contentComponent->getTabBarDepth());
  293. if (isShowingHeavyweightDemo)
  294. {
  295. safeBounds.removeFromLeft (sidePanelWidth);
  296. contentComponent->setTabBarIndent (jmax (0, 150 - sidePanelWidth));
  297. }
  298. else
  299. {
  300. contentComponent->setTabBarIndent (150);
  301. }
  302. contentComponent->setBounds (safeBounds);
  303. }
  304. void MainComponent::homeButtonClicked()
  305. {
  306. if (auto* list = dynamic_cast<DemoList*> (demosPanel.getContent()))
  307. list->showCategory ({});
  308. if (contentComponent != nullptr)
  309. {
  310. if (contentComponent->isShowingHomeScreen())
  311. return;
  312. contentComponent->showHomeScreen();
  313. if (isShowingHeavyweightDemo)
  314. {
  315. isShowingHeavyweightDemo = false;
  316. resized();
  317. }
  318. }
  319. }
  320. void MainComponent::settingsButtonClicked()
  321. {
  322. if (contentComponent != nullptr)
  323. contentComponent->setCurrentTabIndex (2);
  324. }
  325. void MainComponent::setRenderingEngine (int renderingEngineIndex)
  326. {
  327. if (renderingEngineIndex != currentRenderingEngineIdx)
  328. updateRenderingEngine (renderingEngineIndex);
  329. }
  330. void MainComponent::parentHierarchyChanged()
  331. {
  332. auto* newPeer = getPeer();
  333. if (peer != newPeer)
  334. {
  335. peer = newPeer;
  336. auto previousRenderingEngine = renderingEngines[currentRenderingEngineIdx];
  337. renderingEngines.clear();
  338. if (peer != nullptr)
  339. renderingEngines = peer->getAvailableRenderingEngines();
  340. renderingEngines.add ("OpenGL Renderer");
  341. currentRenderingEngineIdx = renderingEngines.indexOf (previousRenderingEngine);
  342. if (currentRenderingEngineIdx < 0)
  343. {
  344. #if JUCE_ANDROID
  345. currentRenderingEngineIdx = (renderingEngines.size() - 1);
  346. #else
  347. currentRenderingEngineIdx = peer->getCurrentRenderingEngine();
  348. #endif
  349. }
  350. updateRenderingEngine (currentRenderingEngineIdx);
  351. }
  352. }
  353. void MainComponent::updateRenderingEngine (int renderingEngineIndex)
  354. {
  355. if (renderingEngineIndex == (renderingEngines.size() - 1))
  356. {
  357. if (isShowingHeavyweightDemo)
  358. return;
  359. openGLContext.attachTo (*getTopLevelComponent());
  360. }
  361. else
  362. {
  363. openGLContext.detach();
  364. peer->setCurrentRenderingEngine (renderingEngineIndex);
  365. }
  366. currentRenderingEngineIdx = renderingEngineIndex;
  367. }