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.

205 lines
6.1KB

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