editable.cpp (1789B)
1 #include "editable.h" 2 3 namespace jucePluginEditorLib::patchManager 4 { 5 Editable::~Editable() 6 { 7 destroyEditorLabel(); 8 } 9 10 bool Editable::beginEdit(juce::Component* _parent, const juce::Rectangle<int>& _position, const std::string& _initialText, FinishedEditingCallback&& _callback) 11 { 12 if (m_editorLabel) 13 return false; 14 15 m_editorInitialText = _initialText; 16 m_editorLabel = new juce::Label({}, _initialText); 17 18 const auto pos = _position; 19 20 m_editorLabel->setTopLeftPosition(pos.getTopLeft()); 21 m_editorLabel->setSize(pos.getWidth(), pos.getHeight()); 22 23 m_editorLabel->setEditable(true, true, true); 24 m_editorLabel->setColour(juce::Label::backgroundColourId, juce::Colour(0xff333333)); 25 26 m_editorLabel->addListener(this); 27 28 _parent->addAndMakeVisible(m_editorLabel); 29 30 m_editorLabel->showEditor(); 31 m_editorLabel->getCurrentTextEditor()->addListener(this); 32 33 m_finishedEditingCallback = std::move(_callback); 34 35 return true; 36 } 37 38 void Editable::editorHidden(juce::Label* _label, juce::TextEditor& _textEditor) 39 { 40 if (m_editorLabel) 41 { 42 const auto text = m_editorLabel->getText().toStdString(); 43 if(text != m_editorInitialText) 44 m_finishedEditingCallback(true, text); 45 destroyEditorLabel(); 46 } 47 juce::Label::Listener::editorHidden(_label, _textEditor); 48 } 49 50 void Editable::labelTextChanged(juce::Label* _label) 51 { 52 if(m_editorLabel) 53 m_editorLabel->repaint(); 54 } 55 56 void Editable::textEditorTextChanged(juce::TextEditor& _textEditor) 57 { 58 if(m_editorLabel) 59 m_editorLabel->repaint(); 60 _textEditor.repaint(); 61 } 62 63 void Editable::destroyEditorLabel() 64 { 65 if (!m_editorLabel) 66 return; 67 68 m_editorLabel->getParentComponent()->removeChildComponent(m_editorLabel); 69 delete m_editorLabel; 70 m_editorLabel = nullptr; 71 72 m_finishedEditingCallback = {}; 73 } 74 }