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.

467 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 final : 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. updateLookAndFeel();
  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 updateLookAndFeel()
  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. void lookAndFeelChanged() override
  97. {
  98. updateLookAndFeel();
  99. }
  100. MainComponent& owner;
  101. Label titleLabel;
  102. ShapeButton homeButton { "Home", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack },
  103. settingsButton { "Settings", Colours::transparentBlack, Colours::transparentBlack, Colours::transparentBlack };
  104. };
  105. //==============================================================================
  106. class DemoList final : public Component,
  107. public ListBoxModel
  108. {
  109. public:
  110. DemoList (DemoContentComponent& holder)
  111. : demoHolder (holder)
  112. {
  113. addAndMakeVisible (demos);
  114. demos.setModel (this);
  115. demos.setRowHeight (40);
  116. }
  117. void resized() override
  118. {
  119. demos.setBounds (getLocalBounds());
  120. }
  121. //==============================================================================
  122. void paintListBoxItem (int rowNumber, Graphics& g, int width, int height, bool rowIsSelected) override
  123. {
  124. Rectangle<int> bounds (0, 0, width, height);
  125. auto textColour = findColour (Label::textColourId);
  126. g.setColour (textColour.withAlpha (0.4f));
  127. if (rowNumber == 0)
  128. g.fillRect (bounds.removeFromTop (2).reduced (7, 0));
  129. g.fillRect (bounds.removeFromBottom (2).reduced (7, 0));
  130. if (rowIsSelected)
  131. {
  132. g.setColour (findColour (TextEditor::highlightColourId).withAlpha (0.4f));
  133. g.fillRect (bounds);
  134. textColour = findColour (TextEditor::highlightedTextColourId);
  135. }
  136. g.setColour (textColour);
  137. g.drawFittedText (getNameForRow (rowNumber), bounds, Justification::centred, 1);
  138. }
  139. int getNumRows() override
  140. {
  141. return (int) (selectedCategory.isEmpty() ? JUCEDemos::getCategories().size()
  142. : JUCEDemos::getCategory (selectedCategory).demos.size());
  143. }
  144. String getNameForRow (int rowNumber) override
  145. {
  146. if (selectedCategory.isEmpty())
  147. {
  148. if (isPositiveAndBelow (rowNumber, JUCEDemos::getCategories().size()))
  149. return JUCEDemos::getCategories()[(size_t) rowNumber].name;
  150. }
  151. else
  152. {
  153. auto& category = JUCEDemos::getCategory (selectedCategory);
  154. if (isPositiveAndBelow (rowNumber, category.demos.size()))
  155. return category.demos[(size_t) rowNumber].demoFile.getFileName();
  156. }
  157. return {};
  158. }
  159. void returnKeyPressed (int row) override { selectRow (row); }
  160. void listBoxItemClicked (int row, const MouseEvent&) override { selectRow (row); }
  161. //==============================================================================
  162. void showCategory (const String& categoryName) noexcept
  163. {
  164. selectedCategory = categoryName;
  165. demos.deselectAllRows();
  166. demos.setHeaderComponent (categoryName.isEmpty() ? nullptr
  167. : std::make_unique<CategoryListHeaderComponent> (*this));
  168. demos.updateContent();
  169. }
  170. private:
  171. //==============================================================================
  172. class CategoryListHeaderComponent final : public Button
  173. {
  174. public:
  175. explicit CategoryListHeaderComponent (DemoList& o)
  176. : Button ({}),
  177. owner (o)
  178. {
  179. setTitle ("Previous");
  180. setSize (0, 30);
  181. }
  182. void paintButton (Graphics& g, bool, bool) override
  183. {
  184. g.setColour (findColour (Label::textColourId));
  185. g.drawFittedText ("<", getLocalBounds().reduced (20, 0), Justification::centredLeft, 1);
  186. }
  187. void clicked() override
  188. {
  189. owner.showCategory ({});
  190. }
  191. using Button::clicked;
  192. private:
  193. DemoList& owner;
  194. };
  195. //==============================================================================
  196. void selectRow (int row)
  197. {
  198. if (row < 0)
  199. return;
  200. if (selectedCategory.isEmpty())
  201. showCategory (JUCEDemos::getCategories()[(size_t) row].name);
  202. else
  203. demoHolder.setDemo (selectedCategory, row);
  204. if (demos.isShowing())
  205. selectFirstRow();
  206. }
  207. void selectFirstRow()
  208. {
  209. if (auto* handler = demos.getAccessibilityHandler())
  210. {
  211. for (auto* child : handler->getChildren())
  212. {
  213. if (child->getRole() == AccessibilityRole::listItem)
  214. {
  215. child->grabFocus();
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. //==============================================================================
  222. String selectedCategory;
  223. DemoContentComponent& demoHolder;
  224. ListBox demos;
  225. };
  226. //==============================================================================
  227. MainComponent::MainComponent()
  228. {
  229. contentComponent.reset (new DemoContentComponent (*this, [this] (bool isHeavyweight)
  230. {
  231. demosPanel.showOrHide (false);
  232. if (isHeavyweight)
  233. {
  234. #if JUCE_MAC && USE_COREGRAPHICS_RENDERING
  235. setRenderingEngine (1);
  236. #else
  237. setRenderingEngine (0);
  238. #endif
  239. }
  240. isShowingHeavyweightDemo = isHeavyweight;
  241. resized();
  242. }));
  243. demosPanel.setContent (new DemoList (*contentComponent));
  244. demosPanel.setTitleBarComponent (new SidePanelHeader (*this), true);
  245. addAndMakeVisible (contentComponent.get());
  246. addAndMakeVisible (showDemosButton);
  247. addAndMakeVisible (demosPanel);
  248. demosPanel.setTitle ("Demos");
  249. demosPanel.setFocusContainerType (FocusContainerType::focusContainer);
  250. showDemosButton.onClick = [this] { demosPanel.showOrHide (true); };
  251. demosPanel.onPanelMove = [this]
  252. {
  253. sidePanelWidth = jmax (0, demosPanel.getRight());
  254. if (isShowingHeavyweightDemo)
  255. resized();
  256. };
  257. demosPanel.onPanelShowHide = [this] (bool isShowing)
  258. {
  259. if (isShowing)
  260. {
  261. sidePanelWidth = jmax (0, demosPanel.getWidth());
  262. if (isShowingHeavyweightDemo)
  263. resized();
  264. if (auto* handler = demosPanel.getAccessibilityHandler())
  265. handler->grabFocus();
  266. }
  267. else
  268. {
  269. sidePanelWidth = 0;
  270. if (isShowingHeavyweightDemo)
  271. Timer::callAfterDelay (250, [this] { resized(); });
  272. }
  273. };
  274. contentComponent->showHomeScreen();
  275. setOpaque (true);
  276. setSize (800, 800);
  277. }
  278. MainComponent::~MainComponent()
  279. {
  280. contentComponent->clearCurrentDemo();
  281. }
  282. //==============================================================================
  283. void MainComponent::paint (Graphics& g)
  284. {
  285. g.fillAll (findColour (ResizableWindow::backgroundColourId));
  286. }
  287. void MainComponent::resized()
  288. {
  289. auto safeBounds = [this]
  290. {
  291. auto bounds = getLocalBounds();
  292. if (auto* display = Desktop::getInstance().getDisplays().getDisplayForRect (getScreenBounds()))
  293. return display->safeAreaInsets.subtractedFrom (display->keyboardInsets.subtractedFrom (bounds));
  294. return bounds;
  295. }();
  296. showDemosButton.setBounds (safeBounds.getX(), safeBounds.getY(), 150, contentComponent->getTabBarDepth());
  297. if (isShowingHeavyweightDemo)
  298. {
  299. safeBounds.removeFromLeft (sidePanelWidth);
  300. contentComponent->setTabBarIndent (jmax (0, 150 - sidePanelWidth));
  301. }
  302. else
  303. {
  304. contentComponent->setTabBarIndent (150);
  305. }
  306. contentComponent->setBounds (safeBounds);
  307. }
  308. void MainComponent::homeButtonClicked()
  309. {
  310. if (auto* list = dynamic_cast<DemoList*> (demosPanel.getContent()))
  311. list->showCategory ({});
  312. if (contentComponent != nullptr)
  313. {
  314. if (contentComponent->isShowingHomeScreen())
  315. return;
  316. contentComponent->showHomeScreen();
  317. if (isShowingHeavyweightDemo)
  318. {
  319. isShowingHeavyweightDemo = false;
  320. resized();
  321. }
  322. }
  323. }
  324. void MainComponent::settingsButtonClicked()
  325. {
  326. if (contentComponent != nullptr)
  327. contentComponent->setCurrentTabIndex (2);
  328. }
  329. void MainComponent::setRenderingEngine (int renderingEngineIndex)
  330. {
  331. if (renderingEngineIndex != currentRenderingEngineIdx)
  332. updateRenderingEngine (renderingEngineIndex);
  333. }
  334. void MainComponent::parentHierarchyChanged()
  335. {
  336. auto* newPeer = getPeer();
  337. if (peer != newPeer)
  338. {
  339. peer = newPeer;
  340. auto previousRenderingEngine = renderingEngines[currentRenderingEngineIdx];
  341. renderingEngines.clear();
  342. if (peer != nullptr)
  343. renderingEngines = peer->getAvailableRenderingEngines();
  344. renderingEngines.add ("OpenGL Renderer");
  345. currentRenderingEngineIdx = renderingEngines.indexOf (previousRenderingEngine);
  346. if (currentRenderingEngineIdx < 0)
  347. {
  348. #if JUCE_ANDROID
  349. currentRenderingEngineIdx = (renderingEngines.size() - 1);
  350. #else
  351. currentRenderingEngineIdx = peer->getCurrentRenderingEngine();
  352. #endif
  353. }
  354. updateRenderingEngine (currentRenderingEngineIdx);
  355. }
  356. }
  357. void MainComponent::updateRenderingEngine (int renderingEngineIndex)
  358. {
  359. if (renderingEngineIndex == (renderingEngines.size() - 1))
  360. {
  361. if (isShowingHeavyweightDemo)
  362. return;
  363. openGLContext.attachTo (*getTopLevelComponent());
  364. }
  365. else
  366. {
  367. openGLContext.detach();
  368. peer->setCurrentRenderingEngine (renderingEngineIndex);
  369. }
  370. currentRenderingEngineIdx = renderingEngineIndex;
  371. }