Component: Replace parent_item to parent_node

Remove the `parent_item` function. After the recent changes that did not
return an item anymore and since the item tree is exposed, this function
was only used to find the repeater a component was created by.

So replace the old function with a new one that only returns the parent
node in the parent component.

This saves a few lines of generated code that is not used anymore.

Co-authored-by: Simon Hausmann <hausmann@gmail.com>
This commit is contained in:
Tobias Hunger 2022-04-08 16:46:17 +02:00 committed by Tobias Hunger
parent ca5bfd3426
commit 416aa42d90
6 changed files with 49 additions and 84 deletions

View file

@ -933,35 +933,30 @@ fn generate_item_tree(
}),
));
let parent_item_from_parent_component = if let Some(parent_index) =
parent_ctx.as_ref().and_then(|parent| {
let parent_item_from_parent_component = parent_ctx.as_ref()
.and_then(|parent| {
parent
.repeater_index
.map(|idx| parent.ctx.current_sub_component.unwrap().repeated[idx].index_in_tree)
}) {
format!(
// that does not work when the parent is not a component with a ComponentVTable
//" *result = slint::private_api::parent_item(self->parent->self_weak.into_dyn(), self->parent->get_item_tree(), {});",
"*result = {{ self->parent->self_weak, self->parent->tree_index_of_first_child + {} - 1 }};",
parent_index,
)
} else {
"".to_owned()
};
}).map(|parent_index|
vec![
format!(
"auto self = reinterpret_cast<const {}*>(component.instance);",
item_tree_class_name,
),
format!(
"*result = {{ self->parent->self_weak, self->parent->tree_index_of_first_child + {} - 1 }};",
parent_index,
)
])
.unwrap_or_default();
target_struct.members.push((
Access::Private,
Declaration::Function(Function {
name: "parent_item".into(),
signature: "(slint::private_api::ComponentRef component, uintptr_t index, slint::private_api::ItemWeak *result) -> void".into(),
name: "parent_node".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] slint::private_api::ItemWeak *result) -> void".into(),
is_static: true,
statements: Some(vec![
format!("auto self = reinterpret_cast<const {}*>(component.instance);", item_tree_class_name),
"if (index == 0) {".into(),
parent_item_from_parent_component,
" return;".into(),
"}".into(),
"*result = slint::private_api::parent_item(self->self_weak.into_dyn(), get_item_tree(component), index);".into(),
]),
statements: Some(parent_item_from_parent_component,),
..Default::default()
}),
));
@ -1040,7 +1035,7 @@ fn generate_item_tree(
ty: "const slint::private_api::ComponentVTable".to_owned(),
name: format!("{}::static_vtable", item_tree_class_name),
init: Some(format!(
"{{ visit_children, get_item_ref, get_subtree_range, get_subtree_component, get_item_tree, parent_item, subtree_index, layout_info, slint::private_api::drop_in_place<{}>, slint::private_api::dealloc }}",
"{{ visit_children, get_item_ref, get_subtree_range, get_subtree_component, get_item_tree, parent_node, subtree_index, layout_info, slint::private_api::drop_in_place<{}>, slint::private_api::dealloc }}",
item_tree_class_name)
),
..Default::default()

View file

@ -1031,7 +1031,7 @@ fn generate_item_tree(
.upgrade()
.map(|sc| (VRcMapped::origin(&sc), sc.tree_index_of_first_child.get()))
{
*result = slint::re_exports::ItemRc::new(parent_component, parent_index as usize + #sub_component_offset - 1)
*_result = slint::re_exports::ItemRc::new(parent_component, parent_index as usize + #sub_component_offset - 1)
.downgrade();
})
})
@ -1178,14 +1178,8 @@ fn generate_item_tree(
self.index_property()
}
fn parent_item(self: ::core::pin::Pin<&Self>, index: usize, result: &mut slint::re_exports::ItemWeak) {
if index == 0 {
#parent_item_expression
return;
}
let parent_index = self.get_item_tree().as_slice()[index].parent_index();
let self_rc = slint::re_exports::VRcMapped::origin(&self.self_weak.get().unwrap().upgrade().unwrap());
*result = ItemRc::new(self_rc, parent_index).downgrade();
fn parent_node(self: ::core::pin::Pin<&Self>, _result: &mut slint::re_exports::ItemWeak) {
#parent_item_expression
}
fn layout_info(self: ::core::pin::Pin<&Self>, orientation: slint::re_exports::Orientation) -> slint::re_exports::LayoutInfo {