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.

195 lines
9.4KB

  1. #include "PluginSelectorComponent.h"
  2. #include "PluginUtils.h"
  3. namespace {
  4. constexpr int MARGIN_SIZE {10};
  5. constexpr int ROW_HEIGHT {24};
  6. }
  7. PluginSelectorComponent::PluginSelectorComponent(PluginSelectorListParameters selectorListParameters,
  8. std::function<void()> onCloseCallback,
  9. const SelectorComponentStyle& style)
  10. : _state(selectorListParameters.state),
  11. _onCloseCallback(onCloseCallback),
  12. _backgroundColour(style.backgroundColour) {
  13. setComponentID(Utils::pluginSelectorComponentID);
  14. // Position the header row
  15. _setupHeaderRow(style);
  16. // Position the status bar
  17. statusBar.reset(new PluginScanStatusBar(selectorListParameters.scanner, style));
  18. addAndMakeVisible(statusBar.get());
  19. statusBar->setName("Plugin Scan Status Bar");
  20. // Position the table to use the remaining space
  21. pluginTableListBox.reset(new PluginSelectorTableListBox(selectorListParameters, style));
  22. addAndMakeVisible(pluginTableListBox.get());
  23. pluginTableListBox->setName("Plugin Table List Box");
  24. pluginTableListBox->getHeader().setLookAndFeel(style.tableHeaderLookAndFeel.get());
  25. pluginTableListBox->getHeader().setColour(juce::TableHeaderComponent::textColourId, style.highlightColour);
  26. pluginTableListBox->getHeader().setColour(juce::TableHeaderComponent::outlineColourId, style.highlightColour);
  27. pluginTableListBox->getHeader().setColour(juce::TableHeaderComponent::backgroundColourId, style.backgroundColour);
  28. pluginTableListBox->getHeader().setColour(juce::TableHeaderComponent::highlightColourId, style.neutralColour);
  29. pluginTableListBox->getVerticalScrollBar().setColour(juce::ScrollBar::ColourIds::backgroundColourId, juce::Colour(0x00000000));
  30. pluginTableListBox->getVerticalScrollBar().setColour(juce::ScrollBar::ColourIds::thumbColourId, style.neutralColour.withAlpha(0.5f));
  31. pluginTableListBox->getVerticalScrollBar().setColour(juce::ScrollBar::ColourIds::trackColourId, juce::Colour(0x00000000));
  32. pluginTableListBox->getHorizontalScrollBar().setColour(juce::ScrollBar::ColourIds::backgroundColourId, juce::Colour(0x00000000));
  33. pluginTableListBox->getHorizontalScrollBar().setColour(juce::ScrollBar::ColourIds::thumbColourId, style.neutralColour.withAlpha(0.5f));
  34. pluginTableListBox->getHorizontalScrollBar().setColour(juce::ScrollBar::ColourIds::trackColourId, juce::Colour(0x00000000));
  35. // Recall UI from state
  36. searchEdt->setText(_state.filterString, false);
  37. vstBtn->setToggleState(_state.includeVST, juce::dontSendNotification);
  38. vst3Btn->setToggleState(_state.includeVST3, juce::dontSendNotification);
  39. auBtn->setToggleState(_state.includeAU, juce::dontSendNotification);
  40. }
  41. PluginSelectorComponent::~PluginSelectorComponent() {
  42. // Save the scroll position
  43. _state.scrollPosition = pluginTableListBox->getVerticalPosition();
  44. searchEdt->setLookAndFeel(nullptr);
  45. vstBtn->setLookAndFeel(nullptr);
  46. vst3Btn->setLookAndFeel(nullptr);
  47. auBtn->setLookAndFeel(nullptr);
  48. pluginTableListBox->getHeader().setLookAndFeel(nullptr);
  49. searchEdt = nullptr;
  50. vstBtn = nullptr;
  51. vst3Btn = nullptr;
  52. auBtn = nullptr;
  53. pluginTableListBox = nullptr;
  54. statusBar = nullptr;
  55. }
  56. void PluginSelectorComponent::resized() {
  57. juce::Rectangle<int> availableArea = getLocalBounds().reduced(MARGIN_SIZE);
  58. // Header
  59. juce::Rectangle<int> headerArea = availableArea.removeFromTop(ROW_HEIGHT);
  60. juce::FlexBox flexBox;
  61. flexBox.flexDirection = juce::FlexBox::Direction::row;
  62. flexBox.flexWrap = juce::FlexBox::Wrap::wrap;
  63. flexBox.justifyContent = juce::FlexBox::JustifyContent::spaceAround;
  64. flexBox.alignContent = juce::FlexBox::AlignContent::center;
  65. constexpr int MAX_BUTTONS_WIDTH {220};
  66. const int buttonsTotalWidth {
  67. getWidth() < MAX_BUTTONS_WIDTH * 2 ? getWidth() / 2 : MAX_BUTTONS_WIDTH
  68. };
  69. constexpr int buttonWidth {56};
  70. flexBox.items.add(juce::FlexItem(*vstBtn.get()).withMinWidth(buttonWidth).withMinHeight(ROW_HEIGHT));
  71. flexBox.items.add(juce::FlexItem(*vst3Btn.get()).withMinWidth(buttonWidth).withMinHeight(ROW_HEIGHT));
  72. #ifdef __APPLE__
  73. flexBox.items.add(juce::FlexItem(*auBtn.get()).withMinWidth(buttonWidth).withMinHeight(ROW_HEIGHT));
  74. #endif
  75. flexBox.performLayout(headerArea.removeFromRight(buttonsTotalWidth).toFloat());
  76. headerArea.removeFromRight(MARGIN_SIZE);
  77. searchEdt->setBounds(headerArea);
  78. availableArea.removeFromTop(MARGIN_SIZE);
  79. // Status bar
  80. constexpr int STATUS_BAR_THRESHOLD_WIDTH {PluginScanStatusBar::MIN_STATUS_WIDTH + PluginScanStatusBar::MAX_BUTTONS_WIDTH};
  81. const int statusBarHeight {availableArea.getWidth() < STATUS_BAR_THRESHOLD_WIDTH ? ROW_HEIGHT * 2 : ROW_HEIGHT};
  82. statusBar->setBounds(availableArea.removeFromBottom(statusBarHeight));
  83. availableArea.removeFromBottom(MARGIN_SIZE);
  84. // Table
  85. pluginTableListBox->setBounds(availableArea);
  86. }
  87. void PluginSelectorComponent::paint(juce::Graphics& g) {
  88. g.fillAll(_backgroundColour);
  89. }
  90. void PluginSelectorComponent::buttonClicked(juce::Button* buttonThatWasClicked) {
  91. if (buttonThatWasClicked == vstBtn.get()) {
  92. vstBtn->setToggleState(!vstBtn->getToggleState(), juce::dontSendNotification);
  93. _state.includeVST = vstBtn->getToggleState();
  94. pluginTableListBox->onFiltersOrSortUpdate();
  95. } else if (buttonThatWasClicked == vst3Btn.get()) {
  96. vst3Btn->setToggleState(!vst3Btn->getToggleState(), juce::dontSendNotification);
  97. _state.includeVST3 = vst3Btn->getToggleState();
  98. pluginTableListBox->onFiltersOrSortUpdate();
  99. } else if (buttonThatWasClicked == auBtn.get()) {
  100. auBtn->setToggleState(!auBtn->getToggleState(), juce::dontSendNotification);
  101. _state.includeAU = auBtn->getToggleState();
  102. pluginTableListBox->onFiltersOrSortUpdate();
  103. }
  104. }
  105. bool PluginSelectorComponent::keyPressed(const juce::KeyPress& key) {
  106. if (key.isKeyCode(juce::KeyPress::escapeKey)) {
  107. // Close the window
  108. _onCloseCallback();
  109. return true;
  110. }
  111. return false; // Return true if your handler uses this key event, or false to allow it to be passed-on.
  112. }
  113. void PluginSelectorComponent::textEditorTextChanged(juce::TextEditor& textEditor) {
  114. _state.filterString = searchEdt->getText();
  115. pluginTableListBox->onFiltersOrSortUpdate();
  116. }
  117. void PluginSelectorComponent::_setupHeaderRow(const SelectorComponentStyle& style) {
  118. searchEdt.reset(new juce::TextEditor("Search Text Editor"));
  119. addAndMakeVisible(searchEdt.get());
  120. searchEdt->setMultiLine(false);
  121. searchEdt->setReturnKeyStartsNewLine(false);
  122. searchEdt->setReadOnly(false);
  123. searchEdt->setScrollbarsShown(true);
  124. searchEdt->setCaretVisible(true);
  125. searchEdt->setPopupMenuEnabled(true);
  126. searchEdt->setText(juce::String());
  127. searchEdt->addListener(this);
  128. searchEdt->setEscapeAndReturnKeysConsumed(false);
  129. searchEdt->setSelectAllWhenFocused(true);
  130. searchEdt->setWantsKeyboardFocus(true);
  131. searchEdt->setLookAndFeel(style.searchBarLookAndFeel.get());
  132. searchEdt->setColour(juce::TextEditor::outlineColourId, style.highlightColour);
  133. searchEdt->setColour(juce::TextEditor::backgroundColourId, style.backgroundColour);
  134. searchEdt->setColour(juce::TextEditor::textColourId, style.highlightColour);
  135. searchEdt->setColour(juce::TextEditor::highlightColourId, style.highlightColour);
  136. searchEdt->setColour(juce::TextEditor::highlightedTextColourId, style.neutralColour);
  137. searchEdt->setColour(juce::CaretComponent::caretColourId, style.highlightColour);
  138. vstBtn.reset(new juce::TextButton("VST Button"));
  139. addAndMakeVisible(vstBtn.get());
  140. vstBtn->setButtonText(TRANS("VST"));
  141. vstBtn->addListener(this);
  142. vstBtn->setLookAndFeel(style.headerButtonLookAndFeel.get());
  143. vstBtn->setColour(UIUtils::ToggleButtonLookAndFeel::backgroundColour, style.buttonBackgroundColour);
  144. vstBtn->setColour(UIUtils::ToggleButtonLookAndFeel::highlightColour, style.highlightColour);
  145. vstBtn->setColour(UIUtils::ToggleButtonLookAndFeel::disabledColour, style.disabledColour);
  146. vst3Btn.reset(new juce::TextButton ("VST3 Button"));
  147. addAndMakeVisible(vst3Btn.get());
  148. vst3Btn->setButtonText(TRANS("VST3"));
  149. vst3Btn->addListener(this);
  150. vst3Btn->setLookAndFeel(style.headerButtonLookAndFeel.get());
  151. vst3Btn->setColour(UIUtils::ToggleButtonLookAndFeel::backgroundColour, style.buttonBackgroundColour);
  152. vst3Btn->setColour(UIUtils::ToggleButtonLookAndFeel::highlightColour, style.highlightColour);
  153. vst3Btn->setColour(UIUtils::ToggleButtonLookAndFeel::disabledColour, style.disabledColour);
  154. auBtn.reset(new juce::TextButton("AU Button"));
  155. addAndMakeVisible(auBtn.get());
  156. auBtn->setButtonText(TRANS("AU"));
  157. auBtn->addListener(this);
  158. auBtn->setLookAndFeel(style.headerButtonLookAndFeel.get());
  159. auBtn->setColour(UIUtils::ToggleButtonLookAndFeel::backgroundColour, style.buttonBackgroundColour);
  160. auBtn->setColour(UIUtils::ToggleButtonLookAndFeel::highlightColour, style.highlightColour);
  161. auBtn->setColour(UIUtils::ToggleButtonLookAndFeel::disabledColour, style.disabledColour);
  162. }
  163. void PluginSelectorComponent::restoreScrollPosition() {
  164. pluginTableListBox->setVerticalPosition(_state.scrollPosition);
  165. }