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.

248 lines
7.4KB

  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. #pragma once
  14. //==============================================================================
  15. class ConcertinaHeader : public Component,
  16. public ChangeBroadcaster
  17. {
  18. public:
  19. ConcertinaHeader (String n, Path p)
  20. : Component (n), name (n), iconPath (p)
  21. {
  22. panelIcon = Icon (iconPath, Colours::white);
  23. nameLabel.setText (name, dontSendNotification);
  24. nameLabel.setJustificationType (Justification::centredLeft);
  25. nameLabel.setInterceptsMouseClicks (false, false);
  26. nameLabel.setColour (Label::textColourId, Colours::white);
  27. addAndMakeVisible (nameLabel);
  28. }
  29. void resized() override
  30. {
  31. auto b = getLocalBounds().toFloat();
  32. iconBounds = b.removeFromLeft (b.getHeight()).reduced (7, 7);
  33. arrowBounds = b.removeFromRight (b.getHeight());
  34. nameLabel.setBounds (b.toNearestInt());
  35. }
  36. void paint (Graphics& g) override
  37. {
  38. g.setColour (findColour (defaultButtonBackgroundColourId));
  39. g.fillRoundedRectangle (getLocalBounds().reduced (2, 3).toFloat(), 2.0f);
  40. g.setColour (Colours::white);
  41. g.fillPath (arrowPath = ProjucerLookAndFeel::getArrowPath (arrowBounds,
  42. getParentComponent()->getBoundsInParent().getY() == yPosition ? 2 : 0,
  43. true, Justification::centred));
  44. panelIcon.draw (g, iconBounds.toFloat(), false);
  45. }
  46. void mouseUp (const MouseEvent& e) override
  47. {
  48. if (! e.mouseWasDraggedSinceMouseDown())
  49. sendChangeMessage();
  50. }
  51. int direction = 0;
  52. int yPosition = 0;
  53. private:
  54. String name;
  55. Label nameLabel;
  56. Path iconPath;
  57. Icon panelIcon;
  58. Rectangle<float> arrowBounds, iconBounds;
  59. Path arrowPath;
  60. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConcertinaHeader)
  61. };
  62. //==============================================================================
  63. class FindPanel : public Component,
  64. private Timer,
  65. private FocusChangeListener
  66. {
  67. public:
  68. FindPanel (std::function<void (const String&)> cb)
  69. : callback (cb)
  70. {
  71. addAndMakeVisible (editor);
  72. editor.onTextChange = [this] { startTimer (250); };
  73. editor.onFocusLost = [this]
  74. {
  75. isFocused = false;
  76. repaint();
  77. };
  78. Desktop::getInstance().addFocusChangeListener (this);
  79. lookAndFeelChanged();
  80. }
  81. ~FindPanel() override
  82. {
  83. Desktop::getInstance().removeFocusChangeListener (this);
  84. }
  85. void paintOverChildren (Graphics& g) override
  86. {
  87. if (! isFocused)
  88. return;
  89. g.setColour (findColour (defaultHighlightColourId));
  90. Path p;
  91. p.addRoundedRectangle (getLocalBounds().reduced (2), 3.0f);
  92. g.strokePath (p, PathStrokeType (2.0f));
  93. }
  94. void resized() override
  95. {
  96. editor.setBounds (getLocalBounds().reduced (2));
  97. }
  98. private:
  99. TextEditor editor;
  100. bool isFocused = false;
  101. std::function<void (const String&)> callback;
  102. //==============================================================================
  103. void lookAndFeelChanged() override
  104. {
  105. editor.setTextToShowWhenEmpty ("Filter...", findColour (widgetTextColourId).withAlpha (0.3f));
  106. }
  107. void globalFocusChanged (Component* focusedComponent) override
  108. {
  109. if (focusedComponent == &editor)
  110. {
  111. isFocused = true;
  112. repaint();
  113. }
  114. }
  115. void timerCallback() override
  116. {
  117. stopTimer();
  118. callback (editor.getText());
  119. }
  120. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (FindPanel)
  121. };
  122. //==============================================================================
  123. class ConcertinaTreeComponent : public Component
  124. {
  125. public:
  126. ConcertinaTreeComponent (TreePanelBase* tree, bool hasAddButton = false,
  127. bool hasSettingsButton = false, bool hasFindPanel = false)
  128. : treeToDisplay (tree)
  129. {
  130. if (hasAddButton)
  131. {
  132. addButton = std::make_unique<IconButton> ("Add", getIcons().plus);
  133. addAndMakeVisible (addButton.get());
  134. addButton->onClick = [this] { showAddMenu(); };
  135. }
  136. if (hasSettingsButton)
  137. {
  138. settingsButton = std::make_unique<IconButton> ("Settings", getIcons().settings);
  139. addAndMakeVisible (settingsButton.get());
  140. settingsButton->onClick = [this] { showSettings(); };
  141. }
  142. if (hasFindPanel)
  143. {
  144. findPanel = std::make_unique<FindPanel> ([this] (const String& filter) { treeToDisplay->rootItem->setSearchFilter (filter); });
  145. addAndMakeVisible (findPanel.get());
  146. }
  147. addAndMakeVisible (treeToDisplay.get());
  148. }
  149. void resized() override
  150. {
  151. auto bounds = getLocalBounds();
  152. if (addButton != nullptr || settingsButton != nullptr || findPanel != nullptr)
  153. {
  154. auto bottomSlice = bounds.removeFromBottom (25);
  155. bottomSlice.removeFromRight (3);
  156. if (addButton != nullptr)
  157. addButton->setBounds (bottomSlice.removeFromRight (25).reduced (2));
  158. if (settingsButton != nullptr)
  159. settingsButton->setBounds (bottomSlice.removeFromRight (25).reduced (2));
  160. if (findPanel != nullptr)
  161. findPanel->setBounds (bottomSlice.reduced (2));
  162. }
  163. treeToDisplay->setBounds (bounds);
  164. }
  165. TreePanelBase* getTree() const noexcept { return treeToDisplay.get(); }
  166. private:
  167. std::unique_ptr<TreePanelBase> treeToDisplay;
  168. std::unique_ptr<IconButton> addButton, settingsButton;
  169. std::unique_ptr<FindPanel> findPanel;
  170. void showAddMenu()
  171. {
  172. auto numSelected = treeToDisplay->tree.getNumSelectedItems();
  173. if (numSelected > 1)
  174. return;
  175. if (numSelected == 0)
  176. {
  177. if (auto* root = dynamic_cast<JucerTreeViewBase*> (treeToDisplay->tree.getRootItem()))
  178. root->showPopupMenu();
  179. }
  180. else
  181. {
  182. if (auto* item = dynamic_cast<JucerTreeViewBase*> (treeToDisplay->tree.getSelectedItem (0)))
  183. item->showAddMenu();
  184. }
  185. }
  186. void showSettings()
  187. {
  188. if (auto* root = dynamic_cast<JucerTreeViewBase*> (treeToDisplay->tree.getRootItem()))
  189. {
  190. treeToDisplay->tree.clearSelectedItems();
  191. root->showDocument();
  192. }
  193. }
  194. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ConcertinaTreeComponent)
  195. };