Skip to main content

i_slint_compiler/passes/
lower_platform.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 lowers the access to the global Platform to constants and builtin function calls.
5
6use crate::expression_tree::{BuiltinFunction, Callable, Expression, NamedReference};
7use crate::object_tree::{Component, visit_all_expressions};
8use std::rc::Rc;
9
10pub fn lower_platform(component: &Rc<Component>, type_loader: &mut crate::typeloader::TypeLoader) {
11    visit_all_expressions(component, |e, _| {
12        e.visit_recursive_mut(&mut |e| match e {
13            Expression::PropertyReference(nr) if is_platform(nr) => {
14                if nr.name() == "os" {
15                    *e = Expression::FunctionCall {
16                        function: BuiltinFunction::DetectOperatingSystem.into(),
17                        arguments: Vec::new(),
18                        source_location: None,
19                    };
20                } else if nr.name() == "style-name" {
21                    let style =
22                        type_loader.resolved_style.strip_suffix("-dark").unwrap_or_else(|| {
23                            type_loader
24                                .resolved_style
25                                .strip_suffix("-light")
26                                .unwrap_or(&type_loader.resolved_style)
27                        });
28                    *e = Expression::StringLiteral(style.into());
29                }
30            }
31            Expression::FunctionCall { function, .. }
32                if matches!(&*function, Callable::Function(nr)
33                    if is_platform(nr) && nr.name() == "open-url") =>
34            {
35                *function = BuiltinFunction::OpenUrl.into();
36            }
37            _ => {}
38        })
39    })
40}
41
42fn is_platform(nr: &NamedReference) -> bool {
43    nr.element().borrow().builtin_type().is_some_and(|bt| bt.name == "Platform")
44}