From a3fd3fb02da420726eb2d5e50e4014778c25d95b Mon Sep 17 00:00:00 2001 From: Joep Vanlier Date: Tue, 24 Dec 2024 03:30:04 +0100 Subject: [PATCH] listener --- plugin/components/ide_view.cpp | 7 +++++ plugin/components/ysfx_document.h | 48 ++++++++++++++++++++++--------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/plugin/components/ide_view.cpp b/plugin/components/ide_view.cpp index b9ff640..647033c 100644 --- a/plugin/components/ide_view.cpp +++ b/plugin/components/ide_view.cpp @@ -147,6 +147,12 @@ void YsfxIDEView::focusOfChildComponentChanged(FocusChangeType cause) if (m_impl->getCurrentEditor()->hasFocus()) { juce::Timer *timer = FunctionalTimer::create([this]() { m_impl->getCurrentEditor()->checkFileForModifications(); + + int idx = 0; + for (auto& m : m_impl->m_editors) { + m_impl->m_tabs->setTabName(idx, m->getName() + (m->wasModified() ? "*" : "")); + ++idx; + } }); m_impl->m_fileCheckTimer.reset(timer); timer->startTimer(100); @@ -287,6 +293,7 @@ void YsfxIDEView::Impl::openDocument(juce::File file) auto editor = addEditor(); editor->loadFile(file); + setCurrentEditor(m_editors.size() - 1); } diff --git a/plugin/components/ysfx_document.h b/plugin/components/ysfx_document.h index bb80876..4b2e348 100644 --- a/plugin/components/ysfx_document.h +++ b/plugin/components/ysfx_document.h @@ -124,46 +124,66 @@ class CodeEditor : public juce::CodeEditorComponent }; -class YSFXCodeEditor +class YSFXCodeEditor : public juce::CodeDocument::Listener { public: YSFXCodeEditor(juce::CodeTokeniser* tokenizer, std::function keyPressCallback, std::function dblClickCallback) { m_document = std::make_unique(); + m_document->addListener(this); m_editor = std::make_unique(*m_document, tokenizer, keyPressCallback, dblClickCallback); m_editor->setVisible(false); }; ~YSFXCodeEditor() { // Make sure we kill the editor first since it may be referencing the document! + m_document->removeListener(this); m_editor.reset(); m_document.reset(); } - void setColourScheme(juce::CodeEditorComponent::ColourScheme colourScheme) { m_editor->setColourScheme(colourScheme); }; - void checkFileForModifications() { m_document->checkFileForModifications(); }; - void reset() { m_document->reset(); }; - void setReadOnly(bool readOnly) { m_editor->setReadOnly(readOnly); }; + void codeDocumentTextDeleted(int startIndex, int endIndex) override { + (void) startIndex; + (void) endIndex; + m_modified = true; + } + void codeDocumentTextInserted(const juce::String& newText, int insertIndex) override { + (void) newText; + (void) insertIndex; + m_modified = true; + } - juce::File getPath() { return m_document->getPath(); }; - juce::String getName() { return m_document->getName(); }; - void loadFile(juce::File file) { m_document->loadFile(file); }; - bool saveFile(juce::File file = juce::File{}) { return m_document->saveFile(file); }; - int search(juce::String text, bool reverse = false) { return m_editor->search(text, reverse); }; + void setColourScheme(juce::CodeEditorComponent::ColourScheme colourScheme) { m_editor->setColourScheme(colourScheme); } + void checkFileForModifications() { m_document->checkFileForModifications(); } + void reset() { m_document->reset(); } + void setReadOnly(bool readOnly) { m_editor->setReadOnly(readOnly); } + bool wasModified() { return m_modified; }; + + juce::File getPath() { return m_document->getPath(); } + juce::String getName() { return m_document->getName(); } + void loadFile(juce::File file) { m_document->loadFile(file); m_modified = false; } + bool saveFile(juce::File file = juce::File{}) { + if (m_document->saveFile(file)) { + m_modified = false; + return true; + } else return false; + } + int search(juce::String text, bool reverse = false) { return m_editor->search(text, reverse); } bool hasFocus() { juce::Component *focus = m_editor->getCurrentlyFocusedComponent(); return focus == m_editor.get(); }; - juce::String getLineAt(int x, int y) const { return m_editor->getLineAt(x, y); }; - CodeEditor* getVisibleComponent() { return m_editor.get(); }; + juce::String getLineAt(int x, int y) const { return m_editor->getLineAt(x, y); } + CodeEditor* getVisibleComponent() { return m_editor.get(); } - void setVisible(bool visible) { m_editor->setVisible(visible); }; + void setVisible(bool visible) { m_editor->setVisible(visible); } template - void setBounds(T&& arg) { m_editor->setBounds(std::forward(arg)); }; + void setBounds(T&& arg) { m_editor->setBounds(std::forward(arg)); } private: std::unique_ptr m_editor; std::unique_ptr m_document; + bool m_modified{false}; };