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