From 724a3c55b8d291aa03065b86b0c803387eaf058e Mon Sep 17 00:00:00 2001 From: Alexandre Bique Date: Tue, 17 Sep 2024 12:48:49 +0200 Subject: [PATCH] Add a way to invoke code from the GUI --- plugins/core-plugin.cc | 6 +++++ plugins/core-plugin.hh | 3 +++ plugins/gui/abstract-gui-listener.hh | 9 ++++++++ plugins/gui/plugin-proxy.cc | 22 ++++++++++++++++++- plugins/gui/plugin-proxy.hh | 2 ++ plugins/gui/qml/clap/skins/undo-test/main.qml | 4 ++++ plugins/plugs/undo-test/undo-test.cc | 10 +++++++++ plugins/plugs/undo-test/undo-test.hh | 8 ++++++- 8 files changed, 62 insertions(+), 2 deletions(-) diff --git a/plugins/core-plugin.cc b/plugins/core-plugin.cc index 30bbe92..cfd302e 100644 --- a/plugins/core-plugin.cc +++ b/plugins/core-plugin.cc @@ -363,6 +363,12 @@ namespace clap { }); } + void CorePlugin::onGuiInvoke( + const std::string &method, + const std::vector> &args) { + std::cerr << "Unhandled gui method invocation: " << method << std::endl; + } + #endif // CLAP_PLUGINS_HEADLESS //------------------// diff --git a/plugins/core-plugin.hh b/plugins/core-plugin.hh index c926fee..2d259a4 100644 --- a/plugins/core-plugin.hh +++ b/plugins/core-plugin.hh @@ -180,6 +180,9 @@ namespace clap { void onGuiWindowClosed(bool wasDestroyed) override; void onGuiUndo() override; void onGuiRedo() override; + void onGuiInvoke( + const std::string &method, + const std::vector> &args) override; #endif //------------------------// diff --git a/plugins/gui/abstract-gui-listener.hh b/plugins/gui/abstract-gui-listener.hh index 261fb07..b94ecae 100644 --- a/plugins/gui/abstract-gui-listener.hh +++ b/plugins/gui/abstract-gui-listener.hh @@ -1,8 +1,15 @@ #pragma once +#include +#include +#include + #include namespace clap { + using InvocationArgumentType = std::variant; + using InvocationArgumentsType = std::vector; + // Listener for events produced by the Gui, to the destination of the plugin. // // Beaware that the callbacks happen on the plugin's GUI thread, not the host's main thread. @@ -24,5 +31,7 @@ namespace clap { virtual void onGuiUndo() = 0; virtual void onGuiRedo() = 0; + + virtual void onGuiInvoke(const std::string &method, const InvocationArgumentsType &args) = 0; }; } // namespace clap \ No newline at end of file diff --git a/plugins/gui/plugin-proxy.cc b/plugins/gui/plugin-proxy.cc index 927cefa..4fa48c4 100644 --- a/plugins/gui/plugin-proxy.cc +++ b/plugins/gui/plugin-proxy.cc @@ -1,6 +1,7 @@ #include "plugin-proxy.hh" -#include "gui.hh" #include "../modules/module.hh" +#include "abstract-gui-listener.hh" +#include "gui.hh" namespace clap { PluginProxy::PluginProxy(Gui &client) : super(&client), _client(client) {} @@ -26,4 +27,23 @@ namespace clap { if (!it.second) it.first->second->redefine(info); } + + void PluginProxy::invoke(const QString &method) { + _client.guiListener().onGuiInvoke(method.toStdString(), {}); + } + + void PluginProxy::invoke(const QString &method, const QVariantList &args) { + InvocationArgumentsType targetArgs; + for (auto &a : args) { + if (get_if(&a)) + targetArgs.push_back(get(a)); + else if (get_if(&a)) + targetArgs.push_back(get(a)); + else if (get_if(&a)) + targetArgs.push_back(get(a)); + else if (get_if(&a)) + targetArgs.push_back(get(a).toStdString()); + } + _client.guiListener().onGuiInvoke(method.toStdString(), targetArgs); + } } // namespace clap \ No newline at end of file diff --git a/plugins/gui/plugin-proxy.hh b/plugins/gui/plugin-proxy.hh index d918330..0dc4a56 100644 --- a/plugins/gui/plugin-proxy.hh +++ b/plugins/gui/plugin-proxy.hh @@ -24,6 +24,8 @@ namespace clap { Q_INVOKABLE ParameterProxy *param(clap_id paramId); Q_INVOKABLE ParameterProxy *param(clap_id moduleId, clap_id paramId); Q_INVOKABLE QString toString() const; + Q_INVOKABLE void invoke(const QString &method); + Q_INVOKABLE void invoke(const QString &method, const QVariantList &args); private: Gui &_client; diff --git a/plugins/gui/qml/clap/skins/undo-test/main.qml b/plugins/gui/qml/clap/skins/undo-test/main.qml index 1990fd1..30ea7bc 100644 --- a/plugins/gui/qml/clap/skins/undo-test/main.qml +++ b/plugins/gui/qml/clap/skins/undo-test/main.qml @@ -31,5 +31,9 @@ Rectangle { text: undo.redoName } } + Button { + text: "Do something" + onClicked: plugin.invoke("incrementState") + } } } diff --git a/plugins/plugs/undo-test/undo-test.cc b/plugins/plugs/undo-test/undo-test.cc index b1a5dab..be36ca0 100644 --- a/plugins/plugs/undo-test/undo-test.cc +++ b/plugins/plugs/undo-test/undo-test.cc @@ -92,6 +92,7 @@ namespace clap { delta.new_value = ++_state; _host.undoChangeMade("inc", &delta, sizeof(delta), true); } + bool UndoTest::init() noexcept { if (!super::init()) return false; @@ -101,4 +102,13 @@ namespace clap { } return true; } + + void UndoTest::onGuiInvoke( + const std::string &method, + const std::vector> &args) { + if (method == "incrementState") + incrementState(); + else + super::onGuiInvoke(method, args); + } } // namespace clap diff --git a/plugins/plugs/undo-test/undo-test.hh b/plugins/plugs/undo-test/undo-test.hh index b7076c0..d3f923a 100644 --- a/plugins/plugs/undo-test/undo-test.hh +++ b/plugins/plugs/undo-test/undo-test.hh @@ -1,7 +1,7 @@ #pragma once -#include #include +#include #include "../../core-plugin.hh" @@ -25,6 +25,12 @@ namespace clap { void incrementState(); +#ifndef CLAP_PLUGINS_HEADLESS + void onGuiInvoke( + const std::string &method, + const std::vector> &args) override; +#endif + private: uint32_t _state{0}; };