Notices.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #include "ui/Notices.h"
  2. #include "ui/HintBar.h"
  3. #include "ui/I18n.h"
  4. #include "ui/Onboarding.h"
  5. #include "ui/Theme.h"
  6. #include <algorithm>
  7. #include <memory>
  8. #include <vector>
  9. namespace lynx::ui
  10. {
  11. namespace
  12. {
  13. class NoticeCard final : public juce::Component,
  14. private juce::Timer
  15. {
  16. public:
  17. std::function<void (NoticeCard&)> closed;
  18. explicit NoticeCard (Notice n) : notice (std::move (n))
  19. {
  20. setComponentID ("notice");
  21. closeButton.setComponentID ("notice.close");
  22. closeButton.setTooltip (tr ("Close"));
  23. closeButton.onClick = [this] { close(); };
  24. closeButton.setColour (juce::TextButton::buttonColourId, juce::Colours::transparentBlack);
  25. closeButton.setColour (juce::TextButton::textColourOffId, theme::colour::textSecondary);
  26. addAndMakeVisible (closeButton);
  27. if (notice.actionText.isNotEmpty() && notice.action)
  28. {
  29. actionButton.setButtonText (notice.actionText);
  30. actionButton.setComponentID ("notice.action");
  31. actionButton.setColour (juce::TextButton::buttonColourId, theme::colour::bg5); // (a raised button on the card)
  32. actionButton.onClick = [this] {
  33. auto fn = notice.action;
  34. close();
  35. if (fn)
  36. fn();
  37. };
  38. addAndMakeVisible (actionButton);
  39. }
  40. setSize (width, idealHeight());
  41. restart();
  42. }
  43. ~NoticeCard() override { stopTimer(); }
  44. static constexpr int width = 380;
  45. const Notice& getNotice() const noexcept { return notice; }
  46. int idealHeight() const
  47. {
  48. auto h = 14 + textHeight (notice.text, theme::font (theme::type::body, true));
  49. if (notice.detail.isNotEmpty())
  50. h += 3 + textHeight (notice.detail, theme::font (theme::type::small));
  51. if (actionButton.isVisible())
  52. h += 8 + 24;
  53. return juce::jmax (44, h + 12);
  54. }
  55. void paint (juce::Graphics& g) override
  56. {
  57. using namespace theme::colour;
  58. const auto b = getLocalBounds().toFloat().reduced (0.5f);
  59. g.setColour (juce::Colours::black.withAlpha (0.35f)); // (a soft shadow, drawn: no effect component)
  60. g.fillRoundedRectangle (b.translated (0.0f, 2.0f), theme::radius::lg);
  61. g.setColour (bg3);
  62. g.fillRoundedRectangle (b, theme::radius::lg);
  63. g.setColour (borderStrong);
  64. g.drawRoundedRectangle (b, theme::radius::lg, 1.0f);
  65. g.setColour (stripe());
  66. g.fillRoundedRectangle (b.withWidth (4.0f).reduced (0.0f, 6.0f).translated (6.0f, 0.0f), 2.0f);
  67. auto r = textArea();
  68. g.setColour (textPrimary);
  69. g.setFont (theme::font (theme::type::body, true));
  70. const auto th = textHeight (notice.text, g.getCurrentFont());
  71. g.drawFittedText (notice.text, r.removeFromTop (th), juce::Justification::topLeft, 3, 1.0f);
  72. if (notice.detail.isNotEmpty())
  73. {
  74. r.removeFromTop (3);
  75. g.setColour (textSecondary);
  76. g.setFont (theme::font (theme::type::small));
  77. g.drawFittedText (notice.detail, r.removeFromTop (textHeight (notice.detail, g.getCurrentFont())), juce::Justification::topLeft, 3, 1.0f);
  78. }
  79. }
  80. void resized() override
  81. {
  82. closeButton.setBounds (getWidth() - 30, 6, 24, 22);
  83. if (actionButton.isVisible())
  84. {
  85. const auto w = juce::roundToInt (juce::GlyphArrangement::getStringWidth (getLookAndFeel().getTextButtonFont (actionButton, 24), actionButton.getButtonText())) + 24;
  86. actionButton.setBounds (textArea().getX(), getHeight() - 12 - 24, juce::jmin (w, getWidth() - 40), 24);
  87. }
  88. }
  89. void mouseEnter (const juce::MouseEvent&) override { stopTimer(); }
  90. void mouseExit (const juce::MouseEvent&) override
  91. {
  92. if (! isMouseOverOrDragging (true))
  93. restart();
  94. }
  95. void restart() { startTimer (notice.holdMs > 0 ? notice.holdMs : notice.kind == Notice::Kind::problem ? 10000 : 6000); }
  96. private:
  97. juce::Colour stripe() const
  98. {
  99. switch (notice.kind)
  100. {
  101. case Notice::Kind::done: return theme::colour::ledOn;
  102. case Notice::Kind::problem: return theme::colour::warning;
  103. case Notice::Kind::info: break;
  104. }
  105. return theme::colour::accent;
  106. }
  107. juce::Rectangle<int> textArea() const { return getLocalBounds().reduced (20, 12).withTrimmedRight (20); }
  108. int textHeight (const juce::String& text, const juce::Font& font) const
  109. {
  110. juce::GlyphArrangement glyphs;
  111. glyphs.addJustifiedText (font, text, 0.0f, 0.0f, (float) (width - 60), juce::Justification::topLeft);
  112. return juce::jmax ((int) std::ceil (font.getHeight()), (int) std::ceil (glyphs.getBoundingBox (0, -1, true).getHeight()) + 2);
  113. }
  114. void timerCallback() override
  115. {
  116. if (isMouseOverOrDragging (true))
  117. return; // (read on: it waits)
  118. close();
  119. }
  120. void close()
  121. {
  122. stopTimer();
  123. if (closed)
  124. closed (*this);
  125. }
  126. Notice notice;
  127. juce::TextButton closeButton { juce::String::fromUTF8 ("\xc3\x97") }, actionButton;
  128. };
  129. struct Notices
  130. {
  131. std::vector<juce::Component::SafePointer<juce::Component>> hosts;
  132. std::vector<std::unique_ptr<NoticeCard>> cards;
  133. juce::Component::SafePointer<juce::Component> hostShown;
  134. juce::Component* host()
  135. {
  136. hosts.erase (std::remove_if (hosts.begin(), hosts.end(), [] (const auto& h) { return h == nullptr; }), hosts.end());
  137. for (auto& h : hosts)
  138. if (auto* top = h->getTopLevelComponent(); top != nullptr && top->isShowing())
  139. if (auto* peer = top->getPeer(); peer != nullptr && peer->isFocused())
  140. return h.getComponent();
  141. for (auto& h : hosts)
  142. if (h->isShowing())
  143. return h.getComponent();
  144. return hosts.empty() ? nullptr : hosts.front().getComponent();
  145. }
  146. void place()
  147. {
  148. auto* window = hostShown.getComponent();
  149. if (window == nullptr)
  150. return;
  151. // the lower right, over the hint bar's height from the bottom edge; the newest at the bottom
  152. auto bottom = window->getHeight() - 52;
  153. for (auto it = cards.rbegin(); it != cards.rend(); ++it)
  154. {
  155. auto& card = **it;
  156. const auto h = card.idealHeight();
  157. const auto bounds = juce::Rectangle<int> (window->getWidth() - NoticeCard::width - 16, bottom - h, NoticeCard::width, h);
  158. onboardui::hostOnTop (card, *window, bounds);
  159. bottom -= h + 8;
  160. }
  161. }
  162. void remove (NoticeCard& card)
  163. {
  164. // (later: the card's own button is still on the stack)
  165. juce::Component::SafePointer<NoticeCard> safe (&card);
  166. juce::MessageManager::callAsync ([this, safe] {
  167. if (safe == nullptr)
  168. return;
  169. auto it = std::find_if (cards.begin(), cards.end(), [&safe] (const auto& c) { return c.get() == safe.getComponent(); });
  170. if (it == cards.end())
  171. return;
  172. onboardui::removeHosted (**it);
  173. cards.erase (it);
  174. place();
  175. });
  176. }
  177. };
  178. Notices& notices()
  179. {
  180. static Notices n;
  181. return n;
  182. }
  183. } // namespace
  184. void notify (Notice notice)
  185. {
  186. // the hint bar says it too (FL users read the hint bar)
  187. postNotice ({ notice.text, notice.detail, false, false, notice.kind == Notice::Kind::problem ? Hint::Mark::warning : Hint::Mark::none },
  188. notice.kind == Notice::Kind::problem ? 6000 : 4000);
  189. auto& n = notices();
  190. auto* window = n.host();
  191. if (window == nullptr)
  192. return;
  193. if (n.hostShown != window)
  194. {
  195. clearNotices();
  196. n.hostShown = window;
  197. }
  198. if (notice.id.isNotEmpty())
  199. if (auto it = std::find_if (n.cards.begin(), n.cards.end(), [&notice] (const auto& c) { return c->getNotice().id == notice.id; }); it != n.cards.end())
  200. {
  201. onboardui::removeHosted (**it);
  202. n.cards.erase (it);
  203. }
  204. while (n.cards.size() >= 3)
  205. {
  206. onboardui::removeHosted (*n.cards.front());
  207. n.cards.erase (n.cards.begin());
  208. }
  209. auto card = std::make_unique<NoticeCard> (std::move (notice));
  210. card->closed = [] (NoticeCard& c) { notices().remove (c); };
  211. n.cards.push_back (std::move (card));
  212. n.place();
  213. }
  214. void addNoticeHost (juce::Component* window)
  215. {
  216. if (window != nullptr)
  217. notices().hosts.emplace_back (window);
  218. }
  219. void removeNoticeHost (juce::Component* window)
  220. {
  221. auto& n = notices();
  222. if (n.hostShown == window)
  223. clearNotices();
  224. n.hosts.erase (std::remove_if (n.hosts.begin(), n.hosts.end(), [window] (const auto& h) { return h == nullptr || h.getComponent() == window; }),
  225. n.hosts.end());
  226. }
  227. void pluginNotLoaded (const juce::String& pluginName)
  228. {
  229. Notice n;
  230. n.kind = Notice::Kind::problem;
  231. n.id = "plugin.notLoaded";
  232. n.text = pluginName.isNotEmpty() ? tr ("Couldn't open {0}", { pluginName }) : tr ("Couldn't open this plugin");
  233. n.detail = tr ("It isn't installed on this computer, or it hasn't been scanned yet. Its slot stays as it is in the project.");
  234. if (openPluginManager)
  235. {
  236. n.actionText = tr ("Plugin manager...");
  237. n.action = [] {
  238. if (openPluginManager)
  239. openPluginManager();
  240. };
  241. }
  242. notify (std::move (n));
  243. }
  244. void savedFile (const juce::String& what, const juce::File& file)
  245. {
  246. Notice n;
  247. n.kind = Notice::Kind::done;
  248. n.id = "file.saved";
  249. n.text = what;
  250. n.detail = file.getFileName();
  251. if (file.exists())
  252. {
  253. n.actionText = tr ("Show in folder");
  254. n.action = [file] { file.revealToUser(); };
  255. }
  256. notify (std::move (n));
  257. }
  258. int shownNoticeCount() { return (int) notices().cards.size(); }
  259. juce::Component* shownNotice (int index)
  260. {
  261. auto& cards = notices().cards;
  262. return juce::isPositiveAndBelow (index, (int) cards.size()) ? cards[(std::size_t) index].get() : nullptr;
  263. }
  264. void clearNotices()
  265. {
  266. auto& n = notices();
  267. for (auto& c : n.cards)
  268. onboardui::removeHosted (*c);
  269. n.cards.clear();
  270. }
  271. std::unique_ptr<juce::Component> makeNoticeCard (const Notice& notice)
  272. {
  273. auto card = std::make_unique<NoticeCard> (notice);
  274. card->setSize (NoticeCard::width, card->idealHeight());
  275. return card;
  276. }
  277. } // namespace lynx::ui