Skip to content

Commit

Permalink
Serve files from chocs resource loader
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-thompson committed Nov 16, 2023
1 parent ce37c3f commit fb134d0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
5 changes: 1 addition & 4 deletions native/PluginProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 28 additions & 3 deletions native/WebViewEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> 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);
Expand All @@ -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<choc::ui::WebView::Options::Resource> {
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<uint8_t>(mb.begin(), mb.end()),
getMimeType(f.getFileExtension().toStdString())
};
};

webView = std::make_unique<choc::ui::WebView>(opts);

#if JUCE_MAC
Expand Down Expand Up @@ -58,8 +85,6 @@ WebViewEditor::WebViewEditor(juce::AudioProcessor* proc, std::string const& inde

return {};
});

webView->navigate(indexFilePath);
}

choc::ui::WebView* WebViewEditor::getWebViewPtr()
Expand Down
2 changes: 1 addition & 1 deletion native/WebViewEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit fb134d0

Please sign in to comment.