i_slint_compiler/passes/
lower_text_input_interface.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 TextInputInterface.text-input-focused to getter or setter.
5
6use crate::expression_tree::{BuiltinFunction, Expression};
7use crate::namedreference::NamedReference;
8use crate::object_tree::{visit_all_expressions, Component};
9use std::rc::Rc;
10
11pub fn lower_text_input_interface(component: &Rc<Component>) {
12    visit_all_expressions(component, |e, _| {
13        e.visit_recursive_mut(&mut |e| match e {
14            Expression::PropertyReference(nr) if is_input_text_focused_prop(nr) => {
15                *e = Expression::FunctionCall {
16                    function: BuiltinFunction::TextInputFocused.into(),
17                    arguments: vec![],
18                    source_location: None,
19                };
20            }
21            Expression::SelfAssignment{ lhs, rhs, .. } => {
22                if matches!(&**lhs, Expression::PropertyReference(nr)  if is_input_text_focused_prop(nr) ) {
23                    let rhs = std::mem::take(&mut **rhs);
24                    *e = Expression::FunctionCall {
25                        function: BuiltinFunction::SetTextInputFocused.into(),
26                        arguments: vec![rhs],
27                        source_location: None,
28                    };
29                }
30
31            }
32            _ => {}
33        })
34    })
35}
36
37fn is_input_text_focused_prop(nr: &NamedReference) -> bool {
38    if !nr.element().borrow().builtin_type().is_some_and(|bt| bt.name == "TextInputInterface") {
39        return false;
40    }
41    assert_eq!(nr.name(), "text-input-focused");
42    true
43}