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.

273 lines
8.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. //==============================================================================
  18. HeaderComponent::HeaderComponent (ProjectContentComponent* pcc)
  19. : projectContentComponent (pcc)
  20. {
  21. setTitle ("Header");
  22. setFocusContainerType (FocusContainerType::focusContainer);
  23. addAndMakeVisible (configLabel);
  24. addAndMakeVisible (exporterBox);
  25. exporterBox.onChange = [this] { updateExporterButton(); };
  26. juceIcon.setImage (ImageCache::getFromMemory (BinaryData::juce_icon_png, BinaryData::juce_icon_pngSize), RectanglePlacement::centred);
  27. addAndMakeVisible (juceIcon);
  28. addAndMakeVisible (userAvatar);
  29. userAvatar.addChangeListener (this);
  30. projectNameLabel.setText ({}, dontSendNotification);
  31. addAndMakeVisible (projectNameLabel);
  32. initialiseButtons();
  33. }
  34. //==============================================================================
  35. void HeaderComponent::resized()
  36. {
  37. auto bounds = getLocalBounds();
  38. configLabel.setFont ({ (float) bounds.getHeight() / 3.0f });
  39. {
  40. auto headerBounds = bounds.removeFromLeft (tabsWidth);
  41. const int buttonSize = 25;
  42. auto buttonBounds = headerBounds.removeFromRight (buttonSize);
  43. projectSettingsButton.setBounds (buttonBounds.removeFromBottom (buttonSize).reduced (2));
  44. juceIcon.setBounds (headerBounds.removeFromLeft (headerBounds.getHeight()).reduced (2));
  45. headerBounds.removeFromRight (5);
  46. projectNameLabel.setBounds (headerBounds);
  47. }
  48. {
  49. auto exporterWidth = jmin (400, bounds.getWidth() / 2);
  50. Rectangle<int> exporterBounds (0, 0, exporterWidth, bounds.getHeight());
  51. exporterBounds.setCentre (bounds.getCentre());
  52. saveAndOpenInIDEButton.setBounds (exporterBounds.removeFromRight (exporterBounds.getHeight()).reduced (2));
  53. exporterBounds.removeFromRight (5);
  54. exporterBox.setBounds (exporterBounds.removeFromBottom (roundToInt ((float) exporterBounds.getHeight() / 1.8f)));
  55. configLabel.setBounds (exporterBounds);
  56. }
  57. userAvatar.setBounds (bounds.removeFromRight (userAvatar.isDisplaingGPLLogo() ? roundToInt ((float) bounds.getHeight() * 1.9f)
  58. : bounds.getHeight()).reduced (2));
  59. }
  60. void HeaderComponent::paint (Graphics& g)
  61. {
  62. g.fillAll (findColour (backgroundColourId));
  63. }
  64. //==============================================================================
  65. void HeaderComponent::setCurrentProject (Project* newProject)
  66. {
  67. stopTimer();
  68. repaint();
  69. projectNameLabel.setText ({}, dontSendNotification);
  70. project = newProject;
  71. if (project != nullptr)
  72. {
  73. exportersTree = project->getExporters();
  74. exportersTree.addListener (this);
  75. updateExporters();
  76. projectNameValue.referTo (project->getProjectValue (Ids::name));
  77. projectNameValue.addListener (this);
  78. updateName();
  79. }
  80. }
  81. //==============================================================================
  82. void HeaderComponent::updateExporters()
  83. {
  84. auto selectedExporter = getSelectedExporter();
  85. exporterBox.clear();
  86. auto preferredExporterIndex = -1;
  87. int i = 0;
  88. for (Project::ExporterIterator exporter (*project); exporter.next(); ++i)
  89. {
  90. auto exporterName = exporter->getUniqueName();
  91. exporterBox.addItem (exporterName, i + 1);
  92. if (selectedExporter != nullptr && exporterName == selectedExporter->getUniqueName())
  93. exporterBox.setSelectedId (i + 1);
  94. if (exporterName.contains (ProjectExporter::getCurrentPlatformExporterTypeInfo().displayName) && preferredExporterIndex == -1)
  95. preferredExporterIndex = i;
  96. }
  97. if (exporterBox.getSelectedItemIndex() == -1)
  98. {
  99. if (preferredExporterIndex == -1)
  100. {
  101. i = 0;
  102. for (Project::ExporterIterator exporter (*project); exporter.next(); ++i)
  103. {
  104. if (exporter->canLaunchProject())
  105. {
  106. preferredExporterIndex = i;
  107. break;
  108. }
  109. }
  110. }
  111. exporterBox.setSelectedItemIndex (preferredExporterIndex != -1 ? preferredExporterIndex : 0);
  112. }
  113. updateExporterButton();
  114. }
  115. std::unique_ptr<ProjectExporter> HeaderComponent::getSelectedExporter() const
  116. {
  117. if (project != nullptr)
  118. {
  119. int i = 0;
  120. auto selectedIndex = exporterBox.getSelectedItemIndex();
  121. for (Project::ExporterIterator exporter (*project); exporter.next();)
  122. if (i++ == selectedIndex)
  123. return std::move (exporter.exporter);
  124. }
  125. return nullptr;
  126. }
  127. bool HeaderComponent::canCurrentExporterLaunchProject() const
  128. {
  129. if (project != nullptr)
  130. {
  131. if (auto selectedExporter = getSelectedExporter())
  132. {
  133. for (Project::ExporterIterator exporter (*project); exporter.next();)
  134. if (exporter->canLaunchProject() && exporter->getUniqueName() == selectedExporter->getUniqueName())
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. //==============================================================================
  141. void HeaderComponent::sidebarTabsWidthChanged (int newWidth)
  142. {
  143. tabsWidth = newWidth;
  144. resized();
  145. }
  146. //==============================================================================
  147. void HeaderComponent::changeListenerCallback (ChangeBroadcaster* source)
  148. {
  149. if (source == &userAvatar)
  150. resized();
  151. }
  152. void HeaderComponent::valueChanged (Value&)
  153. {
  154. updateName();
  155. }
  156. void HeaderComponent::timerCallback()
  157. {
  158. repaint();
  159. }
  160. //==============================================================================
  161. void HeaderComponent::initialiseButtons()
  162. {
  163. addAndMakeVisible (projectSettingsButton);
  164. projectSettingsButton.onClick = [this] { projectContentComponent->showProjectSettings(); };
  165. addAndMakeVisible (saveAndOpenInIDEButton);
  166. saveAndOpenInIDEButton.setBackgroundColour (Colours::white);
  167. saveAndOpenInIDEButton.setIconInset (7);
  168. saveAndOpenInIDEButton.onClick = [this]
  169. {
  170. if (project == nullptr)
  171. return;
  172. if (! project->isSaveAndExportDisabled())
  173. {
  174. projectContentComponent->openInSelectedIDE (true);
  175. return;
  176. }
  177. auto setWarningVisible = [this] (const Identifier& identifier)
  178. {
  179. auto child = project->getProjectMessages().getChildWithName (ProjectMessages::Ids::warning)
  180. .getChildWithName (identifier);
  181. if (child.isValid())
  182. child.setProperty (ProjectMessages::Ids::isVisible, true, nullptr);
  183. };
  184. if (project->hasIncompatibleLicenseTypeAndSplashScreenSetting())
  185. setWarningVisible (ProjectMessages::Ids::incompatibleLicense);
  186. if (project->isFileModificationCheckPending())
  187. setWarningVisible (ProjectMessages::Ids::jucerFileModified);
  188. };
  189. updateExporterButton();
  190. }
  191. void HeaderComponent::updateName()
  192. {
  193. if (project != nullptr)
  194. projectNameLabel.setText (project->getDocumentTitle(), dontSendNotification);
  195. }
  196. void HeaderComponent::updateExporterButton()
  197. {
  198. if (auto selectedExporter = getSelectedExporter())
  199. {
  200. auto selectedName = selectedExporter->getUniqueName();
  201. for (auto info : ProjectExporter::getExporterTypeInfos())
  202. {
  203. if (selectedName.contains (info.displayName))
  204. {
  205. saveAndOpenInIDEButton.setImage (info.icon);
  206. saveAndOpenInIDEButton.repaint();
  207. saveAndOpenInIDEButton.setEnabled (canCurrentExporterLaunchProject());
  208. }
  209. }
  210. }
  211. }