Skip to content

Commit

Permalink
listener
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 24, 2024
1 parent d69f68c commit a3fd3fb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 14 deletions.
7 changes: 7 additions & 0 deletions plugin/components/ide_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -287,6 +293,7 @@ void YsfxIDEView::Impl::openDocument(juce::File file)

auto editor = addEditor();
editor->loadFile(file);

setCurrentEditor(m_editors.size() - 1);
}

Expand Down
48 changes: 34 additions & 14 deletions plugin/components/ysfx_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,66 @@ class CodeEditor : public juce::CodeEditorComponent
};


class YSFXCodeEditor
class YSFXCodeEditor : public juce::CodeDocument::Listener
{
public:
YSFXCodeEditor(juce::CodeTokeniser* tokenizer, std::function<bool(const juce::KeyPress&)> keyPressCallback, std::function<bool(int x, int y)> dblClickCallback) {
m_document = std::make_unique<YSFXCodeDocument>();
m_document->addListener(this);
m_editor = std::make_unique<CodeEditor>(*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 <typename T>
void setBounds(T&& arg) { m_editor->setBounds(std::forward<T>(arg)); };
void setBounds(T&& arg) { m_editor->setBounds(std::forward<T>(arg)); }

private:
std::unique_ptr<CodeEditor> m_editor;
std::unique_ptr<YSFXCodeDocument> m_document;
bool m_modified{false};
};


Expand Down

0 comments on commit a3fd3fb

Please sign in to comment.