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