Skip to content

Commit

Permalink
Add a way to invoke code from the GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
abique committed Sep 17, 2024
1 parent ee654c3 commit 724a3c5
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 2 deletions.
6 changes: 6 additions & 0 deletions plugins/core-plugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,12 @@ namespace clap {
});
}

void CorePlugin::onGuiInvoke(
const std::string &method,
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) {
std::cerr << "Unhandled gui method invocation: " << method << std::endl;
}

#endif // CLAP_PLUGINS_HEADLESS

//------------------//
Expand Down
3 changes: 3 additions & 0 deletions plugins/core-plugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::variant<bool, int64_t, double, std::string>> &args) override;
#endif

//------------------------//
Expand Down
9 changes: 9 additions & 0 deletions plugins/gui/abstract-gui-listener.hh
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
#pragma once

#include <string>
#include <variant>
#include <vector>

#include <clap/clap.h>

namespace clap {
using InvocationArgumentType = std::variant<bool, int64_t, double, std::string>;
using InvocationArgumentsType = std::vector<InvocationArgumentType>;

// 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.
Expand All @@ -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
22 changes: 21 additions & 1 deletion plugins/gui/plugin-proxy.cc
Original file line number Diff line number Diff line change
@@ -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) {}
Expand All @@ -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<double>(&a))
targetArgs.push_back(get<double>(a));
else if (get_if<int64_t>(&a))
targetArgs.push_back(get<int64_t>(a));
else if (get_if<bool>(&a))
targetArgs.push_back(get<bool>(a));
else if (get_if<QString>(&a))
targetArgs.push_back(get<QString>(a).toStdString());
}
_client.guiListener().onGuiInvoke(method.toStdString(), targetArgs);
}
} // namespace clap
2 changes: 2 additions & 0 deletions plugins/gui/plugin-proxy.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions plugins/gui/qml/clap/skins/undo-test/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ Rectangle {
text: undo.redoName
}
}
Button {
text: "Do something"
onClicked: plugin.invoke("incrementState")
}
}
}
10 changes: 10 additions & 0 deletions plugins/plugs/undo-test/undo-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -101,4 +102,13 @@ namespace clap {
}
return true;
}

void UndoTest::onGuiInvoke(
const std::string &method,
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) {
if (method == "incrementState")
incrementState();
else
super::onGuiInvoke(method, args);
}
} // namespace clap
8 changes: 7 additions & 1 deletion plugins/plugs/undo-test/undo-test.hh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <optional>
#include <string>

#include "../../core-plugin.hh"

Expand All @@ -25,6 +25,12 @@ namespace clap {

void incrementState();

#ifndef CLAP_PLUGINS_HEADLESS
void onGuiInvoke(
const std::string &method,
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) override;
#endif

private:
uint32_t _state{0};
};
Expand Down

0 comments on commit 724a3c5

Please sign in to comment.