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.

328 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. #pragma once
  14. #include "../../Utility/UI/PropertyComponents/jucer_ColourPropertyComponent.h"
  15. //==============================================================================
  16. class EditorColourSchemeWindowComponent : public Component
  17. {
  18. public:
  19. EditorColourSchemeWindowComponent()
  20. {
  21. if (getAppSettings().monospacedFontNames.size() == 0)
  22. changeContent (new AppearanceEditor::FontScanPanel());
  23. else
  24. changeContent (new AppearanceEditor::EditorPanel());
  25. }
  26. void paint (Graphics& g) override
  27. {
  28. g.fillAll (findColour (backgroundColourId));
  29. }
  30. void resized() override
  31. {
  32. content->setBounds (getLocalBounds());
  33. }
  34. void changeContent (Component* newContent)
  35. {
  36. content.reset (newContent);
  37. addAndMakeVisible (newContent);
  38. content->setBounds (getLocalBounds().reduced (10));
  39. }
  40. private:
  41. std::unique_ptr<Component> content;
  42. //==============================================================================
  43. struct AppearanceEditor
  44. {
  45. struct FontScanPanel : public Component,
  46. private Timer
  47. {
  48. FontScanPanel()
  49. {
  50. fontsToScan = Font::findAllTypefaceNames();
  51. startTimer (1);
  52. }
  53. void paint (Graphics& g) override
  54. {
  55. g.fillAll (findColour (backgroundColourId));
  56. g.setFont (14.0f);
  57. g.setColour (findColour (defaultTextColourId));
  58. g.drawFittedText ("Scanning for fonts..", getLocalBounds(), Justification::centred, 2);
  59. const auto size = 30;
  60. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white, (getWidth() - size) / 2, getHeight() / 2 - 50, size, size);
  61. }
  62. void timerCallback() override
  63. {
  64. repaint();
  65. if (fontsToScan.size() == 0)
  66. {
  67. getAppSettings().monospacedFontNames = fontsFound;
  68. if (auto* owner = findParentComponentOfClass<EditorColourSchemeWindowComponent>())
  69. owner->changeContent (new EditorPanel());
  70. }
  71. else
  72. {
  73. if (isMonospacedTypeface (fontsToScan[0]))
  74. fontsFound.add (fontsToScan[0]);
  75. fontsToScan.remove (0);
  76. }
  77. }
  78. // A rather hacky trick to select only the fixed-pitch fonts..
  79. // This is unfortunately a bit slow, but will work on all platforms.
  80. static bool isMonospacedTypeface (const String& name)
  81. {
  82. const Font font (name, 20.0f, Font::plain);
  83. const auto width = font.getStringWidth ("....");
  84. return width == font.getStringWidth ("WWWW")
  85. && width == font.getStringWidth ("0000")
  86. && width == font.getStringWidth ("1111")
  87. && width == font.getStringWidth ("iiii");
  88. }
  89. StringArray fontsToScan, fontsFound;
  90. };
  91. //==============================================================================
  92. struct EditorPanel : public Component
  93. {
  94. EditorPanel()
  95. : loadButton ("Load Scheme..."),
  96. saveButton ("Save Scheme...")
  97. {
  98. rebuildProperties();
  99. addAndMakeVisible (panel);
  100. addAndMakeVisible (loadButton);
  101. addAndMakeVisible (saveButton);
  102. loadButton.onClick = [this] { loadScheme(); };
  103. saveButton.onClick = [this] { saveScheme (false); };
  104. lookAndFeelChanged();
  105. saveSchemeState();
  106. }
  107. ~EditorPanel() override
  108. {
  109. if (hasSchemeBeenModifiedSinceSave())
  110. saveScheme (true);
  111. }
  112. void rebuildProperties()
  113. {
  114. auto& scheme = getAppSettings().appearance;
  115. Array<PropertyComponent*> props;
  116. auto fontValue = scheme.getCodeFontValue();
  117. props.add (FontNameValueSource::createProperty ("Code Editor Font", fontValue));
  118. props.add (FontSizeValueSource::createProperty ("Font Size", fontValue));
  119. const auto colourNames = scheme.getColourNames();
  120. for (int i = 0; i < colourNames.size(); ++i)
  121. props.add (new ColourPropertyComponent (nullptr, colourNames[i],
  122. scheme.getColourValue (colourNames[i]),
  123. Colours::white, false));
  124. panel.clear();
  125. panel.addProperties (props);
  126. }
  127. void resized() override
  128. {
  129. auto r = getLocalBounds();
  130. panel.setBounds (r.removeFromTop (getHeight() - 28).reduced (10, 2));
  131. loadButton.setBounds (r.removeFromLeft (getWidth() / 2).reduced (10, 1));
  132. saveButton.setBounds (r.reduced (10, 1));
  133. }
  134. private:
  135. PropertyPanel panel;
  136. TextButton loadButton, saveButton;
  137. Font codeFont;
  138. Array<var> colourValues;
  139. void saveScheme (bool isExit)
  140. {
  141. FileChooser fc ("Select a file in which to save this colour-scheme...",
  142. getAppSettings().appearance.getSchemesFolder()
  143. .getNonexistentChildFile ("Scheme", AppearanceSettings::getSchemeFileSuffix()),
  144. AppearanceSettings::getSchemeFileWildCard());
  145. if (fc.browseForFileToSave (true))
  146. {
  147. File file (fc.getResult().withFileExtension (AppearanceSettings::getSchemeFileSuffix()));
  148. getAppSettings().appearance.writeToFile (file);
  149. getAppSettings().appearance.refreshPresetSchemeList();
  150. saveSchemeState();
  151. ProjucerApplication::getApp().selectEditorColourSchemeWithName (file.getFileNameWithoutExtension());
  152. }
  153. else if (isExit)
  154. {
  155. restorePreviousScheme();
  156. }
  157. }
  158. void loadScheme()
  159. {
  160. FileChooser fc ("Please select a colour-scheme file to load...",
  161. getAppSettings().appearance.getSchemesFolder(),
  162. AppearanceSettings::getSchemeFileWildCard());
  163. if (fc.browseForFileToOpen())
  164. {
  165. if (getAppSettings().appearance.readFromFile (fc.getResult()))
  166. {
  167. rebuildProperties();
  168. saveSchemeState();
  169. }
  170. }
  171. }
  172. void lookAndFeelChanged() override
  173. {
  174. loadButton.setColour (TextButton::buttonColourId,
  175. findColour (secondaryButtonBackgroundColourId));
  176. }
  177. void saveSchemeState()
  178. {
  179. auto& appearance = getAppSettings().appearance;
  180. const auto colourNames = appearance.getColourNames();
  181. codeFont = appearance.getCodeFont();
  182. colourValues.clear();
  183. for (int i = 0; i < colourNames.size(); ++i)
  184. colourValues.add (appearance.getColourValue (colourNames[i]).getValue());
  185. }
  186. bool hasSchemeBeenModifiedSinceSave()
  187. {
  188. auto& appearance = getAppSettings().appearance;
  189. const auto colourNames = appearance.getColourNames();
  190. if (codeFont != appearance.getCodeFont())
  191. return true;
  192. for (int i = 0; i < colourNames.size(); ++i)
  193. if (colourValues[i] != appearance.getColourValue (colourNames[i]).getValue())
  194. return true;
  195. return false;
  196. }
  197. void restorePreviousScheme()
  198. {
  199. auto& appearance = getAppSettings().appearance;
  200. const auto colourNames = appearance.getColourNames();
  201. appearance.getCodeFontValue().setValue (codeFont.toString());
  202. for (int i = 0; i < colourNames.size(); ++i)
  203. appearance.getColourValue (colourNames[i]).setValue (colourValues[i]);
  204. }
  205. JUCE_DECLARE_NON_COPYABLE (EditorPanel)
  206. };
  207. //==============================================================================
  208. struct FontNameValueSource : public ValueSourceFilter
  209. {
  210. FontNameValueSource (const Value& source) : ValueSourceFilter (source) {}
  211. var getValue() const override
  212. {
  213. return Font::fromString (sourceValue.toString()).getTypefaceName();
  214. }
  215. void setValue (const var& newValue) override
  216. {
  217. auto font = Font::fromString (sourceValue.toString());
  218. font.setTypefaceName (newValue.toString().isEmpty() ? Font::getDefaultMonospacedFontName()
  219. : newValue.toString());
  220. sourceValue = font.toString();
  221. }
  222. static ChoicePropertyComponent* createProperty (const String& title, const Value& value)
  223. {
  224. auto fontNames = getAppSettings().monospacedFontNames;
  225. Array<var> values;
  226. values.add (Font::getDefaultMonospacedFontName());
  227. values.add (var());
  228. for (int i = 0; i < fontNames.size(); ++i)
  229. values.add (fontNames[i]);
  230. StringArray names;
  231. names.add ("<Default Monospaced>");
  232. names.add (String());
  233. names.addArray (getAppSettings().monospacedFontNames);
  234. return new ChoicePropertyComponent (Value (new FontNameValueSource (value)),
  235. title, names, values);
  236. }
  237. };
  238. //==============================================================================
  239. struct FontSizeValueSource : public ValueSourceFilter
  240. {
  241. FontSizeValueSource (const Value& source) : ValueSourceFilter (source) {}
  242. var getValue() const override
  243. {
  244. return Font::fromString (sourceValue.toString()).getHeight();
  245. }
  246. void setValue (const var& newValue) override
  247. {
  248. sourceValue = Font::fromString (sourceValue.toString()).withHeight (newValue).toString();
  249. }
  250. static PropertyComponent* createProperty (const String& title, const Value& value)
  251. {
  252. return new SliderPropertyComponent (Value (new FontSizeValueSource (value)),
  253. title, 5.0, 40.0, 0.1, 0.5);
  254. }
  255. };
  256. };
  257. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorColourSchemeWindowComponent)
  258. };