| 1 | // Copyright © SixtyFPS GmbH <info@slint.dev> |
| 2 | // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0 |
| 3 | |
| 4 | /*! |
| 5 | Make sure that the Repeated expression are just components without any children |
| 6 | */ |
| 7 | |
| 8 | use crate::expression_tree::{Expression, NamedReference}; |
| 9 | use crate::langtype::ElementType; |
| 10 | use crate::object_tree::*; |
| 11 | use smol_str::SmolStr; |
| 12 | use std::cell::RefCell; |
| 13 | use std::rc::Rc; |
| 14 | |
| 15 | pub fn process_repeater_components(component: &Rc<Component>) { |
| 16 | create_repeater_components(component); |
| 17 | adjust_references(comp:component); |
| 18 | } |
| 19 | |
| 20 | fn create_repeater_components(component: &Rc<Component>) { |
| 21 | recurse_elem(&component.root_element, &(), &mut |elem, _| { |
| 22 | let is_listview = match &elem.borrow().repeated { |
| 23 | Some(r) => r.is_listview.clone(), |
| 24 | None => return, |
| 25 | }; |
| 26 | let parent_element = Rc::downgrade(elem); |
| 27 | let mut elem = elem.borrow_mut(); |
| 28 | |
| 29 | if matches!(&elem.base_type, ElementType::Component(c) if c.parent_element.upgrade().is_some()) |
| 30 | { |
| 31 | debug_assert!(std::rc::Weak::ptr_eq( |
| 32 | &parent_element, |
| 33 | &elem.base_type.as_component().parent_element |
| 34 | )); |
| 35 | // Already processed (can happen if a component is both used and exported root) |
| 36 | return; |
| 37 | } |
| 38 | |
| 39 | let comp = Rc::new(Component { |
| 40 | root_element: Rc::new(RefCell::new(Element { |
| 41 | id: elem.id.clone(), |
| 42 | base_type: std::mem::take(&mut elem.base_type), |
| 43 | bindings: std::mem::take(&mut elem.bindings), |
| 44 | change_callbacks: std::mem::take(&mut elem.change_callbacks), |
| 45 | property_analysis: std::mem::take(&mut elem.property_analysis), |
| 46 | children: std::mem::take(&mut elem.children), |
| 47 | property_declarations: std::mem::take(&mut elem.property_declarations), |
| 48 | named_references: Default::default(), |
| 49 | repeated: None, |
| 50 | is_component_placeholder: false, |
| 51 | debug: elem.debug.clone(), |
| 52 | enclosing_component: Default::default(), |
| 53 | states: std::mem::take(&mut elem.states), |
| 54 | transitions: std::mem::take(&mut elem.transitions), |
| 55 | child_of_layout: elem.child_of_layout || is_listview.is_some(), |
| 56 | layout_info_prop: elem.layout_info_prop.take(), |
| 57 | default_fill_parent: elem.default_fill_parent, |
| 58 | accessibility_props: std::mem::take(&mut elem.accessibility_props), |
| 59 | geometry_props: elem.geometry_props.clone(), |
| 60 | is_flickable_viewport: elem.is_flickable_viewport, |
| 61 | has_popup_child: elem.has_popup_child, |
| 62 | item_index: Default::default(), // Not determined yet |
| 63 | item_index_of_first_children: Default::default(), |
| 64 | is_legacy_syntax: elem.is_legacy_syntax, |
| 65 | inline_depth: 0, |
| 66 | })), |
| 67 | parent_element, |
| 68 | ..Component::default() |
| 69 | }); |
| 70 | |
| 71 | if let Some(listview) = is_listview { |
| 72 | if !comp.root_element.borrow().is_binding_set("height" , false) { |
| 73 | let preferred = Expression::PropertyReference(NamedReference::new( |
| 74 | &comp.root_element, |
| 75 | SmolStr::new_static("preferred-height" ), |
| 76 | )); |
| 77 | comp.root_element |
| 78 | .borrow_mut() |
| 79 | .bindings |
| 80 | .insert("height" .into(), RefCell::new(preferred.into())); |
| 81 | } |
| 82 | if !comp.root_element.borrow().is_binding_set("width" , false) { |
| 83 | comp.root_element.borrow_mut().bindings.insert( |
| 84 | "width" .into(), |
| 85 | RefCell::new(Expression::PropertyReference(listview.listview_width).into()), |
| 86 | ); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | let weak = Rc::downgrade(&comp); |
| 91 | recurse_elem(&comp.root_element, &(), &mut |e, _| { |
| 92 | e.borrow_mut().enclosing_component = weak.clone() |
| 93 | }); |
| 94 | create_repeater_components(&comp); |
| 95 | elem.base_type = ElementType::Component(comp); |
| 96 | }); |
| 97 | |
| 98 | for p in component.popup_windows.borrow().iter() { |
| 99 | create_repeater_components(&p.component); |
| 100 | } |
| 101 | for c in component.menu_item_tree.borrow().iter() { |
| 102 | create_repeater_components(c); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /// Make sure that references to property within the repeated element actually point to the reference |
| 107 | /// to the root of the newly created component |
| 108 | fn adjust_references(comp: &Rc<Component>) { |
| 109 | visit_all_named_references(comp, &mut |nr| { |
| 110 | if nr.name() == "$model" { |
| 111 | return; |
| 112 | } |
| 113 | let e = nr.element(); |
| 114 | if e.borrow().repeated.is_some() { |
| 115 | if let ElementType::Component(c) = e.borrow().base_type.clone() { |
| 116 | *nr = NamedReference::new(&c.root_element, nr.name().clone()) |
| 117 | }; |
| 118 | } |
| 119 | }); |
| 120 | // Transform any references to the repeated element to refer to the root of each instance. |
| 121 | visit_all_expressions(comp, |expr, _| { |
| 122 | expr.visit_recursive_mut(&mut |expr| { |
| 123 | if let Expression::ElementReference(ref mut element_ref) = expr { |
| 124 | if let Some(repeater_element) = |
| 125 | element_ref.upgrade().filter(|e| e.borrow().repeated.is_some()) |
| 126 | { |
| 127 | let inner_element = |
| 128 | repeater_element.borrow().base_type.as_component().root_element.clone(); |
| 129 | *element_ref = Rc::downgrade(&inner_element); |
| 130 | } |
| 131 | } |
| 132 | }) |
| 133 | }); |
| 134 | } |
| 135 | |