| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #include "ui/WindowOwner.h"
- #include "ui/PluginWindow.h"
- #include <algorithm>
- #include <map>
- #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<void> 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<juce::AudioProcessorEditor*> (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<void> owner;
- juce::AudioPluginInstance& plugin;
- };
- std::map<juce::AudioPluginInstance*, std::unique_ptr<PluginWindow>>& windows()
- {
- static std::map<juce::AudioPluginInstance*, std::unique_ptr<PluginWindow>> 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<juce::AudioPluginInstance*> (k)); });
- }
- class ScanThread final : public juce::ThreadWithProgressWindow
- {
- public:
- ScanThread (AudioEngine& e, std::function<void (int)> 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<void (int)> onDone;
- };
- class ProjectPluginScan final : public juce::ThreadWithProgressWindow
- {
- public:
- ProjectPluginScan (AudioEngine& e, std::vector<FlpPluginUse> u, std::function<void()> 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<FlpPluginUse> uses;
- std::function<void()> onDone;
- };
- } // namespace
- void findProjectPlugins (AudioEngine& engine, std::vector<FlpPluginUse> uses, std::function<void()> 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<void> 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<PluginWindow> (std::move (owner), plugin, title);
- }
- void closeAll()
- {
- windows().clear();
- }
- bool isOpen (const juce::AudioPluginInstance& plugin)
- {
- return windows().count (const_cast<juce::AudioPluginInstance*> (&plugin)) > 0;
- }
- void close (const juce::AudioPluginInstance& plugin)
- {
- windows().erase (const_cast<juce::AudioPluginInstance*> (&plugin));
- }
- } // namespace PluginWindows
- void scanPluginsWithProgress (AudioEngine& engine, std::function<void (int)> onDone)
- {
- (new ScanThread (engine, std::move (onDone)))->launchThread(); // deletes itself when done
- }
- void addPluginItems (juce::PopupMenu& menu, AudioEngine& engine, bool instruments, std::function<void (PluginRef)> 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
|