Skip to main content

i_slint_compiler/passes/
deprecated_rotation_origin.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 handles the deprecated `rotation-origin-*` properties on Text and Image that were replaced by `transform-origin`
5
6use crate::diagnostics::{BuildDiagnostics, Spanned};
7use crate::expression_tree::Expression;
8use crate::namedreference::NamedReference;
9use crate::object_tree::{Component, ElementRc};
10use smol_str::SmolStr;
11
12pub fn handle_rotation_origin(component: &Component, diag: &mut BuildDiagnostics) {
13    let transform_origin = crate::typeregister::transform_origin_property();
14
15    crate::object_tree::recurse_elem_including_sub_components_no_borrow(
16        component,
17        &(),
18        &mut |elem, _| {
19            let mut must_materialize = false;
20            let mut seen = false;
21            for (prop, _) in crate::typeregister::DEPRECATED_ROTATION_ORIGIN_PROPERTIES {
22                if elem.borrow().is_property_set(prop) {
23                    let span = match elem
24                        .borrow()
25                        .binding(prop)
26                        .and_then(|binding| binding.span.clone())
27                    {
28                        Some(span) => span,
29                        None => {
30                            if seen {
31                                return;
32                            }
33                            elem.borrow().to_source_location()
34                        }
35                    };
36
37                    seen = true;
38
39                    if !is_image_or_text(elem) {
40                        diag.push_error(format!("'{prop}' cannot be set on this element"), &span);
41                    } else {
42                        diag.push_property_deprecation_warning(prop, transform_origin.0, &span);
43                        must_materialize = true;
44                    }
45                }
46            }
47            if !must_materialize {
48                return;
49            }
50
51            let expr = Expression::Struct {
52                ty: transform_origin.1.clone(),
53                values: crate::typeregister::DEPRECATED_ROTATION_ORIGIN_PROPERTIES
54                    .iter()
55                    .map(|(prop, _)| {
56                        (
57                            SmolStr::new_static(&prop[prop.len() - 1..]),
58                            Expression::PropertyReference(NamedReference::new(
59                                elem,
60                                SmolStr::new_static(prop),
61                            )),
62                        )
63                    })
64                    .collect(),
65            };
66
67            if let Some(old_binding) =
68                elem.borrow_mut().set_binding(transform_origin.0.into(), expr.into())
69            {
70                diag.push_error(
71                    "Can't specify transform-origin if rotation-origin-x or rotation-origin-y is used on this element".into(),
72                    &old_binding,
73                );
74            }
75        },
76    );
77}
78
79/// true if this element had a rotation-origin property
80fn is_image_or_text(e: &ElementRc) -> bool {
81    e.borrow().builtin_type().is_some_and(|bt| matches!(bt.name.as_str(), "Image" | "Text"))
82}