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, Expression};
7use crate::object_tree::{visit_all_expressions, Component};
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)
14                if nr.element().borrow().builtin_type().is_some_and(|bt| bt.name == "Platform") =>
15            {
16                if nr.name() == "os" {
17                    *e = Expression::FunctionCall {
18                        function: BuiltinFunction::DetectOperatingSystem.into(),
19                        arguments: vec![],
20                        source_location: None,
21                    };
22                } else if nr.name() == "style-name" {
23                    let style =
24                        type_loader.resolved_style.strip_suffix("-dark").unwrap_or_else(|| {
25                            type_loader
26                                .resolved_style
27                                .strip_suffix("-light")
28                                .unwrap_or(&type_loader.resolved_style)
29                        });
30                    *e = Expression::StringLiteral(style.into()).into();
31                }
32            }
33
34            _ => {}
35        })
36    })
37}