Skip to main content

slint_interpreter/
eval.rs

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