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.

274 lines
10KB

  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 "jucer_LicenseQueryThread.h"
  15. #include "../../Project/UI/jucer_UserAvatarComponent.h"
  16. //==============================================================================
  17. class LoginFormComponent : public Component
  18. {
  19. public:
  20. LoginFormComponent (MainWindow& window)
  21. : mainWindow (window)
  22. {
  23. addAndMakeVisible (emailBox);
  24. emailBox.setTextToShowWhenEmpty ("Email", Colours::black.withAlpha (0.2f));
  25. emailBox.setJustification (Justification::centredLeft);
  26. emailBox.onReturnKey = [this] { submitDetails(); };
  27. addAndMakeVisible (passwordBox);
  28. passwordBox.setTextToShowWhenEmpty ("Password", Colours::black.withAlpha (0.2f));
  29. passwordBox.setPasswordCharacter ((juce_wchar) 0x2022);
  30. passwordBox.setJustification (Justification::centredLeft);
  31. passwordBox.onReturnKey = [this] { submitDetails(); };
  32. addAndMakeVisible (logInButton);
  33. logInButton.onClick = [this] { submitDetails(); };
  34. addAndMakeVisible (enableGPLButton);
  35. enableGPLButton.onClick = [this]
  36. {
  37. ProjucerApplication::getApp().getLicenseController().setState (LicenseController::getGPLState());
  38. mainWindow.hideLoginFormOverlay();
  39. };
  40. addAndMakeVisible (userAvatar);
  41. addAndMakeVisible (createAccountLabel);
  42. createAccountLabel.setFont (Font (14.0f, Font::underlined));
  43. createAccountLabel.addMouseListener (this, false);
  44. createAccountLabel.setMouseCursor (MouseCursor::PointingHandCursor);
  45. addAndMakeVisible (errorMessageLabel);
  46. errorMessageLabel.setMinimumHorizontalScale (1.0f);
  47. errorMessageLabel.setFont (12.0f);
  48. errorMessageLabel.setColour (Label::textColourId, Colours::red);
  49. errorMessageLabel.setVisible (false);
  50. dismissButton.setShape (getLookAndFeel().getCrossShape (1.0f), false, true, false);
  51. addAndMakeVisible (dismissButton);
  52. dismissButton.onClick = [this] { mainWindow.hideLoginFormOverlay(); };
  53. setWantsKeyboardFocus (true);
  54. setOpaque (true);
  55. lookAndFeelChanged();
  56. setSize (300, 350);
  57. }
  58. void resized() override
  59. {
  60. auto bounds = getLocalBounds().reduced (20);
  61. auto spacing = bounds.getHeight() / 20;
  62. userAvatar.setBounds (bounds.removeFromTop (iconHeight).reduced ((bounds.getWidth() / 2) - (iconHeight / 2), 0));
  63. errorMessageLabel.setBounds (bounds.removeFromTop (spacing));
  64. bounds.removeFromTop (spacing / 2);
  65. auto textEditorHeight = bounds.getHeight() / 5;
  66. emailBox.setBounds (bounds.removeFromTop (textEditorHeight));
  67. bounds.removeFromTop (spacing);
  68. passwordBox.setBounds (bounds.removeFromTop (textEditorHeight));
  69. bounds.removeFromTop (spacing * 2);
  70. emailBox.setFont (Font (textEditorHeight / 2.5f));
  71. passwordBox.setFont (Font (textEditorHeight / 2.5f));
  72. logInButton.setBounds (bounds.removeFromTop (textEditorHeight));
  73. auto slice = bounds.removeFromTop (textEditorHeight);
  74. createAccountLabel.setBounds (slice.removeFromLeft (createAccountLabel.getFont().getStringWidth (createAccountLabel.getText()) + 5));
  75. slice.removeFromLeft (15);
  76. enableGPLButton.setBounds (slice.reduced (0, 5));
  77. dismissButton.setBounds (getLocalBounds().reduced (10).removeFromTop (20).removeFromRight (20));
  78. }
  79. void paint (Graphics& g) override
  80. {
  81. g.fillAll (findColour (secondaryBackgroundColourId).contrasting (0.1f));
  82. }
  83. void mouseUp (const MouseEvent& event) override
  84. {
  85. if (event.eventComponent == &createAccountLabel)
  86. URL ("https://auth.roli.com/register").launchInDefaultBrowser();
  87. }
  88. void lookAndFeelChanged() override
  89. {
  90. enableGPLButton.setColour (TextButton::buttonColourId, findColour (secondaryButtonBackgroundColourId));
  91. }
  92. private:
  93. class ProgressButton : public TextButton,
  94. private Timer
  95. {
  96. public:
  97. ProgressButton (const String& buttonName)
  98. : TextButton (buttonName), text (buttonName)
  99. {
  100. }
  101. void setBusy (bool shouldBeBusy)
  102. {
  103. isInProgress = shouldBeBusy;
  104. if (isInProgress)
  105. {
  106. setEnabled (false);
  107. setButtonText ({});
  108. startTimerHz (30);
  109. }
  110. else
  111. {
  112. setEnabled (true);
  113. setButtonText (text);
  114. stopTimer();
  115. }
  116. }
  117. void paint (Graphics& g) override
  118. {
  119. TextButton::paint (g);
  120. if (isInProgress)
  121. {
  122. auto size = getHeight() - 10;
  123. auto halfSize = size / 2;
  124. getLookAndFeel().drawSpinningWaitAnimation (g, Colours::white,
  125. (getWidth() / 2) - halfSize, (getHeight() / 2) - halfSize,
  126. size, size);
  127. }
  128. }
  129. private:
  130. void timerCallback() override
  131. {
  132. repaint();
  133. }
  134. String text;
  135. bool isInProgress = false;
  136. };
  137. //==============================================================================
  138. void updateLoginButtonStates (bool isLoggingIn)
  139. {
  140. logInButton.setBusy (isLoggingIn);
  141. emailBox.setEnabled (! isLoggingIn);
  142. passwordBox.setEnabled (! isLoggingIn);
  143. }
  144. void submitDetails()
  145. {
  146. if ((licenseQueryThread != nullptr && licenseQueryThread->isThreadRunning()))
  147. return;
  148. auto loginFormError = checkLoginFormsAreValid();
  149. if (loginFormError.isNotEmpty())
  150. {
  151. showErrorMessage (loginFormError);
  152. return;
  153. }
  154. updateLoginButtonStates (true);
  155. WeakReference<Component> weakThis (this);
  156. licenseQueryThread.reset (new LicenseQueryThread (emailBox.getText(), passwordBox.getText(),
  157. [this, weakThis] (LicenseState newState, 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. auto& licenseController = ProjucerApplication::getApp().getLicenseController();
  170. licenseController.setState (newState);
  171. mainWindow.hideLoginFormOverlay();
  172. ProjucerApplication::getApp().getCommandManager().commandStatusChanged();
  173. }
  174. }));
  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, false };
  207. Label createAccountLabel { {}, "Create an account" },
  208. errorMessageLabel { {}, {} };
  209. std::unique_ptr<LicenseQueryThread> licenseQueryThread;
  210. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (LoginFormComponent)
  211. };