i_slint_compiler/passes/
check_rotation.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
4use crate::diagnostics::BuildDiagnostics;
5use crate::diagnostics::Spanned;
6use crate::langtype::ElementType;
7use crate::object_tree::Element;
8
9/// Check that the rotation is only on Image and Text
10pub fn check_rotation(doc: &crate::object_tree::Document, diag: &mut BuildDiagnostics) {
11    for cmp in &doc.inner_components {
12        crate::object_tree::recurse_elem_including_sub_components(cmp, &(), &mut |elem, _| {
13            let e = elem.borrow();
14            if crate::typeregister::RESERVED_ROTATION_PROPERTIES
15                .iter()
16                .any(|(property_name, _)| is_property_set(&e, property_name))
17            {
18                if !e.builtin_type().is_some_and(|b| matches!(b.name.as_str(), "Image" | "Text")) {
19                    let span = e
20                        .bindings
21                        .get("rotation-angle")
22                        .and_then(|e| e.borrow().span.clone())
23                        .unwrap_or_else(|| e.to_source_location());
24
25                    diag.push_error_with_span(
26                        "rotation properties can only be applied to the Image or Text element"
27                            .into(),
28                        span,
29                    );
30                } else if has_any_children(&e) {
31                    diag.push_error_with_span(
32                        "Elements with rotation properties cannot have children elements".into(),
33                        e.to_source_location(),
34                    );
35                }
36            }
37        });
38    }
39}
40
41/// Returns true if this element or its base have any children.
42fn has_any_children(e: &Element) -> bool {
43    !e.children.is_empty()
44        || matches!(&e.base_type, ElementType::Component(base) if has_any_children(&base.root_element.borrow()))
45}
46
47/// Returns true if the property is set.
48fn is_property_set(e: &Element, property_name: &str) -> bool {
49    e.bindings.contains_key(property_name)
50        || e.property_analysis.borrow().get(property_name).is_some_and(|a| a.is_set || a.is_linked)
51        || matches!(&e.base_type, ElementType::Component(base) if is_property_set(&base.root_element.borrow(), property_name))
52}