PluginWindow.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "ui/WindowOwner.h"
  2. #include "ui/PluginWindow.h"
  3. #include <algorithm>
  4. #include <map>
  5. #include "engine/Text.h"
  6. #include "ui/I18n.h"
  7. #include "ui/Theme.h"
  8. namespace lynx
  9. {
  10. namespace
  11. {
  12. class PluginWindow final : public juce::DocumentWindow
  13. {
  14. public:
  15. PluginWindow (std::shared_ptr<void> keepAlive, juce::AudioPluginInstance& p, const juce::String& title)
  16. : DocumentWindow (title, theme::colour::bg1, DocumentWindow::closeButton | DocumentWindow::minimiseButton),
  17. owner (std::move (keepAlive)),
  18. plugin (p)
  19. {
  20. setUsingNativeTitleBar (true);
  21. if (auto* editor = plugin.createEditorIfNeeded())
  22. setContentOwned (editor, true);
  23. else
  24. setContentOwned (new juce::GenericAudioProcessorEditor (plugin), true); // plugin without a UI
  25. if (auto* editor = dynamic_cast<juce::AudioProcessorEditor*> (getContentComponent()))
  26. setResizable (editor->isResizable(), false);
  27. centreWithSize (getWidth(), getHeight());
  28. setVisible (true);
  29. keepAboveMainWindow (*this);
  30. }
  31. ~PluginWindow() override
  32. {
  33. // the UI must be destroyed before the plugin (which may die together with `owner`)
  34. clearContentComponent();
  35. }
  36. void closeButtonPressed() override;
  37. juce::AudioPluginInstance* key() const noexcept { return &plugin; }
  38. private:
  39. std::shared_ptr<void> owner;
  40. juce::AudioPluginInstance& plugin;
  41. };
  42. std::map<juce::AudioPluginInstance*, std::unique_ptr<PluginWindow>>& windows()
  43. {
  44. static std::map<juce::AudioPluginInstance*, std::unique_ptr<PluginWindow>> open;
  45. return open;
  46. }
  47. void PluginWindow::closeButtonPressed()
  48. {
  49. const auto* k = key();
  50. // close after leaving this callback (the window deletes itself)
  51. juce::MessageManager::callAsync ([k] { windows().erase (const_cast<juce::AudioPluginInstance*> (k)); });
  52. }
  53. class ScanThread final : public juce::ThreadWithProgressWindow
  54. {
  55. public:
  56. ScanThread (AudioEngine& e, std::function<void (int)> done)
  57. : juce::ThreadWithProgressWindow (tr ("Scanning for plugins (VST, VST3 and CLAP)..."), true, true),
  58. engine (e),
  59. onDone (std::move (done))
  60. {
  61. }
  62. // called on the UI thread when the scan finishes (or is cancelled)
  63. void threadComplete (bool) override
  64. {
  65. if (onDone)
  66. onDone (found);
  67. juce::MessageManager::callAsync ([this] { delete this; });
  68. }
  69. void run() override
  70. {
  71. found = engine.getPluginHost().scan (engine.getPluginHost().pluginFolders(), [this] (float progress, const juce::String& file) {
  72. setProgress (progress);
  73. setStatusMessage (juce::File (file).getFileName());
  74. return ! threadShouldExit();
  75. });
  76. }
  77. int found = 0;
  78. private:
  79. AudioEngine& engine;
  80. std::function<void (int)> onDone;
  81. };
  82. class ProjectPluginScan final : public juce::ThreadWithProgressWindow
  83. {
  84. public:
  85. ProjectPluginScan (AudioEngine& e, std::vector<FlpPluginUse> u, std::function<void()> done)
  86. : juce::ThreadWithProgressWindow (tr ("Finding this project's plugins..."), true, true),
  87. engine (e),
  88. uses (std::move (u)),
  89. onDone (std::move (done))
  90. {
  91. }
  92. void run() override
  93. {
  94. auto& host = engine.getPluginHost();
  95. setStatusMessage (tr ("Looking in the plugin folders..."));
  96. const auto files = host.filesToScanFor (uses);
  97. host.scanFiles (files, [this] (float progress, const juce::String& file) {
  98. setProgress (progress);
  99. setStatusMessage (juce::File::createFileWithoutCheckingPath (file).getFileName());
  100. return ! threadShouldExit();
  101. });
  102. }
  103. void threadComplete (bool) override
  104. {
  105. if (resumeBackground)
  106. engine.getPluginHost().startBackgroundScan();
  107. if (onDone)
  108. onDone();
  109. juce::MessageManager::callAsync ([this] { delete this; });
  110. }
  111. bool resumeBackground = false;
  112. private:
  113. AudioEngine& engine;
  114. std::vector<FlpPluginUse> uses;
  115. std::function<void()> onDone;
  116. };
  117. } // namespace
  118. void findProjectPlugins (AudioEngine& engine, std::vector<FlpPluginUse> uses, std::function<void()> then)
  119. {
  120. auto& host = engine.getPluginHost();
  121. // quick check on the UI thread: everything already known (from the cache or FL's database) → nothing to scan
  122. const auto unknown = std::any_of (uses.begin(), uses.end(), [&host] (const FlpPluginUse& u) { return ! host.findForeign (u.plugin, u.instrument); });
  123. if (! unknown)
  124. {
  125. then();
  126. return;
  127. }
  128. auto* scan = new ProjectPluginScan (engine, std::move (uses), std::move (then)); // deletes itself when done
  129. if (host.backgroundScan().running)
  130. {
  131. host.cancelBackgroundScan(); // the targeted scan goes first; the full one resumes afterwards
  132. scan->resumeBackground = true;
  133. }
  134. scan->launchThread();
  135. }
  136. namespace PluginWindows
  137. {
  138. void open (std::shared_ptr<void> owner, juce::AudioPluginInstance& plugin, const juce::String& title)
  139. {
  140. auto& open = windows();
  141. if (auto it = open.find (&plugin); it != open.end())
  142. {
  143. it->second->toFront (true);
  144. return;
  145. }
  146. open[&plugin] = std::make_unique<PluginWindow> (std::move (owner), plugin, title);
  147. }
  148. void closeAll()
  149. {
  150. windows().clear();
  151. }
  152. bool isOpen (const juce::AudioPluginInstance& plugin)
  153. {
  154. return windows().count (const_cast<juce::AudioPluginInstance*> (&plugin)) > 0;
  155. }
  156. void close (const juce::AudioPluginInstance& plugin)
  157. {
  158. windows().erase (const_cast<juce::AudioPluginInstance*> (&plugin));
  159. }
  160. } // namespace PluginWindows
  161. void scanPluginsWithProgress (AudioEngine& engine, std::function<void (int)> onDone)
  162. {
  163. (new ScanThread (engine, std::move (onDone)))->launchThread(); // deletes itself when done
  164. }
  165. void addPluginItems (juce::PopupMenu& menu, AudioEngine& engine, bool instruments, std::function<void (PluginRef)> onChosen)
  166. {
  167. auto types = engine.getPluginHost().getKnownPlugins().getTypes();
  168. std::sort (types.begin(), types.end(), [] (const auto& a, const auto& b) { return a.name.compareIgnoreCase (b.name) < 0; });
  169. const auto& favourites = engine.getPluginHost().favourites();
  170. int added = 0;
  171. // favourites first (Plugin manager), then all of them
  172. for (const auto favouritesPass : { true, false })
  173. {
  174. const auto before = added;
  175. for (const auto& type : types)
  176. {
  177. if (type.isInstrument != instruments)
  178. continue;
  179. PluginRef ref;
  180. ref.id = type.createIdentifierString().toStdString();
  181. ref.name = type.name.toStdString();
  182. if (favouritesPass && favourites.count (ref.id) == 0)
  183. continue;
  184. menu.addItem ((favouritesPass ? juce::String::fromUTF8 ("\xe2\x98\x85 ") : juce::String()) + type.name + " (" + type.manufacturerName + ") ["
  185. + type.pluginFormatName + "]",
  186. [ref, onChosen] { onChosen (ref); });
  187. ++added;
  188. }
  189. if (favouritesPass && added > before)
  190. menu.addSeparator();
  191. }
  192. added = 0;
  193. for (const auto& type : types)
  194. added += type.isInstrument == instruments ? 1 : 0;
  195. if (added == 0)
  196. menu.addItem (tr ("(no plugins found; use \"Scan for plugins\")"), false, false, [] {});
  197. menu.addSeparator();
  198. menu.addItem (tr ("Scan for plugins (VST, VST3 and CLAP)..."), [&engine] { engine.getPluginHost().startBackgroundScan(); });
  199. }
  200. } // namespace lynx