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::{recurse_elem, Component};
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            if let ElementType::Component(base) = &elem.borrow().base_type {
15                if base.parent_element.upgrade().is_some() {
16                    collect_init_code(base);
17                }
18            }
19        }
20
21        if let Some(init_callback) = elem.borrow_mut().bindings.remove("init") {
22            component
23                .init_code
24                .borrow_mut()
25                .constructor_code
26                .push(init_callback.into_inner().expression);
27        }
28    });
29    for popup in component.popup_windows.borrow().iter() {
30        collect_init_code(&popup.component);
31    }
32}