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.

165 lines
5.4KB

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