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.

222 lines
6.4KB

  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 "DemoContentComponent.h"
  19. #include "SettingsContent.h"
  20. #include "MainComponent.h"
  21. //==============================================================================
  22. struct DemoContent final : public Component
  23. {
  24. DemoContent() noexcept {}
  25. void resized() override
  26. {
  27. if (comp != nullptr)
  28. comp->setBounds (getLocalBounds());
  29. }
  30. void setComponent (Component* newComponent)
  31. {
  32. comp.reset (newComponent);
  33. if (comp != nullptr)
  34. {
  35. addAndMakeVisible (comp.get());
  36. resized();
  37. }
  38. }
  39. Component* getComponent() const noexcept { return comp.get(); }
  40. void showHomeScreen() { setComponent (createIntroDemo()); }
  41. private:
  42. std::unique_ptr<Component> comp;
  43. };
  44. //==============================================================================
  45. #if ! (JUCE_ANDROID || JUCE_IOS)
  46. struct CodeContent final : public Component
  47. {
  48. CodeContent()
  49. {
  50. addAndMakeVisible (codeEditor);
  51. codeEditor.setReadOnly (true);
  52. codeEditor.setScrollbarThickness (8);
  53. updateLookAndFeel();
  54. }
  55. void resized() override
  56. {
  57. codeEditor.setBounds (getLocalBounds());
  58. }
  59. void setDefaultCodeContent()
  60. {
  61. document.replaceAllContent ("\n/*******************************************************************************\n"
  62. " Select one of the demos from the side panel on the left to see\n"
  63. " its code here and an instance running in the \"Demo\" tab!\n"
  64. "*******************************************************************************/\n");
  65. }
  66. void updateLookAndFeel()
  67. {
  68. auto* v4 = dynamic_cast<LookAndFeel_V4*> (&Desktop::getInstance().getDefaultLookAndFeel());
  69. if (v4 != nullptr && (v4->getCurrentColourScheme() != LookAndFeel_V4::getLightColourScheme()))
  70. codeEditor.setColourScheme (getDarkColourScheme());
  71. else
  72. codeEditor.setColourScheme (getLightColourScheme());
  73. }
  74. void lookAndFeelChanged() override
  75. {
  76. updateLookAndFeel();
  77. }
  78. CodeDocument document;
  79. CPlusPlusCodeTokeniser cppTokensier;
  80. CodeEditorComponent codeEditor { document, &cppTokensier };
  81. };
  82. #endif
  83. //==============================================================================
  84. DemoContentComponent::DemoContentComponent (Component& mainComponent, std::function<void (bool)> callback)
  85. : TabbedComponent (TabbedButtonBar::Orientation::TabsAtTop),
  86. demoChangedCallback (std::move (callback))
  87. {
  88. demoContent.reset (new DemoContent());
  89. addTab ("Demo", Colours::transparentBlack, demoContent.get(), false);
  90. #if ! (JUCE_ANDROID || JUCE_IOS)
  91. codeContent.reset (new CodeContent());
  92. addTab ("Code", Colours::transparentBlack, codeContent.get(), false);
  93. #endif
  94. addTab ("Settings", Colours::transparentBlack, new SettingsContent (dynamic_cast<MainComponent&> (mainComponent)), true);
  95. setTabBarDepth (40);
  96. updateLookAndFeel();
  97. }
  98. DemoContentComponent::~DemoContentComponent()
  99. {
  100. }
  101. void DemoContentComponent::resized()
  102. {
  103. TabbedComponent::resized();
  104. if (tabBarIndent > 0)
  105. getTabbedButtonBar().setBounds (getTabbedButtonBar().getBounds().withTrimmedLeft (tabBarIndent));
  106. }
  107. void DemoContentComponent::setDemo (const String& category, int selectedDemoIndex)
  108. {
  109. if ((currentDemoCategory == category)
  110. && (currentDemoIndex == selectedDemoIndex))
  111. return;
  112. auto demo = JUCEDemos::getCategory (category).demos[(size_t) selectedDemoIndex];
  113. #if ! (JUCE_ANDROID || JUCE_IOS)
  114. codeContent->document.replaceAllContent (trimPIP (demo.demoFile.loadFileAsString()));
  115. codeContent->codeEditor.scrollToLine (0);
  116. #endif
  117. auto* content = demo.callback();
  118. demoContent->setComponent (content);
  119. demoChangedCallback (demo.isHeavyweight);
  120. ensureDemoIsShowing();
  121. currentDemoCategory = category;
  122. currentDemoIndex = selectedDemoIndex;
  123. }
  124. bool DemoContentComponent::isShowingHomeScreen() const noexcept
  125. {
  126. return isComponentIntroDemo (demoContent->getComponent()) && getCurrentTabIndex() == 0;
  127. }
  128. void DemoContentComponent::showHomeScreen()
  129. {
  130. demoContent->showHomeScreen();
  131. #if ! (JUCE_ANDROID || JUCE_IOS)
  132. codeContent->setDefaultCodeContent();
  133. #endif
  134. demoChangedCallback (false);
  135. ensureDemoIsShowing();
  136. resized();
  137. currentDemoCategory = {};
  138. currentDemoIndex = -1;
  139. }
  140. void DemoContentComponent::clearCurrentDemo()
  141. {
  142. demoContent->setComponent (nullptr);
  143. demoChangedCallback (false);
  144. }
  145. void DemoContentComponent::updateLookAndFeel()
  146. {
  147. auto backgroundColour = findColour (ResizableWindow::backgroundColourId);
  148. for (int i = 0; i < getNumTabs(); ++i)
  149. setTabBackgroundColour (i, backgroundColour);
  150. }
  151. void DemoContentComponent::lookAndFeelChanged()
  152. {
  153. updateLookAndFeel();
  154. }
  155. String DemoContentComponent::trimPIP (const String& fileContents)
  156. {
  157. auto lines = StringArray::fromLines (fileContents);
  158. auto metadataEndIndex = lines.indexOf (" END_JUCE_PIP_METADATA");
  159. if (metadataEndIndex == -1)
  160. return fileContents;
  161. lines.removeRange (0, metadataEndIndex + 3); // account for newline and comment block end
  162. return lines.joinIntoString ("\n");
  163. }
  164. void DemoContentComponent::ensureDemoIsShowing()
  165. {
  166. if (getCurrentTabIndex() == (getNumTabs() - 1))
  167. setCurrentTabIndex (0);
  168. }