Skip to main content

i_slint_compiler/passes/
collect_init_code.rs

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//! This pass collects the code from init callbacks from elements and moves it into the component's init_code.
5
6use std::rc::Rc;
7
8use crate::langtype::ElementType;
9use crate::object_tree::{Component, recurse_elem};
10
11pub fn collect_init_code(component: &Rc<Component>) {
12    recurse_elem(&component.root_element, &(), &mut |elem, _| {
13        if elem.borrow().repeated.is_some()
14            && let ElementType::Component(base) = &elem.borrow().base_type
15            && base.parent_element().is_some()
16        {
17            collect_init_code(base);
18        }
19
20        if let Some(init_callback) = elem.borrow_mut().take_binding("init") {
21            component.init_code.borrow_mut().constructor_code.push(init_callback.expression);
22        }
23    });
24    for popup in component.popup_windows.borrow().iter() {
25        collect_init_code(&popup.component);
26    }
27}