mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 14:21:16 +00:00
Add C++ API to introspect exported global singletons in the interpreter
This adds the necessary shims to expose the same API as Rust.
This commit is contained in:
parent
a855d868fc
commit
c3d0fd04af
3 changed files with 116 additions and 4 deletions
|
@ -888,6 +888,42 @@ public:
|
||||||
cbindgen_private::sixtyfps_interpreter_component_definition_name(&inner, &name);
|
cbindgen_private::sixtyfps_interpreter_component_definition_name(&inner, &name);
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of strings with the names of all exported global singletons.
|
||||||
|
sixtyfps::SharedVector<sixtyfps::SharedString> globals() const
|
||||||
|
{
|
||||||
|
sixtyfps::SharedVector<sixtyfps::SharedString> names;
|
||||||
|
cbindgen_private::sixtyfps_interpreter_component_definition_globals(&inner, &names);
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of the property descriptors of the properties of the specified
|
||||||
|
/// publicly exported global singleton. An empty optional is returned if there exists no
|
||||||
|
/// exported global singleton under the specified name.
|
||||||
|
std::optional<sixtyfps::SharedVector<PropertyDescriptor>>
|
||||||
|
global_properties(std::string_view global_name) const
|
||||||
|
{
|
||||||
|
sixtyfps::SharedVector<PropertyDescriptor> properties;
|
||||||
|
if (cbindgen_private::sixtyfps_interpreter_component_definition_global_properties(
|
||||||
|
&inner, sixtyfps::private_api::string_to_slice(global_name), &properties)) {
|
||||||
|
return properties;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of the names of the callbacks of the specified publicly exported global
|
||||||
|
/// singleton. An empty optional is returned if there exists no exported global singleton
|
||||||
|
/// under the specified name.
|
||||||
|
std::optional<sixtyfps::SharedVector<sixtyfps::SharedString>>
|
||||||
|
global_callbacks(std::string_view global_name) const
|
||||||
|
{
|
||||||
|
sixtyfps::SharedVector<sixtyfps::SharedString> names;
|
||||||
|
if (cbindgen_private::sixtyfps_interpreter_component_definition_global_callbacks(
|
||||||
|
&inner, sixtyfps::private_api::string_to_slice(global_name), &names)) {
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#if !defined(DOXYGEN)
|
#if !defined(DOXYGEN)
|
||||||
|
|
|
@ -101,7 +101,8 @@ SCENARIO("Value API")
|
||||||
sixtyfps::private_api::WindowRc wnd;
|
sixtyfps::private_api::WindowRc wnd;
|
||||||
|
|
||||||
REQUIRE(!value.to_image().has_value());
|
REQUIRE(!value.to_image().has_value());
|
||||||
sixtyfps::Image image = sixtyfps::Image::load_from_path(SOURCE_DIR "/../../vscode_extension/extension-logo.png");
|
sixtyfps::Image image = sixtyfps::Image::load_from_path(
|
||||||
|
SOURCE_DIR "/../../vscode_extension/extension-logo.png");
|
||||||
REQUIRE(image.size().width == 128);
|
REQUIRE(image.size().width == 128);
|
||||||
value = Value(image);
|
value = Value(image);
|
||||||
REQUIRE(value.type() == Value::Type::Image);
|
REQUIRE(value.type() == Value::Type::Image);
|
||||||
|
@ -433,8 +434,10 @@ SCENARIO("Angle between .60 and C++")
|
||||||
|
|
||||||
ComponentCompiler compiler;
|
ComponentCompiler compiler;
|
||||||
|
|
||||||
auto result = compiler.build_from_source(
|
auto result =
|
||||||
"export Dummy := Rectangle { property <angle> the_angle: 0.25turn; property <bool> test: the_angle == 0.5turn; }", "");
|
compiler.build_from_source("export Dummy := Rectangle { property <angle> the_angle: "
|
||||||
|
"0.25turn; property <bool> test: the_angle == 0.5turn; }",
|
||||||
|
"");
|
||||||
REQUIRE(result.has_value());
|
REQUIRE(result.has_value());
|
||||||
auto instance = result->create();
|
auto instance = result->create();
|
||||||
|
|
||||||
|
@ -513,7 +516,30 @@ SCENARIO("Global properties")
|
||||||
for (auto &&x : compiler.diagnostics())
|
for (auto &&x : compiler.diagnostics())
|
||||||
std::cerr << x.message << std::endl;
|
std::cerr << x.message << std::endl;
|
||||||
REQUIRE(result.has_value());
|
REQUIRE(result.has_value());
|
||||||
auto instance = result->create();
|
auto component_definition = *result;
|
||||||
|
|
||||||
|
SECTION("Globals introspection")
|
||||||
|
{
|
||||||
|
auto globals = component_definition.globals();
|
||||||
|
REQUIRE(globals.size() == 1);
|
||||||
|
REQUIRE(globals[0] == "The-Global");
|
||||||
|
|
||||||
|
REQUIRE(!component_definition.global_properties("not there").has_value());
|
||||||
|
|
||||||
|
REQUIRE(component_definition.global_properties("The_Global").has_value());
|
||||||
|
REQUIRE(component_definition.global_properties("The-Global").has_value());
|
||||||
|
|
||||||
|
auto properties = *component_definition.global_properties("The-Global");
|
||||||
|
REQUIRE(properties.size() == 1);
|
||||||
|
REQUIRE(properties[0].property_name == "the-property");
|
||||||
|
REQUIRE(properties[0].property_type == Value::Type::String);
|
||||||
|
|
||||||
|
auto callbacks = *component_definition.global_callbacks("The-Global");
|
||||||
|
REQUIRE(callbacks.size() == 1);
|
||||||
|
REQUIRE(callbacks[0] == "to_uppercase");
|
||||||
|
}
|
||||||
|
|
||||||
|
auto instance = component_definition.create();
|
||||||
|
|
||||||
SECTION("Invalid read")
|
SECTION("Invalid read")
|
||||||
{
|
{
|
||||||
|
|
|
@ -890,3 +890,53 @@ pub unsafe extern "C" fn sixtyfps_interpreter_component_definition_name(
|
||||||
) {
|
) {
|
||||||
*name = (&*def).as_component_definition().name().into()
|
*name = (&*def).as_component_definition().name().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of strings with the names of all exported global singletons.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn sixtyfps_interpreter_component_definition_globals(
|
||||||
|
def: &ComponentDefinitionOpaque,
|
||||||
|
names: &mut SharedVector<SharedString>,
|
||||||
|
) {
|
||||||
|
names.extend((&*def).as_component_definition().globals().map(|name| name.into()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of the property descriptors of the properties of the specified publicly exported global
|
||||||
|
/// singleton. Returns true if a global exists under the specified name; false otherwise.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn sixtyfps_interpreter_component_definition_global_properties(
|
||||||
|
def: &ComponentDefinitionOpaque,
|
||||||
|
global_name: Slice<u8>,
|
||||||
|
properties: &mut SharedVector<PropertyDescriptor>,
|
||||||
|
) -> bool {
|
||||||
|
if let Some(property_it) = (&*def)
|
||||||
|
.as_component_definition()
|
||||||
|
.global_properties(std::str::from_utf8(&global_name).unwrap())
|
||||||
|
{
|
||||||
|
properties.extend(property_it.map(|(property_name, property_type)| PropertyDescriptor {
|
||||||
|
property_name: property_name.into(),
|
||||||
|
property_type,
|
||||||
|
}));
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns a vector of the names of the callbacks of the specified publicly exported global
|
||||||
|
/// singleton. Returns true if a global exists under the specified name; false otherwise.
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn sixtyfps_interpreter_component_definition_global_callbacks(
|
||||||
|
def: &ComponentDefinitionOpaque,
|
||||||
|
global_name: Slice<u8>,
|
||||||
|
names: &mut SharedVector<SharedString>,
|
||||||
|
) -> bool {
|
||||||
|
if let Some(name_it) = (&*def)
|
||||||
|
.as_component_definition()
|
||||||
|
.global_callbacks(std::str::from_utf8(&global_name).unwrap())
|
||||||
|
{
|
||||||
|
names.extend(name_it.map(|name| name.into()));
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue