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.

242 lines
5.3KB

  1. #include <thread>
  2. #include "system.hpp"
  3. #include "app/PluginManagerWidget.hpp"
  4. #include "ui/SequentialLayout.hpp"
  5. #include "ui/Button.hpp"
  6. #include "ui/ProgressBar.hpp"
  7. #include "ui/TextField.hpp"
  8. #include "ui/PasswordField.hpp"
  9. #include "ui/Label.hpp"
  10. #include "plugin/PluginManager.hpp"
  11. #include "context.hpp"
  12. #include "window.hpp"
  13. #include "helpers.hpp"
  14. #include "osdialog.h"
  15. namespace rack {
  16. struct RegisterButton : Button {
  17. void onAction(event::Action &e) override {
  18. std::thread t([&]() {
  19. system::openBrowser("https://vcvrack.com/");
  20. });
  21. t.detach();
  22. }
  23. };
  24. struct LogInButton : Button {
  25. TextField *emailField;
  26. TextField *passwordField;
  27. void onAction(event::Action &e) override {
  28. std::thread t([&]() {
  29. context()->plugin->logIn(emailField->text, passwordField->text);
  30. });
  31. t.detach();
  32. passwordField->text = "";
  33. }
  34. };
  35. struct StatusLabel : Label {
  36. void step() override {
  37. text = context()->plugin->loginStatus;
  38. }
  39. };
  40. struct ManageButton : Button {
  41. void onAction(event::Action &e) override {
  42. std::thread t([&]() {
  43. system::openBrowser("https://vcvrack.com/plugins.html");
  44. });
  45. t.detach();
  46. }
  47. };
  48. struct SyncButton : Button {
  49. bool checked = false;
  50. /** Updates are available */
  51. bool available = false;
  52. /** Plugins have been updated */
  53. bool completed = false;
  54. void step() override {
  55. // Check for plugin update on first step()
  56. if (!checked) {
  57. std::thread t([this]() {
  58. if (context()->plugin->sync(true))
  59. available = true;
  60. });
  61. t.detach();
  62. checked = true;
  63. }
  64. // Display message if we've completed updates
  65. if (completed) {
  66. if (osdialog_message(OSDIALOG_INFO, OSDIALOG_OK_CANCEL, "All plugins have been updated. Close Rack and re-launch it to load new updates.")) {
  67. windowClose();
  68. }
  69. completed = false;
  70. }
  71. }
  72. void draw(NVGcontext *vg) override {
  73. Button::draw(vg);
  74. if (available) {
  75. // Notification circle
  76. nvgBeginPath(vg);
  77. nvgCircle(vg, 3, 3, 4.0);
  78. nvgFillColor(vg, nvgRGBf(1.0, 0.0, 0.0));
  79. nvgFill(vg);
  80. nvgStrokeColor(vg, nvgRGBf(0.5, 0.0, 0.0));
  81. nvgStroke(vg);
  82. }
  83. }
  84. void onAction(event::Action &e) override {
  85. available = false;
  86. std::thread t([this]() {
  87. if (context()->plugin->sync(false))
  88. completed = true;
  89. });
  90. t.detach();
  91. }
  92. };
  93. struct LogOutButton : Button {
  94. void onAction(event::Action &e) override {
  95. context()->plugin->logOut();
  96. }
  97. };
  98. struct DownloadQuantity : Quantity {
  99. float getValue() override {
  100. return context()->plugin->downloadProgress;
  101. }
  102. float getDisplayValue() override {
  103. return getValue() * 100.f;
  104. }
  105. int getDisplayPrecision() override {return 0;}
  106. std::string getLabel() override {
  107. return "Downloading " + context()->plugin->downloadName;
  108. }
  109. std::string getUnit() override {return "%";}
  110. };
  111. struct DownloadProgressBar : ProgressBar {
  112. DownloadProgressBar() {
  113. quantity = new DownloadQuantity;
  114. }
  115. };
  116. struct CancelButton : Button {
  117. void onAction(event::Action &e) override {
  118. context()->plugin->cancelDownload();
  119. }
  120. };
  121. PluginManagerWidget::PluginManagerWidget() {
  122. box.size.y = BND_WIDGET_HEIGHT;
  123. {
  124. SequentialLayout *layout = createWidget<SequentialLayout>(Vec(0, 0));
  125. layout->spacing = 5;
  126. loginWidget = layout;
  127. Button *registerButton = new RegisterButton;
  128. registerButton->box.size.x = 75;
  129. registerButton->text = "Register";
  130. loginWidget->addChild(registerButton);
  131. TextField *emailField = new TextField;
  132. emailField->box.size.x = 175;
  133. emailField->placeholder = "Email";
  134. loginWidget->addChild(emailField);
  135. PasswordField *passwordField = new PasswordField;
  136. passwordField->box.size.x = 175;
  137. passwordField->placeholder = "Password";
  138. loginWidget->addChild(passwordField);
  139. LogInButton *logInButton = new LogInButton;
  140. logInButton->box.size.x = 100;
  141. logInButton->text = "Log in";
  142. logInButton->emailField = emailField;
  143. logInButton->passwordField = passwordField;
  144. loginWidget->addChild(logInButton);
  145. Label *label = new StatusLabel;
  146. loginWidget->addChild(label);
  147. addChild(loginWidget);
  148. }
  149. {
  150. SequentialLayout *layout = createWidget<SequentialLayout>(Vec(0, 0));
  151. layout->spacing = 5;
  152. manageWidget = layout;
  153. Button *manageButton = new ManageButton;
  154. manageButton->box.size.x = 125;
  155. manageButton->text = "Manage plugins";
  156. manageWidget->addChild(manageButton);
  157. Button *syncButton = new SyncButton;
  158. syncButton->box.size.x = 125;
  159. syncButton->text = "Update plugins";
  160. manageWidget->addChild(syncButton);
  161. Button *logOutButton = new LogOutButton;
  162. logOutButton->box.size.x = 100;
  163. logOutButton->text = "Log out";
  164. manageWidget->addChild(logOutButton);
  165. addChild(manageWidget);
  166. }
  167. {
  168. SequentialLayout *layout = createWidget<SequentialLayout>(Vec(0, 0));
  169. layout->spacing = 5;
  170. downloadWidget = layout;
  171. ProgressBar *downloadProgress = new DownloadProgressBar;
  172. downloadProgress->box.size.x = 300;
  173. downloadWidget->addChild(downloadProgress);
  174. // Button *cancelButton = new CancelButton;
  175. // cancelButton->box.size.x = 100;
  176. // cancelButton->text = "Cancel";
  177. // downloadWidget->addChild(cancelButton);
  178. addChild(downloadWidget);
  179. }
  180. }
  181. void PluginManagerWidget::step() {
  182. loginWidget->visible = false;
  183. manageWidget->visible = false;
  184. downloadWidget->visible = false;
  185. if (context()->plugin->isDownloading)
  186. downloadWidget->visible = true;
  187. else if (context()->plugin->isLoggedIn())
  188. manageWidget->visible = true;
  189. else
  190. loginWidget->visible = true;
  191. Widget::step();
  192. }
  193. } // namespace rack