mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
Use item_array in C++
This commit is contained in:
parent
6681545aca
commit
1fd14272cf
3 changed files with 53 additions and 18 deletions
|
@ -52,6 +52,9 @@ using cbindgen_private::TraversalOrder;
|
|||
|
||||
namespace private_api {
|
||||
using ItemTreeNode = cbindgen_private::ItemTreeNode<uint8_t>;
|
||||
using ItemArrayEntry =
|
||||
vtable::VOffset<uint8_t, slint::cbindgen_private::ItemVTable, vtable::AllowPin>;
|
||||
using ItemArray = slint::cbindgen_private::Slice<ItemArrayEntry>;
|
||||
using cbindgen_private::KeyboardModifiers;
|
||||
using cbindgen_private::KeyEvent;
|
||||
using cbindgen_private::PointerEvent;
|
||||
|
@ -168,10 +171,14 @@ private:
|
|||
constexpr inline ItemTreeNode make_item_node(std::uintptr_t offset,
|
||||
const cbindgen_private::ItemVTable *vtable,
|
||||
uint32_t child_count, uint32_t child_index,
|
||||
uint32_t parent_index)
|
||||
uint32_t parent_index, uint32_t item_array_index)
|
||||
{
|
||||
return ItemTreeNode { ItemTreeNode::Item_Body {
|
||||
ItemTreeNode::Tag::Item, { vtable, offset }, child_count, child_index, parent_index } };
|
||||
return ItemTreeNode { ItemTreeNode::Item_Body { ItemTreeNode::Tag::Item,
|
||||
{ vtable, offset },
|
||||
child_count,
|
||||
child_index,
|
||||
parent_index,
|
||||
item_array_index } };
|
||||
}
|
||||
|
||||
constexpr inline ItemTreeNode make_dyn_node(std::uintptr_t offset, std::uint32_t parent_index)
|
||||
|
@ -180,10 +187,12 @@ constexpr inline ItemTreeNode make_dyn_node(std::uintptr_t offset, std::uint32_t
|
|||
parent_index } };
|
||||
}
|
||||
|
||||
inline ItemRef get_item_ref(ComponentRef component, cbindgen_private::Slice<ItemTreeNode> item_tree,
|
||||
int index)
|
||||
inline ItemRef get_item_ref(ComponentRef component,
|
||||
const cbindgen_private::Slice<ItemTreeNode> item_tree,
|
||||
const private_api::ItemArray item_array, int index)
|
||||
{
|
||||
const auto &item = item_tree.ptr[index].item.item;
|
||||
const auto item_array_index = item_tree.ptr[index].item.item_array_index;
|
||||
const auto item = item_array[item_array_index];
|
||||
return ItemRef { item.vtable, reinterpret_cast<char *>(component.instance) + item.offset };
|
||||
}
|
||||
|
||||
|
|
|
@ -788,7 +788,8 @@ fn generate_item_tree(
|
|||
|
||||
let root_access = if parent_ctx.is_some() { "parent->root" } else { "self" };
|
||||
|
||||
let mut tree_array: Vec<String> = Default::default();
|
||||
let mut item_tree_array: Vec<String> = Default::default();
|
||||
let mut item_array: Vec<String> = Default::default();
|
||||
|
||||
sub_tree.tree.visit_in_array(&mut |node, children_offset, parent_index| {
|
||||
let parent_index = parent_index as u32;
|
||||
|
@ -801,7 +802,7 @@ fn generate_item_tree(
|
|||
repeater_index += sub_component.sub_components[*i].repeater_offset;
|
||||
sub_component = &sub_component.sub_components[*i].ty;
|
||||
}
|
||||
tree_array.push(format!(
|
||||
item_tree_array.push(format!(
|
||||
"slint::private_api::make_dyn_node({}, {})",
|
||||
repeater_index, parent_index
|
||||
));
|
||||
|
@ -828,9 +829,10 @@ fn generate_item_tree(
|
|||
|
||||
let children_count = node.children.len() as u32;
|
||||
let children_index = children_offset as u32;
|
||||
let item_array_index = item_array.len() as u32;
|
||||
|
||||
tree_array.push(format!(
|
||||
"slint::private_api::make_item_node({} offsetof({}, {}), {}, {}, {}, {})",
|
||||
item_tree_array.push(format!(
|
||||
"slint::private_api::make_item_node({} offsetof({}, {}), {}, {}, {}, {}, {})",
|
||||
compo_offset,
|
||||
&ident(&sub_component.name),
|
||||
ident(&item.name),
|
||||
|
@ -838,6 +840,14 @@ fn generate_item_tree(
|
|||
children_count,
|
||||
children_index,
|
||||
parent_index,
|
||||
item_array_index,
|
||||
));
|
||||
item_array.push(format!(
|
||||
"{{ {}, {} offsetof({}, {}) }}",
|
||||
item.ty.cpp_vtable_getter,
|
||||
compo_offset,
|
||||
&ident(&sub_component.name),
|
||||
ident(&item.name),
|
||||
));
|
||||
}
|
||||
});
|
||||
|
@ -878,7 +888,7 @@ fn generate_item_tree(
|
|||
signature: "(slint::private_api::ComponentRef component, uintptr_t index) -> slint::private_api::ItemRef".into(),
|
||||
is_static: true,
|
||||
statements: Some(vec![
|
||||
"return slint::private_api::get_item_ref(component, item_tree(), index);".to_owned(),
|
||||
"return slint::private_api::get_item_ref(component, item_tree(), item_array(), index);".to_owned(),
|
||||
]),
|
||||
..Default::default()
|
||||
}),
|
||||
|
@ -925,7 +935,7 @@ fn generate_item_tree(
|
|||
is_static: true,
|
||||
statements: Some(vec![
|
||||
"static const slint::private_api::ItemTreeNode children[] {".to_owned(),
|
||||
format!(" {} }};", tree_array.join(", \n")),
|
||||
format!(" {} }};", item_tree_array.join(", \n")),
|
||||
"return { const_cast<slint::private_api::ItemTreeNode*>(children), std::size(children) };"
|
||||
.to_owned(),
|
||||
]),
|
||||
|
@ -933,6 +943,22 @@ fn generate_item_tree(
|
|||
}),
|
||||
));
|
||||
|
||||
target_struct.members.push((
|
||||
Access::Private,
|
||||
Declaration::Function(Function {
|
||||
name: "item_array".into(),
|
||||
signature: "() -> const slint::private_api::ItemArray".into(),
|
||||
is_static: true,
|
||||
statements: Some(vec![
|
||||
"static const slint::private_api::ItemArrayEntry items[] {".to_owned(),
|
||||
format!(" {} }};", item_array.join(", \n")),
|
||||
"return { const_cast<slint::private_api::ItemArrayEntry*>(items), std::size(items) };"
|
||||
.to_owned(),
|
||||
]),
|
||||
..Default::default()
|
||||
}),
|
||||
));
|
||||
|
||||
target_struct.members.push((
|
||||
Access::Private,
|
||||
Declaration::Function(Function {
|
||||
|
|
|
@ -134,9 +134,9 @@ pub(crate) mod ffi {
|
|||
window_handle: *const crate::window::ffi::WindowRcOpaque,
|
||||
) {
|
||||
let window = &*(window_handle as *const WindowRc);
|
||||
super::free_component_item_array_graphics_resources(
|
||||
super::free_component_item_graphics_resources(
|
||||
core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)),
|
||||
item_array.as_slice(),
|
||||
item_tree.as_slice(),
|
||||
window,
|
||||
)
|
||||
}
|
||||
|
@ -145,13 +145,13 @@ pub(crate) mod ffi {
|
|||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_component_init_items_array(
|
||||
component: ComponentRefPin,
|
||||
item_array: Slice<Ivtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
|
||||
item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
|
||||
window_handle: *const crate::window::ffi::WindowRcOpaque,
|
||||
) {
|
||||
let window = &*(window_handle as *const WindowRc);
|
||||
super::init_component_items(
|
||||
super::init_component_items_array(
|
||||
core::pin::Pin::new_unchecked(&*(component.as_ptr() as *const u8)),
|
||||
item_tree.as_slice(),
|
||||
item_array.as_slice(),
|
||||
window,
|
||||
)
|
||||
}
|
||||
|
@ -160,7 +160,7 @@ pub(crate) mod ffi {
|
|||
#[no_mangle]
|
||||
pub unsafe extern "C" fn slint_component_free_item_array_graphics_resources(
|
||||
component: ComponentRefPin,
|
||||
item_array: Slice<Ivtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
|
||||
item_array: Slice<vtable::VOffset<u8, ItemVTable, vtable::AllowPin>>,
|
||||
window_handle: *const crate::window::ffi::WindowRcOpaque,
|
||||
) {
|
||||
let window = &*(window_handle as *const WindowRc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue