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.

375 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include "jucer_HeaderComponent.h"
  14. #include "../../Application/jucer_Application.h"
  15. #include "../../ProjectSaving/jucer_ProjectExporter.h"
  16. #include "../../Project/UI/jucer_ProjectContentComponent.h"
  17. #include "../../LiveBuildEngine/jucer_MessageIDs.h"
  18. #include "../../LiveBuildEngine/jucer_SourceCodeRange.h"
  19. #include "../../LiveBuildEngine/jucer_ClassDatabase.h"
  20. #include "../../LiveBuildEngine/jucer_DiagnosticMessage.h"
  21. #include "../../LiveBuildEngine/jucer_CompileEngineClient.h"
  22. //==============================================================================
  23. HeaderComponent::HeaderComponent (ProjectContentComponent* pcc)
  24. : projectContentComponent (pcc)
  25. {
  26. addAndMakeVisible (configLabel);
  27. addAndMakeVisible (exporterBox);
  28. exporterBox.onChange = [this] { updateExporterButton(); };
  29. juceIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize), RectanglePlacement::centred);
  30. addAndMakeVisible (juceIcon);
  31. addAndMakeVisible (userAvatar);
  32. userAvatar.addChangeListener (this);
  33. projectNameLabel.setText ({}, dontSendNotification);
  34. addAndMakeVisible (projectNameLabel);
  35. initialiseButtons();
  36. }
  37. HeaderComponent::~HeaderComponent()
  38. {
  39. if (childProcess != nullptr)
  40. {
  41. childProcess->activityList.removeChangeListener (this);
  42. childProcess->errorList.removeChangeListener (this);
  43. }
  44. }
  45. //==============================================================================
  46. void HeaderComponent::resized()
  47. {
  48. auto bounds = getLocalBounds();
  49. configLabel.setFont ({ bounds.getHeight() / 3.0f });
  50. {
  51. auto headerBounds = bounds.removeFromLeft (tabsWidth);
  52. const int buttonSize = 25;
  53. auto buttonBounds = headerBounds.removeFromRight (buttonSize);
  54. projectSettingsButton.setBounds (buttonBounds.removeFromBottom (buttonSize).reduced (2));
  55. juceIcon.setBounds (headerBounds.removeFromLeft (headerBounds.getHeight()).reduced (2));
  56. headerBounds.removeFromRight (5);
  57. projectNameLabel.setBounds (headerBounds);
  58. }
  59. {
  60. auto exporterWidth = jmin (400, bounds.getWidth() / 2);
  61. Rectangle<int> exporterBounds (0, 0, exporterWidth, bounds.getHeight());
  62. exporterBounds.setCentre (bounds.getCentre());
  63. runAppButton.setBounds (exporterBounds.removeFromRight (exporterBounds.getHeight()).reduced (2));
  64. saveAndOpenInIDEButton.setBounds (exporterBounds.removeFromRight (exporterBounds.getHeight()).reduced (2));
  65. exporterBounds.removeFromRight (5);
  66. exporterBox.setBounds (exporterBounds.removeFromBottom (roundToInt (exporterBounds.getHeight() / 1.8f)));
  67. configLabel.setBounds (exporterBounds);
  68. }
  69. userAvatar.setBounds (bounds.removeFromRight (userAvatar.isDisplaingGPLLogo() ? roundToInt (bounds.getHeight() * 1.9f)
  70. : bounds.getHeight()).reduced (2));
  71. }
  72. void HeaderComponent::paint (Graphics& g)
  73. {
  74. g.fillAll (findColour (backgroundColourId));
  75. if (isBuilding)
  76. getLookAndFeel().drawSpinningWaitAnimation (g, findColour (treeIconColourId),
  77. runAppButton.getX(), runAppButton.getY(),
  78. runAppButton.getWidth(), runAppButton.getHeight());
  79. }
  80. //==============================================================================
  81. void HeaderComponent::setCurrentProject (Project* newProject)
  82. {
  83. isBuilding = false;
  84. stopTimer();
  85. repaint();
  86. projectNameLabel.setText ({}, dontSendNotification);
  87. project = newProject;
  88. if (project != nullptr)
  89. {
  90. exportersTree = project->getExporters();
  91. exportersTree.addListener (this);
  92. updateExporters();
  93. projectNameValue.referTo (project->getProjectValue (Ids::name));
  94. projectNameValue.addListener (this);
  95. updateName();
  96. childProcess = ProjucerApplication::getApp().childProcessCache->getExisting (*project);
  97. if (childProcess != nullptr)
  98. {
  99. childProcess->activityList.addChangeListener (this);
  100. childProcess->errorList.addChangeListener (this);
  101. runAppButton.setTooltip ({});
  102. runAppButton.setEnabled (true);
  103. }
  104. else
  105. {
  106. runAppButton.setTooltip ("Enable live-build engine to launch application");
  107. runAppButton.setEnabled (false);
  108. }
  109. }
  110. }
  111. //==============================================================================
  112. void HeaderComponent::updateExporters()
  113. {
  114. auto selectedExporter = getSelectedExporter();
  115. exporterBox.clear();
  116. auto preferredExporterIndex = -1;
  117. int i = 0;
  118. for (Project::ExporterIterator exporter (*project); exporter.next(); ++i)
  119. {
  120. exporterBox.addItem (exporter->getName(), i + 1);
  121. if (selectedExporter != nullptr && exporter->getName() == selectedExporter->getName())
  122. exporterBox.setSelectedId (i + 1);
  123. if (exporter->getName().contains (ProjectExporter::getCurrentPlatformExporterName()) && preferredExporterIndex == -1)
  124. preferredExporterIndex = i;
  125. }
  126. if (exporterBox.getSelectedItemIndex() == -1)
  127. {
  128. if (preferredExporterIndex == -1)
  129. {
  130. i = 0;
  131. for (Project::ExporterIterator exporter (*project); exporter.next(); ++i)
  132. {
  133. if (exporter->canLaunchProject())
  134. {
  135. preferredExporterIndex = i;
  136. break;
  137. }
  138. }
  139. }
  140. exporterBox.setSelectedItemIndex (preferredExporterIndex != -1 ? preferredExporterIndex : 0);
  141. }
  142. updateExporterButton();
  143. }
  144. std::unique_ptr<ProjectExporter> HeaderComponent::getSelectedExporter() const
  145. {
  146. if (project != nullptr)
  147. {
  148. int i = 0;
  149. auto selectedIndex = exporterBox.getSelectedItemIndex();
  150. for (Project::ExporterIterator exporter (*project); exporter.next();)
  151. if (i++ == selectedIndex)
  152. return std::move (exporter.exporter);
  153. }
  154. return nullptr;
  155. }
  156. bool HeaderComponent::canCurrentExporterLaunchProject() const
  157. {
  158. if (project != nullptr)
  159. {
  160. if (auto selectedExporter = getSelectedExporter())
  161. {
  162. for (Project::ExporterIterator exporter (*project); exporter.next();)
  163. if (exporter->canLaunchProject() && exporter->getName() == selectedExporter->getName())
  164. return true;
  165. }
  166. }
  167. return false;
  168. }
  169. //==============================================================================
  170. void HeaderComponent::sidebarTabsWidthChanged (int newWidth)
  171. {
  172. tabsWidth = newWidth;
  173. resized();
  174. }
  175. void HeaderComponent::liveBuildEnablementChanged (bool isEnabled)
  176. {
  177. runAppButton.setVisible (isEnabled);
  178. }
  179. //==============================================================================
  180. void HeaderComponent::changeListenerCallback (ChangeBroadcaster* source)
  181. {
  182. if (source == &userAvatar)
  183. {
  184. resized();
  185. }
  186. else if (childProcess != nullptr && source == &childProcess->activityList)
  187. {
  188. if (childProcess->activityList.getNumActivities() > 0)
  189. buildPing();
  190. else
  191. buildFinished (childProcess->errorList.getNumErrors() == 0);
  192. }
  193. }
  194. void HeaderComponent::valueChanged (Value&)
  195. {
  196. updateName();
  197. }
  198. void HeaderComponent::timerCallback()
  199. {
  200. repaint();
  201. }
  202. //==============================================================================
  203. void HeaderComponent::initialiseButtons()
  204. {
  205. addAndMakeVisible (projectSettingsButton);
  206. projectSettingsButton.onClick = [this] { projectContentComponent->showProjectSettings(); };
  207. addAndMakeVisible (saveAndOpenInIDEButton);
  208. saveAndOpenInIDEButton.setBackgroundColour (Colours::white);
  209. saveAndOpenInIDEButton.setIconInset (7);
  210. saveAndOpenInIDEButton.onClick = [this]
  211. {
  212. if (project != nullptr)
  213. {
  214. if (project->hasIncompatibleLicenseTypeAndSplashScreenSetting())
  215. {
  216. auto child = project->getProjectMessages().getChildWithName (ProjectMessages::Ids::warning)
  217. .getChildWithName (ProjectMessages::Ids::incompatibleLicense);
  218. if (child.isValid())
  219. child.setProperty (ProjectMessages::Ids::isVisible, true, nullptr);
  220. }
  221. else
  222. {
  223. if (auto exporter = getSelectedExporter())
  224. project->openProjectInIDE (*exporter, true);
  225. }
  226. }
  227. };
  228. addAndMakeVisible (runAppButton);
  229. runAppButton.setIconInset (7);
  230. runAppButton.onClick = [this]
  231. {
  232. if (childProcess != nullptr)
  233. childProcess->launchApp();
  234. };
  235. updateExporterButton();
  236. }
  237. void HeaderComponent::updateName()
  238. {
  239. if (project != nullptr)
  240. projectNameLabel.setText (project->getDocumentTitle(), dontSendNotification);
  241. }
  242. void HeaderComponent::updateExporterButton()
  243. {
  244. if (auto selectedExporter = getSelectedExporter())
  245. {
  246. auto selectedName = selectedExporter->getName();
  247. for (auto info : ProjectExporter::getExporterTypes())
  248. {
  249. if (selectedName.contains (info.name))
  250. {
  251. saveAndOpenInIDEButton.setImage (info.getIcon());
  252. saveAndOpenInIDEButton.repaint();
  253. saveAndOpenInIDEButton.setEnabled (canCurrentExporterLaunchProject());
  254. }
  255. }
  256. }
  257. }
  258. //==============================================================================
  259. void HeaderComponent::buildPing()
  260. {
  261. if (! isTimerRunning())
  262. {
  263. isBuilding = true;
  264. runAppButton.setEnabled (false);
  265. runAppButton.setTooltip ("Building...");
  266. startTimer (50);
  267. }
  268. }
  269. void HeaderComponent::buildFinished (bool success)
  270. {
  271. stopTimer();
  272. isBuilding = false;
  273. repaint();
  274. setRunAppButtonState (success);
  275. }
  276. void HeaderComponent::setRunAppButtonState (bool buildWasSuccessful)
  277. {
  278. bool shouldEnableButton = false;
  279. if (buildWasSuccessful)
  280. {
  281. if (childProcess != nullptr)
  282. {
  283. if (childProcess->isAppRunning() || (! childProcess->isAppRunning() && childProcess->canLaunchApp()))
  284. {
  285. runAppButton.setTooltip ("Launch application");
  286. shouldEnableButton = true;
  287. }
  288. else
  289. {
  290. runAppButton.setTooltip ("Application can't be launched");
  291. }
  292. }
  293. else
  294. {
  295. runAppButton.setTooltip ("Enable live-build engine to launch application");
  296. }
  297. }
  298. else
  299. {
  300. runAppButton.setTooltip ("Error building application");
  301. }
  302. runAppButton.setEnabled (shouldEnableButton);
  303. }