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