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.

264 lines
9.0KB

  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 "../jucer_Headers.h"
  14. #include "../jucer_Application.h"
  15. #include "jucer_StartPageComponent.h"
  16. #include "jucer_StartPageTreeHolder.h"
  17. #include "jucer_NewProjectTemplates.h"
  18. #include "jucer_ContentComponents.h"
  19. //==============================================================================
  20. struct ContentComponent : public Component
  21. {
  22. void resized() override
  23. {
  24. if (content != nullptr)
  25. content->setBounds (getLocalBounds());
  26. }
  27. void setContent (std::unique_ptr<Component>&& newContent)
  28. {
  29. if (content.get() != newContent.get())
  30. {
  31. content = std::move (newContent);
  32. addAndMakeVisible (content.get());
  33. resized();
  34. }
  35. }
  36. private:
  37. std::unique_ptr<Component> content;
  38. //==============================================================================
  39. JUCE_LEAK_DETECTOR (ContentComponent)
  40. };
  41. //==============================================================================
  42. static File findExampleFile (int dirIndex, int index)
  43. {
  44. auto dir = ProjucerApplication::getSortedExampleDirectories()[dirIndex];
  45. return ProjucerApplication::getSortedExampleFilesInDirectory (dir)[index];
  46. }
  47. static std::unique_ptr<Component> createExampleProjectsTab (ContentComponent& content, std::function<void (const File&)> cb)
  48. {
  49. StringArray exampleCategories;
  50. std::vector<StringArray> examples;
  51. for (auto& dir : ProjucerApplication::getSortedExampleDirectories())
  52. {
  53. exampleCategories.add (dir.getFileName());
  54. StringArray ex;
  55. for (auto& f : ProjucerApplication::getSortedExampleFilesInDirectory (dir))
  56. ex.add (f.getFileNameWithoutExtension());
  57. examples.push_back (ex);
  58. }
  59. if (exampleCategories.isEmpty())
  60. return nullptr;
  61. auto selectedCallback = [&, cb] (int category, int index) mutable
  62. {
  63. content.setContent (std::make_unique<ExampleComponent> (findExampleFile (category, index), cb));
  64. };
  65. return std::make_unique<StartPageTreeHolder> (exampleCategories,
  66. examples,
  67. std::move (selectedCallback),
  68. StartPageTreeHolder::Open::no);
  69. }
  70. //==============================================================================
  71. static StringArray getAllTemplateCategoryStrings()
  72. {
  73. StringArray categories;
  74. for (auto& t : NewProjectTemplates::getAllTemplates())
  75. categories.addIfNotAlreadyThere (NewProjectTemplates::getProjectCategoryString (t.category));
  76. return categories;
  77. }
  78. static std::vector<NewProjectTemplates::ProjectTemplate> getTemplatesInCategory (const String& category)
  79. {
  80. std::vector<NewProjectTemplates::ProjectTemplate> templates;
  81. for (auto& t : NewProjectTemplates::getAllTemplates())
  82. if (NewProjectTemplates::getProjectCategoryString (t.category) == category)
  83. templates.push_back (t);
  84. return templates;
  85. }
  86. static StringArray getAllTemplateNamesForCategory (const String& category)
  87. {
  88. StringArray types;
  89. for (auto& t : getTemplatesInCategory (category))
  90. types.add (t.displayName);
  91. return types;
  92. }
  93. static std::unique_ptr<Component> createProjectTemplatesTab (ContentComponent& content,
  94. std::function<void (std::unique_ptr<Project>&&)>&& cb)
  95. {
  96. auto categories = getAllTemplateCategoryStrings();
  97. std::vector<StringArray> templateNames;
  98. for (auto& c : categories)
  99. templateNames.push_back (getAllTemplateNamesForCategory (c));
  100. auto selectedCallback = [&, cb] (int category, int index)
  101. {
  102. auto categoryString = getAllTemplateCategoryStrings()[category];
  103. auto templates = getTemplatesInCategory (categoryString);
  104. content.setContent (std::make_unique<TemplateComponent> (templates[(size_t) index], std::move (cb)));
  105. };
  106. auto holder = std::make_unique<StartPageTreeHolder> (categories,
  107. templateNames,
  108. std::move (selectedCallback),
  109. StartPageTreeHolder::Open::yes);
  110. holder->setSelectedItem (categories[0], 1);
  111. return std::move (holder);
  112. }
  113. //==============================================================================
  114. struct ProjectTemplatesAndExamples : public TabbedComponent
  115. {
  116. ProjectTemplatesAndExamples (ContentComponent& c,
  117. std::function<void (std::unique_ptr<Project>&&)>&& newProjectCb,
  118. std::function<void (const File&)>&& exampleCb)
  119. : TabbedComponent (TabbedButtonBar::Orientation::TabsAtTop),
  120. content (c),
  121. exampleSelectedCallback (std::move (exampleCb))
  122. {
  123. addTab ("New Project", Colours::transparentBlack, createProjectTemplatesTab (content, std::move (newProjectCb)).release(), true);
  124. refreshExamplesTab();
  125. }
  126. void refreshExamplesTab()
  127. {
  128. auto wasOpen = (getCurrentTabIndex() == 1);
  129. removeTab (1);
  130. auto exampleTabs = createExampleProjectsTab (content, exampleSelectedCallback);
  131. addTab ("Open Example", Colours::transparentBlack, exampleTabs == nullptr ? new SetJUCEPathComponent (*this)
  132. : exampleTabs.release(),
  133. true);
  134. if (wasOpen)
  135. setCurrentTabIndex (1);
  136. }
  137. private:
  138. //==============================================================================
  139. struct SetJUCEPathComponent : public Component,
  140. private ChangeListener
  141. {
  142. explicit SetJUCEPathComponent (ProjectTemplatesAndExamples& o)
  143. : owner (o)
  144. {
  145. getGlobalProperties().addChangeListener (this);
  146. setPathButton.setButtonText ("Set path to JUCE...");
  147. setPathButton.onClick = [] { ProjucerApplication::getApp().showPathsWindow (true); };
  148. addAndMakeVisible (setPathButton);
  149. }
  150. ~SetJUCEPathComponent() override
  151. {
  152. getGlobalProperties().removeChangeListener (this);
  153. }
  154. void paint (Graphics& g) override
  155. {
  156. g.fillAll (findColour (secondaryBackgroundColourId));
  157. }
  158. void resized() override
  159. {
  160. auto bounds = getLocalBounds().reduced (5);
  161. bounds.removeFromTop (25);
  162. setPathButton.setBounds (bounds.removeFromTop (25));
  163. }
  164. private:
  165. void changeListenerCallback (ChangeBroadcaster*) override
  166. {
  167. if (isValidJUCEExamplesDirectory (ProjucerApplication::getJUCEExamplesDirectoryPathFromGlobal()))
  168. owner.refreshExamplesTab();
  169. }
  170. ProjectTemplatesAndExamples& owner;
  171. TextButton setPathButton;
  172. };
  173. ContentComponent& content;
  174. std::function<void (const File&)> exampleSelectedCallback;
  175. };
  176. //==============================================================================
  177. StartPageComponent::StartPageComponent (std::function<void (std::unique_ptr<Project>&&)>&& newProjectCb,
  178. std::function<void (const File&)>&& exampleCb)
  179. : content (std::make_unique<ContentComponent>()),
  180. tabs (std::make_unique<ProjectTemplatesAndExamples> (*content, std::move (newProjectCb), std::move (exampleCb)))
  181. {
  182. tabs->setOutline (0);
  183. addAndMakeVisible (*tabs);
  184. addAndMakeVisible (openExistingButton);
  185. openExistingButton.setCommandToTrigger (&ProjucerApplication::getCommandManager(), CommandIDs::open, true);
  186. addAndMakeVisible (*content);
  187. setSize (900, 600);
  188. }
  189. void StartPageComponent::paint (Graphics& g)
  190. {
  191. g.fillAll (findColour (backgroundColourId));
  192. }
  193. void StartPageComponent::resized()
  194. {
  195. auto bounds = getLocalBounds().reduced (10);
  196. auto tabBounds = bounds.removeFromLeft (bounds.getWidth() / 3);
  197. openExistingButton.setBounds (tabBounds.removeFromBottom (30).reduced (10, 0));
  198. tabBounds.removeFromBottom (5);
  199. tabs->setBounds (tabBounds);
  200. bounds.removeFromLeft (10);
  201. content->setBounds (bounds);
  202. }