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.

212 lines
6.3KB

  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 : 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 : public Component
  47. {
  48. CodeContent()
  49. {
  50. addAndMakeVisible (codeEditor);
  51. codeEditor.setReadOnly (true);
  52. codeEditor.setScrollbarThickness (8);
  53. lookAndFeelChanged();
  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 lookAndFeelChanged() override
  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. CodeDocument document;
  75. CPlusPlusCodeTokeniser cppTokensier;
  76. CodeEditorComponent codeEditor { document, &cppTokensier };
  77. };
  78. #endif
  79. //==============================================================================
  80. DemoContentComponent::DemoContentComponent (Component& mainComponent, std::function<void (bool)> callback)
  81. : TabbedComponent (TabbedButtonBar::Orientation::TabsAtTop),
  82. demoChangedCallback (std::move (callback))
  83. {
  84. demoContent.reset (new DemoContent());
  85. addTab ("Demo", Colours::transparentBlack, demoContent.get(), false);
  86. #if ! (JUCE_ANDROID || JUCE_IOS)
  87. codeContent.reset (new CodeContent());
  88. addTab ("Code", Colours::transparentBlack, codeContent.get(), false);
  89. #endif
  90. addTab ("Settings", Colours::transparentBlack, new SettingsContent (dynamic_cast<MainComponent&> (mainComponent)), true);
  91. setTabBarDepth (40);
  92. lookAndFeelChanged();
  93. }
  94. DemoContentComponent::~DemoContentComponent()
  95. {
  96. }
  97. void DemoContentComponent::resized()
  98. {
  99. TabbedComponent::resized();
  100. if (tabBarIndent > 0)
  101. getTabbedButtonBar().setBounds (getTabbedButtonBar().getBounds().withTrimmedLeft (tabBarIndent));
  102. }
  103. void DemoContentComponent::setDemo (const String& category, int selectedDemoIndex)
  104. {
  105. if ((currentDemoCategory == category)
  106. && (currentDemoIndex == selectedDemoIndex))
  107. return;
  108. auto demo = JUCEDemos::getCategory (category).demos[(size_t) selectedDemoIndex];
  109. #if ! (JUCE_ANDROID || JUCE_IOS)
  110. codeContent->document.replaceAllContent (trimPIP (demo.demoFile.loadFileAsString()));
  111. codeContent->codeEditor.scrollToLine (0);
  112. #endif
  113. auto* content = demo.callback();
  114. demoContent->setComponent (content);
  115. demoChangedCallback (demo.isHeavyweight);
  116. ensureDemoIsShowing();
  117. currentDemoCategory = category;
  118. currentDemoIndex = selectedDemoIndex;
  119. }
  120. bool DemoContentComponent::isShowingHomeScreen() const noexcept
  121. {
  122. return isComponentIntroDemo (demoContent->getComponent()) && getCurrentTabIndex() == 0;
  123. }
  124. void DemoContentComponent::showHomeScreen()
  125. {
  126. demoContent->showHomeScreen();
  127. #if ! (JUCE_ANDROID || JUCE_IOS)
  128. codeContent->setDefaultCodeContent();
  129. #endif
  130. demoChangedCallback (false);
  131. ensureDemoIsShowing();
  132. resized();
  133. currentDemoCategory = {};
  134. currentDemoIndex = -1;
  135. }
  136. void DemoContentComponent::clearCurrentDemo()
  137. {
  138. demoContent->setComponent (nullptr);
  139. demoChangedCallback (false);
  140. }
  141. void DemoContentComponent::lookAndFeelChanged()
  142. {
  143. auto backgroundColour = findColour (ResizableWindow::backgroundColourId);
  144. for (int i = 0; i < getNumTabs(); ++i)
  145. setTabBackgroundColour (i, backgroundColour);
  146. }
  147. String DemoContentComponent::trimPIP (const String& fileContents)
  148. {
  149. auto lines = StringArray::fromLines (fileContents);
  150. auto metadataEndIndex = lines.indexOf (" END_JUCE_PIP_METADATA");
  151. if (metadataEndIndex == -1)
  152. return fileContents;
  153. lines.removeRange (0, metadataEndIndex + 3); // account for newline and comment block end
  154. return lines.joinIntoString ("\n");
  155. }
  156. void DemoContentComponent::ensureDemoIsShowing()
  157. {
  158. if (getCurrentTabIndex() == (getNumTabs() - 1))
  159. setCurrentTabIndex (0);
  160. }