Skip to main content

slint_interpreter/
eval.rs

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