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