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.

289 lines
9.7KB

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