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.

454 lines
16KB

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