From 88daa6202c0df542dbccb8101013b3ad34817f02 Mon Sep 17 00:00:00 2001 From: Joep Vanlier Date: Sun, 8 Dec 2024 02:12:57 +0100 Subject: [PATCH] plugin: open save as dialog if no file loaded --- plugin/components/ide_view.cpp | 66 ++++++++++++++++++++++++++++++---- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/plugin/components/ide_view.cpp b/plugin/components/ide_view.cpp index bf01535..0f10e9d 100644 --- a/plugin/components/ide_view.cpp +++ b/plugin/components/ide_view.cpp @@ -37,6 +37,8 @@ struct YsfxIDEView::Impl { std::unique_ptr m_lblStatus; std::unique_ptr m_relayoutTimer; std::unique_ptr m_fileCheckTimer; + std::unique_ptr m_fileChooser; + bool m_fileChooserActive{false}; struct VariableUI { ysfx_real *m_var = nullptr; @@ -52,6 +54,8 @@ struct YsfxIDEView::Impl { //========================================================================== void setupNewFx(); void saveCurrentFile(); + void saveFile(juce::File filename); + void saveAs(); void checkFileForModifications(); //========================================================================== @@ -197,21 +201,57 @@ void YsfxIDEView::Impl::setupNewFx() } } -void YsfxIDEView::Impl::saveCurrentFile() +void YsfxIDEView::Impl::saveAs() { - ysfx_t *fx = m_fx.get(); - if (!fx) - return; + if (m_fileChooserActive) return; - juce::File file{juce::CharPointer_UTF8{ysfx_get_file_path(fx)}}; + juce::File initialPath; + juce::File prevFilePath{juce::CharPointer_UTF8{ysfx_get_file_path(m_fx.get())}}; + if (prevFilePath != juce::File{}) { + initialPath = prevFilePath.getParentDirectory(); + } + + m_fileChooser.reset(new juce::FileChooser(TRANS("Choose filename to save JSFX to"), initialPath)); + m_fileChooser->launchAsync( + juce::FileBrowserComponent::saveMode|juce::FileBrowserComponent::canSelectFiles, + [this](const juce::FileChooser &chooser) { + juce::File chosenFile = chooser.getResult(); + if (chosenFile != juce::File()) { + if (chosenFile.exists()) { + juce::AlertWindow::showAsync( + juce::MessageBoxOptions{} + .withParentComponent(m_self) + .withIconType(juce::MessageBoxIconType::QuestionIcon) + .withTitle(TRANS("Overwrite?")) + .withButton(TRANS("Yes")) + .withButton(TRANS("No")) + .withMessage(TRANS("File already exists! Overwrite?")), + [this, chosenFile](int result) { + if (result == 1) { + this->saveFile(chosenFile); + m_self->onFileSaved(chosenFile); + }; + } + ); + } else { + saveFile(chosenFile); + m_self->onFileSaved(chosenFile); + } + } + m_fileChooserActive = false; + } + ); +} +void YsfxIDEView::Impl::saveFile(juce::File file) +{ const juce::String content = m_document->getAllContent(); bool success = file.replaceWithData(content.toRawUTF8(), content.getNumBytesAsUTF8()); if (!success) { juce::AlertWindow::showAsync( juce::MessageBoxOptions{} - .withAssociatedComponent(m_self) + .withParentComponent(m_self) .withIconType(juce::MessageBoxIconType::WarningIcon) .withTitle(TRANS("Error")) .withButton(TRANS("OK")) @@ -226,6 +266,20 @@ void YsfxIDEView::Impl::saveCurrentFile() m_self->onFileSaved(file); } +void YsfxIDEView::Impl::saveCurrentFile() +{ + ysfx_t *fx = m_fx.get(); + if (!fx) + return; + + juce::File file = juce::File{juce::CharPointer_UTF8{ysfx_get_file_path(fx)}}; + if (file.existsAsFile()) { + saveFile(file); + } else { + saveAs(); + } +} + void YsfxIDEView::Impl::checkFileForModifications() { ysfx_t *fx = m_fx.get();