Skip to content

Commit

Permalink
ide: show asterisk for unsaved files
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Dec 25, 2024
1 parent c192c01 commit 46a435a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 16 deletions.
13 changes: 11 additions & 2 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->getDisplayName());
++idx;
}
});
m_impl->m_fileCheckTimer.reset(timer);
timer->startTimer(100);
Expand All @@ -170,7 +176,9 @@ void YsfxIDEView::Impl::setupNewFx()
}
else {
juce::File file{juce::CharPointer_UTF8{ysfx_get_file_path(fx)}};
m_editors[0]->loadFile(file);
if ((m_editors[0]->getPath() != file) || (!m_editors[0]->wasModified())) {
m_editors[0]->loadFile(file);
};

m_vars.ensureStorageAllocated(64);

Expand Down Expand Up @@ -287,6 +295,7 @@ void YsfxIDEView::Impl::openDocument(juce::File file)

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

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

Expand Down Expand Up @@ -458,7 +467,7 @@ void YsfxIDEView::Impl::relayoutUI()
m_tabs->clearTabs();
int idx = 0;
for (const auto m : m_editors) {
m_tabs->addTab(m->getName(), m_self->getLookAndFeel().findColour(m_btnSave->buttonColourId), idx);
m_tabs->addTab(m->getDisplayName(), m_self->getLookAndFeel().findColour(m_btnSave->buttonColourId), idx);
++idx;
}
m_tabs->setCurrentTabIndex(m_currentEditorIndex, false);
Expand Down
52 changes: 38 additions & 14 deletions plugin/components/ysfx_document.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,46 +124,70 @@ 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(); }
juce::String getDisplayName() { return m_document->getName() + (m_modified ? "*" : ""); }
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 46a435a

Please sign in to comment.