Skip to content

Commit

Permalink
Add C++ API for find_by_element_type_name
Browse files Browse the repository at this point in the history
  • Loading branch information
tronical committed May 21, 2024
1 parent 7fb1ef8 commit 6058260
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
17 changes: 17 additions & 0 deletions api/cpp/include/slint-testing.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ class ElementHandle
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.
bool is_valid() const { return private_api::upgrade_item_weak(inner.item).has_value(); }

Expand Down
12 changes: 12 additions & 0 deletions internal/backends/testing/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
pub extern "C" fn slint_testing_element_type_names_and_ids(
element: &crate::search_api::ElementHandle,
Expand Down

0 comments on commit 6058260

Please sign in to comment.