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.

943 lines
32KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #include <JuceHeader.h>
  19. #include "MainHostWindow.h"
  20. #include "../Plugins/InternalPlugins.h"
  21. constexpr const char* scanModeKey = "pluginScanMode";
  22. class CustomPluginScanner : public KnownPluginList::CustomScanner,
  23. private ChangeListener
  24. {
  25. public:
  26. CustomPluginScanner()
  27. {
  28. if (auto* file = getAppProperties().getUserSettings())
  29. file->addChangeListener (this);
  30. changeListenerCallback (nullptr);
  31. }
  32. ~CustomPluginScanner() override
  33. {
  34. if (auto* file = getAppProperties().getUserSettings())
  35. file->removeChangeListener (this);
  36. }
  37. bool findPluginTypesFor (AudioPluginFormat& format,
  38. OwnedArray<PluginDescription>& result,
  39. const String& fileOrIdentifier) override
  40. {
  41. if (scanInProcess)
  42. {
  43. superprocess = nullptr;
  44. format.findAllTypesForFile (result, fileOrIdentifier);
  45. return true;
  46. }
  47. if (superprocess == nullptr)
  48. {
  49. superprocess = std::make_unique<Superprocess> (*this);
  50. std::unique_lock<std::mutex> lock (mutex);
  51. connectionLost = false;
  52. }
  53. MemoryBlock block;
  54. MemoryOutputStream stream { block, true };
  55. stream.writeString (format.getName());
  56. stream.writeString (fileOrIdentifier);
  57. if (superprocess->sendMessageToSlave (block))
  58. {
  59. std::unique_lock<std::mutex> lock (mutex);
  60. gotResponse = false;
  61. pluginDescription = nullptr;
  62. for (;;)
  63. {
  64. if (condvar.wait_for (lock,
  65. std::chrono::milliseconds (50),
  66. [this] { return gotResponse || shouldExit(); }))
  67. {
  68. break;
  69. }
  70. }
  71. if (shouldExit())
  72. {
  73. superprocess = nullptr;
  74. return true;
  75. }
  76. if (connectionLost)
  77. {
  78. superprocess = nullptr;
  79. return false;
  80. }
  81. if (pluginDescription != nullptr)
  82. {
  83. for (const auto* item : pluginDescription->getChildIterator())
  84. {
  85. auto desc = std::make_unique<PluginDescription>();
  86. if (desc->loadFromXml (*item))
  87. result.add (std::move (desc));
  88. }
  89. }
  90. return true;
  91. }
  92. superprocess = nullptr;
  93. return false;
  94. }
  95. void scanFinished() override
  96. {
  97. superprocess = nullptr;
  98. }
  99. private:
  100. class Superprocess : public ChildProcessMaster
  101. {
  102. public:
  103. explicit Superprocess (CustomPluginScanner& o)
  104. : owner (o)
  105. {
  106. launchSlaveProcess (File::getSpecialLocation (File::currentExecutableFile), processUID, 0, 0);
  107. }
  108. private:
  109. void handleMessageFromSlave (const MemoryBlock& mb) override
  110. {
  111. auto xml = parseXML (mb.toString());
  112. const std::lock_guard<std::mutex> lock (owner.mutex);
  113. owner.pluginDescription = std::move (xml);
  114. owner.gotResponse = true;
  115. owner.condvar.notify_one();
  116. }
  117. void handleConnectionLost() override
  118. {
  119. const std::lock_guard<std::mutex> lock (owner.mutex);
  120. owner.pluginDescription = nullptr;
  121. owner.gotResponse = true;
  122. owner.connectionLost = true;
  123. owner.condvar.notify_one();
  124. }
  125. CustomPluginScanner& owner;
  126. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Superprocess)
  127. };
  128. void changeListenerCallback (ChangeBroadcaster*) override
  129. {
  130. if (auto* file = getAppProperties().getUserSettings())
  131. scanInProcess = (file->getIntValue (scanModeKey) == 0);
  132. }
  133. std::unique_ptr<Superprocess> superprocess;
  134. std::mutex mutex;
  135. std::condition_variable condvar;
  136. std::unique_ptr<XmlElement> pluginDescription;
  137. bool gotResponse = false;
  138. bool connectionLost = false;
  139. std::atomic<bool> scanInProcess { true };
  140. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomPluginScanner)
  141. };
  142. //==============================================================================
  143. class CustomPluginListComponent : public PluginListComponent
  144. {
  145. public:
  146. CustomPluginListComponent (AudioPluginFormatManager& manager,
  147. KnownPluginList& listToRepresent,
  148. const File& pedal,
  149. PropertiesFile* props,
  150. bool async)
  151. : PluginListComponent (manager, listToRepresent, pedal, props, async)
  152. {
  153. addAndMakeVisible (validationModeLabel);
  154. addAndMakeVisible (validationModeBox);
  155. validationModeLabel.attachToComponent (&validationModeBox, true);
  156. validationModeLabel.setJustificationType (Justification::right);
  157. validationModeLabel.setSize (100, 30);
  158. auto unusedId = 1;
  159. for (const auto mode : { "In-process", "Out-of-process" })
  160. validationModeBox.addItem (mode, unusedId++);
  161. validationModeBox.setSelectedItemIndex (getAppProperties().getUserSettings()->getIntValue (scanModeKey));
  162. validationModeBox.onChange = [this]
  163. {
  164. getAppProperties().getUserSettings()->setValue (scanModeKey, validationModeBox.getSelectedItemIndex());
  165. };
  166. resized();
  167. }
  168. void resized() override
  169. {
  170. PluginListComponent::resized();
  171. const auto& buttonBounds = getOptionsButton().getBounds();
  172. validationModeBox.setBounds (buttonBounds.withWidth (130).withRightX (getWidth() - buttonBounds.getX()));
  173. }
  174. private:
  175. Label validationModeLabel { {}, "Scan mode" };
  176. ComboBox validationModeBox;
  177. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (CustomPluginListComponent)
  178. };
  179. //==============================================================================
  180. class MainHostWindow::PluginListWindow : public DocumentWindow
  181. {
  182. public:
  183. PluginListWindow (MainHostWindow& mw, AudioPluginFormatManager& pluginFormatManager)
  184. : DocumentWindow ("Available Plugins",
  185. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  186. DocumentWindow::minimiseButton | DocumentWindow::closeButton),
  187. owner (mw)
  188. {
  189. auto deadMansPedalFile = getAppProperties().getUserSettings()
  190. ->getFile().getSiblingFile ("RecentlyCrashedPluginsList");
  191. setContentOwned (new CustomPluginListComponent (pluginFormatManager,
  192. owner.knownPluginList,
  193. deadMansPedalFile,
  194. getAppProperties().getUserSettings(),
  195. true), true);
  196. setResizable (true, false);
  197. setResizeLimits (300, 400, 800, 1500);
  198. setTopLeftPosition (60, 60);
  199. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("listWindowPos"));
  200. setVisible (true);
  201. }
  202. ~PluginListWindow() override
  203. {
  204. getAppProperties().getUserSettings()->setValue ("listWindowPos", getWindowStateAsString());
  205. clearContentComponent();
  206. }
  207. void closeButtonPressed() override
  208. {
  209. owner.pluginListWindow = nullptr;
  210. }
  211. private:
  212. MainHostWindow& owner;
  213. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (PluginListWindow)
  214. };
  215. //==============================================================================
  216. MainHostWindow::MainHostWindow()
  217. : DocumentWindow (JUCEApplication::getInstance()->getApplicationName(),
  218. LookAndFeel::getDefaultLookAndFeel().findColour (ResizableWindow::backgroundColourId),
  219. DocumentWindow::allButtons)
  220. {
  221. formatManager.addDefaultFormats();
  222. formatManager.addFormat (new InternalPluginFormat());
  223. auto safeThis = SafePointer<MainHostWindow> (this);
  224. RuntimePermissions::request (RuntimePermissions::recordAudio,
  225. [safeThis] (bool granted) mutable
  226. {
  227. auto savedState = getAppProperties().getUserSettings()->getXmlValue ("audioDeviceState");
  228. safeThis->deviceManager.initialise (granted ? 256 : 0, 256, savedState.get(), true);
  229. });
  230. #if JUCE_IOS || JUCE_ANDROID
  231. setFullScreen (true);
  232. #else
  233. setResizable (true, false);
  234. setResizeLimits (500, 400, 10000, 10000);
  235. centreWithSize (800, 600);
  236. #endif
  237. knownPluginList.setCustomScanner (std::make_unique<CustomPluginScanner>());
  238. graphHolder.reset (new GraphDocumentComponent (formatManager, deviceManager, knownPluginList));
  239. setContentNonOwned (graphHolder.get(), false);
  240. restoreWindowStateFromString (getAppProperties().getUserSettings()->getValue ("mainWindowPos"));
  241. setVisible (true);
  242. InternalPluginFormat internalFormat;
  243. internalTypes = internalFormat.getAllTypes();
  244. if (auto savedPluginList = getAppProperties().getUserSettings()->getXmlValue ("pluginList"))
  245. knownPluginList.recreateFromXml (*savedPluginList);
  246. for (auto& t : internalTypes)
  247. knownPluginList.addType (t);
  248. pluginSortMethod = (KnownPluginList::SortMethod) getAppProperties().getUserSettings()
  249. ->getIntValue ("pluginSortMethod", KnownPluginList::sortByManufacturer);
  250. knownPluginList.addChangeListener (this);
  251. if (auto* g = graphHolder->graph.get())
  252. g->addChangeListener (this);
  253. addKeyListener (getCommandManager().getKeyMappings());
  254. Process::setPriority (Process::HighPriority);
  255. #if JUCE_IOS || JUCE_ANDROID
  256. graphHolder->burgerMenu.setModel (this);
  257. #else
  258. #if JUCE_MAC
  259. setMacMainMenu (this);
  260. #else
  261. setMenuBar (this);
  262. #endif
  263. #endif
  264. getCommandManager().setFirstCommandTarget (this);
  265. }
  266. MainHostWindow::~MainHostWindow()
  267. {
  268. pluginListWindow = nullptr;
  269. knownPluginList.removeChangeListener (this);
  270. if (auto* g = graphHolder->graph.get())
  271. g->removeChangeListener (this);
  272. getAppProperties().getUserSettings()->setValue ("mainWindowPos", getWindowStateAsString());
  273. clearContentComponent();
  274. #if ! (JUCE_ANDROID || JUCE_IOS)
  275. #if JUCE_MAC
  276. setMacMainMenu (nullptr);
  277. #else
  278. setMenuBar (nullptr);
  279. #endif
  280. #endif
  281. graphHolder = nullptr;
  282. }
  283. void MainHostWindow::closeButtonPressed()
  284. {
  285. tryToQuitApplication();
  286. }
  287. struct AsyncQuitRetrier : private Timer
  288. {
  289. AsyncQuitRetrier() { startTimer (500); }
  290. void timerCallback() override
  291. {
  292. stopTimer();
  293. delete this;
  294. if (auto app = JUCEApplicationBase::getInstance())
  295. app->systemRequestedQuit();
  296. }
  297. };
  298. void MainHostWindow::tryToQuitApplication()
  299. {
  300. if (graphHolder->closeAnyOpenPluginWindows())
  301. {
  302. // Really important thing to note here: if the last call just deleted any plugin windows,
  303. // we won't exit immediately - instead we'll use our AsyncQuitRetrier to let the message
  304. // loop run for another brief moment, then try again. This will give any plugins a chance
  305. // to flush any GUI events that may have been in transit before the app forces them to
  306. // be unloaded
  307. new AsyncQuitRetrier();
  308. return;
  309. }
  310. if (ModalComponentManager::getInstance()->cancelAllModalComponents())
  311. {
  312. new AsyncQuitRetrier();
  313. return;
  314. }
  315. if (graphHolder != nullptr)
  316. {
  317. auto releaseAndQuit = [this]
  318. {
  319. // Some plug-ins do not want [NSApp stop] to be called
  320. // before the plug-ins are not deallocated.
  321. graphHolder->releaseGraph();
  322. JUCEApplication::quit();
  323. };
  324. #if JUCE_ANDROID || JUCE_IOS
  325. if (graphHolder->graph->saveDocument (PluginGraph::getDefaultGraphDocumentOnMobile()))
  326. releaseAndQuit();
  327. #else
  328. SafePointer<MainHostWindow> parent { this };
  329. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent, releaseAndQuit] (FileBasedDocument::SaveResult r)
  330. {
  331. if (parent == nullptr)
  332. return;
  333. if (r == FileBasedDocument::savedOk)
  334. releaseAndQuit();
  335. });
  336. #endif
  337. return;
  338. }
  339. JUCEApplication::quit();
  340. }
  341. void MainHostWindow::changeListenerCallback (ChangeBroadcaster* changed)
  342. {
  343. if (changed == &knownPluginList)
  344. {
  345. menuItemsChanged();
  346. // save the plugin list every time it gets changed, so that if we're scanning
  347. // and it crashes, we've still saved the previous ones
  348. if (auto savedPluginList = std::unique_ptr<XmlElement> (knownPluginList.createXml()))
  349. {
  350. getAppProperties().getUserSettings()->setValue ("pluginList", savedPluginList.get());
  351. getAppProperties().saveIfNeeded();
  352. }
  353. }
  354. else if (graphHolder != nullptr && changed == graphHolder->graph.get())
  355. {
  356. auto title = JUCEApplication::getInstance()->getApplicationName();
  357. auto f = graphHolder->graph->getFile();
  358. if (f.existsAsFile())
  359. title = f.getFileName() + " - " + title;
  360. setName (title);
  361. }
  362. }
  363. StringArray MainHostWindow::getMenuBarNames()
  364. {
  365. StringArray names;
  366. names.add ("File");
  367. names.add ("Plugins");
  368. names.add ("Options");
  369. names.add ("Windows");
  370. return names;
  371. }
  372. PopupMenu MainHostWindow::getMenuForIndex (int topLevelMenuIndex, const String& /*menuName*/)
  373. {
  374. PopupMenu menu;
  375. if (topLevelMenuIndex == 0)
  376. {
  377. // "File" menu
  378. #if ! (JUCE_IOS || JUCE_ANDROID)
  379. menu.addCommandItem (&getCommandManager(), CommandIDs::newFile);
  380. menu.addCommandItem (&getCommandManager(), CommandIDs::open);
  381. #endif
  382. RecentlyOpenedFilesList recentFiles;
  383. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  384. ->getValue ("recentFilterGraphFiles"));
  385. PopupMenu recentFilesMenu;
  386. recentFiles.createPopupMenuItems (recentFilesMenu, 100, true, true);
  387. menu.addSubMenu ("Open recent file", recentFilesMenu);
  388. #if ! (JUCE_IOS || JUCE_ANDROID)
  389. menu.addCommandItem (&getCommandManager(), CommandIDs::save);
  390. menu.addCommandItem (&getCommandManager(), CommandIDs::saveAs);
  391. #endif
  392. menu.addSeparator();
  393. menu.addCommandItem (&getCommandManager(), StandardApplicationCommandIDs::quit);
  394. }
  395. else if (topLevelMenuIndex == 1)
  396. {
  397. // "Plugins" menu
  398. PopupMenu pluginsMenu;
  399. addPluginsToMenu (pluginsMenu);
  400. menu.addSubMenu ("Create Plug-in", pluginsMenu);
  401. menu.addSeparator();
  402. menu.addItem (250, "Delete All Plug-ins");
  403. }
  404. else if (topLevelMenuIndex == 2)
  405. {
  406. // "Options" menu
  407. menu.addCommandItem (&getCommandManager(), CommandIDs::showPluginListEditor);
  408. PopupMenu sortTypeMenu;
  409. sortTypeMenu.addItem (200, "List Plug-ins in Default Order", true, pluginSortMethod == KnownPluginList::defaultOrder);
  410. sortTypeMenu.addItem (201, "List Plug-ins in Alphabetical Order", true, pluginSortMethod == KnownPluginList::sortAlphabetically);
  411. sortTypeMenu.addItem (202, "List Plug-ins by Category", true, pluginSortMethod == KnownPluginList::sortByCategory);
  412. sortTypeMenu.addItem (203, "List Plug-ins by Manufacturer", true, pluginSortMethod == KnownPluginList::sortByManufacturer);
  413. sortTypeMenu.addItem (204, "List Plug-ins Based on the Directory Structure", true, pluginSortMethod == KnownPluginList::sortByFileSystemLocation);
  414. menu.addSubMenu ("Plug-in Menu Type", sortTypeMenu);
  415. menu.addSeparator();
  416. menu.addCommandItem (&getCommandManager(), CommandIDs::showAudioSettings);
  417. menu.addCommandItem (&getCommandManager(), CommandIDs::toggleDoublePrecision);
  418. if (autoScaleOptionAvailable)
  419. menu.addCommandItem (&getCommandManager(), CommandIDs::autoScalePluginWindows);
  420. menu.addSeparator();
  421. menu.addCommandItem (&getCommandManager(), CommandIDs::aboutBox);
  422. }
  423. else if (topLevelMenuIndex == 3)
  424. {
  425. menu.addCommandItem (&getCommandManager(), CommandIDs::allWindowsForward);
  426. }
  427. return menu;
  428. }
  429. void MainHostWindow::menuItemSelected (int menuItemID, int /*topLevelMenuIndex*/)
  430. {
  431. if (menuItemID == 250)
  432. {
  433. if (graphHolder != nullptr)
  434. if (auto* graph = graphHolder->graph.get())
  435. graph->clear();
  436. }
  437. #if ! (JUCE_ANDROID || JUCE_IOS)
  438. else if (menuItemID >= 100 && menuItemID < 200)
  439. {
  440. RecentlyOpenedFilesList recentFiles;
  441. recentFiles.restoreFromString (getAppProperties().getUserSettings()
  442. ->getValue ("recentFilterGraphFiles"));
  443. if (graphHolder != nullptr)
  444. {
  445. if (auto* graph = graphHolder->graph.get())
  446. {
  447. SafePointer<MainHostWindow> parent { this };
  448. graph->saveIfNeededAndUserAgreesAsync ([parent, recentFiles, menuItemID] (FileBasedDocument::SaveResult r)
  449. {
  450. if (parent == nullptr)
  451. return;
  452. if (r == FileBasedDocument::savedOk)
  453. parent->graphHolder->graph->loadFrom (recentFiles.getFile (menuItemID - 100), true);
  454. });
  455. }
  456. }
  457. }
  458. #endif
  459. else if (menuItemID >= 200 && menuItemID < 210)
  460. {
  461. if (menuItemID == 200) pluginSortMethod = KnownPluginList::defaultOrder;
  462. else if (menuItemID == 201) pluginSortMethod = KnownPluginList::sortAlphabetically;
  463. else if (menuItemID == 202) pluginSortMethod = KnownPluginList::sortByCategory;
  464. else if (menuItemID == 203) pluginSortMethod = KnownPluginList::sortByManufacturer;
  465. else if (menuItemID == 204) pluginSortMethod = KnownPluginList::sortByFileSystemLocation;
  466. getAppProperties().getUserSettings()->setValue ("pluginSortMethod", (int) pluginSortMethod);
  467. menuItemsChanged();
  468. }
  469. else
  470. {
  471. if (KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuItemID) >= 0)
  472. createPlugin (getChosenType (menuItemID), { proportionOfWidth (0.3f + Random::getSystemRandom().nextFloat() * 0.6f),
  473. proportionOfHeight (0.3f + Random::getSystemRandom().nextFloat() * 0.6f) });
  474. }
  475. }
  476. void MainHostWindow::menuBarActivated (bool isActivated)
  477. {
  478. if (isActivated && graphHolder != nullptr)
  479. graphHolder->unfocusKeyboardComponent();
  480. }
  481. void MainHostWindow::createPlugin (const PluginDescription& desc, Point<int> pos)
  482. {
  483. if (graphHolder != nullptr)
  484. graphHolder->createNewPlugin (desc, pos);
  485. }
  486. void MainHostWindow::addPluginsToMenu (PopupMenu& m)
  487. {
  488. if (graphHolder != nullptr)
  489. {
  490. int i = 0;
  491. for (auto& t : internalTypes)
  492. m.addItem (++i, t.name + " (" + t.pluginFormatName + ")");
  493. }
  494. m.addSeparator();
  495. pluginDescriptions = knownPluginList.getTypes();
  496. // This avoids showing the internal types again later on in the list
  497. pluginDescriptions.removeIf ([] (PluginDescription& desc)
  498. {
  499. return desc.pluginFormatName == InternalPluginFormat::getIdentifier();
  500. });
  501. KnownPluginList::addToMenu (m, pluginDescriptions, pluginSortMethod);
  502. }
  503. PluginDescription MainHostWindow::getChosenType (const int menuID) const
  504. {
  505. if (menuID >= 1 && menuID < (int) (1 + internalTypes.size()))
  506. return internalTypes[(size_t) (menuID - 1)];
  507. return pluginDescriptions[KnownPluginList::getIndexChosenByMenu (pluginDescriptions, menuID)];
  508. }
  509. //==============================================================================
  510. ApplicationCommandTarget* MainHostWindow::getNextCommandTarget()
  511. {
  512. return findFirstTargetParentComponent();
  513. }
  514. void MainHostWindow::getAllCommands (Array<CommandID>& commands)
  515. {
  516. // this returns the set of all commands that this target can perform..
  517. const CommandID ids[] = {
  518. #if ! (JUCE_IOS || JUCE_ANDROID)
  519. CommandIDs::newFile,
  520. CommandIDs::open,
  521. CommandIDs::save,
  522. CommandIDs::saveAs,
  523. #endif
  524. CommandIDs::showPluginListEditor,
  525. CommandIDs::showAudioSettings,
  526. CommandIDs::toggleDoublePrecision,
  527. CommandIDs::aboutBox,
  528. CommandIDs::allWindowsForward,
  529. CommandIDs::autoScalePluginWindows
  530. };
  531. commands.addArray (ids, numElementsInArray (ids));
  532. }
  533. void MainHostWindow::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  534. {
  535. const String category ("General");
  536. switch (commandID)
  537. {
  538. #if ! (JUCE_IOS || JUCE_ANDROID)
  539. case CommandIDs::newFile:
  540. result.setInfo ("New", "Creates a new filter graph file", category, 0);
  541. result.defaultKeypresses.add(KeyPress('n', ModifierKeys::commandModifier, 0));
  542. break;
  543. case CommandIDs::open:
  544. result.setInfo ("Open...", "Opens a filter graph file", category, 0);
  545. result.defaultKeypresses.add (KeyPress ('o', ModifierKeys::commandModifier, 0));
  546. break;
  547. case CommandIDs::save:
  548. result.setInfo ("Save", "Saves the current graph to a file", category, 0);
  549. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::commandModifier, 0));
  550. break;
  551. case CommandIDs::saveAs:
  552. result.setInfo ("Save As...",
  553. "Saves a copy of the current graph to a file",
  554. category, 0);
  555. result.defaultKeypresses.add (KeyPress ('s', ModifierKeys::shiftModifier | ModifierKeys::commandModifier, 0));
  556. break;
  557. #endif
  558. case CommandIDs::showPluginListEditor:
  559. result.setInfo ("Edit the List of Available Plug-ins...", {}, category, 0);
  560. result.addDefaultKeypress ('p', ModifierKeys::commandModifier);
  561. break;
  562. case CommandIDs::showAudioSettings:
  563. result.setInfo ("Change the Audio Device Settings", {}, category, 0);
  564. result.addDefaultKeypress ('a', ModifierKeys::commandModifier);
  565. break;
  566. case CommandIDs::toggleDoublePrecision:
  567. updatePrecisionMenuItem (result);
  568. break;
  569. case CommandIDs::aboutBox:
  570. result.setInfo ("About...", {}, category, 0);
  571. break;
  572. case CommandIDs::allWindowsForward:
  573. result.setInfo ("All Windows Forward", "Bring all plug-in windows forward", category, 0);
  574. result.addDefaultKeypress ('w', ModifierKeys::commandModifier);
  575. break;
  576. case CommandIDs::autoScalePluginWindows:
  577. updateAutoScaleMenuItem (result);
  578. break;
  579. default:
  580. break;
  581. }
  582. }
  583. bool MainHostWindow::perform (const InvocationInfo& info)
  584. {
  585. switch (info.commandID)
  586. {
  587. #if ! (JUCE_IOS || JUCE_ANDROID)
  588. case CommandIDs::newFile:
  589. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  590. {
  591. SafePointer<MainHostWindow> parent { this };
  592. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent] (FileBasedDocument::SaveResult r)
  593. {
  594. if (parent == nullptr)
  595. return;
  596. if (r == FileBasedDocument::savedOk)
  597. parent->graphHolder->graph->newDocument();
  598. });
  599. }
  600. break;
  601. case CommandIDs::open:
  602. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  603. {
  604. SafePointer<MainHostWindow> parent { this };
  605. graphHolder->graph->saveIfNeededAndUserAgreesAsync ([parent] (FileBasedDocument::SaveResult r)
  606. {
  607. if (parent == nullptr)
  608. return;
  609. if (r == FileBasedDocument::savedOk)
  610. parent->graphHolder->graph->loadFromUserSpecifiedFileAsync (true, [] (Result) {});
  611. });
  612. }
  613. break;
  614. case CommandIDs::save:
  615. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  616. graphHolder->graph->saveAsync (true, true, nullptr);
  617. break;
  618. case CommandIDs::saveAs:
  619. if (graphHolder != nullptr && graphHolder->graph != nullptr)
  620. graphHolder->graph->saveAsAsync ({}, true, true, true, nullptr);
  621. break;
  622. #endif
  623. case CommandIDs::showPluginListEditor:
  624. if (pluginListWindow == nullptr)
  625. pluginListWindow.reset (new PluginListWindow (*this, formatManager));
  626. pluginListWindow->toFront (true);
  627. break;
  628. case CommandIDs::showAudioSettings:
  629. showAudioSettings();
  630. break;
  631. case CommandIDs::toggleDoublePrecision:
  632. if (auto* props = getAppProperties().getUserSettings())
  633. {
  634. auto newIsDoublePrecision = ! isDoublePrecisionProcessingEnabled();
  635. props->setValue ("doublePrecisionProcessing", var (newIsDoublePrecision));
  636. ApplicationCommandInfo cmdInfo (info.commandID);
  637. updatePrecisionMenuItem (cmdInfo);
  638. menuItemsChanged();
  639. if (graphHolder != nullptr)
  640. graphHolder->setDoublePrecision (newIsDoublePrecision);
  641. }
  642. break;
  643. case CommandIDs::autoScalePluginWindows:
  644. if (auto* props = getAppProperties().getUserSettings())
  645. {
  646. auto newAutoScale = ! isAutoScalePluginWindowsEnabled();
  647. props->setValue ("autoScalePluginWindows", var (newAutoScale));
  648. ApplicationCommandInfo cmdInfo (info.commandID);
  649. updateAutoScaleMenuItem (cmdInfo);
  650. menuItemsChanged();
  651. }
  652. break;
  653. case CommandIDs::aboutBox:
  654. // TODO
  655. break;
  656. case CommandIDs::allWindowsForward:
  657. {
  658. auto& desktop = Desktop::getInstance();
  659. for (int i = 0; i < desktop.getNumComponents(); ++i)
  660. desktop.getComponent (i)->toBehind (this);
  661. break;
  662. }
  663. default:
  664. return false;
  665. }
  666. return true;
  667. }
  668. void MainHostWindow::showAudioSettings()
  669. {
  670. auto* audioSettingsComp = new AudioDeviceSelectorComponent (deviceManager,
  671. 0, 256,
  672. 0, 256,
  673. true, true,
  674. true, false);
  675. audioSettingsComp->setSize (500, 450);
  676. DialogWindow::LaunchOptions o;
  677. o.content.setOwned (audioSettingsComp);
  678. o.dialogTitle = "Audio Settings";
  679. o.componentToCentreAround = this;
  680. o.dialogBackgroundColour = getLookAndFeel().findColour (ResizableWindow::backgroundColourId);
  681. o.escapeKeyTriggersCloseButton = true;
  682. o.useNativeTitleBar = false;
  683. o.resizable = false;
  684. auto* w = o.create();
  685. auto safeThis = SafePointer<MainHostWindow> (this);
  686. w->enterModalState (true,
  687. ModalCallbackFunction::create
  688. ([safeThis] (int)
  689. {
  690. auto audioState = safeThis->deviceManager.createStateXml();
  691. getAppProperties().getUserSettings()->setValue ("audioDeviceState", audioState.get());
  692. getAppProperties().getUserSettings()->saveIfNeeded();
  693. if (safeThis->graphHolder != nullptr)
  694. if (safeThis->graphHolder->graph != nullptr)
  695. safeThis->graphHolder->graph->graph.removeIllegalConnections();
  696. }), true);
  697. }
  698. bool MainHostWindow::isInterestedInFileDrag (const StringArray&)
  699. {
  700. return true;
  701. }
  702. void MainHostWindow::fileDragEnter (const StringArray&, int, int)
  703. {
  704. }
  705. void MainHostWindow::fileDragMove (const StringArray&, int, int)
  706. {
  707. }
  708. void MainHostWindow::fileDragExit (const StringArray&)
  709. {
  710. }
  711. void MainHostWindow::filesDropped (const StringArray& files, int x, int y)
  712. {
  713. if (graphHolder != nullptr)
  714. {
  715. #if ! (JUCE_ANDROID || JUCE_IOS)
  716. File firstFile { files[0] };
  717. if (files.size() == 1 && firstFile.hasFileExtension (PluginGraph::getFilenameSuffix()))
  718. {
  719. if (auto* g = graphHolder->graph.get())
  720. {
  721. SafePointer<MainHostWindow> parent;
  722. g->saveIfNeededAndUserAgreesAsync ([parent, g, firstFile] (FileBasedDocument::SaveResult r)
  723. {
  724. if (parent == nullptr)
  725. return;
  726. if (r == FileBasedDocument::savedOk)
  727. g->loadFrom (firstFile, true);
  728. });
  729. }
  730. }
  731. else
  732. #endif
  733. {
  734. OwnedArray<PluginDescription> typesFound;
  735. knownPluginList.scanAndAddDragAndDroppedFiles (formatManager, files, typesFound);
  736. auto pos = graphHolder->getLocalPoint (this, Point<int> (x, y));
  737. for (int i = 0; i < jmin (5, typesFound.size()); ++i)
  738. if (auto* desc = typesFound.getUnchecked(i))
  739. createPlugin (*desc, pos);
  740. }
  741. }
  742. }
  743. bool MainHostWindow::isDoublePrecisionProcessingEnabled()
  744. {
  745. if (auto* props = getAppProperties().getUserSettings())
  746. return props->getBoolValue ("doublePrecisionProcessing", false);
  747. return false;
  748. }
  749. bool MainHostWindow::isAutoScalePluginWindowsEnabled()
  750. {
  751. if (auto* props = getAppProperties().getUserSettings())
  752. return props->getBoolValue ("autoScalePluginWindows", false);
  753. return false;
  754. }
  755. void MainHostWindow::updatePrecisionMenuItem (ApplicationCommandInfo& info)
  756. {
  757. info.setInfo ("Double Floating-Point Precision Rendering", {}, "General", 0);
  758. info.setTicked (isDoublePrecisionProcessingEnabled());
  759. }
  760. void MainHostWindow::updateAutoScaleMenuItem (ApplicationCommandInfo& info)
  761. {
  762. info.setInfo ("Auto-Scale Plug-in Windows", {}, "General", 0);
  763. info.setTicked (isAutoScalePluginWindowsEnabled());
  764. }