#include "ui/WindowOwner.h" #include "ui/PluginWindow.h" #include #include #include "engine/Text.h" #include "ui/I18n.h" #include "ui/Theme.h" namespace lynx { namespace { class PluginWindow final : public juce::DocumentWindow { public: PluginWindow (std::shared_ptr keepAlive, juce::AudioPluginInstance& p, const juce::String& title) : DocumentWindow (title, theme::colour::bg1, DocumentWindow::closeButton | DocumentWindow::minimiseButton), owner (std::move (keepAlive)), plugin (p) { setUsingNativeTitleBar (true); if (auto* editor = plugin.createEditorIfNeeded()) setContentOwned (editor, true); else setContentOwned (new juce::GenericAudioProcessorEditor (plugin), true); // plugin without a UI if (auto* editor = dynamic_cast (getContentComponent())) setResizable (editor->isResizable(), false); centreWithSize (getWidth(), getHeight()); setVisible (true); keepAboveMainWindow (*this); } ~PluginWindow() override { // the UI must be destroyed before the plugin (which may die together with `owner`) clearContentComponent(); } void closeButtonPressed() override; juce::AudioPluginInstance* key() const noexcept { return &plugin; } private: std::shared_ptr owner; juce::AudioPluginInstance& plugin; }; std::map>& windows() { static std::map> open; return open; } void PluginWindow::closeButtonPressed() { const auto* k = key(); // close after leaving this callback (the window deletes itself) juce::MessageManager::callAsync ([k] { windows().erase (const_cast (k)); }); } class ScanThread final : public juce::ThreadWithProgressWindow { public: ScanThread (AudioEngine& e, std::function done) : juce::ThreadWithProgressWindow (tr ("Scanning for plugins (VST, VST3 and CLAP)..."), true, true), engine (e), onDone (std::move (done)) { } // called on the UI thread when the scan finishes (or is cancelled) void threadComplete (bool) override { if (onDone) onDone (found); juce::MessageManager::callAsync ([this] { delete this; }); } void run() override { found = engine.getPluginHost().scan (engine.getPluginHost().pluginFolders(), [this] (float progress, const juce::String& file) { setProgress (progress); setStatusMessage (juce::File (file).getFileName()); return ! threadShouldExit(); }); } int found = 0; private: AudioEngine& engine; std::function onDone; }; class ProjectPluginScan final : public juce::ThreadWithProgressWindow { public: ProjectPluginScan (AudioEngine& e, std::vector u, std::function done) : juce::ThreadWithProgressWindow (tr ("Finding this project's plugins..."), true, true), engine (e), uses (std::move (u)), onDone (std::move (done)) { } void run() override { auto& host = engine.getPluginHost(); setStatusMessage (tr ("Looking in the plugin folders...")); const auto files = host.filesToScanFor (uses); host.scanFiles (files, [this] (float progress, const juce::String& file) { setProgress (progress); setStatusMessage (juce::File::createFileWithoutCheckingPath (file).getFileName()); return ! threadShouldExit(); }); } void threadComplete (bool) override { if (resumeBackground) engine.getPluginHost().startBackgroundScan(); if (onDone) onDone(); juce::MessageManager::callAsync ([this] { delete this; }); } bool resumeBackground = false; private: AudioEngine& engine; std::vector uses; std::function onDone; }; } // namespace void findProjectPlugins (AudioEngine& engine, std::vector uses, std::function then) { auto& host = engine.getPluginHost(); // quick check on the UI thread: everything already known (from the cache or FL's database) → nothing to scan const auto unknown = std::any_of (uses.begin(), uses.end(), [&host] (const FlpPluginUse& u) { return ! host.findForeign (u.plugin, u.instrument); }); if (! unknown) { then(); return; } auto* scan = new ProjectPluginScan (engine, std::move (uses), std::move (then)); // deletes itself when done if (host.backgroundScan().running) { host.cancelBackgroundScan(); // the targeted scan goes first; the full one resumes afterwards scan->resumeBackground = true; } scan->launchThread(); } namespace PluginWindows { void open (std::shared_ptr owner, juce::AudioPluginInstance& plugin, const juce::String& title) { auto& open = windows(); if (auto it = open.find (&plugin); it != open.end()) { it->second->toFront (true); return; } open[&plugin] = std::make_unique (std::move (owner), plugin, title); } void closeAll() { windows().clear(); } bool isOpen (const juce::AudioPluginInstance& plugin) { return windows().count (const_cast (&plugin)) > 0; } void close (const juce::AudioPluginInstance& plugin) { windows().erase (const_cast (&plugin)); } } // namespace PluginWindows void scanPluginsWithProgress (AudioEngine& engine, std::function onDone) { (new ScanThread (engine, std::move (onDone)))->launchThread(); // deletes itself when done } void addPluginItems (juce::PopupMenu& menu, AudioEngine& engine, bool instruments, std::function onChosen) { auto types = engine.getPluginHost().getKnownPlugins().getTypes(); std::sort (types.begin(), types.end(), [] (const auto& a, const auto& b) { return a.name.compareIgnoreCase (b.name) < 0; }); const auto& favourites = engine.getPluginHost().favourites(); int added = 0; // favourites first (Plugin manager), then all of them for (const auto favouritesPass : { true, false }) { const auto before = added; for (const auto& type : types) { if (type.isInstrument != instruments) continue; PluginRef ref; ref.id = type.createIdentifierString().toStdString(); ref.name = type.name.toStdString(); if (favouritesPass && favourites.count (ref.id) == 0) continue; menu.addItem ((favouritesPass ? juce::String::fromUTF8 ("\xe2\x98\x85 ") : juce::String()) + type.name + " (" + type.manufacturerName + ") [" + type.pluginFormatName + "]", [ref, onChosen] { onChosen (ref); }); ++added; } if (favouritesPass && added > before) menu.addSeparator(); } added = 0; for (const auto& type : types) added += type.isInstrument == instruments ? 1 : 0; if (added == 0) menu.addItem (tr ("(no plugins found; use \"Scan for plugins\")"), false, false, [] {}); menu.addSeparator(); menu.addItem (tr ("Scan for plugins (VST, VST3 and CLAP)..."), [&engine] { engine.getPluginHost().startBackgroundScan(); }); } } // namespace lynx