diff --git a/native/PluginProcessor.cpp b/native/PluginProcessor.cpp index 9602419..da3dec5 100644 --- a/native/PluginProcessor.cpp +++ b/native/PluginProcessor.cpp @@ -84,10 +84,7 @@ EffectsPluginProcessor::~EffectsPluginProcessor() //============================================================================== juce::AudioProcessorEditor* EffectsPluginProcessor::createEditor() { - auto indexFile = getAssetsDirectory().getChildFile("index.html"); - auto indexFilePath = juce::URL(indexFile).toString(false).toStdString(); - - return new WebViewEditor(this, indexFilePath, 800, 704); + return new WebViewEditor(this, getAssetsDirectory(), 800, 704); } bool EffectsPluginProcessor::hasEditor() const diff --git a/native/WebViewEditor.cpp b/native/WebViewEditor.cpp index eb6fbb2..be9a166 100644 --- a/native/WebViewEditor.cpp +++ b/native/WebViewEditor.cpp @@ -12,8 +12,21 @@ double numberFromChocValue(const choc::value::ValueView& v) { : (double) v.getInt64()))); } +std::string getMimeType(std::string const& ext) { + static std::unordered_map mimeTypes { + { ".html", "text/html" }, + { ".js", "application/javascript" }, + { ".css", "text/css" }, + }; + + if (mimeTypes.count(ext) > 0) + return mimeTypes.at(ext); + + return "application/octet-stream"; +} + //============================================================================== -WebViewEditor::WebViewEditor(juce::AudioProcessor* proc, std::string const& indexFilePath, int width, int height) +WebViewEditor::WebViewEditor(juce::AudioProcessor* proc, juce::File const& assetDirectory, int width, int height) : juce::AudioProcessorEditor(proc) { setSize(720, 444); @@ -24,6 +37,20 @@ WebViewEditor::WebViewEditor(juce::AudioProcessor* proc, std::string const& inde opts.enableDebugMode = true; #endif + opts.fetchResource = [=](const choc::ui::WebView::Options::Path& p) -> std::optional { + auto relPath = "." + (p == "/" ? "/index.html" : p); + auto f = assetDirectory.getChildFile(relPath); + juce::MemoryBlock mb; + + if (!f.existsAsFile() || !f.loadFileAsData(mb)) + return {}; + + return choc::ui::WebView::Options::Resource { + std::vector(mb.begin(), mb.end()), + getMimeType(f.getFileExtension().toStdString()) + }; + }; + webView = std::make_unique(opts); #if JUCE_MAC @@ -58,8 +85,6 @@ WebViewEditor::WebViewEditor(juce::AudioProcessor* proc, std::string const& inde return {}; }); - - webView->navigate(indexFilePath); } choc::ui::WebView* WebViewEditor::getWebViewPtr() diff --git a/native/WebViewEditor.h b/native/WebViewEditor.h index 9c46479..5504d10 100644 --- a/native/WebViewEditor.h +++ b/native/WebViewEditor.h @@ -13,7 +13,7 @@ class WebViewEditor : public juce::AudioProcessorEditor { public: //============================================================================== - WebViewEditor(juce::AudioProcessor* proc, std::string const& indexFilePath, int width, int height); + WebViewEditor(juce::AudioProcessor* proc, juce::File const& assetDirectory, int width, int height); //============================================================================== choc::ui::WebView* getWebViewPtr();