Skip to main content

slint_interpreter/
eval.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
4use crate::api::{SetPropertyError, Struct, Value};
5use crate::dynamic_item_tree::{CallbackHandler, InstanceRef};
6use core::ffi::c_void;
7use core::pin::Pin;
8use corelib::graphics::{
9    ConicGradientBrush, GradientStop, LinearGradientBrush, PathElement, RadialGradientBrush,
10};
11use corelib::input::FocusReason;
12use corelib::items::{ItemRc, ItemRef, PropertyAnimation, WindowItem};
13use corelib::menus::{Menu, MenuFromItemTree};
14use corelib::model::{Model, ModelExt, ModelRc, VecModel};
15use corelib::rtti::AnimatedBindingKind;
16use corelib::window::WindowInner;
17use corelib::{Brush, Color, PathData, SharedString, SharedVector};
18use i_slint_compiler::expression_tree::{
19    BuiltinFunction, Callable, EasingCurve, Expression, MinMaxOp, Path as ExprPath,
20    PathElement as ExprPathElement,
21};
22use i_slint_compiler::langtype::Type;
23use i_slint_compiler::namedreference::NamedReference;
24use i_slint_compiler::object_tree::ElementRc;
25use i_slint_core::api::ToSharedString;
26use i_slint_core::{self as corelib};
27use smol_str::SmolStr;
28use std::collections::HashMap;
29use std::rc::Rc;
30
31pub trait ErasedPropertyInfo {
32    fn get(&self, item: Pin<ItemRef>) -> Value;
33    fn set(
34        &self,
35        item: Pin<ItemRef>,
36        value: Value,
37        animation: Option<PropertyAnimation>,
38    ) -> Result<(), ()>;
39    fn set_binding(
40        &self,
41        item: Pin<ItemRef>,
42        binding: Box<dyn Fn() -> Value>,
43        animation: AnimatedBindingKind,
44    );
45    fn offset(&self) -> usize;
46
47    #[cfg(slint_debug_property)]
48    fn set_debug_name(&self, item: Pin<ItemRef>, name: String);
49
50    /// Safety: Property2 must be a (pinned) pointer to a `Property<T>`
51    /// where T is the same T as the one represented by this property.
52    unsafe fn link_two_ways(&self, item: Pin<ItemRef>, property2: *const c_void);
53
54    fn prepare_for_two_way_binding(&self, item: Pin<ItemRef>) -> Pin<Rc<corelib::Property<Value>>>;
55
56    fn link_two_way_with_map(
57        &self,
58        item: Pin<ItemRef>,
59        property2: Pin<Rc<corelib::Property<Value>>>,
60        map: Option<Rc<dyn corelib::rtti::TwoWayBindingMapping<Value>>>,
61    );
62
63    fn link_two_way_to_model_data(
64        &self,
65        item: Pin<ItemRef>,
66        getter: Box<dyn Fn() -> Option<Value>>,
67        setter: Box<dyn Fn(&Value)>,
68    );
69}
70
71impl<Item: vtable::HasStaticVTable<corelib::items::ItemVTable>> ErasedPropertyInfo
72    for &'static dyn corelib::rtti::PropertyInfo<Item, Value>
73{
74    fn get(&self, item: Pin<ItemRef>) -> Value {
75        (*self).get(ItemRef::downcast_pin(item).unwrap()).unwrap()
76    }
77    fn set(
78        &self,
79        item: Pin<ItemRef>,
80        value: Value,
81        animation: Option<PropertyAnimation>,
82    ) -> Result<(), ()> {
83        (*self).set(ItemRef::downcast_pin(item).unwrap(), value, animation)
84    }
85    fn set_binding(
86        &self,
87        item: Pin<ItemRef>,
88        binding: Box<dyn Fn() -> Value>,
89        animation: AnimatedBindingKind,
90    ) {
91        (*self).set_binding(ItemRef::downcast_pin(item).unwrap(), binding, animation).unwrap();
92    }
93    fn offset(&self) -> usize {
94        (*self).offset()
95    }
96    #[cfg(slint_debug_property)]
97    fn set_debug_name(&self, item: Pin<ItemRef>, name: String) {
98        (*self).set_debug_name(ItemRef::downcast_pin(item).unwrap(), name);
99    }
100    unsafe fn link_two_ways(&self, item: Pin<ItemRef>, property2: *const c_void) {
101        // Safety: ErasedPropertyInfo::link_two_ways and PropertyInfo::link_two_ways have the same safety requirement
102        unsafe { (*self).link_two_ways(ItemRef::downcast_pin(item).unwrap(), property2) }
103    }
104
105    fn prepare_for_two_way_binding(&self, item: Pin<ItemRef>) -> Pin<Rc<corelib::Property<Value>>> {
106        (*self).prepare_for_two_way_binding(ItemRef::downcast_pin(item).unwrap())
107    }
108
109    fn link_two_way_with_map(
110        &self,
111        item: Pin<ItemRef>,
112        property2: Pin<Rc<corelib::Property<Value>>>,
113        map: Option<Rc<dyn corelib::rtti::TwoWayBindingMapping<Value>>>,
114    ) {
115        (*self).link_two_way_with_map(ItemRef::downcast_pin(item).unwrap(), property2, map)
116    }
117
118    fn link_two_way_to_model_data(
119        &self,
120        item: Pin<ItemRef>,
121        getter: Box<dyn Fn() -> Option<Value>>,
122        setter: Box<dyn Fn(&Value)>,
123    ) {
124        (*self).link_two_way_to_model_data(ItemRef::downcast_pin(item).unwrap(), getter, setter)
125    }
126}
127
128pub trait ErasedCallbackInfo {
129    fn call(&self, item: Pin<ItemRef>, args: &[Value]) -> Value;
130    fn set_handler(&self, item: Pin<ItemRef>, handler: Box<dyn Fn(&[Value]) -> Value>);
131}
132
133impl<Item: vtable::HasStaticVTable<corelib::items::ItemVTable>> ErasedCallbackInfo
134    for &'static dyn corelib::rtti::CallbackInfo<Item, Value>
135{
136    fn call(&self, item: Pin<ItemRef>, args: &[Value]) -> Value {
137        (*self).call(ItemRef::downcast_pin(item).unwrap(), args).unwrap()
138    }
139
140    fn set_handler(&self, item: Pin<ItemRef>, handler: Box<dyn Fn(&[Value]) -> Value>) {
141        (*self).set_handler(ItemRef::downcast_pin(item).unwrap(), handler).unwrap()
142    }
143}
144
145impl corelib::rtti::ValueType for Value {}
146
147#[derive(Clone)]
148pub(crate) enum ComponentInstance<'a, 'id> {
149    InstanceRef(InstanceRef<'a, 'id>),
150    GlobalComponent(Pin<Rc<dyn crate::global_component::GlobalComponent>>),
151}
152
153/// The local variable needed for binding evaluation
154pub struct EvalLocalContext<'a, 'id> {
155    local_variables: HashMap<SmolStr, Value>,
156    function_arguments: Vec<Value>,
157    pub(crate) component_instance: InstanceRef<'a, 'id>,
158    /// When Some, a return statement was executed and one must stop evaluating
159    return_value: Option<Value>,
160}
161
162impl<'a, 'id> EvalLocalContext<'a, 'id> {
163    pub fn from_component_instance(component: InstanceRef<'a, 'id>) -> Self {
164        Self {
165            local_variables: Default::default(),
166            function_arguments: Default::default(),
167            component_instance: component,
168            return_value: None,
169        }
170    }
171
172    /// Create a context for a function and passing the arguments
173    pub fn from_function_arguments(
174        component: InstanceRef<'a, 'id>,
175        function_arguments: Vec<Value>,
176    ) -> Self {
177        Self {
178            component_instance: component,
179            function_arguments,
180            local_variables: Default::default(),
181            return_value: None,
182        }
183    }
184}
185
186/// Evaluate `expression` as a length / number and return the resulting f32.
187/// Caller's responsibility to only pass length-typed expressions.
188fn eval_to_f32(expression: &Expression, local_context: &mut EvalLocalContext) -> f32 {
189    match eval_expression(expression, local_context) {
190        Value::Number(n) => n as f32,
191        other => unreachable!("expected length-typed expression; got {other:?} for {expression:?}"),
192    }
193}
194
195/// Evaluate an expression and return a Value as the result of this expression
196pub fn eval_expression(expression: &Expression, local_context: &mut EvalLocalContext) -> Value {
197    if let Some(r) = &local_context.return_value {
198        return r.clone();
199    }
200    match expression {
201        Expression::Invalid => panic!("invalid expression while evaluating"),
202        Expression::Uncompiled(_) => panic!("uncompiled expression while evaluating"),
203        Expression::StringLiteral(s) => Value::String(s.as_str().into()),
204        Expression::NumberLiteral(n, unit) => Value::Number(unit.normalize(*n)),
205        Expression::BoolLiteral(b) => Value::Bool(*b),
206        Expression::ElementReference(_) => todo!(
207            "Element references are only supported in the context of built-in function calls at the moment"
208        ),
209        Expression::PropertyReference(nr) => load_property_helper(
210            &ComponentInstance::InstanceRef(local_context.component_instance),
211            &nr.element(),
212            nr.name(),
213        )
214        .unwrap(),
215        Expression::RepeaterIndexReference { element } => load_property_helper(
216            &ComponentInstance::InstanceRef(local_context.component_instance),
217            &element.upgrade().unwrap().borrow().base_type.as_component().root_element,
218            crate::dynamic_item_tree::SPECIAL_PROPERTY_INDEX,
219        )
220        .unwrap(),
221        Expression::RepeaterModelReference { element } => {
222            let value = load_property_helper(
223                &ComponentInstance::InstanceRef(local_context.component_instance),
224                &element.upgrade().unwrap().borrow().base_type.as_component().root_element,
225                crate::dynamic_item_tree::SPECIAL_PROPERTY_MODEL_DATA,
226            )
227            .unwrap();
228            if matches!(value, Value::Void) {
229                // Uninitialized model data (because the model returned None) should still be initialized to the default value of the type
230                default_value_for_type(&expression.ty())
231            } else {
232                value
233            }
234        }
235        Expression::FunctionParameterReference { index, .. } => {
236            local_context.function_arguments[*index].clone()
237        }
238        Expression::StructFieldAccess { base, name } => {
239            if let Value::Struct(o) = eval_expression(base, local_context) {
240                o.get_field(name).cloned().unwrap_or(Value::Void)
241            } else {
242                Value::Void
243            }
244        }
245        Expression::ArrayIndex { array, index } => {
246            let array = eval_expression(array, local_context);
247            let index = eval_expression(index, local_context);
248            match (array, index) {
249                (Value::Model(model), Value::Number(index)) => model
250                    .row_data_tracked(index as isize as usize)
251                    .unwrap_or_else(|| default_value_for_type(&expression.ty())),
252                _ => Value::Void,
253            }
254        }
255        Expression::Cast { from, to } => {
256            let value = eval_expression(from, local_context);
257            match (value, to) {
258                (Value::Number(n), Type::Int32) => Value::Number(n.trunc()),
259                (Value::Number(n), Type::String) => {
260                    Value::String(i_slint_core::string::shared_string_from_number(n))
261                }
262                (Value::Number(n), Type::Color) => Color::from_argb_encoded(n as u32).into(),
263                (Value::Brush(brush), Type::Color) => brush.color().into(),
264                (Value::EnumerationValue(_, val), Type::String) => Value::String(val.into()),
265                (v, _) => v,
266            }
267        }
268        Expression::CodeBlock(sub) => {
269            let mut v = Value::Void;
270            for e in sub {
271                v = eval_expression(e, local_context);
272                if let Some(r) = &local_context.return_value {
273                    return r.clone();
274                }
275            }
276            v
277        }
278        Expression::FunctionCall { function, arguments, source_location } => match &function {
279            Callable::Function(nr) => {
280                let is_item_member = nr
281                    .element()
282                    .borrow()
283                    .native_class()
284                    .is_some_and(|n| n.properties.contains_key(nr.name()));
285                if is_item_member {
286                    call_item_member_function(nr, local_context)
287                } else {
288                    let args = arguments
289                        .iter()
290                        .map(|e| eval_expression(e, local_context))
291                        .collect::<Vec<_>>();
292                    call_function(
293                        &ComponentInstance::InstanceRef(local_context.component_instance),
294                        &nr.element(),
295                        nr.name(),
296                        args,
297                    )
298                    .unwrap()
299                }
300            }
301            Callable::Callback(nr) => {
302                let args =
303                    arguments.iter().map(|e| eval_expression(e, local_context)).collect::<Vec<_>>();
304                invoke_callback(
305                    &ComponentInstance::InstanceRef(local_context.component_instance),
306                    &nr.element(),
307                    nr.name(),
308                    &args,
309                )
310                .unwrap()
311            }
312            Callable::Builtin(f) => {
313                call_builtin_function(f.clone(), arguments, local_context, source_location)
314            }
315        },
316        Expression::SelfAssignment { lhs, rhs, op, .. } => {
317            let rhs = eval_expression(rhs, local_context);
318            eval_assignment(lhs, *op, rhs, local_context);
319            Value::Void
320        }
321        Expression::BinaryExpression { lhs, rhs, op } => {
322            let lhs = eval_expression(lhs, local_context);
323            let rhs = eval_expression(rhs, local_context);
324
325            match (op, lhs, rhs) {
326                ('+', Value::String(mut a), Value::String(b)) => {
327                    a.push_str(b.as_str());
328                    Value::String(a)
329                }
330                ('+', Value::Number(a), Value::Number(b)) => Value::Number(a + b),
331                ('+', a @ Value::Struct(_), b @ Value::Struct(_)) => {
332                    let a: Option<corelib::layout::LayoutInfo> = a.try_into().ok();
333                    let b: Option<corelib::layout::LayoutInfo> = b.try_into().ok();
334                    if let (Some(a), Some(b)) = (a, b) {
335                        a.merge(&b).into()
336                    } else {
337                        panic!("unsupported {a:?} {op} {b:?}");
338                    }
339                }
340                ('-', Value::Number(a), Value::Number(b)) => Value::Number(a - b),
341                ('/', Value::Number(a), Value::Number(b)) => Value::Number(a / b),
342                ('*', Value::Number(a), Value::Number(b)) => Value::Number(a * b),
343                ('<', Value::Number(a), Value::Number(b)) => Value::Bool(a < b),
344                ('>', Value::Number(a), Value::Number(b)) => Value::Bool(a > b),
345                ('≤', Value::Number(a), Value::Number(b)) => Value::Bool(a <= b),
346                ('≥', Value::Number(a), Value::Number(b)) => Value::Bool(a >= b),
347                ('<', Value::String(a), Value::String(b)) => Value::Bool(a < b),
348                ('>', Value::String(a), Value::String(b)) => Value::Bool(a > b),
349                ('≤', Value::String(a), Value::String(b)) => Value::Bool(a <= b),
350                ('≥', Value::String(a), Value::String(b)) => Value::Bool(a >= b),
351                ('=', a, b) => Value::Bool(a == b),
352                ('!', a, b) => Value::Bool(a != b),
353                ('&', Value::Bool(a), Value::Bool(b)) => Value::Bool(a && b),
354                ('|', Value::Bool(a), Value::Bool(b)) => Value::Bool(a || b),
355                (op, lhs, rhs) => panic!("unsupported {lhs:?} {op} {rhs:?}"),
356            }
357        }
358        Expression::UnaryOp { sub, op } => {
359            let sub = eval_expression(sub, local_context);
360            match (sub, op) {
361                (Value::Number(a), '+') => Value::Number(a),
362                (Value::Number(a), '-') => Value::Number(-a),
363                (Value::Bool(a), '!') => Value::Bool(!a),
364                (sub, op) => panic!("unsupported {op} {sub:?}"),
365            }
366        }
367        Expression::ImageReference { resource_ref, nine_slice, .. } => {
368            let mut image = match resource_ref {
369                i_slint_compiler::expression_tree::ImageReference::None => Ok(Default::default()),
370                i_slint_compiler::expression_tree::ImageReference::AbsolutePath(path) => {
371                    if path.starts_with("data:") {
372                        i_slint_compiler::data_uri::decode_data_uri(path)
373                            .ok()
374                            .and_then(|(data, extension)| {
375                                corelib::graphics::load_image_from_dynamic_data(&data, &extension)
376                                    .ok()
377                            })
378                            .ok_or_else(Default::default)
379                    } else {
380                        let path = std::path::Path::new(path);
381                        if path.starts_with("builtin:/") {
382                            i_slint_compiler::fileaccess::load_file(path)
383                                .and_then(|virtual_file| virtual_file.builtin_contents)
384                                .map(|virtual_file| {
385                                    let extension = path.extension().unwrap().to_str().unwrap();
386                                    corelib::graphics::load_image_from_embedded_data(
387                                        corelib::slice::Slice::from_slice(virtual_file),
388                                        corelib::slice::Slice::from_slice(extension.as_bytes()),
389                                    )
390                                })
391                                .ok_or_else(Default::default)
392                        } else {
393                            corelib::graphics::Image::load_from_path(path)
394                        }
395                    }
396                }
397                i_slint_compiler::expression_tree::ImageReference::EmbeddedData { .. } => {
398                    todo!()
399                }
400                i_slint_compiler::expression_tree::ImageReference::EmbeddedTexture { .. } => {
401                    todo!()
402                }
403            }
404            .unwrap_or_else(|_| {
405                eprintln!("Could not load image {resource_ref:?}");
406                Default::default()
407            });
408            if let Some(n) = nine_slice {
409                image.set_nine_slice_edges(n[0], n[1], n[2], n[3]);
410            }
411            Value::Image(image)
412        }
413        Expression::Condition { condition, true_expr, false_expr } => {
414            match eval_expression(condition, local_context).try_into() as Result<bool, _> {
415                Ok(true) => eval_expression(true_expr, local_context),
416                Ok(false) => eval_expression(false_expr, local_context),
417                _ => local_context
418                    .return_value
419                    .clone()
420                    .expect("conditional expression did not evaluate to boolean"),
421            }
422        }
423        Expression::Array { values, .. } => {
424            Value::Model(ModelRc::new(corelib::model::SharedVectorModel::from(
425                values
426                    .iter()
427                    .map(|e| eval_expression(e, local_context))
428                    .collect::<SharedVector<_>>(),
429            )))
430        }
431        Expression::Struct { values, .. } => Value::Struct(
432            values
433                .iter()
434                .map(|(k, v)| (k.to_string(), eval_expression(v, local_context)))
435                .collect(),
436        ),
437        Expression::PathData(data) => Value::PathData(convert_path(data, local_context)),
438        Expression::StoreLocalVariable { name, value } => {
439            let value = eval_expression(value, local_context);
440            local_context.local_variables.insert(name.clone(), value);
441            Value::Void
442        }
443        Expression::ReadLocalVariable { name, .. } => {
444            local_context.local_variables.get(name).unwrap().clone()
445        }
446        Expression::EasingCurve(curve) => Value::EasingCurve(match curve {
447            EasingCurve::Linear => corelib::animations::EasingCurve::Linear,
448            EasingCurve::EaseInElastic => corelib::animations::EasingCurve::EaseInElastic,
449            EasingCurve::EaseOutElastic => corelib::animations::EasingCurve::EaseOutElastic,
450            EasingCurve::EaseInOutElastic => corelib::animations::EasingCurve::EaseInOutElastic,
451            EasingCurve::EaseInBounce => corelib::animations::EasingCurve::EaseInBounce,
452            EasingCurve::EaseOutBounce => corelib::animations::EasingCurve::EaseOutBounce,
453            EasingCurve::EaseInOutBounce => corelib::animations::EasingCurve::EaseInOutBounce,
454            EasingCurve::CubicBezier(a, b, c, d) => {
455                corelib::animations::EasingCurve::CubicBezier([*a, *b, *c, *d])
456            }
457        }),
458        Expression::LinearGradient { angle, stops } => {
459            let angle = eval_expression(angle, local_context);
460            Value::Brush(Brush::LinearGradient(LinearGradientBrush::new(
461                angle.try_into().unwrap(),
462                stops.iter().map(|(color, stop)| {
463                    let color = eval_expression(color, local_context).try_into().unwrap();
464                    let position = eval_expression(stop, local_context).try_into().unwrap();
465                    GradientStop { color, position }
466                }),
467            )))
468        }
469        Expression::RadialGradient { stops } => Value::Brush(Brush::RadialGradient(
470            RadialGradientBrush::new_circle(stops.iter().map(|(color, stop)| {
471                let color = eval_expression(color, local_context).try_into().unwrap();
472                let position = eval_expression(stop, local_context).try_into().unwrap();
473                GradientStop { color, position }
474            })),
475        )),
476        Expression::ConicGradient { from_angle, stops } => {
477            let from_angle: f32 = eval_expression(from_angle, local_context).try_into().unwrap();
478            Value::Brush(Brush::ConicGradient(ConicGradientBrush::new(
479                from_angle,
480                stops.iter().map(|(color, stop)| {
481                    let color = eval_expression(color, local_context).try_into().unwrap();
482                    let position = eval_expression(stop, local_context).try_into().unwrap();
483                    GradientStop { color, position }
484                }),
485            )))
486        }
487        Expression::EnumerationValue(value) => {
488            Value::EnumerationValue(value.enumeration.name.to_string(), value.to_string())
489        }
490        Expression::Keys(ks) => {
491            let mut modifiers = i_slint_core::input::KeyboardModifiers::default();
492            modifiers.alt = ks.modifiers.alt;
493            modifiers.control = ks.modifiers.control;
494            modifiers.shift = ks.modifiers.shift;
495            modifiers.meta = ks.modifiers.meta;
496
497            Value::Keys(i_slint_core::input::make_keys(
498                SharedString::from(&*ks.key),
499                modifiers,
500                ks.ignore_shift,
501                ks.ignore_alt,
502            ))
503        }
504        Expression::ReturnStatement(x) => {
505            let val = x.as_ref().map_or(Value::Void, |x| eval_expression(x, local_context));
506            if local_context.return_value.is_none() {
507                local_context.return_value = Some(val);
508            }
509            local_context.return_value.clone().unwrap()
510        }
511        Expression::LayoutCacheAccess {
512            layout_cache_prop,
513            index,
514            repeater_index,
515            entries_per_item,
516        } => {
517            let cache = load_property_helper(
518                &ComponentInstance::InstanceRef(local_context.component_instance),
519                &layout_cache_prop.element(),
520                layout_cache_prop.name(),
521            )
522            .unwrap();
523            if let Value::LayoutCache(cache) = cache {
524                // Coordinate cache
525                if let Some(ri) = repeater_index {
526                    let offset: usize = eval_expression(ri, local_context).try_into().unwrap();
527                    Value::Number(
528                        cache
529                            .get((cache[*index] as usize) + offset * entries_per_item)
530                            .copied()
531                            .unwrap_or(0.)
532                            .into(),
533                    )
534                } else {
535                    Value::Number(cache[*index].into())
536                }
537            } else if let Value::ArrayOfU16(cache) = cache {
538                // Organized Data cache
539                if let Some(ri) = repeater_index {
540                    let offset: usize = eval_expression(ri, local_context).try_into().unwrap();
541                    Value::Number(
542                        cache
543                            .get((cache[*index] as usize) + offset * entries_per_item)
544                            .copied()
545                            .unwrap_or(0)
546                            .into(),
547                    )
548                } else {
549                    Value::Number(cache[*index].into())
550                }
551            } else {
552                panic!("invalid layout cache")
553            }
554        }
555        Expression::GridRepeaterCacheAccess {
556            layout_cache_prop,
557            index,
558            repeater_index,
559            stride,
560            child_offset,
561            inner_repeater_index,
562            entries_per_item,
563        } => {
564            let cache = load_property_helper(
565                &ComponentInstance::InstanceRef(local_context.component_instance),
566                &layout_cache_prop.element(),
567                layout_cache_prop.name(),
568            )
569            .unwrap();
570            if let Value::LayoutCache(cache) = cache {
571                // Coordinate cache
572                let row_idx: usize =
573                    eval_expression(repeater_index, local_context).try_into().unwrap();
574                let stride_val: usize = eval_expression(stride, local_context).try_into().unwrap();
575                if let Some(inner_ri) = inner_repeater_index {
576                    let inner_offset: usize =
577                        eval_expression(inner_ri, local_context).try_into().unwrap();
578                    let base = cache[*index] as usize;
579                    let data_idx = base
580                        + row_idx * stride_val
581                        + *child_offset
582                        + inner_offset * *entries_per_item;
583                    Value::Number(cache.get(data_idx).copied().unwrap_or(0.).into())
584                } else {
585                    let base = cache[*index] as usize;
586                    let data_idx = base + row_idx * stride_val + *child_offset;
587                    Value::Number(cache.get(data_idx).copied().unwrap_or(0.).into())
588                }
589            } else if let Value::ArrayOfU16(cache) = cache {
590                // Organized Data cache
591                let row_idx: usize =
592                    eval_expression(repeater_index, local_context).try_into().unwrap();
593                let stride_val: usize = eval_expression(stride, local_context).try_into().unwrap();
594                if let Some(inner_ri) = inner_repeater_index {
595                    let inner_offset: usize =
596                        eval_expression(inner_ri, local_context).try_into().unwrap();
597                    let base = cache[*index] as usize;
598                    let data_idx = base
599                        + row_idx * stride_val
600                        + *child_offset
601                        + inner_offset * *entries_per_item;
602                    Value::Number(cache.get(data_idx).copied().unwrap_or(0).into())
603                } else {
604                    let base = cache[*index] as usize;
605                    let data_idx = base + row_idx * stride_val + *child_offset;
606                    Value::Number(cache.get(data_idx).copied().unwrap_or(0).into())
607                }
608            } else {
609                panic!("invalid layout cache")
610            }
611        }
612        Expression::ComputeBoxLayoutInfo { layout, orientation, cross_axis_size } => {
613            let cross = cross_axis_size.as_deref().map(|e| eval_to_f32(e, local_context));
614            crate::eval_layout::compute_box_layout_info(layout, *orientation, local_context, cross)
615        }
616        Expression::ComputeGridLayoutInfo {
617            layout_organized_data_prop,
618            layout,
619            orientation,
620            cross_axis_size,
621        } => {
622            let cross = cross_axis_size.as_deref().map(|e| eval_to_f32(e, local_context));
623            let cache = load_property_helper(
624                &ComponentInstance::InstanceRef(local_context.component_instance),
625                &layout_organized_data_prop.element(),
626                layout_organized_data_prop.name(),
627            )
628            .unwrap();
629            if let Value::ArrayOfU16(organized_data) = cache {
630                crate::eval_layout::compute_grid_layout_info(
631                    layout,
632                    &organized_data,
633                    *orientation,
634                    local_context,
635                    cross,
636                )
637            } else {
638                panic!("invalid layout organized data cache")
639            }
640        }
641        Expression::OrganizeGridLayout(lay) => {
642            crate::eval_layout::organize_grid_layout(lay, local_context)
643        }
644        Expression::SolveBoxLayout(lay, o) => {
645            crate::eval_layout::solve_box_layout(lay, *o, local_context)
646        }
647        Expression::SolveGridLayout { layout_organized_data_prop, layout, orientation } => {
648            let cache = load_property_helper(
649                &ComponentInstance::InstanceRef(local_context.component_instance),
650                &layout_organized_data_prop.element(),
651                layout_organized_data_prop.name(),
652            )
653            .unwrap();
654            if let Value::ArrayOfU16(organized_data) = cache {
655                crate::eval_layout::solve_grid_layout(
656                    &organized_data,
657                    layout,
658                    *orientation,
659                    local_context,
660                )
661            } else {
662                panic!("invalid layout organized data cache")
663            }
664        }
665        Expression::SolveFlexboxLayout(layout) => {
666            crate::eval_layout::solve_flexbox_layout(layout, local_context)
667        }
668        Expression::ComputeFlexboxLayoutInfo { layout, orientation, cross_axis_size } => {
669            let cross = cross_axis_size.as_deref().map(|e| eval_to_f32(e, local_context));
670            crate::eval_layout::compute_flexbox_layout_info(
671                layout,
672                *orientation,
673                local_context,
674                cross,
675            )
676        }
677        Expression::MinMax { ty: _, op, lhs, rhs } => {
678            let Value::Number(lhs) = eval_expression(lhs, local_context) else {
679                return local_context
680                    .return_value
681                    .clone()
682                    .expect("minmax lhs expression did not evaluate to number");
683            };
684            let Value::Number(rhs) = eval_expression(rhs, local_context) else {
685                return local_context
686                    .return_value
687                    .clone()
688                    .expect("minmax rhs expression did not evaluate to number");
689            };
690            match op {
691                MinMaxOp::Min => Value::Number(lhs.min(rhs)),
692                MinMaxOp::Max => Value::Number(lhs.max(rhs)),
693            }
694        }
695        Expression::EmptyComponentFactory => Value::ComponentFactory(Default::default()),
696        Expression::EmptyDataTransfer => Value::DataTransfer(Default::default()),
697        Expression::DebugHook { expression, .. } => eval_expression(expression, local_context),
698    }
699}
700
701fn call_builtin_function(
702    f: BuiltinFunction,
703    arguments: &[Expression],
704    local_context: &mut EvalLocalContext,
705    source_location: &Option<i_slint_compiler::diagnostics::SourceLocation>,
706) -> Value {
707    match f {
708        BuiltinFunction::GetWindowScaleFactor => Value::Number(
709            local_context.component_instance.access_window(|window| window.scale_factor()) as _,
710        ),
711        BuiltinFunction::GetWindowDefaultFontSize => Value::Number({
712            let component = local_context.component_instance;
713            let item_comp = component.self_weak().get().unwrap().upgrade().unwrap();
714            WindowItem::resolved_default_font_size(vtable::VRc::into_dyn(item_comp)).get() as _
715        }),
716        BuiltinFunction::AnimationTick => {
717            Value::Number(i_slint_core::animations::animation_tick() as f64)
718        }
719        BuiltinFunction::Debug => {
720            let to_print: SharedString =
721                eval_expression(&arguments[0], local_context).try_into().unwrap();
722            local_context.component_instance.description.debug_handler.borrow()(
723                source_location.as_ref(),
724                &to_print,
725            );
726            Value::Void
727        }
728        BuiltinFunction::DecimalSeparator => Value::String(
729            local_context
730                .component_instance
731                .access_window(|window| window.context().locale_decimal_separator())
732                .into(),
733        ),
734        BuiltinFunction::Mod => {
735            let mut to_num = |e| -> f64 { eval_expression(e, local_context).try_into().unwrap() };
736            Value::Number(to_num(&arguments[0]).rem_euclid(to_num(&arguments[1])))
737        }
738        BuiltinFunction::Round => {
739            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
740            Value::Number(x.round())
741        }
742        BuiltinFunction::Ceil => {
743            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
744            Value::Number(x.ceil())
745        }
746        BuiltinFunction::Floor => {
747            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
748            Value::Number(x.floor())
749        }
750        BuiltinFunction::Sqrt => {
751            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
752            Value::Number(x.sqrt())
753        }
754        BuiltinFunction::Abs => {
755            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
756            Value::Number(x.abs())
757        }
758        BuiltinFunction::Sin => {
759            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
760            Value::Number(x.to_radians().sin())
761        }
762        BuiltinFunction::Cos => {
763            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
764            Value::Number(x.to_radians().cos())
765        }
766        BuiltinFunction::Tan => {
767            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
768            Value::Number(x.to_radians().tan())
769        }
770        BuiltinFunction::ASin => {
771            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
772            Value::Number(x.asin().to_degrees())
773        }
774        BuiltinFunction::ACos => {
775            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
776            Value::Number(x.acos().to_degrees())
777        }
778        BuiltinFunction::ATan => {
779            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
780            Value::Number(x.atan().to_degrees())
781        }
782        BuiltinFunction::ATan2 => {
783            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
784            let y: f64 = eval_expression(&arguments[1], local_context).try_into().unwrap();
785            Value::Number(x.atan2(y).to_degrees())
786        }
787        BuiltinFunction::Log => {
788            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
789            let y: f64 = eval_expression(&arguments[1], local_context).try_into().unwrap();
790            Value::Number(x.log(y))
791        }
792        BuiltinFunction::Ln => {
793            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
794            Value::Number(x.ln())
795        }
796        BuiltinFunction::Pow => {
797            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
798            let y: f64 = eval_expression(&arguments[1], local_context).try_into().unwrap();
799            Value::Number(x.powf(y))
800        }
801        BuiltinFunction::Exp => {
802            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
803            Value::Number(x.exp())
804        }
805        BuiltinFunction::ToFixed => {
806            let n: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
807            let digits: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
808            let digits: usize = digits.max(0) as usize;
809            Value::String(i_slint_core::string::shared_string_from_number_fixed(n, digits))
810        }
811        BuiltinFunction::ToPrecision => {
812            let n: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
813            let precision: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
814            let precision: usize = precision.max(0) as usize;
815            Value::String(i_slint_core::string::shared_string_from_number_precision(n, precision))
816        }
817        BuiltinFunction::SetFocusItem => {
818            if arguments.len() != 1 {
819                panic!("internal error: incorrect argument count to SetFocusItem")
820            }
821            let component = local_context.component_instance;
822            if let Expression::ElementReference(focus_item) = &arguments[0] {
823                generativity::make_guard!(guard);
824
825                let focus_item = focus_item.upgrade().unwrap();
826                let enclosing_component =
827                    enclosing_component_for_element(&focus_item, component, guard);
828                let description = enclosing_component.description;
829
830                let item_info = &description.items[focus_item.borrow().id.as_str()];
831
832                let focus_item_comp =
833                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
834
835                component.access_window(|window| {
836                    window.set_focus_item(
837                        &corelib::items::ItemRc::new(
838                            vtable::VRc::into_dyn(focus_item_comp),
839                            item_info.item_index(),
840                        ),
841                        true,
842                        FocusReason::Programmatic,
843                    )
844                });
845                Value::Void
846            } else {
847                panic!("internal error: argument to SetFocusItem must be an element")
848            }
849        }
850        BuiltinFunction::ClearFocusItem => {
851            if arguments.len() != 1 {
852                panic!("internal error: incorrect argument count to SetFocusItem")
853            }
854            let component = local_context.component_instance;
855            if let Expression::ElementReference(focus_item) = &arguments[0] {
856                generativity::make_guard!(guard);
857
858                let focus_item = focus_item.upgrade().unwrap();
859                let enclosing_component =
860                    enclosing_component_for_element(&focus_item, component, guard);
861                let description = enclosing_component.description;
862
863                let item_info = &description.items[focus_item.borrow().id.as_str()];
864
865                let focus_item_comp =
866                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
867
868                component.access_window(|window| {
869                    window.set_focus_item(
870                        &corelib::items::ItemRc::new(
871                            vtable::VRc::into_dyn(focus_item_comp),
872                            item_info.item_index(),
873                        ),
874                        false,
875                        FocusReason::Programmatic,
876                    )
877                });
878                Value::Void
879            } else {
880                panic!("internal error: argument to ClearFocusItem must be an element")
881            }
882        }
883        BuiltinFunction::ShowPopupWindow => {
884            if arguments.len() != 1 {
885                panic!("internal error: incorrect argument count to ShowPopupWindow")
886            }
887            let component = local_context.component_instance;
888            if let Expression::ElementReference(popup_window) = &arguments[0] {
889                let popup_window = popup_window.upgrade().unwrap();
890                let pop_comp = popup_window.borrow().enclosing_component.upgrade().unwrap();
891                let parent_component = {
892                    let parent_elem = pop_comp.parent_element().unwrap();
893                    parent_elem.borrow().enclosing_component.upgrade().unwrap()
894                };
895                let popup_list = parent_component.popup_windows.borrow();
896                let popup =
897                    popup_list.iter().find(|p| Rc::ptr_eq(&p.component, &pop_comp)).unwrap();
898
899                generativity::make_guard!(guard);
900                let enclosing_component =
901                    enclosing_component_for_element(&popup.parent_element, component, guard);
902                let parent_item_info = &enclosing_component.description.items
903                    [popup.parent_element.borrow().id.as_str()];
904                let parent_item_comp =
905                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
906                let parent_item = corelib::items::ItemRc::new(
907                    vtable::VRc::into_dyn(parent_item_comp),
908                    parent_item_info.item_index(),
909                );
910
911                let close_policy = Value::EnumerationValue(
912                    popup.close_policy.enumeration.name.to_string(),
913                    popup.close_policy.to_string(),
914                )
915                .try_into()
916                .expect("Invalid internal enumeration representation for close policy");
917                let popup_x = popup.x.clone();
918                let popup_y = popup.y.clone();
919
920                crate::dynamic_item_tree::show_popup(
921                    popup_window,
922                    enclosing_component,
923                    popup,
924                    move |instance_ref| {
925                        let comp = ComponentInstance::InstanceRef(instance_ref);
926                        let x = load_property_helper(&comp, &popup_x.element(), popup_x.name())
927                            .unwrap();
928                        let y = load_property_helper(&comp, &popup_y.element(), popup_y.name())
929                            .unwrap();
930                        corelib::api::LogicalPosition::new(
931                            x.try_into().unwrap(),
932                            y.try_into().unwrap(),
933                        )
934                    },
935                    close_policy,
936                    (*enclosing_component.self_weak().get().unwrap()).clone(),
937                    component.window_adapter(),
938                    &parent_item,
939                );
940                Value::Void
941            } else {
942                panic!("internal error: argument to ShowPopupWindow must be an element")
943            }
944        }
945        BuiltinFunction::ClosePopupWindow => {
946            let component = local_context.component_instance;
947            if let Expression::ElementReference(popup_window) = &arguments[0] {
948                let popup_window = popup_window.upgrade().unwrap();
949                let pop_comp = popup_window.borrow().enclosing_component.upgrade().unwrap();
950                let parent_component = {
951                    let parent_elem = pop_comp.parent_element().unwrap();
952                    parent_elem.borrow().enclosing_component.upgrade().unwrap()
953                };
954                let popup_list = parent_component.popup_windows.borrow();
955                let popup =
956                    popup_list.iter().find(|p| Rc::ptr_eq(&p.component, &pop_comp)).unwrap();
957
958                generativity::make_guard!(guard);
959                let enclosing_component =
960                    enclosing_component_for_element(&popup.parent_element, component, guard);
961                crate::dynamic_item_tree::close_popup(
962                    popup_window,
963                    enclosing_component,
964                    enclosing_component.window_adapter(),
965                );
966
967                Value::Void
968            } else {
969                panic!("internal error: argument to ClosePopupWindow must be an element")
970            }
971        }
972        BuiltinFunction::ShowPopupMenu | BuiltinFunction::ShowPopupMenuInternal => {
973            let [Expression::ElementReference(element), entries, position] = arguments else {
974                panic!("internal error: incorrect argument count to ShowPopupMenu")
975            };
976            let position = eval_expression(position, local_context)
977                .try_into()
978                .expect("internal error: popup menu position argument should be a point");
979
980            let component = local_context.component_instance;
981            let elem = element.upgrade().unwrap();
982            generativity::make_guard!(guard);
983            let enclosing_component = enclosing_component_for_element(&elem, component, guard);
984            let description = enclosing_component.description;
985            let item_info = &description.items[elem.borrow().id.as_str()];
986            let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
987            let item_tree = vtable::VRc::into_dyn(item_comp);
988            let item_rc = corelib::items::ItemRc::new(item_tree.clone(), item_info.item_index());
989
990            generativity::make_guard!(guard);
991            let compiled = enclosing_component.description.popup_menu_description.unerase(guard);
992            let extra_data = enclosing_component
993                .description
994                .extra_data_offset
995                .apply(enclosing_component.as_ref());
996            let inst = crate::dynamic_item_tree::instantiate(
997                compiled.clone(),
998                Some((*enclosing_component.self_weak().get().unwrap()).clone()),
999                None,
1000                Some(&crate::dynamic_item_tree::WindowOptions::UseExistingWindow(
1001                    component.window_adapter(),
1002                )),
1003                extra_data.globals.get().unwrap().clone(),
1004            );
1005
1006            generativity::make_guard!(guard);
1007            let inst_ref = inst.unerase(guard);
1008            if let Expression::ElementReference(e) = entries {
1009                let menu_item_tree =
1010                    e.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1011                let menu_item_tree = crate::dynamic_item_tree::make_menu_item_tree(
1012                    &menu_item_tree,
1013                    &enclosing_component,
1014                    None,
1015                );
1016
1017                if component.access_window(|window| {
1018                    window.show_native_popup_menu(
1019                        vtable::VRc::into_dyn(menu_item_tree.clone()),
1020                        position,
1021                        &item_rc,
1022                    )
1023                }) {
1024                    return Value::Void;
1025                }
1026
1027                let (entries, sub_menu, activated) = menu_item_tree_properties(menu_item_tree);
1028
1029                compiled.set_binding(inst_ref.borrow(), "entries", entries).unwrap();
1030                compiled.set_callback_handler(inst_ref.borrow(), "sub-menu", sub_menu).unwrap();
1031                compiled.set_callback_handler(inst_ref.borrow(), "activated", activated).unwrap();
1032            } else {
1033                let entries = eval_expression(entries, local_context);
1034                compiled.set_property(inst_ref.borrow(), "entries", entries).unwrap();
1035                let item_weak = item_rc.downgrade();
1036                compiled
1037                    .set_callback_handler(
1038                        inst_ref.borrow(),
1039                        "sub-menu",
1040                        Box::new(move |args: &[Value]| -> Value {
1041                            item_weak
1042                                .upgrade()
1043                                .unwrap()
1044                                .downcast::<corelib::items::ContextMenu>()
1045                                .unwrap()
1046                                .sub_menu
1047                                .call(&(args[0].clone().try_into().unwrap(),))
1048                                .into()
1049                        }),
1050                    )
1051                    .unwrap();
1052                let item_weak = item_rc.downgrade();
1053                compiled
1054                    .set_callback_handler(
1055                        inst_ref.borrow(),
1056                        "activated",
1057                        Box::new(move |args: &[Value]| -> Value {
1058                            item_weak
1059                                .upgrade()
1060                                .unwrap()
1061                                .downcast::<corelib::items::ContextMenu>()
1062                                .unwrap()
1063                                .activated
1064                                .call(&(args[0].clone().try_into().unwrap(),));
1065                            Value::Void
1066                        }),
1067                    )
1068                    .unwrap();
1069            }
1070            let item_weak = item_rc.downgrade();
1071            compiled
1072                .set_callback_handler(
1073                    inst_ref.borrow(),
1074                    "close-popup",
1075                    Box::new(move |_args: &[Value]| -> Value {
1076                        let Some(item_rc) = item_weak.upgrade() else { return Value::Void };
1077                        if let Some(id) = item_rc
1078                            .downcast::<corelib::items::ContextMenu>()
1079                            .unwrap()
1080                            .popup_id
1081                            .take()
1082                        {
1083                            WindowInner::from_pub(item_rc.window_adapter().unwrap().window())
1084                                .close_popup(id);
1085                        }
1086                        Value::Void
1087                    }),
1088                )
1089                .unwrap();
1090            component.access_window(|window| {
1091                let context_menu_elem = item_rc.downcast::<corelib::items::ContextMenu>().unwrap();
1092                if let Some(old_id) = context_menu_elem.popup_id.take() {
1093                    window.close_popup(old_id)
1094                }
1095                let id = window.show_popup(
1096                    &vtable::VRc::into_dyn(inst.clone()),
1097                    Box::new(move || position),
1098                    corelib::items::PopupClosePolicy::CloseOnClickOutside,
1099                    &item_rc,
1100                    false,
1101                    true,
1102                );
1103                context_menu_elem.popup_id.set(Some(id));
1104            });
1105            inst.run_setup_code();
1106            Value::Void
1107        }
1108        BuiltinFunction::SetSelectionOffsets => {
1109            if arguments.len() != 3 {
1110                panic!("internal error: incorrect argument count to select range function call")
1111            }
1112            let component = local_context.component_instance;
1113            if let Expression::ElementReference(element) = &arguments[0] {
1114                generativity::make_guard!(guard);
1115
1116                let elem = element.upgrade().unwrap();
1117                let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1118                let description = enclosing_component.description;
1119                let item_info = &description.items[elem.borrow().id.as_str()];
1120                let item_ref =
1121                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1122
1123                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1124                let item_rc = corelib::items::ItemRc::new(
1125                    vtable::VRc::into_dyn(item_comp),
1126                    item_info.item_index(),
1127                );
1128
1129                let window_adapter = component.window_adapter();
1130
1131                // TODO: Make this generic through RTTI
1132                if let Some(textinput) =
1133                    ItemRef::downcast_pin::<corelib::items::TextInput>(item_ref)
1134                {
1135                    let start: i32 =
1136                        eval_expression(&arguments[1], local_context).try_into().expect(
1137                            "internal error: second argument to set-selection-offsets must be an integer",
1138                        );
1139                    let end: i32 = eval_expression(&arguments[2], local_context).try_into().expect(
1140                        "internal error: third argument to set-selection-offsets must be an integer",
1141                    );
1142
1143                    textinput.set_selection_offsets(&window_adapter, &item_rc, start, end);
1144                } else {
1145                    panic!(
1146                        "internal error: member function called on element that doesn't have it: {}",
1147                        elem.borrow().original_name()
1148                    )
1149                }
1150
1151                Value::Void
1152            } else {
1153                panic!("internal error: first argument to set-selection-offsets must be an element")
1154            }
1155        }
1156        BuiltinFunction::ItemFontMetrics => {
1157            if arguments.len() != 1 {
1158                panic!(
1159                    "internal error: incorrect argument count to item font metrics function call"
1160                )
1161            }
1162            let component = local_context.component_instance;
1163            if let Expression::ElementReference(element) = &arguments[0] {
1164                generativity::make_guard!(guard);
1165
1166                let elem = element.upgrade().unwrap();
1167                let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1168                let description = enclosing_component.description;
1169                let item_info = &description.items[elem.borrow().id.as_str()];
1170                let item_ref =
1171                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1172                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1173                let item_rc = corelib::items::ItemRc::new(
1174                    vtable::VRc::into_dyn(item_comp),
1175                    item_info.item_index(),
1176                );
1177                let window_adapter = component.window_adapter();
1178                let metrics = i_slint_core::items::slint_text_item_fontmetrics(
1179                    &window_adapter,
1180                    item_ref,
1181                    &item_rc,
1182                );
1183                metrics.into()
1184            } else {
1185                panic!("internal error: argument to item-font-metrics must be an element")
1186            }
1187        }
1188        BuiltinFunction::StringIsFloat => {
1189            if arguments.len() != 1 {
1190                panic!("internal error: incorrect argument count to StringIsFloat")
1191            }
1192            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1193                Value::Bool(<f64 as core::str::FromStr>::from_str(s.as_str()).is_ok())
1194            } else {
1195                panic!("Argument not a string");
1196            }
1197        }
1198        BuiltinFunction::StringToFloat => {
1199            if arguments.len() != 1 {
1200                panic!("internal error: incorrect argument count to StringToFloat")
1201            }
1202            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1203                Value::Number(core::str::FromStr::from_str(s.as_str()).unwrap_or(0.))
1204            } else {
1205                panic!("Argument not a string");
1206            }
1207        }
1208        BuiltinFunction::StringIsEmpty => {
1209            if arguments.len() != 1 {
1210                panic!("internal error: incorrect argument count to StringIsEmpty")
1211            }
1212            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1213                Value::Bool(s.is_empty())
1214            } else {
1215                panic!("Argument not a string");
1216            }
1217        }
1218        BuiltinFunction::StringCharacterCount => {
1219            if arguments.len() != 1 {
1220                panic!("internal error: incorrect argument count to StringCharacterCount")
1221            }
1222            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1223                Value::Number(
1224                    unicode_segmentation::UnicodeSegmentation::graphemes(s.as_str(), true).count()
1225                        as f64,
1226                )
1227            } else {
1228                panic!("Argument not a string");
1229            }
1230        }
1231        BuiltinFunction::StringToLowercase => {
1232            if arguments.len() != 1 {
1233                panic!("internal error: incorrect argument count to StringToLowercase")
1234            }
1235            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1236                Value::String(s.to_lowercase().into())
1237            } else {
1238                panic!("Argument not a string");
1239            }
1240        }
1241        BuiltinFunction::StringToUppercase => {
1242            if arguments.len() != 1 {
1243                panic!("internal error: incorrect argument count to StringToUppercase")
1244            }
1245            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1246                Value::String(s.to_uppercase().into())
1247            } else {
1248                panic!("Argument not a string");
1249            }
1250        }
1251        BuiltinFunction::KeysToString => {
1252            if arguments.len() != 1 {
1253                panic!("internal error: incorrect argument count to KeysToString")
1254            }
1255            let Value::Keys(keys) = eval_expression(&arguments[0], local_context) else {
1256                panic!("Argument is not of type keys");
1257            };
1258            Value::String(ToSharedString::to_shared_string(&keys))
1259        }
1260        BuiltinFunction::ColorRgbaStruct => {
1261            if arguments.len() != 1 {
1262                panic!("internal error: incorrect argument count to ColorRGBAComponents")
1263            }
1264            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1265                let color = brush.color();
1266                let values = IntoIterator::into_iter([
1267                    ("red".to_string(), Value::Number(color.red().into())),
1268                    ("green".to_string(), Value::Number(color.green().into())),
1269                    ("blue".to_string(), Value::Number(color.blue().into())),
1270                    ("alpha".to_string(), Value::Number(color.alpha().into())),
1271                ])
1272                .collect();
1273                Value::Struct(values)
1274            } else {
1275                panic!("First argument not a color");
1276            }
1277        }
1278        BuiltinFunction::ColorHsvaStruct => {
1279            if arguments.len() != 1 {
1280                panic!("internal error: incorrect argument count to ColorHSVAComponents")
1281            }
1282            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1283                let color = brush.color().to_hsva();
1284                let values = IntoIterator::into_iter([
1285                    ("hue".to_string(), Value::Number(color.hue.into())),
1286                    ("saturation".to_string(), Value::Number(color.saturation.into())),
1287                    ("value".to_string(), Value::Number(color.value.into())),
1288                    ("alpha".to_string(), Value::Number(color.alpha.into())),
1289                ])
1290                .collect();
1291                Value::Struct(values)
1292            } else {
1293                panic!("First argument not a color");
1294            }
1295        }
1296        BuiltinFunction::ColorOklchStruct => {
1297            if arguments.len() != 1 {
1298                panic!("internal error: incorrect argument count to ColorOklchStruct")
1299            }
1300            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1301                let color = brush.color().to_oklch();
1302                let values = IntoIterator::into_iter([
1303                    ("lightness".to_string(), Value::Number(color.lightness.into())),
1304                    ("chroma".to_string(), Value::Number(color.chroma.into())),
1305                    ("hue".to_string(), Value::Number(color.hue.into())),
1306                    ("alpha".to_string(), Value::Number(color.alpha.into())),
1307                ])
1308                .collect();
1309                Value::Struct(values)
1310            } else {
1311                panic!("First argument not a color");
1312            }
1313        }
1314        BuiltinFunction::ColorBrighter => {
1315            if arguments.len() != 2 {
1316                panic!("internal error: incorrect argument count to ColorBrighter")
1317            }
1318            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1319                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1320                    brush.brighter(factor as _).into()
1321                } else {
1322                    panic!("Second argument not a number");
1323                }
1324            } else {
1325                panic!("First argument not a color");
1326            }
1327        }
1328        BuiltinFunction::ColorDarker => {
1329            if arguments.len() != 2 {
1330                panic!("internal error: incorrect argument count to ColorDarker")
1331            }
1332            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1333                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1334                    brush.darker(factor as _).into()
1335                } else {
1336                    panic!("Second argument not a number");
1337                }
1338            } else {
1339                panic!("First argument not a color");
1340            }
1341        }
1342        BuiltinFunction::ColorTransparentize => {
1343            if arguments.len() != 2 {
1344                panic!("internal error: incorrect argument count to ColorFaded")
1345            }
1346            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1347                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1348                    brush.transparentize(factor as _).into()
1349                } else {
1350                    panic!("Second argument not a number");
1351                }
1352            } else {
1353                panic!("First argument not a color");
1354            }
1355        }
1356        BuiltinFunction::ColorMix => {
1357            if arguments.len() != 3 {
1358                panic!("internal error: incorrect argument count to ColorMix")
1359            }
1360
1361            let arg0 = eval_expression(&arguments[0], local_context);
1362            let arg1 = eval_expression(&arguments[1], local_context);
1363            let arg2 = eval_expression(&arguments[2], local_context);
1364
1365            if !matches!(arg0, Value::Brush(Brush::SolidColor(_))) {
1366                panic!("First argument not a color");
1367            }
1368            if !matches!(arg1, Value::Brush(Brush::SolidColor(_))) {
1369                panic!("Second argument not a color");
1370            }
1371            if !matches!(arg2, Value::Number(_)) {
1372                panic!("Third argument not a number");
1373            }
1374
1375            let (
1376                Value::Brush(Brush::SolidColor(color_a)),
1377                Value::Brush(Brush::SolidColor(color_b)),
1378                Value::Number(factor),
1379            ) = (arg0, arg1, arg2)
1380            else {
1381                unreachable!()
1382            };
1383
1384            color_a.mix(&color_b, factor as _).into()
1385        }
1386        BuiltinFunction::ColorWithAlpha => {
1387            if arguments.len() != 2 {
1388                panic!("internal error: incorrect argument count to ColorWithAlpha")
1389            }
1390            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1391                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1392                    brush.with_alpha(factor as _).into()
1393                } else {
1394                    panic!("Second argument not a number");
1395                }
1396            } else {
1397                panic!("First argument not a color");
1398            }
1399        }
1400        BuiltinFunction::ImageSize => {
1401            if arguments.len() != 1 {
1402                panic!("internal error: incorrect argument count to ImageSize")
1403            }
1404            if let Value::Image(img) = eval_expression(&arguments[0], local_context) {
1405                let size = img.size();
1406                let values = IntoIterator::into_iter([
1407                    ("width".to_string(), Value::Number(size.width as f64)),
1408                    ("height".to_string(), Value::Number(size.height as f64)),
1409                ])
1410                .collect();
1411                Value::Struct(values)
1412            } else {
1413                panic!("First argument not an image");
1414            }
1415        }
1416        BuiltinFunction::ArrayLength => {
1417            if arguments.len() != 1 {
1418                panic!("internal error: incorrect argument count to ArrayLength")
1419            }
1420            match eval_expression(&arguments[0], local_context) {
1421                Value::Model(model) => {
1422                    model.model_tracker().track_row_count_changes();
1423                    Value::Number(model.row_count() as f64)
1424                }
1425                _ => {
1426                    panic!("First argument not an array: {:?}", arguments[0]);
1427                }
1428            }
1429        }
1430        BuiltinFunction::Rgb => {
1431            let r: i32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1432            let g: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1433            let b: i32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1434            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1435            let r: u8 = r.clamp(0, 255) as u8;
1436            let g: u8 = g.clamp(0, 255) as u8;
1437            let b: u8 = b.clamp(0, 255) as u8;
1438            let a: u8 = (255. * a).clamp(0., 255.) as u8;
1439            Value::Brush(Brush::SolidColor(Color::from_argb_u8(a, r, g, b)))
1440        }
1441        BuiltinFunction::Hsv => {
1442            let h: f32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1443            let s: f32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1444            let v: f32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1445            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1446            let a = (1. * a).clamp(0., 1.);
1447            Value::Brush(Brush::SolidColor(Color::from_hsva(h, s, v, a)))
1448        }
1449        BuiltinFunction::Oklch => {
1450            let l: f32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1451            let c: f32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1452            let h: f32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1453            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1454            let l = l.clamp(0., 1.);
1455            let c = c.max(0.);
1456            let a = a.clamp(0., 1.);
1457            Value::Brush(Brush::SolidColor(Color::from_oklch(l, c, h, a)))
1458        }
1459        BuiltinFunction::ColorScheme => {
1460            let root_weak =
1461                vtable::VWeak::into_dyn(local_context.component_instance.root_weak().clone());
1462            let root = root_weak.upgrade().unwrap();
1463            corelib::window::context_for_root(&root)
1464                .map_or(corelib::items::ColorScheme::Unknown, |ctx| ctx.color_scheme(Some(&root)))
1465                .into()
1466        }
1467        BuiltinFunction::AccentColor => {
1468            let root_weak =
1469                vtable::VWeak::into_dyn(local_context.component_instance.root_weak().clone());
1470            let root = root_weak.upgrade().unwrap();
1471            Value::Brush(corelib::Brush::SolidColor(corelib::window::accent_color(&root)))
1472        }
1473        BuiltinFunction::SupportsNativeMenuBar => local_context
1474            .component_instance
1475            .window_adapter()
1476            .internal(corelib::InternalToken)
1477            .is_some_and(|x| x.supports_native_menu_bar())
1478            .into(),
1479        BuiltinFunction::SetupMenuBar => {
1480            let component = local_context.component_instance;
1481            let [
1482                Expression::PropertyReference(entries_nr),
1483                Expression::PropertyReference(sub_menu_nr),
1484                Expression::PropertyReference(activated_nr),
1485                Expression::ElementReference(item_tree_root),
1486                Expression::BoolLiteral(no_native),
1487                rest @ ..,
1488            ] = arguments
1489            else {
1490                panic!("internal error: incorrect argument count to SetupMenuBar")
1491            };
1492
1493            let menu_item_tree =
1494                item_tree_root.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1495            let menu_item_tree = crate::dynamic_item_tree::make_menu_item_tree(
1496                &menu_item_tree,
1497                &component,
1498                rest.first(),
1499            );
1500
1501            let window_adapter = component.window_adapter();
1502            let window_inner = WindowInner::from_pub(window_adapter.window());
1503            let menubar = vtable::VRc::into_dyn(vtable::VRc::clone(&menu_item_tree));
1504            window_inner.setup_menubar_shortcuts(vtable::VRc::clone(&menubar));
1505
1506            if !no_native && window_inner.supports_native_menu_bar() {
1507                window_inner.setup_menubar(menubar);
1508                return Value::Void;
1509            }
1510
1511            let (entries, sub_menu, activated) = menu_item_tree_properties(menu_item_tree);
1512
1513            assert_eq!(
1514                entries_nr.element().borrow().id,
1515                component.description.original.root_element.borrow().id,
1516                "entries need to be in the main element"
1517            );
1518            local_context
1519                .component_instance
1520                .description
1521                .set_binding(component.borrow(), entries_nr.name(), entries)
1522                .unwrap();
1523            let i = &ComponentInstance::InstanceRef(local_context.component_instance);
1524            set_callback_handler(i, &sub_menu_nr.element(), sub_menu_nr.name(), sub_menu).unwrap();
1525            set_callback_handler(i, &activated_nr.element(), activated_nr.name(), activated)
1526                .unwrap();
1527
1528            Value::Void
1529        }
1530        BuiltinFunction::SetupSystemTrayIcon => {
1531            let [
1532                Expression::ElementReference(system_tray_elem),
1533                Expression::ElementReference(item_tree_root),
1534                rest @ ..,
1535            ] = arguments
1536            else {
1537                panic!("internal error: incorrect argument count to SetupSystemTrayIcon")
1538            };
1539
1540            let component = local_context.component_instance;
1541            let elem = system_tray_elem.upgrade().unwrap();
1542            generativity::make_guard!(guard);
1543            let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1544            let description = enclosing_component.description;
1545            let item_info = &description.items[elem.borrow().id.as_str()];
1546            let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1547            let item_tree = vtable::VRc::into_dyn(item_comp);
1548            let item_rc = corelib::items::ItemRc::new(item_tree.clone(), item_info.item_index());
1549
1550            let menu_item_tree_component =
1551                item_tree_root.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1552            let menu_vrc = crate::dynamic_item_tree::make_menu_item_tree(
1553                &menu_item_tree_component,
1554                &enclosing_component,
1555                rest.first(),
1556            );
1557
1558            let system_tray =
1559                item_rc.downcast::<corelib::items::SystemTrayIcon>().expect("SystemTrayIcon item");
1560            system_tray.as_pin_ref().set_menu(&item_rc, vtable::VRc::into_dyn(menu_vrc));
1561
1562            Value::Void
1563        }
1564        BuiltinFunction::MonthDayCount => {
1565            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1566            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1567            Value::Number(i_slint_core::date_time::month_day_count(m, y).unwrap_or(0) as f64)
1568        }
1569        BuiltinFunction::MonthOffset => {
1570            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1571            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1572
1573            Value::Number(i_slint_core::date_time::month_offset(m, y) as f64)
1574        }
1575        BuiltinFunction::FormatDate => {
1576            let f: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1577            let d: u32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1578            let m: u32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1579            let y: i32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1580
1581            Value::String(i_slint_core::date_time::format_date(&f, d, m, y))
1582        }
1583        BuiltinFunction::DateNow => Value::Model(ModelRc::new(VecModel::from(
1584            i_slint_core::date_time::date_now()
1585                .into_iter()
1586                .map(|x| Value::Number(x as f64))
1587                .collect::<Vec<_>>(),
1588        ))),
1589        BuiltinFunction::ValidDate => {
1590            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1591            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1592            Value::Bool(i_slint_core::date_time::parse_date(d.as_str(), f.as_str()).is_some())
1593        }
1594        BuiltinFunction::ParseDate => {
1595            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1596            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1597
1598            Value::Model(ModelRc::new(
1599                i_slint_core::date_time::parse_date(d.as_str(), f.as_str())
1600                    .map(|x| {
1601                        VecModel::from(
1602                            x.into_iter().map(|x| Value::Number(x as f64)).collect::<Vec<_>>(),
1603                        )
1604                    })
1605                    .unwrap_or_default(),
1606            ))
1607        }
1608        BuiltinFunction::TextInputFocused => Value::Bool(
1609            local_context.component_instance.access_window(|window| window.text_input_focused())
1610                as _,
1611        ),
1612        BuiltinFunction::SetTextInputFocused => {
1613            local_context.component_instance.access_window(|window| {
1614                window.set_text_input_focused(
1615                    eval_expression(&arguments[0], local_context).try_into().unwrap(),
1616                )
1617            });
1618            Value::Void
1619        }
1620        BuiltinFunction::ImplicitLayoutInfo(orient) => {
1621            let component = local_context.component_instance;
1622            if let [Expression::ElementReference(item), constraint_expr] = arguments {
1623                generativity::make_guard!(guard);
1624
1625                let constraint: f32 =
1626                    eval_expression(constraint_expr, local_context).try_into().unwrap_or(-1.);
1627
1628                let item = item.upgrade().unwrap();
1629                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1630                let description = enclosing_component.description;
1631                let item_info = &description.items[item.borrow().id.as_str()];
1632                let item_ref =
1633                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1634                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1635                let window_adapter = component.window_adapter();
1636                item_ref
1637                    .as_ref()
1638                    .layout_info(
1639                        crate::eval_layout::to_runtime(orient),
1640                        constraint,
1641                        &window_adapter,
1642                        &ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index()),
1643                    )
1644                    .into()
1645            } else {
1646                panic!("internal error: incorrect arguments to ImplicitLayoutInfo {arguments:?}");
1647            }
1648        }
1649        BuiltinFunction::ItemAbsolutePosition => {
1650            if arguments.len() != 1 {
1651                panic!("internal error: incorrect argument count to ItemAbsolutePosition")
1652            }
1653
1654            let component = local_context.component_instance;
1655
1656            if let Expression::ElementReference(item) = &arguments[0] {
1657                generativity::make_guard!(guard);
1658
1659                let item = item.upgrade().unwrap();
1660                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1661                let description = enclosing_component.description;
1662
1663                let item_info = &description.items[item.borrow().id.as_str()];
1664
1665                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1666
1667                let item_rc = corelib::items::ItemRc::new(
1668                    vtable::VRc::into_dyn(item_comp),
1669                    item_info.item_index(),
1670                );
1671
1672                item_rc.map_to_window(Default::default()).to_untyped().into()
1673            } else {
1674                panic!("internal error: argument to SetFocusItem must be an element")
1675            }
1676        }
1677        BuiltinFunction::RegisterCustomFontByPath => {
1678            if arguments.len() != 1 {
1679                panic!("internal error: incorrect argument count to RegisterCustomFontByPath")
1680            }
1681            let component = local_context.component_instance;
1682            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1683                if let Some(err) = component
1684                    .window_adapter()
1685                    .renderer()
1686                    .register_font_from_path(&std::path::PathBuf::from(s.as_str()))
1687                    .err()
1688                {
1689                    corelib::debug_log!("Error loading custom font {}: {}", s.as_str(), err);
1690                }
1691                Value::Void
1692            } else {
1693                panic!("Argument not a string");
1694            }
1695        }
1696        BuiltinFunction::RegisterCustomFontByMemory | BuiltinFunction::RegisterBitmapFont => {
1697            unimplemented!()
1698        }
1699        BuiltinFunction::Translate => {
1700            let original: SharedString =
1701                eval_expression(&arguments[0], local_context).try_into().unwrap();
1702            let context: SharedString =
1703                eval_expression(&arguments[1], local_context).try_into().unwrap();
1704            let domain: SharedString =
1705                eval_expression(&arguments[2], local_context).try_into().unwrap();
1706            let args = eval_expression(&arguments[3], local_context);
1707            let Value::Model(args) = args else { panic!("Args to translate not a model {args:?}") };
1708            struct StringModelWrapper(ModelRc<Value>);
1709            impl corelib::translations::FormatArgs for StringModelWrapper {
1710                type Output<'a> = SharedString;
1711                fn from_index(&self, index: usize) -> Option<SharedString> {
1712                    self.0.row_data(index).map(|x| x.try_into().unwrap())
1713                }
1714            }
1715            Value::String(corelib::translations::translate(
1716                &original,
1717                &context,
1718                &domain,
1719                &StringModelWrapper(args),
1720                eval_expression(&arguments[4], local_context).try_into().unwrap(),
1721                &SharedString::try_from(eval_expression(&arguments[5], local_context)).unwrap(),
1722            ))
1723        }
1724        BuiltinFunction::Use24HourFormat => Value::Bool(corelib::date_time::use_24_hour_format()),
1725        BuiltinFunction::UpdateTimers => {
1726            crate::dynamic_item_tree::update_timers(local_context.component_instance);
1727            Value::Void
1728        }
1729        BuiltinFunction::DetectOperatingSystem => i_slint_core::detect_operating_system().into(),
1730        // start and stop are unreachable because they are lowered to simple assignment of running
1731        BuiltinFunction::StartTimer => unreachable!(),
1732        BuiltinFunction::StopTimer => unreachable!(),
1733        BuiltinFunction::RestartTimer => {
1734            if let [Expression::ElementReference(timer_element)] = arguments {
1735                crate::dynamic_item_tree::restart_timer(
1736                    timer_element.clone(),
1737                    local_context.component_instance,
1738                );
1739
1740                Value::Void
1741            } else {
1742                panic!("internal error: argument to RestartTimer must be an element")
1743            }
1744        }
1745        BuiltinFunction::OpenUrl => {
1746            let url: SharedString =
1747                eval_expression(&arguments[0], local_context).try_into().unwrap();
1748            let window_adapter = local_context.component_instance.window_adapter();
1749            Value::Bool(corelib::open_url(&url, window_adapter.window()).is_ok())
1750        }
1751        BuiltinFunction::BringAllToFront => {
1752            corelib::bring_all_to_front();
1753            Value::Void
1754        }
1755        BuiltinFunction::ParseMarkdown => {
1756            let format_string: SharedString =
1757                eval_expression(&arguments[0], local_context).try_into().unwrap();
1758            let args: ModelRc<corelib::styled_text::StyledText> =
1759                eval_expression(&arguments[1], local_context).try_into().unwrap();
1760            Value::StyledText(corelib::styled_text::parse_markdown(
1761                &format_string,
1762                &args.iter().collect::<Vec<_>>(),
1763            ))
1764        }
1765        BuiltinFunction::StringToStyledText => {
1766            let string: SharedString =
1767                eval_expression(&arguments[0], local_context).try_into().unwrap();
1768            Value::StyledText(corelib::styled_text::string_to_styled_text(string.to_string()))
1769        }
1770        BuiltinFunction::ColorToStyledText => {
1771            let color: corelib::Color =
1772                eval_expression(&arguments[0], local_context).try_into().unwrap();
1773            Value::StyledText(corelib::styled_text::color_to_styled_text(color))
1774        }
1775    }
1776}
1777
1778fn call_item_member_function(nr: &NamedReference, local_context: &mut EvalLocalContext) -> Value {
1779    let component = local_context.component_instance;
1780    let elem = nr.element();
1781    let name = nr.name().as_str();
1782    generativity::make_guard!(guard);
1783    let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1784    let description = enclosing_component.description;
1785    let item_info = &description.items[elem.borrow().id.as_str()];
1786    let item_ref = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1787
1788    let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1789    let item_rc =
1790        corelib::items::ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index());
1791
1792    let window_adapter = component.window_adapter();
1793
1794    // TODO: Make this generic through RTTI
1795    if let Some(textinput) = ItemRef::downcast_pin::<corelib::items::TextInput>(item_ref) {
1796        match name {
1797            "select-all" => textinput.select_all(&window_adapter, &item_rc),
1798            "clear-selection" => textinput.clear_selection(&window_adapter, &item_rc),
1799            "cut" => textinput.cut(&window_adapter, &item_rc),
1800            "copy" => textinput.copy(&window_adapter, &item_rc),
1801            "paste" => textinput.paste(&window_adapter, &item_rc),
1802            _ => panic!("internal: Unknown member function {name} called on TextInput"),
1803        }
1804    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::SwipeGestureHandler>(item_ref) {
1805        match name {
1806            "cancel" => s.cancel(&window_adapter, &item_rc),
1807            _ => panic!("internal: Unknown member function {name} called on SwipeGestureHandler"),
1808        }
1809    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::ContextMenu>(item_ref) {
1810        match name {
1811            "close" => s.close(&window_adapter, &item_rc),
1812            "is-open" => return Value::Bool(s.is_open(&window_adapter, &item_rc)),
1813            _ => {
1814                panic!("internal: Unknown member function {name} called on ContextMenu")
1815            }
1816        }
1817    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::WindowItem>(item_ref) {
1818        match name {
1819            "hide" => s.hide(&window_adapter, &item_rc),
1820            "close" => return Value::Bool(s.close(&window_adapter, &item_rc)),
1821            _ => {
1822                panic!("internal: Unknown member function {name} called on WindowItem")
1823            }
1824        }
1825    } else {
1826        panic!(
1827            "internal error: member function {name} called on element that doesn't have it: {}",
1828            elem.borrow().original_name()
1829        )
1830    }
1831
1832    Value::Void
1833}
1834
1835fn eval_assignment(lhs: &Expression, op: char, rhs: Value, local_context: &mut EvalLocalContext) {
1836    let eval = |lhs| match (lhs, &rhs, op) {
1837        (Value::String(ref mut a), Value::String(b), '+') => {
1838            a.push_str(b.as_str());
1839            Value::String(a.clone())
1840        }
1841        (Value::Number(a), Value::Number(b), '+') => Value::Number(a + b),
1842        (Value::Number(a), Value::Number(b), '-') => Value::Number(a - b),
1843        (Value::Number(a), Value::Number(b), '/') => Value::Number(a / b),
1844        (Value::Number(a), Value::Number(b), '*') => Value::Number(a * b),
1845        (lhs, rhs, op) => panic!("unsupported {lhs:?} {op} {rhs:?}"),
1846    };
1847    match lhs {
1848        Expression::PropertyReference(nr) => {
1849            let element = nr.element();
1850            generativity::make_guard!(guard);
1851            let enclosing_component = enclosing_component_instance_for_element(
1852                &element,
1853                &ComponentInstance::InstanceRef(local_context.component_instance),
1854                guard,
1855            );
1856
1857            match enclosing_component {
1858                ComponentInstance::InstanceRef(enclosing_component) => {
1859                    if op == '=' {
1860                        store_property(enclosing_component, &element, nr.name(), rhs).unwrap();
1861                        return;
1862                    }
1863
1864                    let component = element.borrow().enclosing_component.upgrade().unwrap();
1865                    if element.borrow().id == component.root_element.borrow().id
1866                        && let Some(x) =
1867                            enclosing_component.description.custom_properties.get(nr.name())
1868                    {
1869                        unsafe {
1870                            let p =
1871                                Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
1872                            x.prop.set(p, eval(x.prop.get(p).unwrap()), None).unwrap();
1873                        }
1874                        return;
1875                    }
1876                    let item_info =
1877                        &enclosing_component.description.items[element.borrow().id.as_str()];
1878                    let item =
1879                        unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1880                    let p = &item_info.rtti.properties[nr.name().as_str()];
1881                    p.set(item, eval(p.get(item)), None).unwrap();
1882                }
1883                ComponentInstance::GlobalComponent(global) => {
1884                    let val = if op == '=' {
1885                        rhs
1886                    } else {
1887                        eval(global.as_ref().get_property(nr.name()).unwrap())
1888                    };
1889                    global.as_ref().set_property(nr.name(), val).unwrap();
1890                }
1891            }
1892        }
1893        Expression::StructFieldAccess { base, name } => {
1894            if let Value::Struct(mut o) = eval_expression(base, local_context) {
1895                let mut r = o.get_field(name).unwrap().clone();
1896                r = if op == '=' { rhs } else { eval(std::mem::take(&mut r)) };
1897                o.set_field(name.to_string(), r);
1898                eval_assignment(base, '=', Value::Struct(o), local_context)
1899            }
1900        }
1901        Expression::RepeaterModelReference { element } => {
1902            let element = element.upgrade().unwrap();
1903            let component_instance = local_context.component_instance;
1904            generativity::make_guard!(g1);
1905            let enclosing_component =
1906                enclosing_component_for_element(&element, component_instance, g1);
1907            // we need a 'static Repeater component in order to call model_set_row_data, so get it.
1908            // Safety: This is the only 'static Id in scope.
1909            let static_guard =
1910                unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
1911            let repeater = crate::dynamic_item_tree::get_repeater_by_name(
1912                enclosing_component,
1913                element.borrow().id.as_str(),
1914                static_guard,
1915            );
1916            repeater.0.model_set_row_data(
1917                eval_expression(
1918                    &Expression::RepeaterIndexReference { element: Rc::downgrade(&element) },
1919                    local_context,
1920                )
1921                .try_into()
1922                .unwrap(),
1923                if op == '=' {
1924                    rhs
1925                } else {
1926                    eval(eval_expression(
1927                        &Expression::RepeaterModelReference { element: Rc::downgrade(&element) },
1928                        local_context,
1929                    ))
1930                },
1931            )
1932        }
1933        Expression::ArrayIndex { array, index } => {
1934            let array = eval_expression(array, local_context);
1935            let index = eval_expression(index, local_context);
1936            match (array, index) {
1937                (Value::Model(model), Value::Number(index)) => {
1938                    if index >= 0. && (index as usize) < model.row_count() {
1939                        let index = index as usize;
1940                        if op == '=' {
1941                            model.set_row_data(index, rhs);
1942                        } else {
1943                            model.set_row_data(
1944                                index,
1945                                eval(
1946                                    model
1947                                        .row_data(index)
1948                                        .unwrap_or_else(|| default_value_for_type(&lhs.ty())),
1949                                ),
1950                            );
1951                        }
1952                    }
1953                }
1954                _ => {
1955                    eprintln!("Attempting to write into an array that cannot be written");
1956                }
1957            }
1958        }
1959        _ => panic!("typechecking should make sure this was a PropertyReference"),
1960    }
1961}
1962
1963pub fn load_property(component: InstanceRef, element: &ElementRc, name: &str) -> Result<Value, ()> {
1964    load_property_helper(&ComponentInstance::InstanceRef(component), element, name)
1965}
1966
1967fn load_property_helper(
1968    component_instance: &ComponentInstance,
1969    element: &ElementRc,
1970    name: &str,
1971) -> Result<Value, ()> {
1972    generativity::make_guard!(guard);
1973    match enclosing_component_instance_for_element(element, component_instance, guard) {
1974        ComponentInstance::InstanceRef(enclosing_component) => {
1975            let element = element.borrow();
1976            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
1977            {
1978                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
1979                    return unsafe {
1980                        x.prop.get(Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset)))
1981                    };
1982                } else if enclosing_component.description.original.is_global() {
1983                    return Err(());
1984                }
1985            };
1986            let item_info = enclosing_component
1987                .description
1988                .items
1989                .get(element.id.as_str())
1990                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, name));
1991            core::mem::drop(element);
1992            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1993            Ok(item_info.rtti.properties.get(name).ok_or(())?.get(item))
1994        }
1995        ComponentInstance::GlobalComponent(glob) => glob.as_ref().get_property(name),
1996    }
1997}
1998
1999pub fn store_property(
2000    component_instance: InstanceRef,
2001    element: &ElementRc,
2002    name: &str,
2003    mut value: Value,
2004) -> Result<(), SetPropertyError> {
2005    generativity::make_guard!(guard);
2006    match enclosing_component_instance_for_element(
2007        element,
2008        &ComponentInstance::InstanceRef(component_instance),
2009        guard,
2010    ) {
2011        ComponentInstance::InstanceRef(enclosing_component) => {
2012            let maybe_animation = match element.borrow().bindings.get(name) {
2013                Some(b) => crate::dynamic_item_tree::animation_for_property(
2014                    enclosing_component,
2015                    &b.borrow().animation,
2016                ),
2017                None => {
2018                    crate::dynamic_item_tree::animation_for_property(enclosing_component, &None)
2019                }
2020            };
2021
2022            let component = element.borrow().enclosing_component.upgrade().unwrap();
2023            if element.borrow().id == component.root_element.borrow().id {
2024                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
2025                    if let Some(orig_decl) = enclosing_component
2026                        .description
2027                        .original
2028                        .root_element
2029                        .borrow()
2030                        .property_declarations
2031                        .get(name)
2032                    {
2033                        // Do an extra type checking because PropertyInfo::set won't do it for custom structures or array
2034                        if !check_value_type(&mut value, &orig_decl.property_type) {
2035                            return Err(SetPropertyError::WrongType);
2036                        }
2037                    }
2038                    unsafe {
2039                        let p = Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
2040                        return x
2041                            .prop
2042                            .set(p, value, maybe_animation.as_animation())
2043                            .map_err(|()| SetPropertyError::WrongType);
2044                    }
2045                } else if enclosing_component.description.original.is_global() {
2046                    return Err(SetPropertyError::NoSuchProperty);
2047                }
2048            };
2049            let item_info = &enclosing_component.description.items[element.borrow().id.as_str()];
2050            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2051            let p = &item_info.rtti.properties.get(name).ok_or(SetPropertyError::NoSuchProperty)?;
2052            p.set(item, value, maybe_animation.as_animation())
2053                .map_err(|()| SetPropertyError::WrongType)?;
2054        }
2055        ComponentInstance::GlobalComponent(glob) => {
2056            glob.as_ref().set_property(name, value)?;
2057        }
2058    }
2059    Ok(())
2060}
2061
2062/// Return true if the Value can be used for a property of the given type
2063fn check_value_type(value: &mut Value, ty: &Type) -> bool {
2064    match ty {
2065        Type::Void => true,
2066        Type::Invalid
2067        | Type::InferredProperty
2068        | Type::InferredCallback
2069        | Type::Callback { .. }
2070        | Type::Function { .. }
2071        | Type::ElementReference => panic!("not valid property type"),
2072        Type::Float32 => matches!(value, Value::Number(_)),
2073        Type::Int32 => matches!(value, Value::Number(_)),
2074        Type::String => matches!(value, Value::String(_)),
2075        Type::Color => matches!(value, Value::Brush(_)),
2076        Type::UnitProduct(_)
2077        | Type::Duration
2078        | Type::PhysicalLength
2079        | Type::LogicalLength
2080        | Type::Rem
2081        | Type::Angle
2082        | Type::Percent => matches!(value, Value::Number(_)),
2083        Type::Image => matches!(value, Value::Image(_)),
2084        Type::Bool => matches!(value, Value::Bool(_)),
2085        Type::Model => {
2086            matches!(value, Value::Model(_) | Value::Bool(_) | Value::Number(_))
2087        }
2088        Type::PathData => matches!(value, Value::PathData(_)),
2089        Type::Easing => matches!(value, Value::EasingCurve(_)),
2090        Type::Brush => matches!(value, Value::Brush(_)),
2091        Type::Array(inner) => {
2092            matches!(value, Value::Model(m) if m.iter().all(|mut v| check_value_type(&mut v, inner)))
2093        }
2094        Type::Struct(s) => {
2095            let Value::Struct(str) = value else { return false };
2096            if !str
2097                .0
2098                .iter_mut()
2099                .all(|(k, v)| s.fields.get(k).is_some_and(|ty| check_value_type(v, ty)))
2100            {
2101                return false;
2102            }
2103            for (k, v) in &s.fields {
2104                str.0.entry(k.clone()).or_insert_with(|| default_value_for_type(v));
2105            }
2106            true
2107        }
2108        Type::Enumeration(en) => {
2109            matches!(value, Value::EnumerationValue(name, _) if name == en.name.as_str())
2110        }
2111        Type::Keys => matches!(value, Value::Keys(_)),
2112        Type::LayoutCache => matches!(value, Value::LayoutCache(_)),
2113        Type::ArrayOfU16 => matches!(value, Value::ArrayOfU16(_)),
2114        Type::ComponentFactory => matches!(value, Value::ComponentFactory(_)),
2115        Type::StyledText => matches!(value, Value::StyledText(_)),
2116        Type::DataTransfer => matches!(value, Value::DataTransfer(_)),
2117    }
2118}
2119
2120pub(crate) fn invoke_callback(
2121    component_instance: &ComponentInstance,
2122    element: &ElementRc,
2123    callback_name: &SmolStr,
2124    args: &[Value],
2125) -> Option<Value> {
2126    generativity::make_guard!(guard);
2127    match enclosing_component_instance_for_element(element, component_instance, guard) {
2128        ComponentInstance::InstanceRef(enclosing_component) => {
2129            let description = enclosing_component.description;
2130            let element = element.borrow();
2131            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2132            {
2133                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2134                    if let Some(tracker_offset) = description.callback_trackers.get(callback_name) {
2135                        tracker_offset.apply_pin(enclosing_component.instance).get();
2136                    }
2137                    let callback = callback_offset.apply(&*enclosing_component.instance);
2138                    let res = callback.call(args);
2139                    return Some(if res != Value::Void {
2140                        res
2141                    } else if let Some(Type::Callback(callback)) = description
2142                        .original
2143                        .root_element
2144                        .borrow()
2145                        .property_declarations
2146                        .get(callback_name)
2147                        .map(|d| &d.property_type)
2148                    {
2149                        // If the callback was not set, the return value will be Value::Void, but we need
2150                        // to make sure that the value is actually of the right type as returned by the
2151                        // callback, otherwise we will get panics later
2152                        default_value_for_type(&callback.return_type)
2153                    } else {
2154                        res
2155                    });
2156                } else if enclosing_component.description.original.is_global() {
2157                    return None;
2158                }
2159            };
2160            let item_info = &description.items[element.id.as_str()];
2161            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2162            item_info
2163                .rtti
2164                .callbacks
2165                .get(callback_name.as_str())
2166                .map(|callback| callback.call(item, args))
2167        }
2168        ComponentInstance::GlobalComponent(global) => {
2169            Some(global.as_ref().invoke_callback(callback_name, args).unwrap())
2170        }
2171    }
2172}
2173
2174pub(crate) fn set_callback_handler(
2175    component_instance: &ComponentInstance,
2176    element: &ElementRc,
2177    callback_name: &str,
2178    handler: CallbackHandler,
2179) -> Result<(), ()> {
2180    generativity::make_guard!(guard);
2181    match enclosing_component_instance_for_element(element, component_instance, guard) {
2182        ComponentInstance::InstanceRef(enclosing_component) => {
2183            let description = enclosing_component.description;
2184            let element = element.borrow();
2185            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2186            {
2187                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2188                    let callback = callback_offset.apply(&*enclosing_component.instance);
2189                    callback.set_handler(handler);
2190                    if let Some(tracker_offset) = description.callback_trackers.get(callback_name) {
2191                        tracker_offset.apply_pin(enclosing_component.instance).mark_dirty();
2192                    }
2193                    return Ok(());
2194                } else if enclosing_component.description.original.is_global() {
2195                    return Err(());
2196                }
2197            };
2198            let item_info = &description.items[element.id.as_str()];
2199            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2200            if let Some(callback) = item_info.rtti.callbacks.get(callback_name) {
2201                callback.set_handler(item, handler);
2202                Ok(())
2203            } else {
2204                Err(())
2205            }
2206        }
2207        ComponentInstance::GlobalComponent(global) => {
2208            global.as_ref().set_callback_handler(callback_name, handler)
2209        }
2210    }
2211}
2212
2213/// Invoke the function.
2214///
2215/// Return None if the function don't exist
2216pub(crate) fn call_function(
2217    component_instance: &ComponentInstance,
2218    element: &ElementRc,
2219    function_name: &str,
2220    args: Vec<Value>,
2221) -> Option<Value> {
2222    generativity::make_guard!(guard);
2223    match enclosing_component_instance_for_element(element, component_instance, guard) {
2224        ComponentInstance::InstanceRef(c) => {
2225            let mut ctx = EvalLocalContext::from_function_arguments(c, args);
2226            eval_expression(
2227                &element.borrow().bindings.get(function_name)?.borrow().expression,
2228                &mut ctx,
2229            )
2230            .into()
2231        }
2232        ComponentInstance::GlobalComponent(g) => g.as_ref().eval_function(function_name, args).ok(),
2233    }
2234}
2235
2236/// Return the component instance which hold the given element.
2237/// Does not take in account the global component.
2238pub fn enclosing_component_for_element<'a, 'old_id, 'new_id>(
2239    element: &'a ElementRc,
2240    component: InstanceRef<'a, 'old_id>,
2241    _guard: generativity::Guard<'new_id>,
2242) -> InstanceRef<'a, 'new_id> {
2243    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2244    if Rc::ptr_eq(enclosing, &component.description.original) {
2245        // Safety: new_id is an unique id
2246        unsafe {
2247            std::mem::transmute::<InstanceRef<'a, 'old_id>, InstanceRef<'a, 'new_id>>(component)
2248        }
2249    } else {
2250        assert!(!enclosing.is_global());
2251        // Safety: this is the only place we use this 'static lifetime in this function and nothing is returned with it
2252        // For some reason we can't make a new guard here because the compiler thinks we are returning that
2253        // (it assumes that the 'id must outlive 'a , which is not true)
2254        let static_guard = unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
2255
2256        let parent_instance = component
2257            .parent_instance(static_guard)
2258            .expect("accessing deleted parent (issue #6426)");
2259        enclosing_component_for_element(element, parent_instance, _guard)
2260    }
2261}
2262
2263/// Return the component instance which hold the given element.
2264/// The difference with enclosing_component_for_element is that it takes the GlobalComponent into account.
2265pub(crate) fn enclosing_component_instance_for_element<'a, 'new_id>(
2266    element: &'a ElementRc,
2267    component_instance: &ComponentInstance<'a, '_>,
2268    guard: generativity::Guard<'new_id>,
2269) -> ComponentInstance<'a, 'new_id> {
2270    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2271    match component_instance {
2272        ComponentInstance::InstanceRef(component) => {
2273            if enclosing.is_global() && !Rc::ptr_eq(enclosing, &component.description.original) {
2274                ComponentInstance::GlobalComponent(
2275                    component
2276                        .description
2277                        .extra_data_offset
2278                        .apply(component.instance.get_ref())
2279                        .globals
2280                        .get()
2281                        .unwrap()
2282                        .get(enclosing.root_element.borrow().id.as_str())
2283                        .unwrap(),
2284                )
2285            } else {
2286                ComponentInstance::InstanceRef(enclosing_component_for_element(
2287                    element, *component, guard,
2288                ))
2289            }
2290        }
2291        ComponentInstance::GlobalComponent(global) => {
2292            //assert!(Rc::ptr_eq(enclosing, &global.component));
2293            ComponentInstance::GlobalComponent(global.clone())
2294        }
2295    }
2296}
2297
2298pub fn new_struct_with_bindings<ElementType: 'static + Default + corelib::rtti::BuiltinItem>(
2299    bindings: &i_slint_compiler::object_tree::BindingsMap,
2300    local_context: &mut EvalLocalContext,
2301) -> ElementType {
2302    let mut element = ElementType::default();
2303    for (prop, info) in ElementType::fields::<Value>().into_iter() {
2304        if let Some(binding) = &bindings.get(prop) {
2305            let value = eval_expression(&binding.borrow(), local_context);
2306            info.set_field(&mut element, value).unwrap();
2307        }
2308    }
2309    element
2310}
2311
2312fn convert_from_lyon_path<'a>(
2313    events_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2314    points_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2315    local_context: &mut EvalLocalContext,
2316) -> PathData {
2317    let events = events_it
2318        .into_iter()
2319        .map(|event_expr| eval_expression(event_expr, local_context).try_into().unwrap())
2320        .collect::<SharedVector<_>>();
2321
2322    let points = points_it
2323        .into_iter()
2324        .map(|point_expr| {
2325            let point_value = eval_expression(point_expr, local_context);
2326            let point_struct: Struct = point_value.try_into().unwrap();
2327            let mut point = i_slint_core::graphics::Point::default();
2328            let x: f64 = point_struct.get_field("x").unwrap().clone().try_into().unwrap();
2329            let y: f64 = point_struct.get_field("y").unwrap().clone().try_into().unwrap();
2330            point.x = x as _;
2331            point.y = y as _;
2332            point
2333        })
2334        .collect::<SharedVector<_>>();
2335
2336    PathData::Events(events, points)
2337}
2338
2339pub fn convert_path(path: &ExprPath, local_context: &mut EvalLocalContext) -> PathData {
2340    match path {
2341        ExprPath::Elements(elements) => PathData::Elements(
2342            elements
2343                .iter()
2344                .map(|element| convert_path_element(element, local_context))
2345                .collect::<SharedVector<PathElement>>(),
2346        ),
2347        ExprPath::Events(events, points) => {
2348            convert_from_lyon_path(events.iter(), points.iter(), local_context)
2349        }
2350        ExprPath::Commands(commands) => {
2351            if let Value::String(commands) = eval_expression(commands, local_context) {
2352                PathData::Commands(commands)
2353            } else {
2354                panic!("binding to path commands does not evaluate to string");
2355            }
2356        }
2357    }
2358}
2359
2360fn convert_path_element(
2361    expr_element: &ExprPathElement,
2362    local_context: &mut EvalLocalContext,
2363) -> PathElement {
2364    match expr_element.element_type.native_class.class_name.as_str() {
2365        "MoveTo" => {
2366            PathElement::MoveTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2367        }
2368        "LineTo" => {
2369            PathElement::LineTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2370        }
2371        "ArcTo" => {
2372            PathElement::ArcTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2373        }
2374        "CubicTo" => {
2375            PathElement::CubicTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2376        }
2377        "QuadraticTo" => PathElement::QuadraticTo(new_struct_with_bindings(
2378            &expr_element.bindings,
2379            local_context,
2380        )),
2381        "Close" => PathElement::Close,
2382        _ => panic!(
2383            "Cannot create unsupported path element {}",
2384            expr_element.element_type.native_class.class_name
2385        ),
2386    }
2387}
2388
2389/// Create a value suitable as the default value of a given type
2390pub fn default_value_for_type(ty: &Type) -> Value {
2391    match ty {
2392        Type::Float32 | Type::Int32 => Value::Number(0.),
2393        Type::String => Value::String(Default::default()),
2394        Type::Color | Type::Brush => Value::Brush(Default::default()),
2395        Type::Duration | Type::Angle | Type::PhysicalLength | Type::LogicalLength | Type::Rem => {
2396            Value::Number(0.)
2397        }
2398        Type::Image => Value::Image(Default::default()),
2399        Type::Bool => Value::Bool(false),
2400        Type::Callback { .. } => Value::Void,
2401        Type::Struct(s) => Value::Struct(
2402            s.fields
2403                .iter()
2404                .map(|(n, t)| (n.to_string(), default_value_for_type(t)))
2405                .collect::<Struct>(),
2406        ),
2407        Type::Array(_) | Type::Model => Value::Model(Default::default()),
2408        Type::Percent => Value::Number(0.),
2409        Type::Enumeration(e) => Value::EnumerationValue(
2410            e.name.to_string(),
2411            e.values.get(e.default_value).unwrap().to_string(),
2412        ),
2413        Type::Keys => Value::Keys(Default::default()),
2414        Type::DataTransfer => Value::DataTransfer(Default::default()),
2415        Type::Easing => Value::EasingCurve(Default::default()),
2416        Type::Void | Type::Invalid => Value::Void,
2417        Type::UnitProduct(_) => Value::Number(0.),
2418        Type::PathData => Value::PathData(Default::default()),
2419        Type::LayoutCache => Value::LayoutCache(Default::default()),
2420        Type::ArrayOfU16 => Value::ArrayOfU16(Default::default()),
2421        Type::ComponentFactory => Value::ComponentFactory(Default::default()),
2422        Type::InferredProperty
2423        | Type::InferredCallback
2424        | Type::ElementReference
2425        | Type::Function { .. } => {
2426            panic!("There can't be such property")
2427        }
2428        Type::StyledText => Value::StyledText(Default::default()),
2429    }
2430}
2431
2432fn menu_item_tree_properties(
2433    context_menu_item_tree: vtable::VRc<i_slint_core::menus::MenuVTable, MenuFromItemTree>,
2434) -> (Box<dyn Fn() -> Value>, CallbackHandler, CallbackHandler) {
2435    let context_menu_item_tree_ = context_menu_item_tree.clone();
2436    let entries = Box::new(move || {
2437        let mut entries = SharedVector::default();
2438        context_menu_item_tree_.sub_menu(None, &mut entries);
2439        Value::Model(ModelRc::new(VecModel::from(
2440            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2441        )))
2442    });
2443    let context_menu_item_tree_ = context_menu_item_tree.clone();
2444    let sub_menu = Box::new(move |args: &[Value]| -> Value {
2445        let mut entries = SharedVector::default();
2446        context_menu_item_tree_.sub_menu(Some(&args[0].clone().try_into().unwrap()), &mut entries);
2447        Value::Model(ModelRc::new(VecModel::from(
2448            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2449        )))
2450    });
2451    let activated = Box::new(move |args: &[Value]| -> Value {
2452        context_menu_item_tree.activate(&args[0].clone().try_into().unwrap());
2453        Value::Void
2454    });
2455    (entries, sub_menu, activated)
2456}