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.

283 lines
10KB

  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. #pragma once
  19. //==============================================================================
  20. class ComponentListComp : public TreePanelBase,
  21. private ActivityList::Listener
  22. {
  23. public:
  24. ComponentListComp (CompileEngineChildProcess& c)
  25. : TreePanelBase (&c.project, "compClassTreeState"),
  26. owner (c)
  27. {
  28. setName ("Components");
  29. tree.setRootItemVisible (false);
  30. tree.setMultiSelectEnabled (false);
  31. tree.setDefaultOpenness (true);
  32. setRoot (new NamespaceItem (&owner.getComponentList().globalNamespace));
  33. classListChanged (owner.getComponentList());
  34. owner.activityList.addListener (this);
  35. }
  36. ~ComponentListComp() override
  37. {
  38. saveOpenness();
  39. owner.activityList.removeListener (this);
  40. }
  41. void classListChanged (const ClassDatabase::ClassList& newClasses) override
  42. {
  43. rootItem->clearSubItems();
  44. static_cast<NamespaceItem*> (rootItem.get())->setNamespace (&newClasses.globalNamespace);
  45. }
  46. void openPreview (const ClassDatabase::Class& comp)
  47. {
  48. owner.openPreview (comp);
  49. }
  50. void showClassDeclaration (const ClassDatabase::Class& comp)
  51. {
  52. owner.handleHighlightCode (comp.getClassDeclarationRange());
  53. }
  54. private:
  55. CompileEngineChildProcess& owner;
  56. struct ClassItem;
  57. struct NamespaceItem : public JucerTreeViewBase
  58. {
  59. NamespaceItem (const ClassDatabase::Namespace* n)
  60. {
  61. setNamespace (n);
  62. }
  63. void setNamespace (const ClassDatabase::Namespace* newNamespace)
  64. {
  65. namespaceToShow = newNamespace;
  66. uniqueID = namespaceToShow != nullptr ? "ns_" + namespaceToShow->fullName : "null";
  67. refreshSubItems();
  68. }
  69. String getRenamingName() const override { return getDisplayName(); }
  70. String getDisplayName() const override { return (namespaceToShow != nullptr ? namespaceToShow->name : String()) + "::"; }
  71. void setName (const String&) override {}
  72. bool isMissing() const override { return false; }
  73. Icon getIcon() const override { return Icon (getIcons().graph, getContentColour (true)); }
  74. bool canBeSelected() const override { return true; }
  75. bool mightContainSubItems() override { return namespaceToShow != nullptr && ! namespaceToShow->isEmpty(); }
  76. String getUniqueName() const override { return uniqueID; }
  77. void addSubItems() override
  78. {
  79. if (namespaceToShow != nullptr)
  80. {
  81. Array<ClassItem*> newComps;
  82. Array<NamespaceItem*> newNamespaces;
  83. for (const auto& c : namespaceToShow->components)
  84. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  85. for(const auto& n : namespaceToShow->namespaces)
  86. {
  87. if (n.getTotalClassesAndNamespaces() < 10)
  88. createFlatItemList (n, newComps);
  89. else
  90. newNamespaces.add (new NamespaceItem (&n));
  91. }
  92. for (auto c : newComps)
  93. addSubItem (c);
  94. for (auto n : newNamespaces)
  95. addSubItem (n);
  96. }
  97. }
  98. void createFlatItemList (const ClassDatabase::Namespace& ns, Array<ClassItem*>& newComps)
  99. {
  100. for (const auto& c : ns.components)
  101. newComps.addSorted (*this, new ClassItem (c, *namespaceToShow));
  102. for (const auto& n : ns.namespaces)
  103. createFlatItemList (n, newComps);
  104. }
  105. static int compareElements (ClassItem* c1, ClassItem* c2)
  106. {
  107. return c1->comp.getName().compareIgnoreCase (c2->comp.getName());
  108. }
  109. private:
  110. const ClassDatabase::Namespace* namespaceToShow = nullptr;
  111. String uniqueID; // must be stored rather than calculated, in case the namespace obj is dangling
  112. };
  113. struct ClassItem : public JucerTreeViewBase
  114. {
  115. ClassItem (const ClassDatabase::Class& c, const ClassDatabase::Namespace& parentNS)
  116. : comp (c),
  117. displayName (comp.getName().substring (parentNS.fullName.length()))
  118. {
  119. }
  120. String getRenamingName() const override { return getDisplayName(); }
  121. String getDisplayName() const override { return displayName; }
  122. void setName (const String&) override {}
  123. bool isMissing() const override { return false; }
  124. Icon getIcon() const override { return Icon (getIcons().box, getContentColour (true)); }
  125. bool canBeSelected() const override { return true; }
  126. bool mightContainSubItems() override { return false; }
  127. String getUniqueName() const override { return comp.getName(); }
  128. int getRightHandButtonSpace() override { return canBeLaunched() ? 60 : 40; }
  129. std::unique_ptr<Component> createItemComponent() override
  130. {
  131. auto content = std::make_unique<TreeItemComponent> (*this);
  132. content->addRightHandButton (new ClassItemButton (*this, true));
  133. if (canBeLaunched())
  134. content->addRightHandButton (new ClassItemButton (*this, false));
  135. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wredundant-move")
  136. return std::move (content);
  137. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  138. }
  139. Colour getContentColour (bool isIcon) const override
  140. {
  141. auto alpha = comp.getInstantiationFlags().canBeInstantiated() ? 1.0f : 0.4f;
  142. auto& lf = ProjucerApplication::getApp().lookAndFeel;
  143. if (isSelected())
  144. return lf.findColour (defaultHighlightedTextColourId).withMultipliedAlpha (alpha);
  145. return lf.findColour (isIcon ? treeIconColourId : defaultTextColourId).withMultipliedAlpha (alpha);
  146. }
  147. bool canBeLaunched() const
  148. {
  149. return comp.getInstantiationFlags().canBeInstantiated();
  150. }
  151. void showClassDeclaration() const
  152. {
  153. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  154. clc->showClassDeclaration (comp);
  155. }
  156. void launchEditor() const
  157. {
  158. if (ComponentListComp* clc = getOwnerView()->findParentComponentOfClass<ComponentListComp>())
  159. clc->openPreview (comp);
  160. }
  161. void itemClicked (const MouseEvent&) override
  162. {
  163. if (! canBeLaunched())
  164. if (ProjectContentComponent* const pcc = getOwnerView()->findParentComponentOfClass<ProjectContentComponent>())
  165. pcc->showBubbleMessage (pcc->getLocalArea (getOwnerView(), getItemPosition (true)),
  166. "Cannot create a live view:\n" + comp.getInstantiationFlags().getReasonForUnavailability());
  167. }
  168. void itemDoubleClicked (const MouseEvent&) override
  169. {
  170. if (canBeLaunched())
  171. launchEditor();
  172. else
  173. showClassDeclaration();
  174. }
  175. struct ClassItemButton : public Button
  176. {
  177. ClassItemButton (const ClassItem& c, bool isShowCodeButton)
  178. : Button (String()), classItem (c), isShowCode (isShowCodeButton)
  179. {
  180. setMouseCursor (MouseCursor::PointingHandCursor);
  181. }
  182. void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
  183. {
  184. const Path& path = isShowCode ? getIcons().code
  185. : getIcons().play;
  186. auto colour = classItem.getContentColour (true).withAlpha (isButtonDown ? 1.0f
  187. : (isMouseOverButton ? 0.8f
  188. : 0.5f));
  189. Icon (path, colour).draw (g, getLocalBounds().reduced (getHeight() / 5).toFloat(), false);
  190. }
  191. void clicked() override
  192. {
  193. if (isShowCode)
  194. classItem.showClassDeclaration();
  195. else
  196. classItem.launchEditor();
  197. }
  198. using Button::clicked;
  199. const ClassItem& classItem;
  200. bool isShowCode;
  201. };
  202. struct ClassComponent : public Component
  203. {
  204. ClassComponent (ClassItem& item, bool canBeLaunched)
  205. {
  206. addAndMakeVisible (buttons.add (new ClassItemButton (item, true)));
  207. if (canBeLaunched)
  208. addAndMakeVisible (buttons.add (new ClassItemButton (item, false)));
  209. setInterceptsMouseClicks (false, true);
  210. }
  211. void resized() override
  212. {
  213. auto bounds = getLocalBounds();
  214. for (auto b : buttons)
  215. b->setBounds (bounds.removeFromRight (25).reduced (2));
  216. }
  217. OwnedArray<ClassItemButton> buttons;
  218. };
  219. const ClassDatabase::Class comp;
  220. String displayName;
  221. };
  222. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentListComp)
  223. };