Skip to content

Commit 724a3c5

Browse files
committed
Add a way to invoke code from the GUI
1 parent ee654c3 commit 724a3c5

File tree

8 files changed

+62
-2
lines changed

8 files changed

+62
-2
lines changed

plugins/core-plugin.cc

+6
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,12 @@ namespace clap {
363363
});
364364
}
365365

366+
void CorePlugin::onGuiInvoke(
367+
const std::string &method,
368+
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) {
369+
std::cerr << "Unhandled gui method invocation: " << method << std::endl;
370+
}
371+
366372
#endif // CLAP_PLUGINS_HEADLESS
367373

368374
//------------------//

plugins/core-plugin.hh

+3
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ namespace clap {
180180
void onGuiWindowClosed(bool wasDestroyed) override;
181181
void onGuiUndo() override;
182182
void onGuiRedo() override;
183+
void onGuiInvoke(
184+
const std::string &method,
185+
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) override;
183186
#endif
184187

185188
//------------------------//

plugins/gui/abstract-gui-listener.hh

+9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
#pragma once
22

3+
#include <string>
4+
#include <variant>
5+
#include <vector>
6+
37
#include <clap/clap.h>
48

59
namespace clap {
10+
using InvocationArgumentType = std::variant<bool, int64_t, double, std::string>;
11+
using InvocationArgumentsType = std::vector<InvocationArgumentType>;
12+
613
// Listener for events produced by the Gui, to the destination of the plugin.
714
//
815
// Beaware that the callbacks happen on the plugin's GUI thread, not the host's main thread.
@@ -24,5 +31,7 @@ namespace clap {
2431

2532
virtual void onGuiUndo() = 0;
2633
virtual void onGuiRedo() = 0;
34+
35+
virtual void onGuiInvoke(const std::string &method, const InvocationArgumentsType &args) = 0;
2736
};
2837
} // namespace clap

plugins/gui/plugin-proxy.cc

+21-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "plugin-proxy.hh"
2-
#include "gui.hh"
32
#include "../modules/module.hh"
3+
#include "abstract-gui-listener.hh"
4+
#include "gui.hh"
45

56
namespace clap {
67
PluginProxy::PluginProxy(Gui &client) : super(&client), _client(client) {}
@@ -26,4 +27,23 @@ namespace clap {
2627
if (!it.second)
2728
it.first->second->redefine(info);
2829
}
30+
31+
void PluginProxy::invoke(const QString &method) {
32+
_client.guiListener().onGuiInvoke(method.toStdString(), {});
33+
}
34+
35+
void PluginProxy::invoke(const QString &method, const QVariantList &args) {
36+
InvocationArgumentsType targetArgs;
37+
for (auto &a : args) {
38+
if (get_if<double>(&a))
39+
targetArgs.push_back(get<double>(a));
40+
else if (get_if<int64_t>(&a))
41+
targetArgs.push_back(get<int64_t>(a));
42+
else if (get_if<bool>(&a))
43+
targetArgs.push_back(get<bool>(a));
44+
else if (get_if<QString>(&a))
45+
targetArgs.push_back(get<QString>(a).toStdString());
46+
}
47+
_client.guiListener().onGuiInvoke(method.toStdString(), targetArgs);
48+
}
2949
} // namespace clap

plugins/gui/plugin-proxy.hh

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ namespace clap {
2424
Q_INVOKABLE ParameterProxy *param(clap_id paramId);
2525
Q_INVOKABLE ParameterProxy *param(clap_id moduleId, clap_id paramId);
2626
Q_INVOKABLE QString toString() const;
27+
Q_INVOKABLE void invoke(const QString &method);
28+
Q_INVOKABLE void invoke(const QString &method, const QVariantList &args);
2729

2830
private:
2931
Gui &_client;

plugins/gui/qml/clap/skins/undo-test/main.qml

+4
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ Rectangle {
3131
text: undo.redoName
3232
}
3333
}
34+
Button {
35+
text: "Do something"
36+
onClicked: plugin.invoke("incrementState")
37+
}
3438
}
3539
}

plugins/plugs/undo-test/undo-test.cc

+10
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ namespace clap {
9292
delta.new_value = ++_state;
9393
_host.undoChangeMade("inc", &delta, sizeof(delta), true);
9494
}
95+
9596
bool UndoTest::init() noexcept {
9697
if (!super::init())
9798
return false;
@@ -101,4 +102,13 @@ namespace clap {
101102
}
102103
return true;
103104
}
105+
106+
void UndoTest::onGuiInvoke(
107+
const std::string &method,
108+
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) {
109+
if (method == "incrementState")
110+
incrementState();
111+
else
112+
super::onGuiInvoke(method, args);
113+
}
104114
} // namespace clap

plugins/plugs/undo-test/undo-test.hh

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <string>
43
#include <optional>
4+
#include <string>
55

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

@@ -25,6 +25,12 @@ namespace clap {
2525

2626
void incrementState();
2727

28+
#ifndef CLAP_PLUGINS_HEADLESS
29+
void onGuiInvoke(
30+
const std::string &method,
31+
const std::vector<std::variant<bool, int64_t, double, std::string>> &args) override;
32+
#endif
33+
2834
private:
2935
uint32_t _state{0};
3036
};

0 commit comments

Comments
 (0)