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                } else if nr.name() == "decimal-separator" {
30                    *e = Expression::FunctionCall {
31                        function: BuiltinFunction::DecimalSeparator.into(),
32                        arguments: Vec::new(),
33                        source_location: None,
34                    }
35                }
36            }
37            Expression::FunctionCall { function, .. }
38                if matches!(&*function, Callable::Function(nr)
39                    if is_platform(nr) && nr.name() == "open-url") =>
40            {
41                *function = BuiltinFunction::OpenUrl.into();
42            }
43            Expression::FunctionCall { function, .. }
44                if matches!(&*function, Callable::Function(nr)
45                    if is_platform(nr) && nr.name() == "macos-bring-all-windows-to-front") =>
46            {
47                *function = BuiltinFunction::MacosBringAllWindowsToFront.into();
48            }
49            _ => {}
50        })
51    })
52}
53
54fn is_platform(nr: &NamedReference) -> bool {
55    nr.element().borrow().builtin_type().is_some_and(|bt| bt.name == "Platform")
56}