Skip to main content

slint_interpreter/
eval.rs

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