From 937244e2ac999b261b805a9291302b0038538236 Mon Sep 17 00:00:00 2001 From: scheffle Date: Sun, 15 Sep 2024 21:22:44 +0200 Subject: [PATCH] change the customization functions initialization --- .../standalone/source/testappdelegate.cpp | 37 ++++++++++- .../uidescription-scripting/uiscripting.cpp | 62 +++++++++++-------- 2 files changed, 71 insertions(+), 28 deletions(-) diff --git a/vstgui/standalone/examples/standalone/source/testappdelegate.cpp b/vstgui/standalone/examples/standalone/source/testappdelegate.cpp index b594d6739..003e2ece5 100644 --- a/vstgui/standalone/examples/standalone/source/testappdelegate.cpp +++ b/vstgui/standalone/examples/standalone/source/testappdelegate.cpp @@ -33,8 +33,11 @@ #include "vstgui/lib/ctexteditor.h" #ifdef VSTGUI_UISCRIPTING +#include "vstgui/uidescription/cstream.h" #include "vstgui/uidescription-scripting/uiscripting.h" +#include #include +#include #endif #include @@ -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; } @@ -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 (); #if MAC auto font = makeOwned ("Menlo", 12); diff --git a/vstgui/uidescription-scripting/uiscripting.cpp b/vstgui/uidescription-scripting/uiscripting.cpp index e850d1d54..19faedb1e 100644 --- a/vstgui/uidescription-scripting/uiscripting.cpp +++ b/vstgui/uidescription-scripting/uiscripting.cpp @@ -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); @@ -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 (); } @@ -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 (uiDesc, onExceptionFunc, readContentsFunc); + impl = + std::make_unique (uiDesc, std::move (onExceptionFunc), std::move (readContentsFunc)); } //------------------------------------------------------------------------ @@ -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)) @@ -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 (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 (desc, std::move (onScriptException), + std::move (readScriptContentsFunc)); auto viewFactory = std::make_unique (scripting.get (), originalFactory); auto result = impl->map.emplace (desc, std::make_pair (std::move (viewFactory), std::move (scripting))); @@ -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 ()); static ScriptingInternal::JavaScriptDrawableViewCreator jsViewCreator; UIViewFactory::registerViewCreator (jsViewCreator);