Skip to content

Commit

Permalink
change the customization functions initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
scheffle committed Sep 15, 2024
1 parent 7eaec12 commit 937244e
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 28 deletions.
37 changes: 34 additions & 3 deletions vstgui/standalone/examples/standalone/source/testappdelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,11 @@
#include "vstgui/lib/ctexteditor.h"

#ifdef VSTGUI_UISCRIPTING
#include "vstgui/uidescription/cstream.h"
#include "vstgui/uidescription-scripting/uiscripting.h"
#include <filesystem>
#include <iostream>
#include <fstream>
#endif

#include <memory>
Expand Down Expand Up @@ -419,9 +422,6 @@ class DatePickerController : public DelegationController
Delegate::Delegate ()
: Application::DelegateAdapter ({"VSTGUI Standalone", "1.0.0", VSTGUI_STANDALONE_APP_URI})
{
#ifdef VSTGUI_UISCRIPTING
UIScripting::init ();
#endif
CFrame::kDefaultKnobMode = CKnobMode::kLinearMode;
}

Expand All @@ -430,6 +430,37 @@ Delegate::~Delegate () = default;
//------------------------------------------------------------------------
void Delegate::finishLaunching ()
{
#ifdef VSTGUI_UISCRIPTING
UIScripting::ReadScriptContentsFunc loadScriptFromRepositoryPath = {};
#if DEBUG
// in Debug mode, we want to load the scripts from the repository instead of from the app
// resource folder as the scripts in the app resource folder are only synchronized when we build
// the app and not in-between.
loadScriptFromRepositoryPath = [] (auto filename) -> std::string {
std::filesystem::path path (__FILE__);
if (!path.empty ())
{
path = path.parent_path ().parent_path ();
path.append ("resource");
path.append ("scripts");
path.append (filename);
if (std::filesystem::exists (path))
{
std::ifstream f (path, std::ios::in | std::ios::binary);
const auto sz = std::filesystem::file_size (path);
std::string result (sz, '\0');
f.read (result.data (), sz);
return result;
}
}
return {};
};
#endif

UIScripting::init ({}, loadScriptFromRepositoryPath);

#endif

textEditorController = std::make_unique<AppTextEditorController> ();
#if MAC
auto font = makeOwned<CFontDesc> ("Menlo", 12);
Expand Down
62 changes: 37 additions & 25 deletions vstgui/uidescription-scripting/uiscripting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class ScriptContext : public IScriptContextInternal
using OnScriptExceptionFunc = UIScripting::OnScriptExceptionFunc;
using ReadScriptContentsFunc = UIScripting::ReadScriptContentsFunc;

ScriptContext (IUIDescription* uiDesc, const OnScriptExceptionFunc& onExceptionFunc,
const ReadScriptContentsFunc& readContentsFunc);
ScriptContext (IUIDescription* uiDesc, OnScriptExceptionFunc&& onExceptionFunc,
ReadScriptContentsFunc&& readContentsFunc);
~ScriptContext () noexcept;

void init (const std::string& initScript);
Expand Down Expand Up @@ -105,9 +105,11 @@ struct ScriptContext::Impl : ViewListenerAdapter,

UIDescScriptObject uiDescObject;

Impl (IUIDescription* uiDesc, const OnScriptExceptionFunc& onExceptionFunc,
const ReadScriptContentsFunc& readContentsFunc)
: uiDesc (uiDesc), onScriptException (onExceptionFunc), readScriptContents (readContentsFunc)
Impl (IUIDescription* uiDesc, OnScriptExceptionFunc&& onExceptionFunc,
ReadScriptContentsFunc&& readContentsFunc)
: uiDesc (uiDesc)
, onScriptException (std::move (onExceptionFunc))
, readScriptContents (std::move (readContentsFunc))
{
init ();
}
Expand Down Expand Up @@ -614,10 +616,11 @@ struct ScriptContext::Impl : ViewListenerAdapter,
//------------------------------------------------------------------------
//------------------------------------------------------------------------
//------------------------------------------------------------------------
ScriptContext::ScriptContext (IUIDescription* uiDesc, const OnScriptExceptionFunc& onExceptionFunc,
const ReadScriptContentsFunc& readContentsFunc)
ScriptContext::ScriptContext (IUIDescription* uiDesc, OnScriptExceptionFunc&& onExceptionFunc,
ReadScriptContentsFunc&& readContentsFunc)
{
impl = std::make_unique<Impl> (uiDesc, onExceptionFunc, readContentsFunc);
impl =
std::make_unique<Impl> (uiDesc, std::move (onExceptionFunc), std::move (readContentsFunc));
}

//------------------------------------------------------------------------
Expand Down Expand Up @@ -670,15 +673,17 @@ struct UIScripting::Impl

static OnScriptExceptionFunc onScriptExceptionFunc;
static ReadScriptContentsFunc readScriptContentsFunc;

static std::string readScriptContentsFromResource (std::string_view filename);
};

