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