|
| 1 | +/* |
| 2 | + // Copyright (c) 2026 Timothy Schoen |
| 3 | + // For information on usage and redistribution, and for a DISCLAIMER OF ALL |
| 4 | + // WARRANTIES, see the file, "LICENSE.txt," in this distribution. |
| 5 | +*/ |
| 6 | +#pragma once |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +class ConsoleMessageDisplay final : public Component { |
| 11 | +public: |
| 12 | + ConsoleMessageDisplay(PluginEditor* e) : editor(e) |
| 13 | + { |
| 14 | + setInterceptsMouseClicks(false, false); |
| 15 | + setVisible(false); |
| 16 | + |
| 17 | + setCachedComponentImage(new NVGSurface::InvalidationListener(e->nvgSurface, this)); |
| 18 | + } |
| 19 | + |
| 20 | + void showMessage(SmallString const& message, bool const isWarning) |
| 21 | + { |
| 22 | + if (message.isEmpty()) { |
| 23 | + hide(); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + currentMessage = message.toString(); |
| 28 | + currentIsWarning = isWarning; |
| 29 | + |
| 30 | + if (auto* parent = getParentComponent()) |
| 31 | + parent->resized(); |
| 32 | + repaint(); |
| 33 | + |
| 34 | + setVisible(true); |
| 35 | + setBounds(getBounds().withWidth(getDesiredWidth()).withRight(getRight())); |
| 36 | + |
| 37 | + hideTimer.startTimer(2000); |
| 38 | + } |
| 39 | + |
| 40 | + void hide() |
| 41 | + { |
| 42 | + setVisible(false); |
| 43 | + currentMessage = {}; |
| 44 | + if (auto* parent = getParentComponent()) |
| 45 | + parent->resized(); |
| 46 | + } |
| 47 | + |
| 48 | + int getDesiredWidth() const |
| 49 | + { |
| 50 | + if (currentMessage.isEmpty()) |
| 51 | + return 0; |
| 52 | + auto const attr = makeAttributedString(currentMessage); |
| 53 | + TextLayout layout; |
| 54 | + layout.createLayout(attr, 10000.0f); |
| 55 | + return static_cast<int>(std::ceil(layout.getWidth())) + 28; |
| 56 | + } |
| 57 | + |
| 58 | + void paint(Graphics& g) override |
| 59 | + { |
| 60 | + auto const bounds = getLocalBounds(); |
| 61 | + auto const textColour = currentIsWarning ? Colours::orange : PlugDataColours::toolbarTextColour; |
| 62 | + |
| 63 | + auto const b = getLocalBounds().reduced(5); |
| 64 | + StackShadow::drawShadowForRect(g, b.reduced(3.0f), 10, Corners::largeCornerRadius, 0.4f, 1); |
| 65 | + |
| 66 | + g.setColour(PlugDataColours::toolbarBackgroundColour); |
| 67 | + g.fillRoundedRectangle(b.toFloat(), Corners::largeCornerRadius); |
| 68 | + |
| 69 | + g.setColour(PlugDataColours::toolbarOutlineColour); |
| 70 | + g.drawRoundedRectangle(b.toFloat(), Corners::largeCornerRadius, 1.0f); |
| 71 | + |
| 72 | + AttributedString attr; |
| 73 | + attr.setJustification(Justification::centred); |
| 74 | + attr.append(currentMessage, |
| 75 | + Fonts::getDefaultFont().withHeight(13.0f), |
| 76 | + textColour); |
| 77 | + attr.draw(g, bounds.reduced(5).toFloat().reduced(10, 0)); |
| 78 | + } |
| 79 | + |
| 80 | + AttributedString makeAttributedString(String const& text) const |
| 81 | + { |
| 82 | + AttributedString attr; |
| 83 | + attr.setJustification(Justification::centredLeft); |
| 84 | + auto const textColour = currentIsWarning ? Colours::orange : PlugDataColours::toolbarTextColour; |
| 85 | + attr.append(text, Fonts::getDefaultFont().withHeight(13.0f), textColour); |
| 86 | + return attr; |
| 87 | + } |
| 88 | + |
| 89 | + PluginEditor* editor; |
| 90 | + String currentMessage; |
| 91 | + bool currentIsWarning = false; |
| 92 | + |
| 93 | + TimedCallback hideTimer = TimedCallback([this] { |
| 94 | + hideTimer.stopTimer(); |
| 95 | + hide(); |
| 96 | + }); |
| 97 | + |
| 98 | + NVGImage componentImage; |
| 99 | + |
| 100 | + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(ConsoleMessageDisplay) |
| 101 | +}; |
0 commit comments