Skip to main content

i_slint_compiler/passes/
visible.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//! Pass that lowers synthetic `visible` properties to Clip element
5
6use smol_str::{SmolStr, format_smolstr};
7use std::cell::RefCell;
8use std::rc::Rc;
9
10use crate::diagnostics::BuildDiagnostics;
11use crate::expression_tree::{Expression, NamedReference};
12use crate::langtype::{ElementType, NativeClass, Type};
13use crate::object_tree::{self, Component, Element, ElementRc};
14use crate::typeregister::TypeRegister;
15
16pub fn handle_visible(
17    component: &Rc<Component>,
18    type_register: &TypeRegister,
19    diag: &mut BuildDiagnostics,
20) {
21    // SystemTrayIcon uses `visible` as a real reactive property (toggling the tray
22    // icon's presence) rather than the lower-to-Clip layout sugar. Skip the
23    // warning + lowering for tray-rooted components.
24    if component.inherits_system_tray_icon() {
25        return;
26    }
27
28    if let Some(b) = component.root_element.borrow().bindings.get("visible") {
29        diag.push_warning(
30            "The visible property cannot be used on the root element, it will not be applied"
31                .into(),
32            &*b.borrow(),
33        );
34    }
35
36    let native_clip =
37        type_register.lookup_builtin_element("Clip").unwrap().as_builtin().native_class.clone();
38
39    crate::object_tree::recurse_elem_including_sub_components(
40        component,
41        &(),
42        &mut |elem: &ElementRc, _| {
43            let is_lowered_from_visible_property = elem.borrow().native_class().is_some_and(|n| {
44                Rc::ptr_eq(&n, &native_clip) && elem.borrow().id.ends_with("-visibility")
45            });
46            if is_lowered_from_visible_property {
47                // This is the element we just created. Skip it.
48                return;
49            }
50
51            let old_children = {
52                let mut elem = elem.borrow_mut();
53                let new_children = Vec::with_capacity(elem.children.len());
54                std::mem::replace(&mut elem.children, new_children)
55            };
56
57            let has_visible_binding = |e: &ElementRc| {
58                e.borrow().base_type.lookup_property("visible").property_type != Type::Invalid
59                    && (e.borrow().bindings.contains_key("visible")
60                        || e.borrow()
61                            .property_analysis
62                            .borrow()
63                            .get("visible")
64                            .is_some_and(|a| a.is_set || a.is_linked))
65            };
66
67            for mut child in old_children {
68                if child.borrow().repeated.is_some() {
69                    let root_elem = child.borrow().base_type.as_component().root_element.clone();
70                    if has_visible_binding(&root_elem) {
71                        let clip_elem = create_visibility_element(&root_elem, &native_clip);
72                        object_tree::inject_element_as_repeated_element(&child, clip_elem.clone());
73                        // The width and the height must be null
74                        let d = NamedReference::new(&clip_elem, SmolStr::new_static("dummy"));
75                        clip_elem.borrow_mut().geometry_props.as_mut().unwrap().width = d.clone();
76                        clip_elem.borrow_mut().geometry_props.as_mut().unwrap().height = d;
77                    }
78                } else if has_visible_binding(&child) {
79                    let new_child = create_visibility_element(&child, &native_clip);
80                    new_child.borrow_mut().children.push(child);
81                    child = new_child;
82                }
83
84                elem.borrow_mut().children.push(child);
85            }
86        },
87    );
88}
89
90fn create_visibility_element(child: &ElementRc, native_clip: &Rc<NativeClass>) -> ElementRc {
91    let child_grid_layout_cell = child.borrow_mut().grid_layout_cell.take();
92    let element = Element {
93        id: format_smolstr!("{}-visibility", child.borrow().id),
94        base_type: ElementType::Native(native_clip.clone()),
95        enclosing_component: child.borrow().enclosing_component.clone(),
96        bindings: std::iter::once((
97            SmolStr::new_static("clip"),
98            RefCell::new(
99                Expression::UnaryOp {
100                    sub: Box::new(Expression::PropertyReference(NamedReference::new(
101                        child,
102                        SmolStr::new_static("visible"),
103                    ))),
104                    op: '!',
105                }
106                .into(),
107            ),
108        ))
109        .collect(),
110        grid_layout_cell: child_grid_layout_cell,
111        ..Default::default()
112    };
113    Element::make_rc(element)
114}