i_slint_compiler/passes/
apply_default_properties_from_style.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 applies the default properties from the style.
5//!
6//! Note that the layout default properties are handled in the lower_layout pass
7
8use crate::diagnostics::BuildDiagnostics;
9use crate::expression_tree::{Expression, NamedReference};
10use crate::langtype::Type;
11use crate::object_tree::Component;
12use smol_str::SmolStr;
13use std::rc::Rc;
14
15/// Ideally we would be able to write this in builtin.slint, but the StyleMetrics is not available there
16pub fn apply_default_properties_from_style(
17    root_component: &Rc<Component>,
18    style_metrics: &Rc<Component>,
19    palette: &Rc<Component>,
20    _diag: &mut BuildDiagnostics,
21) {
22    crate::object_tree::recurse_elem_including_sub_components(
23        root_component,
24        &(),
25        &mut |elem, _| {
26            let mut elem = elem.borrow_mut();
27            match elem.builtin_type().as_ref().map_or("", |b| b.name.as_str()) {
28                "TextInput" => {
29                    elem.set_binding_if_not_set("text-cursor-width".into(), || {
30                        Expression::PropertyReference(NamedReference::new(
31                            &style_metrics.root_element,
32                            SmolStr::new_static("text-cursor-width"),
33                        ))
34                    });
35                    elem.set_binding_if_not_set("color".into(), || {
36                        Expression::PropertyReference(NamedReference::new(
37                            &palette.root_element,
38                            SmolStr::new_static("foreground"),
39                        ))
40                        .into()
41                    });
42                    elem.set_binding_if_not_set("selection-background-color".into(), || {
43                        Expression::Cast {
44                            from: Expression::PropertyReference(NamedReference::new(
45                                &palette.root_element,
46                                SmolStr::new_static("selection-background"),
47                            ))
48                            .into(),
49                            to: Type::Color,
50                        }
51                    });
52                    elem.set_binding_if_not_set("selection-foreground-color".into(), || {
53                        Expression::Cast {
54                            from: Expression::PropertyReference(NamedReference::new(
55                                &palette.root_element,
56                                SmolStr::new_static("selection-foreground"),
57                            ))
58                            .into(),
59                            to: Type::Color,
60                        }
61                    });
62                }
63                "Text" => {
64                    elem.set_binding_if_not_set("color".into(), || Expression::Cast {
65                        from: Expression::PropertyReference(NamedReference::new(
66                            &palette.root_element,
67                            SmolStr::new_static("foreground"),
68                        ))
69                        .into(),
70                        to: Type::Brush,
71                    });
72                }
73                "Dialog" | "Window" => {
74                    elem.set_binding_if_not_set("background".into(), || Expression::Cast {
75                        from: Expression::PropertyReference(NamedReference::new(
76                            &palette.root_element,
77                            SmolStr::new_static("background"),
78                        ))
79                        .into(),
80                        to: Type::Brush,
81                    });
82
83                    let mut bind_style_property_if_exists = |property_name, property_type| {
84                        if !matches!(
85                            style_metrics
86                                .root_element
87                                .borrow()
88                                .lookup_property(property_name)
89                                .property_type,
90                            Type::Invalid,
91                        ) {
92                            elem.set_binding_if_not_set(property_name.into(), || {
93                                Expression::Cast {
94                                    from: Expression::PropertyReference(NamedReference::new(
95                                        &style_metrics.root_element,
96                                        SmolStr::new_static(property_name),
97                                    ))
98                                    .into(),
99                                    to: property_type,
100                                }
101                            });
102                        }
103                    };
104
105                    bind_style_property_if_exists("default-font-family", Type::String);
106                }
107
108                _ => {}
109            }
110        },
111    )
112}