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.

282 lines
9.4KB

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