mirror of
https://github.com/slint-ui/slint.git
synced 2025-08-04 18:58:36 +00:00
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:
parent
ca5bfd3426
commit
416aa42d90
6 changed files with 49 additions and 84 deletions
|
@ -201,17 +201,6 @@ inline ItemRef get_item_ref(ComponentRef component,
|
|||
return ItemRef { item.vtable, reinterpret_cast<char *>(component.instance) + item.offset };
|
||||
}
|
||||
|
||||
inline ItemWeak parent_item(cbindgen_private::ComponentWeak component,
|
||||
cbindgen_private::Slice<ItemTreeNode> item_tree, int index)
|
||||
{
|
||||
const auto &node = item_tree.ptr[index];
|
||||
if (node.tag == ItemTreeNode::Tag::Item) {
|
||||
return { component, node.item.parent_index };
|
||||
} else {
|
||||
return { component, node.dynamic_tree.parent_index };
|
||||
}
|
||||
}
|
||||
|
||||
inline void dealloc(const ComponentVTable *, uint8_t *ptr, vtable::Layout layout)
|
||||
{
|
||||
#ifdef __cpp_sized_deallocation
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -60,14 +60,13 @@ pub struct ComponentVTable {
|
|||
/// And the return value is passed by &mut because ItemWeak has a destructor
|
||||
pub get_item_tree: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>) -> Slice<ItemTreeNode>,
|
||||
|
||||
// FIXME: This does return an invalid ItemWeak now that points to the parent repeater!
|
||||
// FIXME: Get rid of the index and make this always return the "Item" that connects this component
|
||||
// to its parent-component?
|
||||
/// Return the parent item.
|
||||
/// Return the node this component is a part of in the parent component.
|
||||
///
|
||||
/// The return value is an item weak because it can be null if there is no parent.
|
||||
/// And the return value is passed by &mut because ItemWeak has a destructor
|
||||
pub parent_item:
|
||||
extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, index: usize, result: &mut ItemWeak),
|
||||
/// Note that the returned value will typically point to a repeater node, which is
|
||||
/// strictly speaking not an Item at all!
|
||||
pub parent_node: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>, result: &mut ItemWeak),
|
||||
|
||||
/// Return the index of the current subtree or usize::MAX if this is not a subtree
|
||||
pub subtree_index: extern "C" fn(core::pin::Pin<VRef<ComponentVTable>>) -> usize,
|
||||
|
|
|
@ -108,7 +108,7 @@ impl ItemRc {
|
|||
}
|
||||
|
||||
let mut r = ItemWeak::default();
|
||||
comp_ref_pin.as_ref().parent_item(self.index, &mut r);
|
||||
comp_ref_pin.as_ref().parent_node(&mut r);
|
||||
// parent_item returns the repeater node, go up one more level!
|
||||
if let Some(rc) = r.upgrade() {
|
||||
r = rc.parent_item();
|
||||
|
@ -194,7 +194,7 @@ impl ItemRc {
|
|||
let comp_ref_pin = vtable::VRc::borrow_pin(&self.component);
|
||||
if self.index == 0 {
|
||||
let mut parent_item = Default::default();
|
||||
comp_ref_pin.as_ref().parent_item(0, &mut parent_item);
|
||||
comp_ref_pin.as_ref().parent_node(&mut parent_item);
|
||||
let current_component_subtree_index = comp_ref_pin.as_ref().subtree_index();
|
||||
if let Some(parent_item) = parent_item.upgrade() {
|
||||
let parent = parent_item.component();
|
||||
|
@ -835,7 +835,7 @@ mod tests {
|
|||
Slice::from_slice(&self.get_ref().item_tree)
|
||||
}
|
||||
|
||||
fn parent_item(self: core::pin::Pin<&Self>, _1: usize, result: &mut ItemWeak) {
|
||||
fn parent_node(self: core::pin::Pin<&Self>, result: &mut ItemWeak) {
|
||||
if let Some(parent_item) = self.parent_component.clone() {
|
||||
*result =
|
||||
ItemRc::new(parent_item.clone(), self.item_tree[0].parent_index()).downgrade();
|
||||
|
|
|
@ -192,8 +192,8 @@ impl Component for ErasedComponentBox {
|
|||
self.borrow().as_ref().get_subtree_component(index, subindex, result);
|
||||
}
|
||||
|
||||
fn parent_item(self: Pin<&Self>, index: usize, result: &mut ItemWeak) {
|
||||
self.borrow().as_ref().parent_item(index, result)
|
||||
fn parent_node(self: Pin<&Self>, result: &mut ItemWeak) {
|
||||
self.borrow().as_ref().parent_node(result)
|
||||
}
|
||||
|
||||
fn subtree_index(self: Pin<&Self>) -> usize {
|
||||
|
@ -1047,7 +1047,7 @@ pub(crate) fn generate_component<'id>(
|
|||
get_item_tree,
|
||||
get_subtree_range,
|
||||
get_subtree_component,
|
||||
parent_item,
|
||||
parent_node,
|
||||
subtree_index,
|
||||
drop_in_place,
|
||||
dealloc,
|
||||
|
@ -1581,38 +1581,26 @@ extern "C" fn subtree_index(component: ComponentRefPin) -> usize {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn parent_item(component: ComponentRefPin, index: usize, result: &mut ItemWeak) {
|
||||
unsafe extern "C" fn parent_node(component: ComponentRefPin, result: &mut ItemWeak) {
|
||||
generativity::make_guard!(guard);
|
||||
let instance_ref = InstanceRef::from_pin_ref(component, guard);
|
||||
if index == 0 {
|
||||
let parent_item_index = instance_ref
|
||||
.component_type
|
||||
.original
|
||||
.parent_element
|
||||
.upgrade()
|
||||
.and_then(|e| e.borrow().item_index.get().cloned());
|
||||
if let (Some(parent_offset), Some(parent_index)) =
|
||||
(instance_ref.component_type.parent_component_offset, parent_item_index)
|
||||
{
|
||||
if let Some(parent) = parent_offset.apply(instance_ref.as_ref()) {
|
||||
generativity::make_guard!(new_guard);
|
||||
let parent_instance = InstanceRef::from_pin_ref(*parent, new_guard);
|
||||
let parent_rc = parent_instance
|
||||
.self_weak()
|
||||
.get()
|
||||
.unwrap()
|
||||
.clone()
|
||||
.into_dyn()
|
||||
.upgrade()
|
||||
.unwrap();
|
||||
*result = ItemRc::new(parent_rc, parent_index).downgrade();
|
||||
};
|
||||
}
|
||||
return;
|
||||
let parent_item_index = instance_ref
|
||||
.component_type
|
||||
.original
|
||||
.parent_element
|
||||
.upgrade()
|
||||
.and_then(|e| e.borrow().item_index.get().cloned());
|
||||
if let (Some(parent_offset), Some(parent_index)) =
|
||||
(instance_ref.component_type.parent_component_offset, parent_item_index)
|
||||
{
|
||||
if let Some(parent) = parent_offset.apply(instance_ref.as_ref()) {
|
||||
generativity::make_guard!(new_guard);
|
||||
let parent_instance = InstanceRef::from_pin_ref(*parent, new_guard);
|
||||
let parent_rc =
|
||||
parent_instance.self_weak().get().unwrap().clone().into_dyn().upgrade().unwrap();
|
||||
*result = ItemRc::new(parent_rc, parent_index).downgrade();
|
||||
};
|
||||
}
|
||||
let parent_index = get_item_tree(component)[index].parent_index();
|
||||
let self_rc = instance_ref.self_weak().get().unwrap().clone().into_dyn().upgrade().unwrap();
|
||||
*result = ItemRc::new(self_rc, parent_index).downgrade();
|
||||
}
|
||||
|
||||
unsafe extern "C" fn drop_in_place(component: vtable::VRefMut<ComponentVTable>) -> vtable::Layout {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue