C++ interpreter: do the renaming invoke_callback -> invoke

This commit is contained in:
Olivier Goffart 2022-12-23 11:20:57 +01:00 committed by Olivier Goffart
parent 16fdd0595b
commit 629a64ba89
4 changed files with 66 additions and 29 deletions

View file

@ -619,7 +619,7 @@ public:
return {};
}
}
/// Invoke the specified callback declared in .slint with the given arguments
/// Invoke the specified callback or function declared in .slint with the given arguments
///
/// Example: imagine the .slint file contains the given callback declaration:
/// ```
@ -628,20 +628,20 @@ public:
/// Then one can call it with this function
/// ```
/// slint::Value args[] = { SharedString("Hello"), 42. };
/// instance->invoke_callback("foo", { args, 2 });
/// instance->invoke("foo", { args, 2 });
/// ```
///
/// Returns an null optional if the callback don't exist or if the argument don't match
/// Otherwise return the returned value from the callback, which may be an empty Value if
/// the callback did not return a value.
std::optional<Value> invoke_callback(std::string_view name, std::span<const Value> args) const
std::optional<Value> invoke(std::string_view name, std::span<const Value> args) const
{
using namespace cbindgen_private;
Slice<ValueOpaque> args_view { const_cast<ValueOpaque *>(
reinterpret_cast<const ValueOpaque *>(args.data())),
args.size() };
ValueOpaque out;
if (slint_interpreter_component_instance_invoke_callback(
if (slint_interpreter_component_instance_invoke(
inner(), slint::private_api::string_to_slice(name), args_view, &out)) {
return Value(out);
} else {
@ -649,6 +649,13 @@ public:
}
}
/// \deprecated rename to invoke()
[[deprecated("renamed to invoke()")]] std::optional<Value>
invoke_callback(std::string_view name, std::span<const Value> args) const
{
return invoke(name, args);
}
/// Set a handler for the callback with the given name.
///
/// A callback with that name must be defined in the document otherwise the function
@ -757,24 +764,31 @@ public:
[](void *data) { delete reinterpret_cast<F *>(data); });
}
/// Invoke the specified callback declared in an exported global singleton
std::optional<Value> invoke_global_callback(std::string_view global,
std::string_view callback_name,
std::span<const Value> args) const
/// Invoke the specified callback or function declared in an exported global singleton
std::optional<Value> invoke_global(std::string_view global, std::string_view callable_name,
std::span<const Value> args) const
{
using namespace cbindgen_private;
Slice<ValueOpaque> args_view { const_cast<ValueOpaque *>(
reinterpret_cast<const ValueOpaque *>(args.data())),
args.size() };
ValueOpaque out;
if (slint_interpreter_component_instance_invoke_global_callback(
if (slint_interpreter_component_instance_invoke_global(
inner(), slint::private_api::string_to_slice(global),
slint::private_api::string_to_slice(callback_name), args_view, &out)) {
slint::private_api::string_to_slice(callable_name), args_view, &out)) {
return Value(out);
} else {
return {};
}
}
/// \deprecated renamed to invoke_global
[[deprecated("renamed to invoke_global()")]] std::optional<Value>
invoke_global_callback(std::string_view global, std::string_view callback_name,
std::span<const Value> args) const
{
return invoke_global(global, callback_name, args);
}
};
/// ComponentDefinition is a representation of a compiled component from .slint markup.
@ -841,7 +855,7 @@ public:
}
/// Returns a vector of strings that describe the list of public callbacks that can be invoked
/// using ComponentInstance::invoke_callback and set using ComponentInstance::set_callback.
/// using ComponentInstance::invoke and set using ComponentInstance::set_callback.
slint::SharedVector<slint::SharedString> callbacks() const
{
slint::SharedVector<slint::SharedString> callbacks;

View file

@ -369,7 +369,7 @@ SCENARIO("Invoke callback")
return Value(SharedString(res));
}));
Value args[] = { SharedString("Hello"), 42. };
auto res = instance->invoke_callback("some_callback", args);
auto res = instance->invoke("some_callback", args);
REQUIRE(res.has_value());
REQUIRE(*res->to_string() == SharedString("Hello:42_string_on_the_stack_"));
}
@ -382,7 +382,7 @@ SCENARIO("Invoke callback")
auto instance = result->create();
REQUIRE(!instance->set_callback("bar", [](auto) { return Value(); }));
Value args[] = { SharedString("Hello"), 42. };
auto res = instance->invoke_callback("bar", args);
auto res = instance->invoke("bar", args);
REQUIRE(!res.has_value());
}
}
@ -505,6 +505,7 @@ SCENARIO("Global properties")
export global The-Global := {
property <string> the-property: "€€€";
callback to_uppercase(string)->string;
public function ff() -> string { return the-property; }
}
export Dummy := Rectangle {
property <string> result: The-Global.to_uppercase("abc");
@ -578,7 +579,7 @@ SCENARIO("Global properties")
REQUIRE(result->to_string().value() == "ABC");
Value args[] = { SharedString("Hello") };
auto res = instance->invoke_global_callback("The_Global", "to-uppercase", args);
auto res = instance->invoke_global("The_Global", "to-uppercase", args);
REQUIRE(res.has_value());
REQUIRE(*res->to_string() == SharedString("HELLO"));
}
@ -588,7 +589,14 @@ SCENARIO("Global properties")
[](auto) { return Value {}; }));
REQUIRE(!instance->set_global_callback("The-Global", "touppercase",
[](auto) { return Value {}; }));
REQUIRE(!instance->invoke_global_callback("TheGlobal", "touppercase", {}));
REQUIRE(!instance->invoke_global_callback("The-Global", "touppercase", {}));
REQUIRE(!instance->invoke_global("TheGlobal", "touppercase", {}));
REQUIRE(!instance->invoke_global("The-Global", "touppercase", {}));
}
SECTION("invoke function")
{
REQUIRE(instance->set_global_property("The-Global", "the-property", SharedString("&&&")));
auto res = instance->invoke_global("The_Global", "ff", {});
REQUIRE(res.has_value());
REQUIRE(*res->to_string() == SharedString("&&&"));
}
}