Rename ComponentVTable to ItemTreeVTable

and their related name.
Also move the component module in the item_Tree module
register_component -> register_item_tree
ComponentItemTree -> ItemTreeNodeArray

For #3323

Rationale from that issue:

Right now, we use the term `component` in the Slint language and in the compiler to be a a tree of elements that can be used as an element in a .slint file.

The term is also currently used in the runtime as a tree of runtime Items that are allocated together. (declared in ComponentVTable)

But there are no 1 to 1 mapping between a `component` in the slint language, and a runtime Component.
(for example, the items behind a `if` or `for` or `PopupMenu` are in a different runtime component. And `component` declared in Slint are often inlined)

So we should rename the internal runtime `Component` to  `ItemTree`

The currently public `slint::ComponentHandle` wraps the "root" of the ItemTree, but that's ok because it is generated from a .slint `component`,  so it doesn't change name
This commit is contained in:
Olivier Goffart 2023-10-07 12:11:33 +02:00 committed by Olivier Goffart
parent 186c3ef3cb
commit 852b4d906d
37 changed files with 641 additions and 690 deletions

View file

@ -1047,10 +1047,9 @@ fn generate_item_tree(
file: &mut File,
conditional_includes: &ConditionalIncludes,
) {
target_struct.friends.push(format!(
"vtable::VRc<slint::private_api::ComponentVTable, {}>",
item_tree_class_name
));
target_struct
.friends
.push(format!("vtable::VRc<slint::private_api::ItemTreeVTable, {}>", item_tree_class_name));
generate_sub_component(
target_struct,
@ -1147,7 +1146,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "visit_children".into(),
signature: "(slint::private_api::ComponentRef component, intptr_t index, slint::private_api::TraversalOrder order, slint::private_api::ItemVisitorRefMut visitor) -> uint64_t".into(),
signature: "(slint::private_api::ItemTreeRef component, intptr_t index, slint::private_api::TraversalOrder order, slint::private_api::ItemVisitorRefMut visitor) -> uint64_t".into(),
is_static: true,
statements: Some(visit_children_statements),
..Default::default()
@ -1158,7 +1157,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "get_item_ref".into(),
signature: "(slint::private_api::ComponentRef component, uint32_t index) -> slint::private_api::ItemRef".into(),
signature: "(slint::private_api::ItemTreeRef component, uint32_t index) -> slint::private_api::ItemRef".into(),
is_static: true,
statements: Some(vec![
"return slint::private_api::get_item_ref(component, get_item_tree(component), item_array(), index);".to_owned(),
@ -1171,7 +1170,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "get_subtree_range".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] uint32_t dyn_index) -> slint::private_api::IndexRange".into(),
signature: "([[maybe_unused]] slint::private_api::ItemTreeRef component, [[maybe_unused]] uint32_t dyn_index) -> slint::private_api::IndexRange".into(),
is_static: true,
statements: Some(subtree_range_statement),
..Default::default()
@ -1181,8 +1180,8 @@ fn generate_item_tree(
target_struct.members.push((
Access::Private,
Declaration::Function(Function {
name: "get_subtree_component".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] uint32_t dyn_index, [[maybe_unused]] uintptr_t subtree_index, [[maybe_unused]] slint::private_api::ComponentWeak *result) -> void".into(),
name: "get_subtree".into(),
signature: "([[maybe_unused]] slint::private_api::ItemTreeRef component, [[maybe_unused]] uint32_t dyn_index, [[maybe_unused]] uintptr_t subtree_index, [[maybe_unused]] slint::private_api::ItemTreeWeak *result) -> void".into(),
is_static: true,
statements: Some(subtree_component_statement),
..Default::default()
@ -1193,7 +1192,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "get_item_tree".into(),
signature: "(slint::private_api::ComponentRef) -> slint::cbindgen_private::Slice<slint::private_api::ItemTreeNode>".into(),
signature: "(slint::private_api::ItemTreeRef) -> slint::cbindgen_private::Slice<slint::private_api::ItemTreeNode>".into(),
is_static: true,
statements: Some(vec![
"return item_tree();".to_owned(),
@ -1223,7 +1222,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "parent_node".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] slint::private_api::ItemWeak *result) -> void".into(),
signature: "([[maybe_unused]] slint::private_api::ItemTreeRef component, [[maybe_unused]] slint::private_api::ItemWeak *result) -> void".into(),
is_static: true,
statements: Some(parent_item_from_parent_component,),
..Default::default()
@ -1234,7 +1233,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "embed_component".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] const slint::private_api::ComponentWeak *parent_component, [[maybe_unused]] const uint32_t parent_index) -> bool".into(),
signature: "([[maybe_unused]] slint::private_api::ItemTreeRef component, [[maybe_unused]] const slint::private_api::ItemTreeWeak *parent_component, [[maybe_unused]] const uint32_t parent_index) -> bool".into(),
is_static: true,
statements: Some(vec!["return false; /* todo! */".into()]),
..Default::default()
@ -1246,7 +1245,7 @@ fn generate_item_tree(
Access::Private,
Declaration::Function(Function {
name: "subtree_index".into(),
signature: "([[maybe_unused]] slint::private_api::ComponentRef component) -> uintptr_t"
signature: "([[maybe_unused]] slint::private_api::ItemTreeRef component) -> uintptr_t"
.into(),
is_static: true,
statements: Some(vec!["return std::numeric_limits<uintptr_t>::max();".into()]),
@ -1291,7 +1290,7 @@ fn generate_item_tree(
Declaration::Function(Function {
name: "layout_info".into(),
signature:
"([[maybe_unused]] slint::private_api::ComponentRef component, slint::cbindgen_private::Orientation o) -> slint::cbindgen_private::LayoutInfo"
"([[maybe_unused]] slint::private_api::ItemTreeRef component, slint::cbindgen_private::Orientation o) -> slint::cbindgen_private::LayoutInfo"
.into(),
is_static: true,
statements: Some(vec![format!(
@ -1307,7 +1306,7 @@ fn generate_item_tree(
Declaration::Function(Function {
name: "item_geometry".into(),
signature:
"([[maybe_unused]] slint::private_api::ComponentRef component, uint32_t index) -> slint::cbindgen_private::LogicalRect"
"([[maybe_unused]] slint::private_api::ItemTreeRef component, uint32_t index) -> slint::cbindgen_private::LogicalRect"
.into(),
is_static: true,
statements: Some(vec![format!(
@ -1323,7 +1322,7 @@ fn generate_item_tree(
Declaration::Function(Function {
name: "accessible_role".into(),
signature:
"([[maybe_unused]] slint::private_api::ComponentRef component, uint32_t index) -> slint::cbindgen_private::AccessibleRole"
"([[maybe_unused]] slint::private_api::ItemTreeRef component, uint32_t index) -> slint::cbindgen_private::AccessibleRole"
.into(),
is_static: true,
statements: Some(vec![format!(
@ -1339,7 +1338,7 @@ fn generate_item_tree(
Declaration::Function(Function {
name: "accessible_string_property".into(),
signature:
"([[maybe_unused]] slint::private_api::ComponentRef component, uint32_t index, slint::cbindgen_private::AccessibleStringProperty what, slint::SharedString *result) -> void"
"([[maybe_unused]] slint::private_api::ItemTreeRef component, uint32_t index, slint::cbindgen_private::AccessibleStringProperty what, slint::SharedString *result) -> void"
.into(),
is_static: true,
statements: Some(vec![format!(
@ -1355,7 +1354,7 @@ fn generate_item_tree(
Declaration::Function(Function {
name: "window_adapter".into(),
signature:
"([[maybe_unused]] slint::private_api::ComponentRef component, [[maybe_unused]] bool do_create, [[maybe_unused]] slint::cbindgen_private::Option<slint::private_api::WindowAdapterRc>* result) -> void"
"([[maybe_unused]] slint::private_api::ItemTreeRef component, [[maybe_unused]] bool do_create, [[maybe_unused]] slint::cbindgen_private::Option<slint::private_api::WindowAdapterRc>* result) -> void"
.into(),
is_static: true,
statements: Some(vec![format!(
@ -1368,17 +1367,17 @@ fn generate_item_tree(
target_struct.members.push((
Access::Public,
Declaration::Var(Var {
ty: "static const slint::private_api::ComponentVTable".to_owned(),
ty: "static const slint::private_api::ItemTreeVTable".to_owned(),
name: "static_vtable".to_owned(),
..Default::default()
}),
));
file.definitions.push(Declaration::Var(Var {
ty: "inline const slint::private_api::ComponentVTable".to_owned(),
ty: "inline const slint::private_api::ItemTreeVTable".to_owned(),
name: format!("{}::static_vtable", item_tree_class_name),
init: Some(format!(
"{{ visit_children, get_item_ref, get_subtree_range, get_subtree_component, \
"{{ visit_children, get_item_ref, get_subtree_range, get_subtree, \
get_item_tree, parent_node, embed_component, subtree_index, layout_info, \
item_geometry, accessible_role, accessible_string_property, window_adapter, \
slint::private_api::drop_in_place<{}>, slint::private_api::dealloc }}",
@ -1400,7 +1399,7 @@ fn generate_item_tree(
let mut create_code = vec![
format!(
"auto self_rc = vtable::VRc<slint::private_api::ComponentVTable, {0}>::make();",
"auto self_rc = vtable::VRc<slint::private_api::ItemTreeVTable, {0}>::make();",
target_struct.name
),
format!("auto self = const_cast<{0} *>(&*self_rc);", target_struct.name),
@ -1414,7 +1413,7 @@ fn generate_item_tree(
let root_access = if parent_ctx.is_some() { "parent->root" } else { "self" };
create_code.extend([
format!(
"slint::private_api::register_component(&self_rc.into_dyn(), {root_access}->m_window);",
"slint::private_api::register_item_tree(&self_rc.into_dyn(), {root_access}->m_window);",
),
format!("self->init({}, self->self_weak, 0, 1 {});", root_access, init_parent_parameters),
]);
@ -1445,7 +1444,7 @@ fn generate_item_tree(
let root_access = if parent_ctx.is_some() { "root" } else { "this" };
let destructor = vec![format!(
"if (auto &window = {root_access}->m_window) window->window_handle().unregister_component(this, item_array());"
"if (auto &window = {root_access}->m_window) window->window_handle().unregister_item_tree(this, item_array());"
)];
target_struct.members.push((
@ -1473,7 +1472,7 @@ fn generate_sub_component(
let mut init_parameters = vec![
format!("{} root", root_ptr_type),
"slint::cbindgen_private::ComponentWeak enclosing_component".into(),
"slint::cbindgen_private::ItemTreeWeak enclosing_component".into(),
"uint32_t tree_index".into(),
"uint32_t tree_index_of_first_child".into(),
];
@ -1484,7 +1483,7 @@ fn generate_sub_component(
target_struct.members.push((
Access::Public,
Declaration::Var(Var {
ty: "slint::cbindgen_private::ComponentWeak".into(),
ty: "slint::cbindgen_private::ItemTreeWeak".into(),
name: "self_weak".into(),
..Default::default()
}),
@ -1940,7 +1939,7 @@ fn generate_sub_component(
field_access,
Declaration::Function(Function {
name: "subtree_component".into(),
signature: "(uintptr_t dyn_index, [[maybe_unused]] uintptr_t subtree_index, [[maybe_unused]] slint::private_api::ComponentWeak *result) const -> void".into(),
signature: "(uintptr_t dyn_index, [[maybe_unused]] uintptr_t subtree_index, [[maybe_unused]] slint::private_api::ItemTreeWeak *result) const -> void".into(),
statements: Some(vec![
"[[maybe_unused]] auto self = this;".to_owned(),
format!(" switch(dyn_index) {{ {} }};", subtrees_components_cases.join("")),

View file

@ -8,7 +8,7 @@
Some convention used in the generated code:
- `_self` is of type `Pin<&ComponentType>` where ComponentType is the type of the generated sub component,
this is existing for any evaluation of a binding
- `self_rc` is of type `VRc<ComponentVTable, ComponentType>` or `Rc<ComponentType>` for globals
- `self_rc` is of type `VRc<ItemTreeVTable, ComponentType>` or `Rc<ComponentType>` for globals
this is usually a local variable to the init code that shouldn't rbe relied upon by the binding code.
*/
@ -254,7 +254,7 @@ fn generate_public_component(llr: &llr::PublicComponent) -> TokenStream {
quote!(
#component
pub struct #public_component_id(vtable::VRc<sp::ComponentVTable, #inner_component_id>);
pub struct #public_component_id(vtable::VRc<sp::ItemTreeVTable, #inner_component_id>);
impl #public_component_id {
pub fn new() -> core::result::Result<Self, slint::PlatformError> {
@ -267,7 +267,7 @@ fn generate_public_component(llr: &llr::PublicComponent) -> TokenStream {
#property_and_callback_accessors
}
impl From<#public_component_id> for vtable::VRc<sp::ComponentVTable, #inner_component_id> {
impl From<#public_component_id> for vtable::VRc<sp::ItemTreeVTable, #inner_component_id> {
fn from(value: #public_component_id) -> Self {
value.0
}
@ -283,7 +283,7 @@ fn generate_public_component(llr: &llr::PublicComponent) -> TokenStream {
Self(self.0.clone())
}
fn from_inner(inner: vtable::VRc<sp::ComponentVTable, #inner_component_id>) -> Self {
fn from_inner(inner: vtable::VRc<sp::ItemTreeVTable, #inner_component_id>) -> Self {
Self(inner)
}
@ -916,7 +916,7 @@ fn generate_sub_component(
let parent_component_type = parent_ctx.iter().map(|parent| {
let parent_component_id =
self::inner_component_id(parent.ctx.current_sub_component.unwrap());
quote!(sp::VWeakMapped::<sp::ComponentVTable, #parent_component_id>)
quote!(sp::VWeakMapped::<sp::ItemTreeVTable, #parent_component_id>)
});
user_init_code.extend(component.init_code.iter().map(|e| {
@ -955,17 +955,17 @@ fn generate_sub_component(
#(#declared_property_vars : sp::Property<#declared_property_types>,)*
#(#declared_callbacks : sp::Callback<(#(#declared_callbacks_types,)*), #declared_callbacks_ret>,)*
#(#repeated_element_names : sp::Repeater<#repeated_element_components>,)*
self_weak : sp::OnceCell<sp::VWeakMapped<sp::ComponentVTable, #inner_component_id>>,
self_weak : sp::OnceCell<sp::VWeakMapped<sp::ItemTreeVTable, #inner_component_id>>,
#(parent : #parent_component_type,)*
root : sp::OnceCell<sp::VWeak<sp::ComponentVTable, #root_component_id>>,
root : sp::OnceCell<sp::VWeak<sp::ItemTreeVTable, #root_component_id>>,
tree_index: ::core::cell::Cell<u32>,
tree_index_of_first_child: ::core::cell::Cell<u32>,
#extra_fields
}
impl #inner_component_id {
pub fn init(self_rc: sp::VRcMapped<sp::ComponentVTable, Self>,
root : &sp::VRc<sp::ComponentVTable, #root_component_id>,
pub fn init(self_rc: sp::VRcMapped<sp::ItemTreeVTable, Self>,
root : &sp::VRc<sp::ItemTreeVTable, #root_component_id>,
tree_index: u32, tree_index_of_first_child: u32) {
#![allow(unused)]
let _self = self_rc.as_pin_ref();
@ -976,7 +976,7 @@ fn generate_sub_component(
#(#init)*
}
pub fn user_init(self_rc: sp::VRcMapped<sp::ComponentVTable, Self>) {
pub fn user_init(self_rc: sp::VRcMapped<sp::ItemTreeVTable, Self>) {
let _self = self_rc.as_pin_ref();
#(#user_init_code)*
}
@ -1013,7 +1013,7 @@ fn generate_sub_component(
}
}
fn subtree_component(self: ::core::pin::Pin<&Self>, dyn_index: u32, subtree_index: usize, result: &mut sp::ComponentWeak) {
fn subtree_component(self: ::core::pin::Pin<&Self>, dyn_index: u32, subtree_index: usize, result: &mut sp::ItemTreeWeak) {
#![allow(unused)]
let _self = self;
match dyn_index {
@ -1210,14 +1210,14 @@ fn generate_global(global: &llr::GlobalComponent, root: &llr::PublicComponent) -
struct #inner_component_id {
#(#declared_property_vars: sp::Property<#declared_property_types>,)*
#(#declared_callbacks: sp::Callback<(#(#declared_callbacks_types,)*), #declared_callbacks_ret>,)*
root : sp::OnceCell<sp::VWeak<sp::ComponentVTable, #root_component_id>>,
root : sp::OnceCell<sp::VWeak<sp::ItemTreeVTable, #root_component_id>>,
}
impl #inner_component_id {
fn new() -> ::core::pin::Pin<sp::Rc<Self>> {
sp::Rc::pin(Self::default())
}
fn init(self: ::core::pin::Pin<sp::Rc<Self>>, root: &sp::VRc<sp::ComponentVTable, #root_component_id>) {
fn init(self: ::core::pin::Pin<sp::Rc<Self>>, root: &sp::VRc<sp::ItemTreeVTable, #root_component_id>) {
#![allow(unused)]
self.root.set(VRc::downgrade(root));
let self_rc = self;
@ -1253,7 +1253,7 @@ fn generate_item_tree(
.map(|parent| {
let parent_component_id =
self::inner_component_id(parent.ctx.current_sub_component.unwrap());
quote!(sp::VWeakMapped::<sp::ComponentVTable, #parent_component_id>)
quote!(sp::VWeakMapped::<sp::ItemTreeVTable, #parent_component_id>)
})
.collect::<Vec<_>>();
let root_token = if parent_ctx.is_some() {
@ -1278,7 +1278,7 @@ fn generate_item_tree(
.and_then(|root| root.maybe_window_adapter_impl())
}
),
quote!(vtable::VRc<sp::ComponentVTable, Self>),
quote!(vtable::VRc<sp::ItemTreeVTable, Self>),
if parent_ctx.repeater_index.is_some() {
// Repeaters run their user_init() code from RepeatedComponent::init() after update() initialized model_data/index.
quote!(self_rc)
@ -1315,7 +1315,7 @@ fn generate_item_tree(
}
),
quote!(
core::result::Result<vtable::VRc<sp::ComponentVTable, Self>, slint::PlatformError>
core::result::Result<vtable::VRc<sp::ItemTreeVTable, Self>, slint::PlatformError>
),
quote!(core::result::Result::Ok(self_rc)),
)
@ -1399,7 +1399,7 @@ fn generate_item_tree(
#(_self.parent = parent.clone() as #parent_component_type;)*
let self_rc = VRc::new(_self);
let self_dyn_rc = vtable::VRc::into_dyn(self_rc.clone());
sp::register_component(&self_dyn_rc, (*#root_token).maybe_window_adapter_impl());
sp::register_item_tree(&self_dyn_rc, (*#root_token).maybe_window_adapter_impl());
Self::init(sp::VRc::map(self_rc.clone(), |x| x), #root_token, 0, 1);
#new_end
}
@ -1423,15 +1423,15 @@ fn generate_item_tree(
impl sp::PinnedDrop for #inner_component_id {
fn drop(self: core::pin::Pin<&mut #inner_component_id>) {
use slint::private_unstable_api::re_exports::*;
ComponentVTable_static!(static VT for self::#inner_component_id);
new_vref!(let vref : VRef<ComponentVTable> for Component = self.as_ref().get_ref());
ItemTreeVTable_static!(static VT for self::#inner_component_id);
new_vref!(let vref : VRef<ItemTreeVTable> for ItemTree = self.as_ref().get_ref());
if let Some(wa) = self.maybe_window_adapter_impl() {
sp::unregister_component(self.as_ref(), vref, Self::item_array(), &wa);
sp::unregister_item_tree(self.as_ref(), vref, Self::item_array(), &wa);
}
}
}
impl sp::Component for #inner_component_id {
impl sp::ItemTree for #inner_component_id {
fn visit_children_item(self: ::core::pin::Pin<&Self>, index: isize, order: sp::TraversalOrder, visitor: sp::ItemVisitorRefMut<'_>)
-> sp::VisitChildrenResult
{
@ -1464,8 +1464,8 @@ fn generate_item_tree(
self.subtree_range(index)
}
fn get_subtree_component(
self: ::core::pin::Pin<&Self>, index: u32, subtree_index: usize, result: &mut sp::ComponentWeak)
fn get_subtree(
self: ::core::pin::Pin<&Self>, index: u32, subtree_index: usize, result: &mut sp::ItemTreeWeak)
{
self.subtree_component(index, subtree_index, result);
}
@ -1480,7 +1480,7 @@ fn generate_item_tree(
#parent_item_expression
}
fn embed_component(self: ::core::pin::Pin<&Self>, _parent_component: &sp::ComponentWeak, _item_tree_index: u32) -> bool {
fn embed_component(self: ::core::pin::Pin<&Self>, _parent_component: &sp::ItemTreeWeak, _item_tree_index: u32) -> bool {
#embedding_function
}