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                    corelib::window::PopupKind::Menu,
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::SetupSystemTrayIcon => {
1496            let [
1497                Expression::ElementReference(system_tray_elem),
1498                Expression::ElementReference(item_tree_root),
1499                rest @ ..,
1500            ] = arguments
1501            else {
1502                panic!("internal error: incorrect argument count to SetupSystemTrayIcon")
1503            };
1504
1505            let component = local_context.component_instance;
1506            let elem = system_tray_elem.upgrade().unwrap();
1507            generativity::make_guard!(guard);
1508            let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1509            let description = enclosing_component.description;
1510            let item_info = &description.items[elem.borrow().id.as_str()];
1511            let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1512            let item_tree = vtable::VRc::into_dyn(item_comp);
1513            let item_rc = corelib::items::ItemRc::new(item_tree.clone(), item_info.item_index());
1514
1515            let menu_item_tree_component =
1516                item_tree_root.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1517            let menu_vrc = crate::dynamic_item_tree::make_menu_item_tree(
1518                &menu_item_tree_component,
1519                &enclosing_component,
1520                rest.first(),
1521            );
1522
1523            let system_tray =
1524                item_rc.downcast::<corelib::items::SystemTrayIcon>().expect("SystemTrayIcon item");
1525            system_tray.as_pin_ref().set_menu(&item_rc, vtable::VRc::into_dyn(menu_vrc));
1526
1527            Value::Void
1528        }
1529        BuiltinFunction::MonthDayCount => {
1530            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1531            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1532            Value::Number(i_slint_core::date_time::month_day_count(m, y).unwrap_or(0) as f64)
1533        }
1534        BuiltinFunction::MonthOffset => {
1535            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1536            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1537
1538            Value::Number(i_slint_core::date_time::month_offset(m, y) as f64)
1539        }
1540        BuiltinFunction::FormatDate => {
1541            let f: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1542            let d: u32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1543            let m: u32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1544            let y: i32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1545
1546            Value::String(i_slint_core::date_time::format_date(&f, d, m, y))
1547        }
1548        BuiltinFunction::DateNow => Value::Model(ModelRc::new(VecModel::from(
1549            i_slint_core::date_time::date_now()
1550                .into_iter()
1551                .map(|x| Value::Number(x as f64))
1552                .collect::<Vec<_>>(),
1553        ))),
1554        BuiltinFunction::ValidDate => {
1555            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1556            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1557            Value::Bool(i_slint_core::date_time::parse_date(d.as_str(), f.as_str()).is_some())
1558        }
1559        BuiltinFunction::ParseDate => {
1560            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1561            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1562
1563            Value::Model(ModelRc::new(
1564                i_slint_core::date_time::parse_date(d.as_str(), f.as_str())
1565                    .map(|x| {
1566                        VecModel::from(
1567                            x.into_iter().map(|x| Value::Number(x as f64)).collect::<Vec<_>>(),
1568                        )
1569                    })
1570                    .unwrap_or_default(),
1571            ))
1572        }
1573        BuiltinFunction::TextInputFocused => Value::Bool(
1574            local_context.component_instance.access_window(|window| window.text_input_focused())
1575                as _,
1576        ),
1577        BuiltinFunction::SetTextInputFocused => {
1578            local_context.component_instance.access_window(|window| {
1579                window.set_text_input_focused(
1580                    eval_expression(&arguments[0], local_context).try_into().unwrap(),
1581                )
1582            });
1583            Value::Void
1584        }
1585        BuiltinFunction::ImplicitLayoutInfo(orient) => {
1586            let component = local_context.component_instance;
1587            if let [Expression::ElementReference(item), constraint_expr] = arguments {
1588                generativity::make_guard!(guard);
1589
1590                let constraint: f32 =
1591                    eval_expression(constraint_expr, local_context).try_into().unwrap_or(-1.);
1592
1593                let item = item.upgrade().unwrap();
1594                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1595                let description = enclosing_component.description;
1596                let item_info = &description.items[item.borrow().id.as_str()];
1597                let item_ref =
1598                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1599                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1600                let window_adapter = component.window_adapter();
1601                item_ref
1602                    .as_ref()
1603                    .layout_info(
1604                        crate::eval_layout::to_runtime(orient),
1605                        constraint,
1606                        &window_adapter,
1607                        &ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index()),
1608                    )
1609                    .into()
1610            } else {
1611                panic!("internal error: incorrect arguments to ImplicitLayoutInfo {arguments:?}");
1612            }
1613        }
1614        BuiltinFunction::ItemAbsolutePosition => {
1615            if arguments.len() != 1 {
1616                panic!("internal error: incorrect argument count to ItemAbsolutePosition")
1617            }
1618
1619            let component = local_context.component_instance;
1620
1621            if let Expression::ElementReference(item) = &arguments[0] {
1622                generativity::make_guard!(guard);
1623
1624                let item = item.upgrade().unwrap();
1625                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1626                let description = enclosing_component.description;
1627
1628                let item_info = &description.items[item.borrow().id.as_str()];
1629
1630                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1631
1632                let item_rc = corelib::items::ItemRc::new(
1633                    vtable::VRc::into_dyn(item_comp),
1634                    item_info.item_index(),
1635                );
1636
1637                item_rc.map_to_window(Default::default()).to_untyped().into()
1638            } else {
1639                panic!("internal error: argument to SetFocusItem must be an element")
1640            }
1641        }
1642        BuiltinFunction::RegisterCustomFontByPath => {
1643            if arguments.len() != 1 {
1644                panic!("internal error: incorrect argument count to RegisterCustomFontByPath")
1645            }
1646            let component = local_context.component_instance;
1647            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1648                if let Some(err) = component
1649                    .window_adapter()
1650                    .renderer()
1651                    .register_font_from_path(&std::path::PathBuf::from(s.as_str()))
1652                    .err()
1653                {
1654                    corelib::debug_log!("Error loading custom font {}: {}", s.as_str(), err);
1655                }
1656                Value::Void
1657            } else {
1658                panic!("Argument not a string");
1659            }
1660        }
1661        BuiltinFunction::RegisterCustomFontByMemory | BuiltinFunction::RegisterBitmapFont => {
1662            unimplemented!()
1663        }
1664        BuiltinFunction::Translate => {
1665            let original: SharedString =
1666                eval_expression(&arguments[0], local_context).try_into().unwrap();
1667            let context: SharedString =
1668                eval_expression(&arguments[1], local_context).try_into().unwrap();
1669            let domain: SharedString =
1670                eval_expression(&arguments[2], local_context).try_into().unwrap();
1671            let args = eval_expression(&arguments[3], local_context);
1672            let Value::Model(args) = args else { panic!("Args to translate not a model {args:?}") };
1673            struct StringModelWrapper(ModelRc<Value>);
1674            impl corelib::translations::FormatArgs for StringModelWrapper {
1675                type Output<'a> = SharedString;
1676                fn from_index(&self, index: usize) -> Option<SharedString> {
1677                    self.0.row_data(index).map(|x| x.try_into().unwrap())
1678                }
1679            }
1680            Value::String(corelib::translations::translate(
1681                &original,
1682                &context,
1683                &domain,
1684                &StringModelWrapper(args),
1685                eval_expression(&arguments[4], local_context).try_into().unwrap(),
1686                &SharedString::try_from(eval_expression(&arguments[5], local_context)).unwrap(),
1687            ))
1688        }
1689        BuiltinFunction::Use24HourFormat => Value::Bool(corelib::date_time::use_24_hour_format()),
1690        BuiltinFunction::UpdateTimers => {
1691            crate::dynamic_item_tree::update_timers(local_context.component_instance);
1692            Value::Void
1693        }
1694        BuiltinFunction::DetectOperatingSystem => i_slint_core::detect_operating_system().into(),
1695        // start and stop are unreachable because they are lowered to simple assignment of running
1696        BuiltinFunction::StartTimer => unreachable!(),
1697        BuiltinFunction::StopTimer => unreachable!(),
1698        BuiltinFunction::RestartTimer => {
1699            if let [Expression::ElementReference(timer_element)] = arguments {
1700                crate::dynamic_item_tree::restart_timer(
1701                    timer_element.clone(),
1702                    local_context.component_instance,
1703                );
1704
1705                Value::Void
1706            } else {
1707                panic!("internal error: argument to RestartTimer must be an element")
1708            }
1709        }
1710        BuiltinFunction::OpenUrl => {
1711            let url: SharedString =
1712                eval_expression(&arguments[0], local_context).try_into().unwrap();
1713            let window_adapter = local_context.component_instance.window_adapter();
1714            Value::Bool(corelib::open_url(&url, window_adapter.window()).is_ok())
1715        }
1716        BuiltinFunction::ParseMarkdown => {
1717            let format_string: SharedString =
1718                eval_expression(&arguments[0], local_context).try_into().unwrap();
1719            let args: ModelRc<corelib::styled_text::StyledText> =
1720                eval_expression(&arguments[1], local_context).try_into().unwrap();
1721            Value::StyledText(corelib::styled_text::parse_markdown(
1722                &format_string,
1723                &args.iter().collect::<Vec<_>>(),
1724            ))
1725        }
1726        BuiltinFunction::StringToStyledText => {
1727            let string: SharedString =
1728                eval_expression(&arguments[0], local_context).try_into().unwrap();
1729            Value::StyledText(corelib::styled_text::string_to_styled_text(string.to_string()))
1730        }
1731    }
1732}
1733
1734fn call_item_member_function(nr: &NamedReference, local_context: &mut EvalLocalContext) -> Value {
1735    let component = local_context.component_instance;
1736    let elem = nr.element();
1737    let name = nr.name().as_str();
1738    generativity::make_guard!(guard);
1739    let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1740    let description = enclosing_component.description;
1741    let item_info = &description.items[elem.borrow().id.as_str()];
1742    let item_ref = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1743
1744    let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1745    let item_rc =
1746        corelib::items::ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index());
1747
1748    let window_adapter = component.window_adapter();
1749
1750    // TODO: Make this generic through RTTI
1751    if let Some(textinput) = ItemRef::downcast_pin::<corelib::items::TextInput>(item_ref) {
1752        match name {
1753            "select-all" => textinput.select_all(&window_adapter, &item_rc),
1754            "clear-selection" => textinput.clear_selection(&window_adapter, &item_rc),
1755            "cut" => textinput.cut(&window_adapter, &item_rc),
1756            "copy" => textinput.copy(&window_adapter, &item_rc),
1757            "paste" => textinput.paste(&window_adapter, &item_rc),
1758            _ => panic!("internal: Unknown member function {name} called on TextInput"),
1759        }
1760    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::SwipeGestureHandler>(item_ref) {
1761        match name {
1762            "cancel" => s.cancel(&window_adapter, &item_rc),
1763            _ => panic!("internal: Unknown member function {name} called on SwipeGestureHandler"),
1764        }
1765    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::ContextMenu>(item_ref) {
1766        match name {
1767            "close" => s.close(&window_adapter, &item_rc),
1768            "is-open" => return Value::Bool(s.is_open(&window_adapter, &item_rc)),
1769            _ => {
1770                panic!("internal: Unknown member function {name} called on ContextMenu")
1771            }
1772        }
1773    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::WindowItem>(item_ref) {
1774        match name {
1775            "hide" => s.hide(&window_adapter),
1776            _ => {
1777                panic!("internal: Unknown member function {name} called on WindowItem")
1778            }
1779        }
1780    } else {
1781        panic!(
1782            "internal error: member function {name} called on element that doesn't have it: {}",
1783            elem.borrow().original_name()
1784        )
1785    }
1786
1787    Value::Void
1788}
1789
1790fn eval_assignment(lhs: &Expression, op: char, rhs: Value, local_context: &mut EvalLocalContext) {
1791    let eval = |lhs| match (lhs, &rhs, op) {
1792        (Value::String(ref mut a), Value::String(b), '+') => {
1793            a.push_str(b.as_str());
1794            Value::String(a.clone())
1795        }
1796        (Value::Number(a), Value::Number(b), '+') => Value::Number(a + b),
1797        (Value::Number(a), Value::Number(b), '-') => Value::Number(a - b),
1798        (Value::Number(a), Value::Number(b), '/') => Value::Number(a / b),
1799        (Value::Number(a), Value::Number(b), '*') => Value::Number(a * b),
1800        (lhs, rhs, op) => panic!("unsupported {lhs:?} {op} {rhs:?}"),
1801    };
1802    match lhs {
1803        Expression::PropertyReference(nr) => {
1804            let element = nr.element();
1805            generativity::make_guard!(guard);
1806            let enclosing_component = enclosing_component_instance_for_element(
1807                &element,
1808                &ComponentInstance::InstanceRef(local_context.component_instance),
1809                guard,
1810            );
1811
1812            match enclosing_component {
1813                ComponentInstance::InstanceRef(enclosing_component) => {
1814                    if op == '=' {
1815                        store_property(enclosing_component, &element, nr.name(), rhs).unwrap();
1816                        return;
1817                    }
1818
1819                    let component = element.borrow().enclosing_component.upgrade().unwrap();
1820                    if element.borrow().id == component.root_element.borrow().id
1821                        && let Some(x) =
1822                            enclosing_component.description.custom_properties.get(nr.name())
1823                    {
1824                        unsafe {
1825                            let p =
1826                                Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
1827                            x.prop.set(p, eval(x.prop.get(p).unwrap()), None).unwrap();
1828                        }
1829                        return;
1830                    }
1831                    let item_info =
1832                        &enclosing_component.description.items[element.borrow().id.as_str()];
1833                    let item =
1834                        unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1835                    let p = &item_info.rtti.properties[nr.name().as_str()];
1836                    p.set(item, eval(p.get(item)), None).unwrap();
1837                }
1838                ComponentInstance::GlobalComponent(global) => {
1839                    let val = if op == '=' {
1840                        rhs
1841                    } else {
1842                        eval(global.as_ref().get_property(nr.name()).unwrap())
1843                    };
1844                    global.as_ref().set_property(nr.name(), val).unwrap();
1845                }
1846            }
1847        }
1848        Expression::StructFieldAccess { base, name } => {
1849            if let Value::Struct(mut o) = eval_expression(base, local_context) {
1850                let mut r = o.get_field(name).unwrap().clone();
1851                r = if op == '=' { rhs } else { eval(std::mem::take(&mut r)) };
1852                o.set_field(name.to_string(), r);
1853                eval_assignment(base, '=', Value::Struct(o), local_context)
1854            }
1855        }
1856        Expression::RepeaterModelReference { element } => {
1857            let element = element.upgrade().unwrap();
1858            let component_instance = local_context.component_instance;
1859            generativity::make_guard!(g1);
1860            let enclosing_component =
1861                enclosing_component_for_element(&element, component_instance, g1);
1862            // we need a 'static Repeater component in order to call model_set_row_data, so get it.
1863            // Safety: This is the only 'static Id in scope.
1864            let static_guard =
1865                unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
1866            let repeater = crate::dynamic_item_tree::get_repeater_by_name(
1867                enclosing_component,
1868                element.borrow().id.as_str(),
1869                static_guard,
1870            );
1871            repeater.0.model_set_row_data(
1872                eval_expression(
1873                    &Expression::RepeaterIndexReference { element: Rc::downgrade(&element) },
1874                    local_context,
1875                )
1876                .try_into()
1877                .unwrap(),
1878                if op == '=' {
1879                    rhs
1880                } else {
1881                    eval(eval_expression(
1882                        &Expression::RepeaterModelReference { element: Rc::downgrade(&element) },
1883                        local_context,
1884                    ))
1885                },
1886            )
1887        }
1888        Expression::ArrayIndex { array, index } => {
1889            let array = eval_expression(array, local_context);
1890            let index = eval_expression(index, local_context);
1891            match (array, index) {
1892                (Value::Model(model), Value::Number(index)) => {
1893                    if index >= 0. && (index as usize) < model.row_count() {
1894                        let index = index as usize;
1895                        if op == '=' {
1896                            model.set_row_data(index, rhs);
1897                        } else {
1898                            model.set_row_data(
1899                                index,
1900                                eval(
1901                                    model
1902                                        .row_data(index)
1903                                        .unwrap_or_else(|| default_value_for_type(&lhs.ty())),
1904                                ),
1905                            );
1906                        }
1907                    }
1908                }
1909                _ => {
1910                    eprintln!("Attempting to write into an array that cannot be written");
1911                }
1912            }
1913        }
1914        _ => panic!("typechecking should make sure this was a PropertyReference"),
1915    }
1916}
1917
1918pub fn load_property(component: InstanceRef, element: &ElementRc, name: &str) -> Result<Value, ()> {
1919    load_property_helper(&ComponentInstance::InstanceRef(component), element, name)
1920}
1921
1922fn load_property_helper(
1923    component_instance: &ComponentInstance,
1924    element: &ElementRc,
1925    name: &str,
1926) -> Result<Value, ()> {
1927    generativity::make_guard!(guard);
1928    match enclosing_component_instance_for_element(element, component_instance, guard) {
1929        ComponentInstance::InstanceRef(enclosing_component) => {
1930            let element = element.borrow();
1931            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
1932            {
1933                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
1934                    return unsafe {
1935                        x.prop.get(Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset)))
1936                    };
1937                } else if enclosing_component.description.original.is_global() {
1938                    return Err(());
1939                }
1940            };
1941            let item_info = enclosing_component
1942                .description
1943                .items
1944                .get(element.id.as_str())
1945                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, name));
1946            core::mem::drop(element);
1947            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1948            Ok(item_info.rtti.properties.get(name).ok_or(())?.get(item))
1949        }
1950        ComponentInstance::GlobalComponent(glob) => glob.as_ref().get_property(name),
1951    }
1952}
1953
1954pub fn store_property(
1955    component_instance: InstanceRef,
1956    element: &ElementRc,
1957    name: &str,
1958    mut value: Value,
1959) -> Result<(), SetPropertyError> {
1960    generativity::make_guard!(guard);
1961    match enclosing_component_instance_for_element(
1962        element,
1963        &ComponentInstance::InstanceRef(component_instance),
1964        guard,
1965    ) {
1966        ComponentInstance::InstanceRef(enclosing_component) => {
1967            let maybe_animation = match element.borrow().bindings.get(name) {
1968                Some(b) => crate::dynamic_item_tree::animation_for_property(
1969                    enclosing_component,
1970                    &b.borrow().animation,
1971                ),
1972                None => {
1973                    crate::dynamic_item_tree::animation_for_property(enclosing_component, &None)
1974                }
1975            };
1976
1977            let component = element.borrow().enclosing_component.upgrade().unwrap();
1978            if element.borrow().id == component.root_element.borrow().id {
1979                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
1980                    if let Some(orig_decl) = enclosing_component
1981                        .description
1982                        .original
1983                        .root_element
1984                        .borrow()
1985                        .property_declarations
1986                        .get(name)
1987                    {
1988                        // Do an extra type checking because PropertyInfo::set won't do it for custom structures or array
1989                        if !check_value_type(&mut value, &orig_decl.property_type) {
1990                            return Err(SetPropertyError::WrongType);
1991                        }
1992                    }
1993                    unsafe {
1994                        let p = Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
1995                        return x
1996                            .prop
1997                            .set(p, value, maybe_animation.as_animation())
1998                            .map_err(|()| SetPropertyError::WrongType);
1999                    }
2000                } else if enclosing_component.description.original.is_global() {
2001                    return Err(SetPropertyError::NoSuchProperty);
2002                }
2003            };
2004            let item_info = &enclosing_component.description.items[element.borrow().id.as_str()];
2005            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2006            let p = &item_info.rtti.properties.get(name).ok_or(SetPropertyError::NoSuchProperty)?;
2007            p.set(item, value, maybe_animation.as_animation())
2008                .map_err(|()| SetPropertyError::WrongType)?;
2009        }
2010        ComponentInstance::GlobalComponent(glob) => {
2011            glob.as_ref().set_property(name, value)?;
2012        }
2013    }
2014    Ok(())
2015}
2016
2017/// Return true if the Value can be used for a property of the given type
2018fn check_value_type(value: &mut Value, ty: &Type) -> bool {
2019    match ty {
2020        Type::Void => true,
2021        Type::Invalid
2022        | Type::InferredProperty
2023        | Type::InferredCallback
2024        | Type::Callback { .. }
2025        | Type::Function { .. }
2026        | Type::ElementReference => panic!("not valid property type"),
2027        Type::Float32 => matches!(value, Value::Number(_)),
2028        Type::Int32 => matches!(value, Value::Number(_)),
2029        Type::String => matches!(value, Value::String(_)),
2030        Type::Color => matches!(value, Value::Brush(_)),
2031        Type::UnitProduct(_)
2032        | Type::Duration
2033        | Type::PhysicalLength
2034        | Type::LogicalLength
2035        | Type::Rem
2036        | Type::Angle
2037        | Type::Percent => matches!(value, Value::Number(_)),
2038        Type::Image => matches!(value, Value::Image(_)),
2039        Type::Bool => matches!(value, Value::Bool(_)),
2040        Type::Model => {
2041            matches!(value, Value::Model(_) | Value::Bool(_) | Value::Number(_))
2042        }
2043        Type::PathData => matches!(value, Value::PathData(_)),
2044        Type::Easing => matches!(value, Value::EasingCurve(_)),
2045        Type::Brush => matches!(value, Value::Brush(_)),
2046        Type::Array(inner) => {
2047            matches!(value, Value::Model(m) if m.iter().all(|mut v| check_value_type(&mut v, inner)))
2048        }
2049        Type::Struct(s) => {
2050            let Value::Struct(str) = value else { return false };
2051            if !str
2052                .0
2053                .iter_mut()
2054                .all(|(k, v)| s.fields.get(k).is_some_and(|ty| check_value_type(v, ty)))
2055            {
2056                return false;
2057            }
2058            for (k, v) in &s.fields {
2059                str.0.entry(k.clone()).or_insert_with(|| default_value_for_type(v));
2060            }
2061            true
2062        }
2063        Type::Enumeration(en) => {
2064            matches!(value, Value::EnumerationValue(name, _) if name == en.name.as_str())
2065        }
2066        Type::Keys => matches!(value, Value::Keys(_)),
2067        Type::LayoutCache => matches!(value, Value::LayoutCache(_)),
2068        Type::ArrayOfU16 => matches!(value, Value::ArrayOfU16(_)),
2069        Type::ComponentFactory => matches!(value, Value::ComponentFactory(_)),
2070        Type::StyledText => matches!(value, Value::StyledText(_)),
2071    }
2072}
2073
2074pub(crate) fn invoke_callback(
2075    component_instance: &ComponentInstance,
2076    element: &ElementRc,
2077    callback_name: &SmolStr,
2078    args: &[Value],
2079) -> Option<Value> {
2080    generativity::make_guard!(guard);
2081    match enclosing_component_instance_for_element(element, component_instance, guard) {
2082        ComponentInstance::InstanceRef(enclosing_component) => {
2083            let description = enclosing_component.description;
2084            let element = element.borrow();
2085            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2086            {
2087                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2088                    let callback = callback_offset.apply(&*enclosing_component.instance);
2089                    let res = callback.call(args);
2090                    return Some(if res != Value::Void {
2091                        res
2092                    } else if let Some(Type::Callback(callback)) = description
2093                        .original
2094                        .root_element
2095                        .borrow()
2096                        .property_declarations
2097                        .get(callback_name)
2098                        .map(|d| &d.property_type)
2099                    {
2100                        // If the callback was not set, the return value will be Value::Void, but we need
2101                        // to make sure that the value is actually of the right type as returned by the
2102                        // callback, otherwise we will get panics later
2103                        default_value_for_type(&callback.return_type)
2104                    } else {
2105                        res
2106                    });
2107                } else if enclosing_component.description.original.is_global() {
2108                    return None;
2109                }
2110            };
2111            let item_info = &description.items[element.id.as_str()];
2112            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2113            item_info
2114                .rtti
2115                .callbacks
2116                .get(callback_name.as_str())
2117                .map(|callback| callback.call(item, args))
2118        }
2119        ComponentInstance::GlobalComponent(global) => {
2120            Some(global.as_ref().invoke_callback(callback_name, args).unwrap())
2121        }
2122    }
2123}
2124
2125pub(crate) fn set_callback_handler(
2126    component_instance: &ComponentInstance,
2127    element: &ElementRc,
2128    callback_name: &str,
2129    handler: CallbackHandler,
2130) -> Result<(), ()> {
2131    generativity::make_guard!(guard);
2132    match enclosing_component_instance_for_element(element, component_instance, guard) {
2133        ComponentInstance::InstanceRef(enclosing_component) => {
2134            let description = enclosing_component.description;
2135            let element = element.borrow();
2136            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2137            {
2138                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2139                    let callback = callback_offset.apply(&*enclosing_component.instance);
2140                    callback.set_handler(handler);
2141                    return Ok(());
2142                } else if enclosing_component.description.original.is_global() {
2143                    return Err(());
2144                }
2145            };
2146            let item_info = &description.items[element.id.as_str()];
2147            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2148            if let Some(callback) = item_info.rtti.callbacks.get(callback_name) {
2149                callback.set_handler(item, handler);
2150                Ok(())
2151            } else {
2152                Err(())
2153            }
2154        }
2155        ComponentInstance::GlobalComponent(global) => {
2156            global.as_ref().set_callback_handler(callback_name, handler)
2157        }
2158    }
2159}
2160
2161/// Invoke the function.
2162///
2163/// Return None if the function don't exist
2164pub(crate) fn call_function(
2165    component_instance: &ComponentInstance,
2166    element: &ElementRc,
2167    function_name: &str,
2168    args: Vec<Value>,
2169) -> Option<Value> {
2170    generativity::make_guard!(guard);
2171    match enclosing_component_instance_for_element(element, component_instance, guard) {
2172        ComponentInstance::InstanceRef(c) => {
2173            let mut ctx = EvalLocalContext::from_function_arguments(c, args);
2174            eval_expression(
2175                &element.borrow().bindings.get(function_name)?.borrow().expression,
2176                &mut ctx,
2177            )
2178            .into()
2179        }
2180        ComponentInstance::GlobalComponent(g) => g.as_ref().eval_function(function_name, args).ok(),
2181    }
2182}
2183
2184/// Return the component instance which hold the given element.
2185/// Does not take in account the global component.
2186pub fn enclosing_component_for_element<'a, 'old_id, 'new_id>(
2187    element: &'a ElementRc,
2188    component: InstanceRef<'a, 'old_id>,
2189    _guard: generativity::Guard<'new_id>,
2190) -> InstanceRef<'a, 'new_id> {
2191    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2192    if Rc::ptr_eq(enclosing, &component.description.original) {
2193        // Safety: new_id is an unique id
2194        unsafe {
2195            std::mem::transmute::<InstanceRef<'a, 'old_id>, InstanceRef<'a, 'new_id>>(component)
2196        }
2197    } else {
2198        assert!(!enclosing.is_global());
2199        // Safety: this is the only place we use this 'static lifetime in this function and nothing is returned with it
2200        // For some reason we can't make a new guard here because the compiler thinks we are returning that
2201        // (it assumes that the 'id must outlive 'a , which is not true)
2202        let static_guard = unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
2203
2204        let parent_instance = component
2205            .parent_instance(static_guard)
2206            .expect("accessing deleted parent (issue #6426)");
2207        enclosing_component_for_element(element, parent_instance, _guard)
2208    }
2209}
2210
2211/// Return the component instance which hold the given element.
2212/// The difference with enclosing_component_for_element is that it takes the GlobalComponent into account.
2213pub(crate) fn enclosing_component_instance_for_element<'a, 'new_id>(
2214    element: &'a ElementRc,
2215    component_instance: &ComponentInstance<'a, '_>,
2216    guard: generativity::Guard<'new_id>,
2217) -> ComponentInstance<'a, 'new_id> {
2218    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2219    match component_instance {
2220        ComponentInstance::InstanceRef(component) => {
2221            if enclosing.is_global() && !Rc::ptr_eq(enclosing, &component.description.original) {
2222                ComponentInstance::GlobalComponent(
2223                    component
2224                        .description
2225                        .extra_data_offset
2226                        .apply(component.instance.get_ref())
2227                        .globals
2228                        .get()
2229                        .unwrap()
2230                        .get(enclosing.root_element.borrow().id.as_str())
2231                        .unwrap(),
2232                )
2233            } else {
2234                ComponentInstance::InstanceRef(enclosing_component_for_element(
2235                    element, *component, guard,
2236                ))
2237            }
2238        }
2239        ComponentInstance::GlobalComponent(global) => {
2240            //assert!(Rc::ptr_eq(enclosing, &global.component));
2241            ComponentInstance::GlobalComponent(global.clone())
2242        }
2243    }
2244}
2245
2246pub fn new_struct_with_bindings<ElementType: 'static + Default + corelib::rtti::BuiltinItem>(
2247    bindings: &i_slint_compiler::object_tree::BindingsMap,
2248    local_context: &mut EvalLocalContext,
2249) -> ElementType {
2250    let mut element = ElementType::default();
2251    for (prop, info) in ElementType::fields::<Value>().into_iter() {
2252        if let Some(binding) = &bindings.get(prop) {
2253            let value = eval_expression(&binding.borrow(), local_context);
2254            info.set_field(&mut element, value).unwrap();
2255        }
2256    }
2257    element
2258}
2259
2260fn convert_from_lyon_path<'a>(
2261    events_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2262    points_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2263    local_context: &mut EvalLocalContext,
2264) -> PathData {
2265    let events = events_it
2266        .into_iter()
2267        .map(|event_expr| eval_expression(event_expr, local_context).try_into().unwrap())
2268        .collect::<SharedVector<_>>();
2269
2270    let points = points_it
2271        .into_iter()
2272        .map(|point_expr| {
2273            let point_value = eval_expression(point_expr, local_context);
2274            let point_struct: Struct = point_value.try_into().unwrap();
2275            let mut point = i_slint_core::graphics::Point::default();
2276            let x: f64 = point_struct.get_field("x").unwrap().clone().try_into().unwrap();
2277            let y: f64 = point_struct.get_field("y").unwrap().clone().try_into().unwrap();
2278            point.x = x as _;
2279            point.y = y as _;
2280            point
2281        })
2282        .collect::<SharedVector<_>>();
2283
2284    PathData::Events(events, points)
2285}
2286
2287pub fn convert_path(path: &ExprPath, local_context: &mut EvalLocalContext) -> PathData {
2288    match path {
2289        ExprPath::Elements(elements) => PathData::Elements(
2290            elements
2291                .iter()
2292                .map(|element| convert_path_element(element, local_context))
2293                .collect::<SharedVector<PathElement>>(),
2294        ),
2295        ExprPath::Events(events, points) => {
2296            convert_from_lyon_path(events.iter(), points.iter(), local_context)
2297        }
2298        ExprPath::Commands(commands) => {
2299            if let Value::String(commands) = eval_expression(commands, local_context) {
2300                PathData::Commands(commands)
2301            } else {
2302                panic!("binding to path commands does not evaluate to string");
2303            }
2304        }
2305    }
2306}
2307
2308fn convert_path_element(
2309    expr_element: &ExprPathElement,
2310    local_context: &mut EvalLocalContext,
2311) -> PathElement {
2312    match expr_element.element_type.native_class.class_name.as_str() {
2313        "MoveTo" => {
2314            PathElement::MoveTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2315        }
2316        "LineTo" => {
2317            PathElement::LineTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2318        }
2319        "ArcTo" => {
2320            PathElement::ArcTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2321        }
2322        "CubicTo" => {
2323            PathElement::CubicTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2324        }
2325        "QuadraticTo" => PathElement::QuadraticTo(new_struct_with_bindings(
2326            &expr_element.bindings,
2327            local_context,
2328        )),
2329        "Close" => PathElement::Close,
2330        _ => panic!(
2331            "Cannot create unsupported path element {}",
2332            expr_element.element_type.native_class.class_name
2333        ),
2334    }
2335}
2336
2337/// Create a value suitable as the default value of a given type
2338pub fn default_value_for_type(ty: &Type) -> Value {
2339    match ty {
2340        Type::Float32 | Type::Int32 => Value::Number(0.),
2341        Type::String => Value::String(Default::default()),
2342        Type::Color | Type::Brush => Value::Brush(Default::default()),
2343        Type::Duration | Type::Angle | Type::PhysicalLength | Type::LogicalLength | Type::Rem => {
2344            Value::Number(0.)
2345        }
2346        Type::Image => Value::Image(Default::default()),
2347        Type::Bool => Value::Bool(false),
2348        Type::Callback { .. } => Value::Void,
2349        Type::Struct(s) => Value::Struct(
2350            s.fields
2351                .iter()
2352                .map(|(n, t)| (n.to_string(), default_value_for_type(t)))
2353                .collect::<Struct>(),
2354        ),
2355        Type::Array(_) | Type::Model => Value::Model(Default::default()),
2356        Type::Percent => Value::Number(0.),
2357        Type::Enumeration(e) => Value::EnumerationValue(
2358            e.name.to_string(),
2359            e.values.get(e.default_value).unwrap().to_string(),
2360        ),
2361        Type::Keys => Value::Keys(Default::default()),
2362        Type::Easing => Value::EasingCurve(Default::default()),
2363        Type::Void | Type::Invalid => Value::Void,
2364        Type::UnitProduct(_) => Value::Number(0.),
2365        Type::PathData => Value::PathData(Default::default()),
2366        Type::LayoutCache => Value::LayoutCache(Default::default()),
2367        Type::ArrayOfU16 => Value::ArrayOfU16(Default::default()),
2368        Type::ComponentFactory => Value::ComponentFactory(Default::default()),
2369        Type::InferredProperty
2370        | Type::InferredCallback
2371        | Type::ElementReference
2372        | Type::Function { .. } => {
2373            panic!("There can't be such property")
2374        }
2375        Type::StyledText => Value::StyledText(Default::default()),
2376    }
2377}
2378
2379fn menu_item_tree_properties(
2380    context_menu_item_tree: vtable::VRc<i_slint_core::menus::MenuVTable, MenuFromItemTree>,
2381) -> (Box<dyn Fn() -> Value>, CallbackHandler, CallbackHandler) {
2382    let context_menu_item_tree_ = context_menu_item_tree.clone();
2383    let entries = Box::new(move || {
2384        let mut entries = SharedVector::default();
2385        context_menu_item_tree_.sub_menu(None, &mut entries);
2386        Value::Model(ModelRc::new(VecModel::from(
2387            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2388        )))
2389    });
2390    let context_menu_item_tree_ = context_menu_item_tree.clone();
2391    let sub_menu = Box::new(move |args: &[Value]| -> Value {
2392        let mut entries = SharedVector::default();
2393        context_menu_item_tree_.sub_menu(Some(&args[0].clone().try_into().unwrap()), &mut entries);
2394        Value::Model(ModelRc::new(VecModel::from(
2395            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2396        )))
2397    });
2398    let activated = Box::new(move |args: &[Value]| -> Value {
2399        context_menu_item_tree.activate(&args[0].clone().try_into().unwrap());
2400        Value::Void
2401    });
2402    (entries, sub_menu, activated)
2403}