slint/internal/compiler/passes/collect_init_code.rs
Simon Hausmann 047ab1d419 Fix run-time panic when combining forward-focus with text rendering in no_std environments
As outlined in #2199, there may be ways to trigger text layout code
through forward-focus before embedded fonts are registered. To fix this,
this patch replaces the init_code vector, which had the SetFocusItem
code before the font registration, with three explicit vectors for focus
setup code, code from init callbacks, and initial focus, and defines the
order in one central place in the copmiler (iter()).

Fixes #2199
2023-02-08 15:41:35 +01:00

29 lines
1,006 B
Rust

// Copyright © SixtyFPS GmbH <info@slint-ui.com>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-commercial
//! Passe that collects the code from init callbacks from elements and moves it into the component's init_code.
use std::rc::Rc;
use crate::langtype::ElementType;
use crate::object_tree::{recurse_elem, Component};
pub fn collect_init_code(component: &Rc<Component>) {
recurse_elem(&component.root_element, &(), &mut |elem, _| {
if elem.borrow().repeated.is_some() {
if let ElementType::Component(base) = &elem.borrow().base_type {
if base.parent_element.upgrade().is_some() {
collect_init_code(base);
}
}
}
if let Some(init_callback) = elem.borrow_mut().bindings.remove("init") {
component
.init_code
.borrow_mut()
.constructor_code
.push(init_callback.into_inner().expression);
}
});
}