Add C++ API for find_by_element_type_name

This commit is contained in:
Simon Hausmann 2024-05-17 17:26:01 +02:00 committed by Simon Hausmann
parent 1a8ce3c659
commit 7af0e7ae7c
2 changed files with 29 additions and 0 deletions

View file

@ -60,6 +60,23 @@ public:
return result; return result;
} }
/// Find all elements matching the given type name.
template<typename T>
static SharedVector<ElementHandle>
find_by_element_type_name(const ComponentHandle<T> &component, std::string_view type_name)
{
cbindgen_private::Slice<uint8_t> element_type_name_view {
const_cast<unsigned char *>(reinterpret_cast<const unsigned char *>(type_name.data())),
type_name.size()
};
auto vrc = component.into_dyn();
SharedVector<ElementHandle> result;
cbindgen_private::slint_testing_element_find_by_element_type_name(
&vrc, &element_type_name_view,
reinterpret_cast<SharedVector<cbindgen_private::ElementHandle> *>(&result));
return result;
}
/// Returns true if the underlying element still exists; false otherwise. /// Returns true if the underlying element still exists; false otherwise.
bool is_valid() const { return private_api::upgrade_item_weak(inner.item).has_value(); } bool is_valid() const { return private_api::upgrade_item_weak(inner.item).has_value(); }

View file

@ -34,6 +34,18 @@ pub extern "C" fn slint_testing_element_find_by_element_id(
}) })
} }
#[no_mangle]
pub extern "C" fn slint_testing_element_find_by_element_type_name(
root: &ItemTreeRc,
type_name: &Slice<u8>,
out: &mut SharedVector<crate::search_api::ElementHandle>,
) {
let Ok(type_name) = core::str::from_utf8(type_name.as_slice()) else { return };
*out = crate::search_api::search_item(root, |elem| {
elem.element_type_names_and_ids().unwrap().any(|(tid, _)| tid == type_name)
})
}
#[no_mangle] #[no_mangle]
pub extern "C" fn slint_testing_element_type_names_and_ids( pub extern "C" fn slint_testing_element_type_names_and_ids(
element: &crate::search_api::ElementHandle, element: &crate::search_api::ElementHandle,