//------------------------------------------------------------------------
UIScripting::OnScriptExceptionFunc UIScripting::Impl::onScriptExceptionFunc =
[] (std::string_view reason) {
std::cerr << reason << '\n';
};
UIScripting::ReadScriptContentsFunc UIScripting::Impl::readScriptContentsFunc =
[] (std::string_view filename) -> std::string {
UIScripting::OnScriptExceptionFunc UIScripting::Impl::onScriptExceptionFunc;
UIScripting::ReadScriptContentsFunc UIScripting::Impl::readScriptContentsFunc;

//------------------------------------------------------------------------
std::string UIScripting::Impl::readScriptContentsFromResource (std::string_view filename)
{
std::string fileStr (filename);
CResourceDescription desc (fileStr.data ());
if (auto stream = getPlatformFactory ().createResourceInputStream (desc))
Expand Down Expand Up @@ -736,8 +741,23 @@ IViewFactory* UIScripting::getViewFactory (IUIDescription* desc, IViewFactory* o
auto it = impl->map.find (desc);
if (it != impl->map.end ())
return it->second.first.get ();
auto scripting = std::make_unique<ScriptContext> (desc, Impl::onScriptExceptionFunc,
Impl::readScriptContentsFunc);
auto onScriptException = Impl::onScriptExceptionFunc;
if (!onScriptException)
onScriptException = [] (std::string_view reason) {
std::cerr << reason << '\n';
};
auto readScriptContentsFunc = [] (auto filename) {
std::string result;
if (Impl::readScriptContentsFunc)
result = Impl::readScriptContentsFunc (filename);
if (result.empty ())
{
result = Impl::readScriptContentsFromResource (filename);
}
return result;
};
auto scripting = std::make_unique<ScriptContext> (desc, std::move (onScriptException),
std::move (readScriptContentsFunc));
auto viewFactory = std::make_unique<JavaScriptViewFactory> (scripting.get (), originalFactory);
auto result =
impl->map.emplace (desc, std::make_pair (std::move (viewFactory), std::move (scripting)));
Expand Down Expand Up @@ -770,15 +790,7 @@ void UIScripting::init (const OnScriptExceptionFunc& onExceptionFunc,
if (onExceptionFunc)
Impl::onScriptExceptionFunc = onExceptionFunc;
if (readScriptContentsFunc)
{
auto readFromResources = Impl::readScriptContentsFunc;
Impl::readScriptContentsFunc = [=] (std::string_view filename) {
auto result = readScriptContentsFunc (filename);
if (result.empty ())
result = readFromResources (filename);
return result;
};
}
Impl::readScriptContentsFunc = readScriptContentsFunc;
UIDescriptionAddOnRegistry::add (std::make_unique<UIScripting> ());
static ScriptingInternal::JavaScriptDrawableViewCreator jsViewCreator;
UIViewFactory::registerViewCreator (jsViewCreator);
Expand Down

0 comments on commit 937244e

Please sign in to comment.