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