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.

272 lines
9.0KB

  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. #pragma once
  14. #include "../../Project/UI/jucer_UserAvatarComponent.h"
  15. //==============================================================================
  16. class LoginFormComponent : public Component
  17. {
  18. public:
  19. LoginFormComponent (MainWindow& window)
  20. : mainWindow (window)
  21. {
  22. addAndMakeVisible (emailBox);
  23. emailBox.setTextToShowWhenEmpty ("Email", Colours::black.withAlpha (0.2f));
  24. emailBox.setJustification (Justification::centredLeft);
  25. emailBox.onReturnKey = [this] { submitDetails(); };
  26. addAndMakeVisible (passwordBox);
  27. passwordBox.setTextToShowWhenEmpty ("Password", Colours::black.withAlpha (0.2f));
  28. passwordBox.setPasswordCharacter ((juce_wchar) 0x2022);
  29. passwordBox.setJustification (Justification::centredLeft);
  30. passwordBox.onReturnKey = [this] { submitDetails(); };
  31. addAndMakeVisible (logInButton);
  32. logInButton.onClick = [this] { submitDetails(); };
  33. addAndMakeVisible (enableGPLButton);
  34. enableGPLButton.onClick = [this]
  35. {
  36. ProjucerApplication::getApp().getLicenseController().setState (LicenseController::getGPLState());
  37. mainWindow.hideLoginFormOverlay();
  38. };
  39. addAndMakeVisible (userAvatar);
  40. addAndMakeVisible (createAccountLabel);
  41. createAccountLabel.setFont (Font (14.0f, Font::underlined));
  42. createAccountLabel.addMouseListener (this, false);
  43. createAccountLabel.setMouseCursor (MouseCursor::PointingHandCursor);
  44. addAndMakeVisible (errorMessageLabel);
  45. errorMessageLabel.setMinimumHorizontalScale (1.0f);
  46. errorMessageLabel.setFont (12.0f);
  47. errorMessageLabel.setColour (Label::textColourId, Colours::red);
  48. errorMessageLabel.setVisible (false);
  49. dismissButton.setShape (getLookAndFeel().getCrossShape (1.0f), false, true, false);
  50. addAndMakeVisible (dismissButton);
  51. dismissButton.onClick = [this] { mainWindow.hideLoginFormOverlay(); };
  52. setWantsKeyboardFocus (true);
  53. setOpaque (true);
  54. lookAndFeelChanged();
  55. setSize (300, 350);
  56. }
  57. ~LoginFormComponent() override
  58. {
  59. ProjucerApplication::getApp().getLicenseController().cancelSignIn();
  60. }
  61. void resized() override
  62. {
  63. auto bounds = getLocalBounds().reduced (20);
  64. auto spacing = bounds.getHeight() / 20;
  65. userAvatar.setBounds (bounds.removeFromTop (iconHeight).reduced ((bounds.getWidth() / 2) - (iconHeight / 2), 0));
  66. errorMessageLabel.setBounds (bounds.removeFromTop (spacing));
  67. bounds.removeFromTop (spacing / 2);
  68. auto textEditorHeight = bounds.getHeight() / 5;
  69. emailBox.setBounds (bounds.removeFromTop (textEditorHeight));
  70. bounds.removeFromTop (spacing);
  71. passwordBox.setBounds (bounds.removeFromTop (textEditorHeight));
  72. bounds.removeFromTop (spacing * 2);
  73. emailBox.setFont (Font (textEditorHeight / 2.5f));
  74. passwordBox.setFont (Font (textEditorHeight / 2.5f));
  75. logInButton.setBounds (bounds.removeFromTop (textEditorHeight));
  76. auto slice = bounds.removeFromTop (textEditorHeight);
  77. createAccountLabel.setBounds (slice.removeFromLeft (createAccountLabel.getFont().getStringWidth (createAccountLabel.getText()) + 5));
  78. slice.removeFromLeft (15);
  79. enableGPLButton.setBounds (slice.reduced (0, 5));
  80. dismissButton.setBounds (getLocalBounds().reduced (10).removeFromTop (20).removeFromRight (20));
  81. }
  82. void paint (Graphics& g) override
  83. {
  84. g.fillAll (findColour (secondaryBackgroundColourId).contrasting (0.1f));
  85. }
  86. void mouseUp (const MouseEvent& event) override
  87. {
  88. if (event.eventComponent == &createAccountLabel)
  89. URL ("https://juce.com/verification/register").launchInDefaultBrowser();
  90. }
  91. void lookAndFeelChanged() override
  92. {
  93. enableGPLButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  94. }
  95. private:
  96. class ProgressButton : public TextButton,
  97. private Timer
  98. {
  99. public:
  100. ProgressButton (const String& buttonName)
  101. : TextButton (buttonName), text (buttonName)
  102. {
  103. }
  104. void setBusy (bool shouldBeBusy)
  105. {
  106. isInProgress = shouldBeBusy;
  107. if (isInProgress)
  108. {
  109. setEnabled (false);
  110. setButtonText ({});
  111. startTimerHz (30);
  112. }
  113. else
  114. {
  115. setEnabled (true);
  116. setButtonText (text);
  117. stopTimer();
  118. }
  119. }
  120. void paint (Graphics& g) override
  121. {
  122. TextButton::paint (g);
  123. if (isInProgress)
  124. {
  125. auto size = getHeight() - 10;
  126. auto halfSize = size / 2;
  127. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white,
  128. (getWidth() / 2) - halfSize, (getHeight() / 2) - halfSize,
  129. size, size);
  130. }
  131. }
  132. private:
  133. void timerCallback() override
  134. {
  135. repaint();
  136. }
  137. String text;
  138. bool isInProgress = false;
  139. };
  140. //==============================================================================
  141. void updateLoginButtonStates (bool isLoggingIn)
  142. {
  143. logInButton.setBusy (isLoggingIn);
  144. emailBox.setEnabled (! isLoggingIn);
  145. passwordBox.setEnabled (! isLoggingIn);
  146. }
  147. void submitDetails()
  148. {
  149. auto loginFormError = checkLoginFormsAreValid();
  150. if (loginFormError.isNotEmpty())
  151. {
  152. showErrorMessage (loginFormError);
  153. return;
  154. }
  155. updateLoginButtonStates (true);
  156. WeakReference<Component> weakThis (this);
  157. auto completionCallback = [this, weakThis] (const String& errorMessage)
  158. {
  159. if (weakThis == nullptr)
  160. return;
  161. updateLoginButtonStates (false);
  162. if (errorMessage.isNotEmpty())
  163. {
  164. showErrorMessage (errorMessage);
  165. }
  166. else
  167. {
  168. hideErrorMessage();
  169. mainWindow.hideLoginFormOverlay();
  170. ProjucerApplication::getApp().getCommandManager().commandStatusChanged();
  171. }
  172. };
  173. ProjucerApplication::getApp().getLicenseController().signIn (emailBox.getText(), passwordBox.getText(),
  174. std::move (completionCallback));
  175. }
  176. String checkLoginFormsAreValid() const
  177. {
  178. auto email = emailBox.getText();
  179. if (email.isEmpty() || email.indexOfChar ('@') < 0)
  180. return "Please enter a valid email.";
  181. auto password = passwordBox.getText();
  182. if (password.isEmpty() || password.length() < 8)
  183. return "Please enter a valid password.";
  184. return {};
  185. }
  186. void showErrorMessage (const String& errorMessage)
  187. {
  188. errorMessageLabel.setText (errorMessage, dontSendNotification);
  189. errorMessageLabel.setVisible (true);
  190. }
  191. void hideErrorMessage()
  192. {
  193. errorMessageLabel.setText ({}, dontSendNotification);
  194. errorMessageLabel.setVisible (false);
  195. }
  196. //==============================================================================
  197. static constexpr int iconHeight = 50;
  198. MainWindow& mainWindow;
  199. TextEditor emailBox, passwordBox;
  200. ProgressButton logInButton { "Sign In" };
  201. TextButton enableGPLButton { "Enable GPL Mode" };
  202. ShapeButton dismissButton { {},
  203. findColour (treeIconColourId),
  204. findColour (treeIconColourId).overlaidWith (findColour (defaultHighlightedTextColourId).withAlpha (0.2f)),
  205. findColour (treeIconColourId).overlaidWith (findColour (defaultHighlightedTextColourId).withAlpha (0.4f)) };
  206. UserAvatarComponent userAvatar { false };
  207. Label createAccountLabel { {}, "Create an account" },
  208. errorMessageLabel { {}, {} };
  209. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LoginFormComponent)
  210. };