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.

377 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. auto exporterName = exporter->getUniqueName();
  121. exporterBox.addItem (exporterName, i + 1);
  122. if (selectedExporter != nullptr && exporterName == selectedExporter->getUniqueName())
  123. exporterBox.setSelectedId (i + 1);
  124. if (exporterName.contains (ProjectExporter::getCurrentPlatformExporterTypeInfo().displayName) && preferredExporterIndex == -1)
  125. preferredExporterIndex = i;
  126. }
  127. if (exporterBox.getSelectedItemIndex() == -1)
  128. {
  129. if (preferredExporterIndex == -1)
  130. {
  131. i = 0;
  132. for (Project::ExporterIterator exporter (*project); exporter.next(); ++i)
  133. {
  134. if (exporter->canLaunchProject())
  135. {
  136. preferredExporterIndex = i;
  137. break;
  138. }
  139. }
  140. }
  141. exporterBox.setSelectedItemIndex (preferredExporterIndex != -1 ? preferredExporterIndex : 0);
  142. }
  143. updateExporterButton();
  144. }
  145. std::unique_ptr<ProjectExporter> HeaderComponent::getSelectedExporter() const
  146. {
  147. if (project != nullptr)
  148. {
  149. int i = 0;
  150. auto selectedIndex = exporterBox.getSelectedItemIndex();
  151. for (Project::ExporterIterator exporter (*project); exporter.next();)
  152. if (i++ == selectedIndex)
  153. return std::move (exporter.exporter);
  154. }
  155. return nullptr;
  156. }
  157. bool HeaderComponent::canCurrentExporterLaunchProject() const
  158. {
  159. if (project != nullptr)
  160. {
  161. if (auto selectedExporter = getSelectedExporter())
  162. {
  163. for (Project::ExporterIterator exporter (*project); exporter.next();)
  164. if (exporter->canLaunchProject() && exporter->getUniqueName() == selectedExporter->getUniqueName())
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. //==============================================================================
  171. void HeaderComponent::sidebarTabsWidthChanged (int newWidth)
  172. {
  173. tabsWidth = newWidth;
  174. resized();
  175. }
  176. void HeaderComponent::liveBuildEnablementChanged (bool isEnabled)
  177. {
  178. runAppButton.setVisible (isEnabled);
  179. }
  180. //==============================================================================
  181. void HeaderComponent::changeListenerCallback (ChangeBroadcaster* source)
  182. {
  183. if (source == &userAvatar)
  184. {
  185. resized();
  186. }
  187. else if (childProcess != nullptr && source == &childProcess->activityList)
  188. {
  189. if (childProcess->activityList.getNumActivities() > 0)
  190. buildPing();
  191. else
  192. buildFinished (childProcess->errorList.getNumErrors() == 0);
  193. }
  194. }
  195. void HeaderComponent::valueChanged (Value&)
  196. {
  197. updateName();
  198. }
  199. void HeaderComponent::timerCallback()
  200. {
  201. repaint();
  202. }
  203. //==============================================================================
  204. void HeaderComponent::initialiseButtons()
  205. {
  206. addAndMakeVisible (projectSettingsButton);
  207. projectSettingsButton.onClick = [this] { projectContentComponent->showProjectSettings(); };
  208. addAndMakeVisible (saveAndOpenInIDEButton);
  209. saveAndOpenInIDEButton.setBackgroundColour (Colours::white);
  210. saveAndOpenInIDEButton.setIconInset (7);
  211. saveAndOpenInIDEButton.onClick = [this]
  212. {
  213. if (project != nullptr)
  214. {
  215. if (project->hasIncompatibleLicenseTypeAndSplashScreenSetting())
  216. {
  217. auto child = project->getProjectMessages().getChildWithName (ProjectMessages::Ids::warning)
  218. .getChildWithName (ProjectMessages::Ids::incompatibleLicense);
  219. if (child.isValid())
  220. child.setProperty (ProjectMessages::Ids::isVisible, true, nullptr);
  221. }
  222. else
  223. {
  224. if (auto exporter = getSelectedExporter())
  225. project->openProjectInIDE (*exporter, true);
  226. }
  227. }
  228. };
  229. addAndMakeVisible (runAppButton);
  230. runAppButton.setIconInset (7);
  231. runAppButton.onClick = [this]
  232. {
  233. if (childProcess != nullptr)
  234. childProcess->launchApp();
  235. };
  236. updateExporterButton();
  237. }
  238. void HeaderComponent::updateName()
  239. {
  240. if (project != nullptr)
  241. projectNameLabel.setText (project->getDocumentTitle(), dontSendNotification);
  242. }
  243. void HeaderComponent::updateExporterButton()
  244. {
  245. if (auto selectedExporter = getSelectedExporter())
  246. {
  247. auto selectedName = selectedExporter->getUniqueName();
  248. for (auto info : ProjectExporter::getExporterTypeInfos())
  249. {
  250. if (selectedName.contains (info.displayName))
  251. {
  252. saveAndOpenInIDEButton.setImage (info.icon);
  253. saveAndOpenInIDEButton.repaint();
  254. saveAndOpenInIDEButton.setEnabled (canCurrentExporterLaunchProject());
  255. }
  256. }
  257. }
  258. }
  259. //==============================================================================
  260. void HeaderComponent::buildPing()
  261. {
  262. if (! isTimerRunning())
  263. {
  264. isBuilding = true;
  265. runAppButton.setEnabled (false);
  266. runAppButton.setTooltip ("Building...");
  267. startTimer (50);
  268. }
  269. }
  270. void HeaderComponent::buildFinished (bool success)
  271. {
  272. stopTimer();
  273. isBuilding = false;
  274. repaint();
  275. setRunAppButtonState (success);
  276. }
  277. void HeaderComponent::setRunAppButtonState (bool buildWasSuccessful)
  278. {
  279. bool shouldEnableButton = false;
  280. if (buildWasSuccessful)
  281. {
  282. if (childProcess != nullptr)
  283. {
  284. if (childProcess->isAppRunning() || (! childProcess->isAppRunning() && childProcess->canLaunchApp()))
  285. {
  286. runAppButton.setTooltip ("Launch application");
  287. shouldEnableButton = true;
  288. }
  289. else
  290. {
  291. runAppButton.setTooltip ("Application can't be launched");
  292. }
  293. }
  294. else
  295. {
  296. runAppButton.setTooltip ("Enable live-build engine to launch application");
  297. }
  298. }
  299. else
  300. {
  301. runAppButton.setTooltip ("Error building application");
  302. }
  303. runAppButton.setEnabled (shouldEnableButton);
  304. }