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.

158 lines
5.0KB

  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. #pragma once
  19. #include "../../Application/jucer_Application.h"
  20. //==============================================================================
  21. class UserAvatarComponent : public Component,
  22. public SettableTooltipClient,
  23. public ChangeBroadcaster,
  24. private LicenseController::LicenseStateListener
  25. {
  26. public:
  27. UserAvatarComponent (bool isInteractive)
  28. : interactive (isInteractive)
  29. {
  30. ProjucerApplication::getApp().getLicenseController().addListener (this);
  31. lookAndFeelChanged();
  32. }
  33. ~UserAvatarComponent() override
  34. {
  35. ProjucerApplication::getApp().getLicenseController().removeListener (this);
  36. }
  37. void paint (Graphics& g) override
  38. {
  39. auto bounds = getLocalBounds();
  40. if (! isGPL)
  41. {
  42. bounds = bounds.removeFromRight (bounds.getHeight());
  43. Path ellipse;
  44. ellipse.addEllipse (bounds.toFloat());
  45. g.reduceClipRegion (ellipse);
  46. }
  47. g.drawImage (currentAvatar, bounds.toFloat(), RectanglePlacement::fillDestination);
  48. }
  49. void mouseUp (const MouseEvent&) override
  50. {
  51. if (interactive)
  52. {
  53. PopupMenu menu;
  54. menu.addCommandItem (ProjucerApplication::getApp().commandManager.get(), CommandIDs::loginLogout);
  55. menu.showMenuAsync (PopupMenu::Options().withTargetComponent (this));
  56. }
  57. }
  58. bool isDisplaingGPLLogo() const noexcept { return isGPL; }
  59. private:
  60. //==============================================================================
  61. static Image createGPLAvatarImage()
  62. {
  63. if (auto logo = Drawable::createFromImageData (BinaryData::gpl_logo_svg, BinaryData::gpl_logo_svgSize))
  64. {
  65. auto bounds = logo->getDrawableBounds();
  66. Image image (Image::ARGB, roundToInt (bounds.getWidth()), roundToInt (bounds.getHeight()), true);
  67. Graphics g (image);
  68. logo->draw (g, 1.0f);
  69. return image;
  70. }
  71. jassertfalse;
  72. return {};
  73. }
  74. Image createStandardAvatarImage()
  75. {
  76. Image image (Image::ARGB, 250, 250, true);
  77. Graphics g (image);
  78. g.setColour (findColour (defaultButtonBackgroundColourId));
  79. g.fillAll();
  80. g.setColour (findColour (defaultIconColourId));
  81. auto path = getIcons().user;
  82. g.fillPath (path, RectanglePlacement (RectanglePlacement::centred)
  83. .getTransformToFit (path.getBounds(), image.getBounds().reduced (image.getHeight() / 5).toFloat()));
  84. return image;
  85. }
  86. //==============================================================================
  87. void licenseStateChanged() override
  88. {
  89. auto state = ProjucerApplication::getApp().getLicenseController().getCurrentState();
  90. isGPL = ProjucerApplication::getApp().getLicenseController().getCurrentState().isGPL();
  91. if (interactive)
  92. {
  93. auto formattedUserString = [state]() -> String
  94. {
  95. if (state.isSignedIn())
  96. return (state.isGPL() ? "" : (state.username + " - ")) + state.getLicenseTypeString();
  97. return "Not logged in";
  98. }();
  99. setTooltip (formattedUserString);
  100. }
  101. currentAvatar = isGPL ? gplAvatarImage
  102. : state.isSignedIn() ? standardAvatarImage : signedOutAvatarImage;
  103. repaint();
  104. sendChangeMessage();
  105. }
  106. void lookAndFeelChanged() override
  107. {
  108. standardAvatarImage = createStandardAvatarImage();
  109. signedOutAvatarImage = createStandardAvatarImage();
  110. if (interactive)
  111. signedOutAvatarImage.multiplyAllAlphas (0.4f);
  112. licenseStateChanged();
  113. repaint();
  114. }
  115. //==============================================================================
  116. Image standardAvatarImage, signedOutAvatarImage, gplAvatarImage { createGPLAvatarImage() }, currentAvatar;
  117. bool isGPL = false, interactive = false;
  118. };