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            let to_print: SharedString =
738                eval_expression(&arguments[0], local_context).try_into().unwrap();
739            let location = source_location.as_ref().and_then(|location| {
740                location.source_file().map(|file| {
741                    let (line, column) = file.line_column(
742                        location.span.offset,
743                        i_slint_compiler::diagnostics::ByteFormat::Utf8,
744                    );
745                    corelib::debug_log::DebugLogLocation {
746                        path: file.path().to_string_lossy().to_shared_string(),
747                        line,
748                        column,
749                    }
750                })
751            });
752            let root_weak =
753                vtable::VWeak::into_dyn(local_context.component_instance.root_weak().clone());
754            if let Some(root) = root_weak.upgrade()
755                && let Some(ctx) = corelib::window::context_for_root(&root)
756            {
757                ctx.dispatch_debug_log(location.as_ref(), format_args!("{to_print}"));
758            } else {
759                corelib::debug_log::debug_log_with_location(
760                    location.as_ref(),
761                    format_args!("{to_print}"),
762                );
763            }
764            Value::Void
765        }
766        BuiltinFunction::DecimalSeparator => Value::String(
767            local_context
768                .component_instance
769                .access_window(|window| window.context().locale_decimal_separator())
770                .into(),
771        ),
772        BuiltinFunction::Mod => {
773            let mut to_num = |e| -> f64 { eval_expression(e, local_context).try_into().unwrap() };
774            Value::Number(to_num(&arguments[0]).rem_euclid(to_num(&arguments[1])))
775        }
776        BuiltinFunction::Round => {
777            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
778            Value::Number(x.round())
779        }
780        BuiltinFunction::Ceil => {
781            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
782            Value::Number(x.ceil())
783        }
784        BuiltinFunction::Floor => {
785            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
786            Value::Number(x.floor())
787        }
788        BuiltinFunction::Sqrt => {
789            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
790            Value::Number(x.sqrt())
791        }
792        BuiltinFunction::Abs => {
793            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
794            Value::Number(x.abs())
795        }
796        BuiltinFunction::Sin => {
797            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
798            Value::Number(x.to_radians().sin())
799        }
800        BuiltinFunction::Cos => {
801            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
802            Value::Number(x.to_radians().cos())
803        }
804        BuiltinFunction::Tan => {
805            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
806            Value::Number(x.to_radians().tan())
807        }
808        BuiltinFunction::ASin => {
809            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
810            Value::Number(x.asin().to_degrees())
811        }
812        BuiltinFunction::ACos => {
813            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
814            Value::Number(x.acos().to_degrees())
815        }
816        BuiltinFunction::ATan => {
817            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
818            Value::Number(x.atan().to_degrees())
819        }
820        BuiltinFunction::ATan2 => {
821            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
822            let y: f64 = eval_expression(&arguments[1], local_context).try_into().unwrap();
823            Value::Number(x.atan2(y).to_degrees())
824        }
825        BuiltinFunction::Log => {
826            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
827            let y: f64 = eval_expression(&arguments[1], local_context).try_into().unwrap();
828            Value::Number(x.log(y))
829        }
830        BuiltinFunction::Ln => {
831            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
832            Value::Number(x.ln())
833        }
834        BuiltinFunction::Pow => {
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.powf(y))
838        }
839        BuiltinFunction::Exp => {
840            let x: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
841            Value::Number(x.exp())
842        }
843        BuiltinFunction::ToFixed => {
844            let n: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
845            let digits: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
846            let digits: usize = digits.max(0) as usize;
847            Value::String(i_slint_core::string::shared_string_from_number_fixed(n, digits))
848        }
849        BuiltinFunction::ToPrecision => {
850            let n: f64 = eval_expression(&arguments[0], local_context).try_into().unwrap();
851            let precision: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
852            let precision: usize = precision.max(0) as usize;
853            Value::String(i_slint_core::string::shared_string_from_number_precision(n, precision))
854        }
855        BuiltinFunction::SetFocusItem => {
856            if arguments.len() != 1 {
857                panic!("internal error: incorrect argument count to SetFocusItem")
858            }
859            let component = local_context.component_instance;
860            if let Expression::ElementReference(focus_item) = &arguments[0] {
861                generativity::make_guard!(guard);
862
863                let focus_item = focus_item.upgrade().unwrap();
864                let enclosing_component =
865                    enclosing_component_for_element(&focus_item, component, guard);
866                let description = enclosing_component.description;
867
868                let item_info = &description.items[focus_item.borrow().id.as_str()];
869
870                let focus_item_comp =
871                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
872
873                component.access_window(|window| {
874                    window.set_focus_item(
875                        &corelib::items::ItemRc::new(
876                            vtable::VRc::into_dyn(focus_item_comp),
877                            item_info.item_index(),
878                        ),
879                        true,
880                        FocusReason::Programmatic,
881                    )
882                });
883                Value::Void
884            } else {
885                panic!("internal error: argument to SetFocusItem must be an element")
886            }
887        }
888        BuiltinFunction::ClearFocusItem => {
889            if arguments.len() != 1 {
890                panic!("internal error: incorrect argument count to SetFocusItem")
891            }
892            let component = local_context.component_instance;
893            if let Expression::ElementReference(focus_item) = &arguments[0] {
894                generativity::make_guard!(guard);
895
896                let focus_item = focus_item.upgrade().unwrap();
897                let enclosing_component =
898                    enclosing_component_for_element(&focus_item, component, guard);
899                let description = enclosing_component.description;
900
901                let item_info = &description.items[focus_item.borrow().id.as_str()];
902
903                let focus_item_comp =
904                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
905
906                component.access_window(|window| {
907                    window.set_focus_item(
908                        &corelib::items::ItemRc::new(
909                            vtable::VRc::into_dyn(focus_item_comp),
910                            item_info.item_index(),
911                        ),
912                        false,
913                        FocusReason::Programmatic,
914                    )
915                });
916                Value::Void
917            } else {
918                panic!("internal error: argument to ClearFocusItem must be an element")
919            }
920        }
921        BuiltinFunction::ShowPopupWindow => {
922            if arguments.len() != 1 {
923                panic!("internal error: incorrect argument count to ShowPopupWindow")
924            }
925            let component = local_context.component_instance;
926            if let Expression::ElementReference(popup_window) = &arguments[0] {
927                let popup_window = popup_window.upgrade().unwrap();
928                let pop_comp = popup_window.borrow().enclosing_component.upgrade().unwrap();
929                let parent_component = {
930                    let parent_elem = pop_comp.parent_element().unwrap();
931                    parent_elem.borrow().enclosing_component.upgrade().unwrap()
932                };
933                let popup_list = parent_component.popup_windows.borrow();
934                let popup =
935                    popup_list.iter().find(|p| Rc::ptr_eq(&p.component, &pop_comp)).unwrap();
936
937                generativity::make_guard!(guard);
938                let enclosing_component =
939                    enclosing_component_for_element(&popup.parent_element, component, guard);
940                let parent_item_info = &enclosing_component.description.items
941                    [popup.parent_element.borrow().id.as_str()];
942                let parent_item_comp =
943                    enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
944                let parent_item = corelib::items::ItemRc::new(
945                    vtable::VRc::into_dyn(parent_item_comp),
946                    parent_item_info.item_index(),
947                );
948
949                let close_policy = Value::EnumerationValue(
950                    popup.close_policy.enumeration.name.to_string(),
951                    popup.close_policy.to_string(),
952                )
953                .try_into()
954                .expect("Invalid internal enumeration representation for close policy");
955                let popup_x = popup.x.clone();
956                let popup_y = popup.y.clone();
957
958                crate::dynamic_item_tree::show_popup(
959                    popup_window,
960                    enclosing_component,
961                    popup,
962                    move |instance_ref| {
963                        let comp = ComponentInstance::InstanceRef(instance_ref);
964                        let x = load_property_helper(&comp, &popup_x.element(), popup_x.name())
965                            .unwrap();
966                        let y = load_property_helper(&comp, &popup_y.element(), popup_y.name())
967                            .unwrap();
968                        corelib::api::LogicalPosition::new(
969                            x.try_into().unwrap(),
970                            y.try_into().unwrap(),
971                        )
972                    },
973                    close_policy,
974                    (*enclosing_component.self_weak().get().unwrap()).clone(),
975                    component.window_adapter(),
976                    &parent_item,
977                );
978                Value::Void
979            } else {
980                panic!("internal error: argument to ShowPopupWindow must be an element")
981            }
982        }
983        BuiltinFunction::ClosePopupWindow => {
984            let component = local_context.component_instance;
985            if let Expression::ElementReference(popup_window) = &arguments[0] {
986                let popup_window = popup_window.upgrade().unwrap();
987                let pop_comp = popup_window.borrow().enclosing_component.upgrade().unwrap();
988                let parent_component = {
989                    let parent_elem = pop_comp.parent_element().unwrap();
990                    parent_elem.borrow().enclosing_component.upgrade().unwrap()
991                };
992                let popup_list = parent_component.popup_windows.borrow();
993                let popup =
994                    popup_list.iter().find(|p| Rc::ptr_eq(&p.component, &pop_comp)).unwrap();
995
996                generativity::make_guard!(guard);
997                let enclosing_component =
998                    enclosing_component_for_element(&popup.parent_element, component, guard);
999                crate::dynamic_item_tree::close_popup(
1000                    popup_window,
1001                    enclosing_component,
1002                    enclosing_component.window_adapter(),
1003                );
1004
1005                Value::Void
1006            } else {
1007                panic!("internal error: argument to ClosePopupWindow must be an element")
1008            }
1009        }
1010        BuiltinFunction::ShowPopupMenu | BuiltinFunction::ShowPopupMenuInternal => {
1011            let [Expression::ElementReference(element), entries, position] = arguments else {
1012                panic!("internal error: incorrect argument count to ShowPopupMenu")
1013            };
1014            let position = eval_expression(position, local_context)
1015                .try_into()
1016                .expect("internal error: popup menu position argument should be a point");
1017
1018            let component = local_context.component_instance;
1019            let elem = element.upgrade().unwrap();
1020            generativity::make_guard!(guard);
1021            let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1022            let description = enclosing_component.description;
1023            let item_info = &description.items[elem.borrow().id.as_str()];
1024            let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1025            let item_tree = vtable::VRc::into_dyn(item_comp);
1026            let item_rc = corelib::items::ItemRc::new(item_tree.clone(), item_info.item_index());
1027
1028            generativity::make_guard!(guard);
1029            let compiled = enclosing_component.description.popup_menu_description.unerase(guard);
1030            let extra_data = enclosing_component
1031                .description
1032                .extra_data_offset
1033                .apply(enclosing_component.as_ref());
1034            let inst = crate::dynamic_item_tree::instantiate(
1035                compiled.clone(),
1036                Some((*enclosing_component.self_weak().get().unwrap()).clone()),
1037                None,
1038                Some(&crate::dynamic_item_tree::WindowOptions::UseExistingWindow(
1039                    component.window_adapter(),
1040                )),
1041                extra_data.globals.get().unwrap().clone(),
1042            );
1043
1044            generativity::make_guard!(guard);
1045            let inst_ref = inst.unerase(guard);
1046            if let Expression::ElementReference(e) = entries {
1047                let menu_item_tree =
1048                    e.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1049                let menu_item_tree = crate::dynamic_item_tree::make_menu_item_tree(
1050                    &menu_item_tree,
1051                    &enclosing_component,
1052                    None,
1053                    None,
1054                );
1055
1056                if component.access_window(|window| {
1057                    window.show_native_popup_menu(
1058                        vtable::VRc::into_dyn(menu_item_tree.clone()),
1059                        position,
1060                        &item_rc,
1061                    )
1062                }) {
1063                    return Value::Void;
1064                }
1065
1066                let (entries, sub_menu, activated) = menu_item_tree_properties(menu_item_tree);
1067
1068                compiled.set_binding(inst_ref.borrow(), "entries", entries).unwrap();
1069                compiled.set_callback_handler(inst_ref.borrow(), "sub-menu", sub_menu).unwrap();
1070                compiled.set_callback_handler(inst_ref.borrow(), "activated", activated).unwrap();
1071            } else {
1072                let entries = eval_expression(entries, local_context);
1073                compiled.set_property(inst_ref.borrow(), "entries", entries).unwrap();
1074                let item_weak = item_rc.downgrade();
1075                compiled
1076                    .set_callback_handler(
1077                        inst_ref.borrow(),
1078                        "sub-menu",
1079                        Box::new(move |args: &[Value]| -> Value {
1080                            item_weak
1081                                .upgrade()
1082                                .unwrap()
1083                                .downcast::<corelib::items::ContextMenu>()
1084                                .unwrap()
1085                                .sub_menu
1086                                .call(&(args[0].clone().try_into().unwrap(),))
1087                                .into()
1088                        }),
1089                    )
1090                    .unwrap();
1091                let item_weak = item_rc.downgrade();
1092                compiled
1093                    .set_callback_handler(
1094                        inst_ref.borrow(),
1095                        "activated",
1096                        Box::new(move |args: &[Value]| -> Value {
1097                            item_weak
1098                                .upgrade()
1099                                .unwrap()
1100                                .downcast::<corelib::items::ContextMenu>()
1101                                .unwrap()
1102                                .activated
1103                                .call(&(args[0].clone().try_into().unwrap(),));
1104                            Value::Void
1105                        }),
1106                    )
1107                    .unwrap();
1108            }
1109            let item_weak = item_rc.downgrade();
1110            compiled
1111                .set_callback_handler(
1112                    inst_ref.borrow(),
1113                    "close-popup",
1114                    Box::new(move |_args: &[Value]| -> Value {
1115                        let Some(item_rc) = item_weak.upgrade() else { return Value::Void };
1116                        if let Some(id) = item_rc
1117                            .downcast::<corelib::items::ContextMenu>()
1118                            .unwrap()
1119                            .popup_id
1120                            .take()
1121                        {
1122                            WindowInner::from_pub(item_rc.window_adapter().unwrap().window())
1123                                .close_popup(id);
1124                        }
1125                        Value::Void
1126                    }),
1127                )
1128                .unwrap();
1129            component.access_window(|window| {
1130                let context_menu_elem = item_rc.downcast::<corelib::items::ContextMenu>().unwrap();
1131                if let Some(old_id) = context_menu_elem.popup_id.take() {
1132                    window.close_popup(old_id)
1133                }
1134                let id = window.show_popup(
1135                    &vtable::VRc::into_dyn(inst.clone()),
1136                    Box::new(move || position),
1137                    corelib::items::PopupClosePolicy::CloseOnClickOutside,
1138                    &item_rc,
1139                    WindowKind::Menu,
1140                );
1141                context_menu_elem.popup_id.set(Some(id));
1142            });
1143            inst.run_setup_code();
1144            Value::Void
1145        }
1146        BuiltinFunction::SetSelectionOffsets => {
1147            if arguments.len() != 3 {
1148                panic!("internal error: incorrect argument count to select range function call")
1149            }
1150            let component = local_context.component_instance;
1151            if let Expression::ElementReference(element) = &arguments[0] {
1152                generativity::make_guard!(guard);
1153
1154                let elem = element.upgrade().unwrap();
1155                let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1156                let description = enclosing_component.description;
1157                let item_info = &description.items[elem.borrow().id.as_str()];
1158                let item_ref =
1159                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1160
1161                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1162                let item_rc = corelib::items::ItemRc::new(
1163                    vtable::VRc::into_dyn(item_comp),
1164                    item_info.item_index(),
1165                );
1166
1167                let window_adapter = component.window_adapter();
1168
1169                // TODO: Make this generic through RTTI
1170                if let Some(textinput) =
1171                    ItemRef::downcast_pin::<corelib::items::TextInput>(item_ref)
1172                {
1173                    let start: i32 =
1174                        eval_expression(&arguments[1], local_context).try_into().expect(
1175                            "internal error: second argument to set-selection-offsets must be an integer",
1176                        );
1177                    let end: i32 = eval_expression(&arguments[2], local_context).try_into().expect(
1178                        "internal error: third argument to set-selection-offsets must be an integer",
1179                    );
1180
1181                    textinput.set_selection_offsets(&window_adapter, &item_rc, start, end);
1182                } else {
1183                    panic!(
1184                        "internal error: member function called on element that doesn't have it: {}",
1185                        elem.borrow().original_name()
1186                    )
1187                }
1188
1189                Value::Void
1190            } else {
1191                panic!("internal error: first argument to set-selection-offsets must be an element")
1192            }
1193        }
1194        BuiltinFunction::ItemFontMetrics => {
1195            if arguments.len() != 1 {
1196                panic!(
1197                    "internal error: incorrect argument count to item font metrics function call"
1198                )
1199            }
1200            let component = local_context.component_instance;
1201            if let Expression::ElementReference(element) = &arguments[0] {
1202                generativity::make_guard!(guard);
1203
1204                let elem = element.upgrade().unwrap();
1205                let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1206                let description = enclosing_component.description;
1207                let item_info = &description.items[elem.borrow().id.as_str()];
1208                let item_ref =
1209                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1210                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1211                let item_rc = corelib::items::ItemRc::new(
1212                    vtable::VRc::into_dyn(item_comp),
1213                    item_info.item_index(),
1214                );
1215                let window_adapter = component.window_adapter();
1216                let metrics = i_slint_core::items::slint_text_item_fontmetrics(
1217                    &window_adapter,
1218                    item_ref,
1219                    &item_rc,
1220                );
1221                metrics.into()
1222            } else {
1223                panic!("internal error: argument to item-font-metrics must be an element")
1224            }
1225        }
1226        BuiltinFunction::StringIsFloat => {
1227            if arguments.len() != 1 {
1228                panic!("internal error: incorrect argument count to StringIsFloat")
1229            }
1230            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1231                Value::Bool(<f64 as core::str::FromStr>::from_str(s.as_str()).is_ok())
1232            } else {
1233                panic!("Argument not a string");
1234            }
1235        }
1236        BuiltinFunction::StringToFloat => {
1237            if arguments.len() != 1 {
1238                panic!("internal error: incorrect argument count to StringToFloat")
1239            }
1240            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1241                Value::Number(core::str::FromStr::from_str(s.as_str()).unwrap_or(0.))
1242            } else {
1243                panic!("Argument not a string");
1244            }
1245        }
1246        BuiltinFunction::StringIsEmpty => {
1247            if arguments.len() != 1 {
1248                panic!("internal error: incorrect argument count to StringIsEmpty")
1249            }
1250            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1251                Value::Bool(s.is_empty())
1252            } else {
1253                panic!("Argument not a string");
1254            }
1255        }
1256        BuiltinFunction::StringCharacterCount => {
1257            if arguments.len() != 1 {
1258                panic!("internal error: incorrect argument count to StringCharacterCount")
1259            }
1260            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1261                Value::Number(
1262                    unicode_segmentation::UnicodeSegmentation::graphemes(s.as_str(), true).count()
1263                        as f64,
1264                )
1265            } else {
1266                panic!("Argument not a string");
1267            }
1268        }
1269        BuiltinFunction::StringToLowercase => {
1270            if arguments.len() != 1 {
1271                panic!("internal error: incorrect argument count to StringToLowercase")
1272            }
1273            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1274                Value::String(s.to_lowercase().into())
1275            } else {
1276                panic!("Argument not a string");
1277            }
1278        }
1279        BuiltinFunction::StringToUppercase => {
1280            if arguments.len() != 1 {
1281                panic!("internal error: incorrect argument count to StringToUppercase")
1282            }
1283            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1284                Value::String(s.to_uppercase().into())
1285            } else {
1286                panic!("Argument not a string");
1287            }
1288        }
1289        BuiltinFunction::KeysToString => {
1290            if arguments.len() != 1 {
1291                panic!("internal error: incorrect argument count to KeysToString")
1292            }
1293            let Value::Keys(keys) = eval_expression(&arguments[0], local_context) else {
1294                panic!("Argument is not of type keys");
1295            };
1296            Value::String(ToSharedString::to_shared_string(&keys))
1297        }
1298        BuiltinFunction::ColorRgbaStruct => {
1299            if arguments.len() != 1 {
1300                panic!("internal error: incorrect argument count to ColorRGBAComponents")
1301            }
1302            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1303                let color = brush.color();
1304                let values = IntoIterator::into_iter([
1305                    ("red".to_string(), Value::Number(color.red().into())),
1306                    ("green".to_string(), Value::Number(color.green().into())),
1307                    ("blue".to_string(), Value::Number(color.blue().into())),
1308                    ("alpha".to_string(), Value::Number(color.alpha().into())),
1309                ])
1310                .collect();
1311                Value::Struct(values)
1312            } else {
1313                panic!("First argument not a color");
1314            }
1315        }
1316        BuiltinFunction::ColorHsvaStruct => {
1317            if arguments.len() != 1 {
1318                panic!("internal error: incorrect argument count to ColorHSVAComponents")
1319            }
1320            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1321                let color = brush.color().to_hsva();
1322                let values = IntoIterator::into_iter([
1323                    ("hue".to_string(), Value::Number(color.hue.into())),
1324                    ("saturation".to_string(), Value::Number(color.saturation.into())),
1325                    ("value".to_string(), Value::Number(color.value.into())),
1326                    ("alpha".to_string(), Value::Number(color.alpha.into())),
1327                ])
1328                .collect();
1329                Value::Struct(values)
1330            } else {
1331                panic!("First argument not a color");
1332            }
1333        }
1334        BuiltinFunction::ColorOklchStruct => {
1335            if arguments.len() != 1 {
1336                panic!("internal error: incorrect argument count to ColorOklchStruct")
1337            }
1338            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1339                let color = brush.color().to_oklch();
1340                let values = IntoIterator::into_iter([
1341                    ("lightness".to_string(), Value::Number(color.lightness.into())),
1342                    ("chroma".to_string(), Value::Number(color.chroma.into())),
1343                    ("hue".to_string(), Value::Number(color.hue.into())),
1344                    ("alpha".to_string(), Value::Number(color.alpha.into())),
1345                ])
1346                .collect();
1347                Value::Struct(values)
1348            } else {
1349                panic!("First argument not a color");
1350            }
1351        }
1352        BuiltinFunction::ColorBrighter => {
1353            if arguments.len() != 2 {
1354                panic!("internal error: incorrect argument count to ColorBrighter")
1355            }
1356            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1357                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1358                    brush.brighter(factor as _).into()
1359                } else {
1360                    panic!("Second argument not a number");
1361                }
1362            } else {
1363                panic!("First argument not a color");
1364            }
1365        }
1366        BuiltinFunction::ColorDarker => {
1367            if arguments.len() != 2 {
1368                panic!("internal error: incorrect argument count to ColorDarker")
1369            }
1370            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1371                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1372                    brush.darker(factor as _).into()
1373                } else {
1374                    panic!("Second argument not a number");
1375                }
1376            } else {
1377                panic!("First argument not a color");
1378            }
1379        }
1380        BuiltinFunction::ColorTransparentize => {
1381            if arguments.len() != 2 {
1382                panic!("internal error: incorrect argument count to ColorFaded")
1383            }
1384            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1385                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1386                    brush.transparentize(factor as _).into()
1387                } else {
1388                    panic!("Second argument not a number");
1389                }
1390            } else {
1391                panic!("First argument not a color");
1392            }
1393        }
1394        BuiltinFunction::ColorMix => {
1395            if arguments.len() != 3 {
1396                panic!("internal error: incorrect argument count to ColorMix")
1397            }
1398
1399            let arg0 = eval_expression(&arguments[0], local_context);
1400            let arg1 = eval_expression(&arguments[1], local_context);
1401            let arg2 = eval_expression(&arguments[2], local_context);
1402
1403            if !matches!(arg0, Value::Brush(Brush::SolidColor(_))) {
1404                panic!("First argument not a color");
1405            }
1406            if !matches!(arg1, Value::Brush(Brush::SolidColor(_))) {
1407                panic!("Second argument not a color");
1408            }
1409            if !matches!(arg2, Value::Number(_)) {
1410                panic!("Third argument not a number");
1411            }
1412
1413            let (
1414                Value::Brush(Brush::SolidColor(color_a)),
1415                Value::Brush(Brush::SolidColor(color_b)),
1416                Value::Number(factor),
1417            ) = (arg0, arg1, arg2)
1418            else {
1419                unreachable!()
1420            };
1421
1422            color_a.mix(&color_b, factor as _).into()
1423        }
1424        BuiltinFunction::ColorWithAlpha => {
1425            if arguments.len() != 2 {
1426                panic!("internal error: incorrect argument count to ColorWithAlpha")
1427            }
1428            if let Value::Brush(brush) = eval_expression(&arguments[0], local_context) {
1429                if let Value::Number(factor) = eval_expression(&arguments[1], local_context) {
1430                    brush.with_alpha(factor as _).into()
1431                } else {
1432                    panic!("Second argument not a number");
1433                }
1434            } else {
1435                panic!("First argument not a color");
1436            }
1437        }
1438        BuiltinFunction::ImageSize => {
1439            if arguments.len() != 1 {
1440                panic!("internal error: incorrect argument count to ImageSize")
1441            }
1442            if let Value::Image(img) = eval_expression(&arguments[0], local_context) {
1443                let size = img.size();
1444                let values = IntoIterator::into_iter([
1445                    ("width".to_string(), Value::Number(size.width as f64)),
1446                    ("height".to_string(), Value::Number(size.height as f64)),
1447                ])
1448                .collect();
1449                Value::Struct(values)
1450            } else {
1451                panic!("First argument not an image");
1452            }
1453        }
1454        BuiltinFunction::ArrayLength => {
1455            if arguments.len() != 1 {
1456                panic!("internal error: incorrect argument count to ArrayLength")
1457            }
1458            match eval_expression(&arguments[0], local_context) {
1459                Value::Model(model) => {
1460                    model.model_tracker().track_row_count_changes();
1461                    Value::Number(model.row_count() as f64)
1462                }
1463                _ => {
1464                    panic!("First argument not an array: {:?}", arguments[0]);
1465                }
1466            }
1467        }
1468        BuiltinFunction::Rgb => {
1469            let r: i32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1470            let g: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1471            let b: i32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1472            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1473            let r: u8 = r.clamp(0, 255) as u8;
1474            let g: u8 = g.clamp(0, 255) as u8;
1475            let b: u8 = b.clamp(0, 255) as u8;
1476            let a: u8 = (255. * a).clamp(0., 255.) as u8;
1477            Value::Brush(Brush::SolidColor(Color::from_argb_u8(a, r, g, b)))
1478        }
1479        BuiltinFunction::Hsv => {
1480            let h: f32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1481            let s: f32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1482            let v: f32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1483            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1484            let a = (1. * a).clamp(0., 1.);
1485            Value::Brush(Brush::SolidColor(Color::from_hsva(h, s, v, a)))
1486        }
1487        BuiltinFunction::Oklch => {
1488            let l: f32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1489            let c: f32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1490            let h: f32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1491            let a: f32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1492            let l = l.clamp(0., 1.);
1493            let c = c.max(0.);
1494            let a = a.clamp(0., 1.);
1495            Value::Brush(Brush::SolidColor(Color::from_oklch(l, c, h, a)))
1496        }
1497        BuiltinFunction::ColorScheme => {
1498            let root_weak =
1499                vtable::VWeak::into_dyn(local_context.component_instance.root_weak().clone());
1500            let root = root_weak.upgrade().unwrap();
1501            corelib::window::context_for_root(&root)
1502                .map_or(corelib::items::ColorScheme::Unknown, |ctx| ctx.color_scheme(Some(&root)))
1503                .into()
1504        }
1505        BuiltinFunction::AccentColor => {
1506            let root_weak =
1507                vtable::VWeak::into_dyn(local_context.component_instance.root_weak().clone());
1508            let root = root_weak.upgrade().unwrap();
1509            Value::Brush(corelib::Brush::SolidColor(corelib::window::accent_color(&root)))
1510        }
1511        BuiltinFunction::SupportsNativeMenuBar => local_context
1512            .component_instance
1513            .window_adapter()
1514            .internal(corelib::InternalToken)
1515            .is_some_and(|x| x.supports_native_menu_bar())
1516            .into(),
1517        BuiltinFunction::SetupMenuBar => {
1518            let component = local_context.component_instance;
1519            let [
1520                Expression::PropertyReference(entries_nr),
1521                Expression::PropertyReference(sub_menu_nr),
1522                Expression::PropertyReference(activated_nr),
1523                Expression::ElementReference(item_tree_root),
1524                Expression::BoolLiteral(no_native),
1525                condition,
1526                visible,
1527                ..,
1528            ] = arguments
1529            else {
1530                panic!("internal error: incorrect argument count to SetupMenuBar")
1531            };
1532
1533            let menu_item_tree =
1534                item_tree_root.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1535            let menu_item_tree = crate::dynamic_item_tree::make_menu_item_tree(
1536                &menu_item_tree,
1537                &component,
1538                Some(condition),
1539                Some(visible),
1540            );
1541
1542            let window_adapter = component.window_adapter();
1543            let window_inner = WindowInner::from_pub(window_adapter.window());
1544            let menubar = vtable::VRc::into_dyn(vtable::VRc::clone(&menu_item_tree));
1545            window_inner.setup_menubar_shortcuts(vtable::VRc::clone(&menubar));
1546
1547            if !no_native && window_inner.supports_native_menu_bar() {
1548                window_inner.setup_menubar(menubar);
1549                return Value::Void;
1550            }
1551
1552            let (entries, sub_menu, activated) = menu_item_tree_properties(menu_item_tree);
1553
1554            assert_eq!(
1555                entries_nr.element().borrow().id,
1556                component.description.original.root_element.borrow().id,
1557                "entries need to be in the main element"
1558            );
1559            local_context
1560                .component_instance
1561                .description
1562                .set_binding(component.borrow(), entries_nr.name(), entries)
1563                .unwrap();
1564            let i = &ComponentInstance::InstanceRef(local_context.component_instance);
1565            set_callback_handler(i, &sub_menu_nr.element(), sub_menu_nr.name(), sub_menu).unwrap();
1566            set_callback_handler(i, &activated_nr.element(), activated_nr.name(), activated)
1567                .unwrap();
1568
1569            Value::Void
1570        }
1571        BuiltinFunction::SetupSystemTrayIcon => {
1572            let [
1573                Expression::ElementReference(system_tray_elem),
1574                Expression::ElementReference(item_tree_root),
1575                rest @ ..,
1576            ] = arguments
1577            else {
1578                panic!("internal error: incorrect argument count to SetupSystemTrayIcon")
1579            };
1580
1581            let component = local_context.component_instance;
1582            let elem = system_tray_elem.upgrade().unwrap();
1583            generativity::make_guard!(guard);
1584            let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1585            let description = enclosing_component.description;
1586            let item_info = &description.items[elem.borrow().id.as_str()];
1587            let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1588            let item_tree = vtable::VRc::into_dyn(item_comp);
1589            let item_rc = corelib::items::ItemRc::new(item_tree.clone(), item_info.item_index());
1590
1591            let menu_item_tree_component =
1592                item_tree_root.upgrade().unwrap().borrow().enclosing_component.upgrade().unwrap();
1593            let menu_vrc = crate::dynamic_item_tree::make_menu_item_tree(
1594                &menu_item_tree_component,
1595                &enclosing_component,
1596                rest.first(),
1597                None,
1598            );
1599
1600            let system_tray =
1601                item_rc.downcast::<corelib::items::SystemTrayIcon>().expect("SystemTrayIcon item");
1602            system_tray.as_pin_ref().set_menu(&item_rc, vtable::VRc::into_dyn(menu_vrc));
1603
1604            Value::Void
1605        }
1606        BuiltinFunction::MonthDayCount => {
1607            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1608            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1609            Value::Number(i_slint_core::date_time::month_day_count(m, y).unwrap_or(0) as f64)
1610        }
1611        BuiltinFunction::MonthOffset => {
1612            let m: u32 = eval_expression(&arguments[0], local_context).try_into().unwrap();
1613            let y: i32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1614
1615            Value::Number(i_slint_core::date_time::month_offset(m, y) as f64)
1616        }
1617        BuiltinFunction::FormatDate => {
1618            let f: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1619            let d: u32 = eval_expression(&arguments[1], local_context).try_into().unwrap();
1620            let m: u32 = eval_expression(&arguments[2], local_context).try_into().unwrap();
1621            let y: i32 = eval_expression(&arguments[3], local_context).try_into().unwrap();
1622
1623            Value::String(i_slint_core::date_time::format_date(&f, d, m, y))
1624        }
1625        BuiltinFunction::DateNow => Value::Model(ModelRc::new(VecModel::from(
1626            i_slint_core::date_time::date_now()
1627                .into_iter()
1628                .map(|x| Value::Number(x as f64))
1629                .collect::<Vec<_>>(),
1630        ))),
1631        BuiltinFunction::ValidDate => {
1632            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1633            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1634            Value::Bool(i_slint_core::date_time::parse_date(d.as_str(), f.as_str()).is_some())
1635        }
1636        BuiltinFunction::ParseDate => {
1637            let d: SharedString = eval_expression(&arguments[0], local_context).try_into().unwrap();
1638            let f: SharedString = eval_expression(&arguments[1], local_context).try_into().unwrap();
1639
1640            Value::Model(ModelRc::new(
1641                i_slint_core::date_time::parse_date(d.as_str(), f.as_str())
1642                    .map(|x| {
1643                        VecModel::from(
1644                            x.into_iter().map(|x| Value::Number(x as f64)).collect::<Vec<_>>(),
1645                        )
1646                    })
1647                    .unwrap_or_default(),
1648            ))
1649        }
1650        BuiltinFunction::TextInputFocused => Value::Bool(
1651            local_context.component_instance.access_window(|window| window.text_input_focused())
1652                as _,
1653        ),
1654        BuiltinFunction::SetTextInputFocused => {
1655            local_context.component_instance.access_window(|window| {
1656                window.set_text_input_focused(
1657                    eval_expression(&arguments[0], local_context).try_into().unwrap(),
1658                )
1659            });
1660            Value::Void
1661        }
1662        BuiltinFunction::ImplicitLayoutInfo(orient) => {
1663            let component = local_context.component_instance;
1664            if let [Expression::ElementReference(item), constraint_expr] = arguments {
1665                generativity::make_guard!(guard);
1666
1667                let constraint: f32 =
1668                    eval_expression(constraint_expr, local_context).try_into().unwrap_or(-1.);
1669
1670                let item = item.upgrade().unwrap();
1671                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1672                let description = enclosing_component.description;
1673                let item_info = &description.items[item.borrow().id.as_str()];
1674                let item_ref =
1675                    unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1676                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1677                let window_adapter = component.window_adapter();
1678                item_ref
1679                    .as_ref()
1680                    .layout_info(
1681                        crate::eval_layout::to_runtime(orient),
1682                        constraint,
1683                        &window_adapter,
1684                        &ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index()),
1685                    )
1686                    .into()
1687            } else {
1688                panic!("internal error: incorrect arguments to ImplicitLayoutInfo {arguments:?}");
1689            }
1690        }
1691        BuiltinFunction::ItemAbsolutePosition => {
1692            if arguments.len() != 1 {
1693                panic!("internal error: incorrect argument count to ItemAbsolutePosition")
1694            }
1695
1696            let component = local_context.component_instance;
1697
1698            if let Expression::ElementReference(item) = &arguments[0] {
1699                generativity::make_guard!(guard);
1700
1701                let item = item.upgrade().unwrap();
1702                let enclosing_component = enclosing_component_for_element(&item, component, guard);
1703                let description = enclosing_component.description;
1704
1705                let item_info = &description.items[item.borrow().id.as_str()];
1706
1707                let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1708
1709                let item_rc = corelib::items::ItemRc::new(
1710                    vtable::VRc::into_dyn(item_comp),
1711                    item_info.item_index(),
1712                );
1713
1714                item_rc.map_to_window(Default::default()).to_untyped().into()
1715            } else {
1716                panic!("internal error: argument to SetFocusItem must be an element")
1717            }
1718        }
1719        BuiltinFunction::RegisterCustomFontByPath => {
1720            if arguments.len() != 1 {
1721                panic!("internal error: incorrect argument count to RegisterCustomFontByPath")
1722            }
1723            let component = local_context.component_instance;
1724            if let Value::String(s) = eval_expression(&arguments[0], local_context) {
1725                if let Some(err) = component
1726                    .window_adapter()
1727                    .renderer()
1728                    .register_font_from_path(&std::path::PathBuf::from(s.as_str()))
1729                    .err()
1730                {
1731                    corelib::debug_log!("Error loading custom font {}: {}", s.as_str(), err);
1732                }
1733                Value::Void
1734            } else {
1735                panic!("Argument not a string");
1736            }
1737        }
1738        BuiltinFunction::RegisterCustomFontByMemory | BuiltinFunction::RegisterBitmapFont => {
1739            unimplemented!()
1740        }
1741        BuiltinFunction::Translate => {
1742            let original: SharedString =
1743                eval_expression(&arguments[0], local_context).try_into().unwrap();
1744            let context: SharedString =
1745                eval_expression(&arguments[1], local_context).try_into().unwrap();
1746            let domain: SharedString =
1747                eval_expression(&arguments[2], local_context).try_into().unwrap();
1748            let args = eval_expression(&arguments[3], local_context);
1749            let Value::Model(args) = args else { panic!("Args to translate not a model {args:?}") };
1750            struct StringModelWrapper(ModelRc<Value>);
1751            impl corelib::translations::FormatArgs for StringModelWrapper {
1752                type Output<'a> = SharedString;
1753                fn from_index(&self, index: usize) -> Option<SharedString> {
1754                    self.0.row_data(index).map(|x| x.try_into().unwrap())
1755                }
1756            }
1757            Value::String(corelib::translations::translate(
1758                &original,
1759                &context,
1760                &domain,
1761                &StringModelWrapper(args),
1762                eval_expression(&arguments[4], local_context).try_into().unwrap(),
1763                &SharedString::try_from(eval_expression(&arguments[5], local_context)).unwrap(),
1764            ))
1765        }
1766        BuiltinFunction::Use24HourFormat => Value::Bool(corelib::date_time::use_24_hour_format()),
1767        BuiltinFunction::UpdateTimers => {
1768            crate::dynamic_item_tree::update_timers(local_context.component_instance);
1769            Value::Void
1770        }
1771        BuiltinFunction::DetectOperatingSystem => i_slint_core::detect_operating_system().into(),
1772        // start and stop are unreachable because they are lowered to simple assignment of running
1773        BuiltinFunction::StartTimer => unreachable!(),
1774        BuiltinFunction::StopTimer => unreachable!(),
1775        BuiltinFunction::RestartTimer => {
1776            if let [Expression::ElementReference(timer_element)] = arguments {
1777                crate::dynamic_item_tree::restart_timer(
1778                    timer_element.clone(),
1779                    local_context.component_instance,
1780                );
1781
1782                Value::Void
1783            } else {
1784                panic!("internal error: argument to RestartTimer must be an element")
1785            }
1786        }
1787        BuiltinFunction::OpenUrl => {
1788            let url: SharedString =
1789                eval_expression(&arguments[0], local_context).try_into().unwrap();
1790            let window_adapter = local_context.component_instance.window_adapter();
1791            Value::Bool(corelib::open_url(&url, window_adapter.window()).is_ok())
1792        }
1793        BuiltinFunction::MacosBringAllWindowsToFront => {
1794            corelib::macos_bring_all_windows_to_front();
1795            Value::Void
1796        }
1797        BuiltinFunction::ParseMarkdown => {
1798            let format_string: SharedString =
1799                eval_expression(&arguments[0], local_context).try_into().unwrap();
1800            let args: ModelRc<corelib::styled_text::StyledText> =
1801                eval_expression(&arguments[1], local_context).try_into().unwrap();
1802            Value::StyledText(corelib::styled_text::parse_markdown(
1803                &format_string,
1804                &args.iter().collect::<Vec<_>>(),
1805            ))
1806        }
1807        BuiltinFunction::StringToStyledText => {
1808            let string: SharedString =
1809                eval_expression(&arguments[0], local_context).try_into().unwrap();
1810            Value::StyledText(corelib::styled_text::string_to_styled_text(string.to_string()))
1811        }
1812        BuiltinFunction::ColorToStyledText => {
1813            let color: corelib::Color =
1814                eval_expression(&arguments[0], local_context).try_into().unwrap();
1815            Value::StyledText(corelib::styled_text::color_to_styled_text(color))
1816        }
1817    }
1818}
1819
1820fn call_item_member_function(nr: &NamedReference, local_context: &mut EvalLocalContext) -> Value {
1821    let component = local_context.component_instance;
1822    let elem = nr.element();
1823    let name = nr.name().as_str();
1824    generativity::make_guard!(guard);
1825    let enclosing_component = enclosing_component_for_element(&elem, component, guard);
1826    let description = enclosing_component.description;
1827    let item_info = &description.items[elem.borrow().id.as_str()];
1828    let item_ref = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1829
1830    let item_comp = enclosing_component.self_weak().get().unwrap().upgrade().unwrap();
1831    let item_rc =
1832        corelib::items::ItemRc::new(vtable::VRc::into_dyn(item_comp), item_info.item_index());
1833
1834    let window_adapter = component.window_adapter();
1835
1836    // TODO: Make this generic through RTTI
1837    if let Some(textinput) = ItemRef::downcast_pin::<corelib::items::TextInput>(item_ref) {
1838        match name {
1839            "select-all" => textinput.select_all(&window_adapter, &item_rc),
1840            "clear-selection" => textinput.clear_selection(&window_adapter, &item_rc),
1841            "cut" => textinput.cut(&window_adapter, &item_rc),
1842            "copy" => textinput.copy(&window_adapter, &item_rc),
1843            "paste" => textinput.paste(&window_adapter, &item_rc),
1844            _ => panic!("internal: Unknown member function {name} called on TextInput"),
1845        }
1846    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::SwipeGestureHandler>(item_ref) {
1847        match name {
1848            "cancel" => s.cancel(&window_adapter, &item_rc),
1849            _ => panic!("internal: Unknown member function {name} called on SwipeGestureHandler"),
1850        }
1851    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::ContextMenu>(item_ref) {
1852        match name {
1853            "close" => s.close(&window_adapter, &item_rc),
1854            "is-open" => return Value::Bool(s.is_open(&window_adapter, &item_rc)),
1855            _ => {
1856                panic!("internal: Unknown member function {name} called on ContextMenu")
1857            }
1858        }
1859    } else if let Some(s) = ItemRef::downcast_pin::<corelib::items::WindowItem>(item_ref) {
1860        match name {
1861            "hide" => s.hide(&window_adapter, &item_rc),
1862            "close" => return Value::Bool(s.close(&window_adapter, &item_rc)),
1863            _ => {
1864                panic!("internal: Unknown member function {name} called on WindowItem")
1865            }
1866        }
1867    } else {
1868        panic!(
1869            "internal error: member function {name} called on element that doesn't have it: {}",
1870            elem.borrow().original_name()
1871        )
1872    }
1873
1874    Value::Void
1875}
1876
1877fn eval_assignment(lhs: &Expression, op: char, rhs: Value, local_context: &mut EvalLocalContext) {
1878    let eval = |lhs| match (lhs, &rhs, op) {
1879        (Value::String(ref mut a), Value::String(b), '+') => {
1880            a.push_str(b.as_str());
1881            Value::String(a.clone())
1882        }
1883        (Value::Number(a), Value::Number(b), '+') => Value::Number(a + b),
1884        (Value::Number(a), Value::Number(b), '-') => Value::Number(a - b),
1885        (Value::Number(a), Value::Number(b), '/') => Value::Number(a / b),
1886        (Value::Number(a), Value::Number(b), '*') => Value::Number(a * b),
1887        (lhs, rhs, op) => panic!("unsupported {lhs:?} {op} {rhs:?}"),
1888    };
1889    match lhs {
1890        Expression::PropertyReference(nr) => {
1891            let element = nr.element();
1892            generativity::make_guard!(guard);
1893            let enclosing_component = enclosing_component_instance_for_element(
1894                &element,
1895                &ComponentInstance::InstanceRef(local_context.component_instance),
1896                guard,
1897            );
1898
1899            match enclosing_component {
1900                ComponentInstance::InstanceRef(enclosing_component) => {
1901                    if op == '=' {
1902                        store_property(enclosing_component, &element, nr.name(), rhs).unwrap();
1903                        return;
1904                    }
1905
1906                    let component = element.borrow().enclosing_component.upgrade().unwrap();
1907                    if element.borrow().id == component.root_element.borrow().id
1908                        && let Some(x) =
1909                            enclosing_component.description.custom_properties.get(nr.name())
1910                    {
1911                        unsafe {
1912                            let p =
1913                                Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
1914                            x.prop.set(p, eval(x.prop.get(p).unwrap()), None).unwrap();
1915                        }
1916                        return;
1917                    }
1918                    let item_info =
1919                        &enclosing_component.description.items[element.borrow().id.as_str()];
1920                    let item =
1921                        unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1922                    let p = &item_info.rtti.properties[nr.name().as_str()];
1923                    p.set(item, eval(p.get(item)), None).unwrap();
1924                }
1925                ComponentInstance::GlobalComponent(global) => {
1926                    let val = if op == '=' {
1927                        rhs
1928                    } else {
1929                        eval(global.as_ref().get_property(nr.name()).unwrap())
1930                    };
1931                    global.as_ref().set_property(nr.name(), val).unwrap();
1932                }
1933            }
1934        }
1935        Expression::StructFieldAccess { base, name } => {
1936            if let Value::Struct(mut o) = eval_expression(base, local_context) {
1937                let mut r = o.get_field(name).unwrap().clone();
1938                r = if op == '=' { rhs } else { eval(std::mem::take(&mut r)) };
1939                o.set_field(name.to_string(), r);
1940                eval_assignment(base, '=', Value::Struct(o), local_context)
1941            }
1942        }
1943        Expression::RepeaterModelReference { element } => {
1944            let element = element.upgrade().unwrap();
1945            let component_instance = local_context.component_instance;
1946            generativity::make_guard!(g1);
1947            let enclosing_component =
1948                enclosing_component_for_element(&element, component_instance, g1);
1949            // we need a 'static Repeater component in order to call model_set_row_data, so get it.
1950            // Safety: This is the only 'static Id in scope.
1951            let static_guard =
1952                unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
1953            let repeater = crate::dynamic_item_tree::get_repeater_by_name(
1954                enclosing_component,
1955                element.borrow().id.as_str(),
1956                static_guard,
1957            );
1958            repeater.0.model_set_row_data(
1959                eval_expression(
1960                    &Expression::RepeaterIndexReference { element: Rc::downgrade(&element) },
1961                    local_context,
1962                )
1963                .try_into()
1964                .unwrap(),
1965                if op == '=' {
1966                    rhs
1967                } else {
1968                    eval(eval_expression(
1969                        &Expression::RepeaterModelReference { element: Rc::downgrade(&element) },
1970                        local_context,
1971                    ))
1972                },
1973            )
1974        }
1975        Expression::ArrayIndex { array, index } => {
1976            let array = eval_expression(array, local_context);
1977            let index = eval_expression(index, local_context);
1978            match (array, index) {
1979                (Value::Model(model), Value::Number(index)) => {
1980                    if index >= 0. && (index as usize) < model.row_count() {
1981                        let index = index as usize;
1982                        if op == '=' {
1983                            model.set_row_data(index, rhs);
1984                        } else {
1985                            model.set_row_data(
1986                                index,
1987                                eval(
1988                                    model
1989                                        .row_data(index)
1990                                        .unwrap_or_else(|| default_value_for_type(&lhs.ty())),
1991                                ),
1992                            );
1993                        }
1994                    }
1995                }
1996                _ => {
1997                    eprintln!("Attempting to write into an array that cannot be written");
1998                }
1999            }
2000        }
2001        _ => panic!("typechecking should make sure this was a PropertyReference"),
2002    }
2003}
2004
2005pub fn load_property(component: InstanceRef, element: &ElementRc, name: &str) -> Result<Value, ()> {
2006    load_property_helper(&ComponentInstance::InstanceRef(component), element, name)
2007}
2008
2009fn load_property_helper(
2010    component_instance: &ComponentInstance,
2011    element: &ElementRc,
2012    name: &str,
2013) -> Result<Value, ()> {
2014    generativity::make_guard!(guard);
2015    match enclosing_component_instance_for_element(element, component_instance, guard) {
2016        ComponentInstance::InstanceRef(enclosing_component) => {
2017            let element = element.borrow();
2018            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2019            {
2020                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
2021                    return unsafe {
2022                        x.prop.get(Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset)))
2023                    };
2024                } else if enclosing_component.description.original.is_global() {
2025                    return Err(());
2026                }
2027            };
2028            let item_info = enclosing_component
2029                .description
2030                .items
2031                .get(element.id.as_str())
2032                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, name));
2033            core::mem::drop(element);
2034            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2035            Ok(item_info.rtti.properties.get(name).ok_or(())?.get(item))
2036        }
2037        ComponentInstance::GlobalComponent(glob) => glob.as_ref().get_property(name),
2038    }
2039}
2040
2041pub fn store_property(
2042    component_instance: InstanceRef,
2043    element: &ElementRc,
2044    name: &str,
2045    mut value: Value,
2046) -> Result<(), SetPropertyError> {
2047    generativity::make_guard!(guard);
2048    match enclosing_component_instance_for_element(
2049        element,
2050        &ComponentInstance::InstanceRef(component_instance),
2051        guard,
2052    ) {
2053        ComponentInstance::InstanceRef(enclosing_component) => {
2054            let maybe_animation = match element.borrow().bindings.get(name) {
2055                Some(b) => crate::dynamic_item_tree::animation_for_property(
2056                    enclosing_component,
2057                    &b.borrow().animation,
2058                ),
2059                None => {
2060                    crate::dynamic_item_tree::animation_for_property(enclosing_component, &None)
2061                }
2062            };
2063
2064            let component = element.borrow().enclosing_component.upgrade().unwrap();
2065            if element.borrow().id == component.root_element.borrow().id {
2066                if let Some(x) = enclosing_component.description.custom_properties.get(name) {
2067                    if let Some(orig_decl) = enclosing_component
2068                        .description
2069                        .original
2070                        .root_element
2071                        .borrow()
2072                        .property_declarations
2073                        .get(name)
2074                    {
2075                        // Do an extra type checking because PropertyInfo::set won't do it for custom structures or array
2076                        if !check_value_type(&mut value, &orig_decl.property_type) {
2077                            return Err(SetPropertyError::WrongType);
2078                        }
2079                    }
2080                    unsafe {
2081                        let p = Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset));
2082                        return x
2083                            .prop
2084                            .set(p, value, maybe_animation.as_animation())
2085                            .map_err(|()| SetPropertyError::WrongType);
2086                    }
2087                } else if enclosing_component.description.original.is_global() {
2088                    return Err(SetPropertyError::NoSuchProperty);
2089                }
2090            };
2091            let item_info = &enclosing_component.description.items[element.borrow().id.as_str()];
2092            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2093            let p = &item_info.rtti.properties.get(name).ok_or(SetPropertyError::NoSuchProperty)?;
2094            p.set(item, value, maybe_animation.as_animation())
2095                .map_err(|()| SetPropertyError::WrongType)?;
2096        }
2097        ComponentInstance::GlobalComponent(glob) => {
2098            glob.as_ref().set_property(name, value)?;
2099        }
2100    }
2101    Ok(())
2102}
2103
2104/// Return true if the Value can be used for a property of the given type
2105fn check_value_type(value: &mut Value, ty: &Type) -> bool {
2106    match ty {
2107        Type::Void => true,
2108        Type::Invalid
2109        | Type::InferredProperty
2110        | Type::InferredCallback
2111        | Type::Callback { .. }
2112        | Type::Function { .. }
2113        | Type::ElementReference => panic!("not valid property type"),
2114        Type::Float32 => matches!(value, Value::Number(_)),
2115        Type::Int32 => matches!(value, Value::Number(_)),
2116        Type::String => matches!(value, Value::String(_)),
2117        Type::Color => matches!(value, Value::Brush(_)),
2118        Type::UnitProduct(_)
2119        | Type::Duration
2120        | Type::PhysicalLength
2121        | Type::LogicalLength
2122        | Type::Rem
2123        | Type::Angle
2124        | Type::Percent => matches!(value, Value::Number(_)),
2125        Type::Image => matches!(value, Value::Image(_)),
2126        Type::Bool => matches!(value, Value::Bool(_)),
2127        Type::Model => {
2128            matches!(value, Value::Model(_) | Value::Bool(_) | Value::Number(_))
2129        }
2130        Type::PathData => matches!(value, Value::PathData(_)),
2131        Type::Easing => matches!(value, Value::EasingCurve(_)),
2132        Type::Brush => matches!(value, Value::Brush(_)),
2133        Type::Array(inner) => {
2134            matches!(value, Value::Model(m) if m.iter().all(|mut v| check_value_type(&mut v, inner)))
2135        }
2136        Type::Struct(s) => {
2137            let Value::Struct(str) = value else { return false };
2138            if !str
2139                .0
2140                .iter_mut()
2141                .all(|(k, v)| s.fields.get(k).is_some_and(|ty| check_value_type(v, ty)))
2142            {
2143                return false;
2144            }
2145            for (k, v) in &s.fields {
2146                str.0.entry(k.clone()).or_insert_with(|| default_value_for_type(v));
2147            }
2148            true
2149        }
2150        Type::Enumeration(en) => {
2151            matches!(value, Value::EnumerationValue(name, _) if name == en.name.as_str())
2152        }
2153        Type::Keys => matches!(value, Value::Keys(_)),
2154        Type::LayoutCache => matches!(value, Value::LayoutCache(_)),
2155        Type::ArrayOfU16 => matches!(value, Value::ArrayOfU16(_)),
2156        Type::ComponentFactory => matches!(value, Value::ComponentFactory(_)),
2157        Type::StyledText => matches!(value, Value::StyledText(_)),
2158        Type::DataTransfer => matches!(value, Value::DataTransfer(_)),
2159    }
2160}
2161
2162pub(crate) fn invoke_callback(
2163    component_instance: &ComponentInstance,
2164    element: &ElementRc,
2165    callback_name: &SmolStr,
2166    args: &[Value],
2167) -> Option<Value> {
2168    generativity::make_guard!(guard);
2169    match enclosing_component_instance_for_element(element, component_instance, guard) {
2170        ComponentInstance::InstanceRef(enclosing_component) => {
2171            // Keep the component alive while the callback runs: the callback may close the popup
2172            // that owns this callback, and Callback::call() restores the handler after returning.
2173            let _component_guard = enclosing_component
2174                .self_weak()
2175                .get()
2176                .expect("component self weak must be initialized before invoking callbacks")
2177                .upgrade()
2178                .expect("component must be alive while invoking callbacks");
2179            let description = enclosing_component.description;
2180            let element = element.borrow();
2181            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2182            {
2183                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2184                    if let Some(tracker_offset) = description.callback_trackers.get(callback_name) {
2185                        tracker_offset.apply_pin(enclosing_component.instance).get();
2186                    }
2187                    let callback = callback_offset.apply(&*enclosing_component.instance);
2188                    let res = callback.call(args);
2189                    return Some(if res != Value::Void {
2190                        res
2191                    } else if let Some(Type::Callback(callback)) = description
2192                        .original
2193                        .root_element
2194                        .borrow()
2195                        .property_declarations
2196                        .get(callback_name)
2197                        .map(|d| &d.property_type)
2198                    {
2199                        // If the callback was not set, the return value will be Value::Void, but we need
2200                        // to make sure that the value is actually of the right type as returned by the
2201                        // callback, otherwise we will get panics later
2202                        default_value_for_type(&callback.return_type)
2203                    } else {
2204                        res
2205                    });
2206                } else if enclosing_component.description.original.is_global() {
2207                    return None;
2208                }
2209            };
2210            let item_info = &description.items[element.id.as_str()];
2211            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2212            item_info
2213                .rtti
2214                .callbacks
2215                .get(callback_name.as_str())
2216                .map(|callback| callback.call(item, args))
2217        }
2218        ComponentInstance::GlobalComponent(global) => {
2219            Some(global.as_ref().invoke_callback(callback_name, args).unwrap())
2220        }
2221    }
2222}
2223
2224pub(crate) fn set_callback_handler(
2225    component_instance: &ComponentInstance,
2226    element: &ElementRc,
2227    callback_name: &str,
2228    handler: CallbackHandler,
2229) -> Result<(), ()> {
2230    generativity::make_guard!(guard);
2231    match enclosing_component_instance_for_element(element, component_instance, guard) {
2232        ComponentInstance::InstanceRef(enclosing_component) => {
2233            let description = enclosing_component.description;
2234            let element = element.borrow();
2235            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2236            {
2237                if let Some(callback_offset) = description.custom_callbacks.get(callback_name) {
2238                    let callback = callback_offset.apply(&*enclosing_component.instance);
2239                    callback.set_handler(handler);
2240                    if let Some(tracker_offset) = description.callback_trackers.get(callback_name) {
2241                        tracker_offset.apply_pin(enclosing_component.instance).mark_dirty();
2242                    }
2243                    return Ok(());
2244                } else if enclosing_component.description.original.is_global() {
2245                    return Err(());
2246                }
2247            };
2248            let item_info = &description.items[element.id.as_str()];
2249            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2250            if let Some(callback) = item_info.rtti.callbacks.get(callback_name) {
2251                callback.set_handler(item, handler);
2252                Ok(())
2253            } else {
2254                Err(())
2255            }
2256        }
2257        ComponentInstance::GlobalComponent(global) => {
2258            global.as_ref().set_callback_handler(callback_name, handler)
2259        }
2260    }
2261}
2262
2263/// Invoke the function.
2264///
2265/// Return None if the function don't exist
2266pub(crate) fn call_function(
2267    component_instance: &ComponentInstance,
2268    element: &ElementRc,
2269    function_name: &str,
2270    args: Vec<Value>,
2271) -> Option<Value> {
2272    generativity::make_guard!(guard);
2273    match enclosing_component_instance_for_element(element, component_instance, guard) {
2274        ComponentInstance::InstanceRef(c) => {
2275            // Keep the component alive while the function runs: the function may close the popup
2276            // that owns this function or callbacks it invokes.
2277            let _component_guard = c
2278                .self_weak()
2279                .get()
2280                .expect("component self weak must be initialized before invoking functions")
2281                .upgrade()
2282                .expect("component must be alive while invoking functions");
2283            let mut ctx = EvalLocalContext::from_function_arguments(c, args);
2284            eval_expression(
2285                &element.borrow().bindings.get(function_name)?.borrow().expression,
2286                &mut ctx,
2287            )
2288            .into()
2289        }
2290        ComponentInstance::GlobalComponent(g) => g.as_ref().eval_function(function_name, args).ok(),
2291    }
2292}
2293
2294/// Return the component instance which hold the given element.
2295/// Does not take in account the global component.
2296pub fn enclosing_component_for_element<'a, 'old_id, 'new_id>(
2297    element: &'a ElementRc,
2298    component: InstanceRef<'a, 'old_id>,
2299    _guard: generativity::Guard<'new_id>,
2300) -> InstanceRef<'a, 'new_id> {
2301    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2302    if Rc::ptr_eq(enclosing, &component.description.original) {
2303        // Safety: new_id is an unique id
2304        unsafe {
2305            std::mem::transmute::<InstanceRef<'a, 'old_id>, InstanceRef<'a, 'new_id>>(component)
2306        }
2307    } else {
2308        assert!(!enclosing.is_global());
2309        // Safety: this is the only place we use this 'static lifetime in this function and nothing is returned with it
2310        // For some reason we can't make a new guard here because the compiler thinks we are returning that
2311        // (it assumes that the 'id must outlive 'a , which is not true)
2312        let static_guard = unsafe { generativity::Guard::new(generativity::Id::<'static>::new()) };
2313
2314        let parent_instance = component
2315            .parent_instance(static_guard)
2316            .expect("accessing deleted parent (issue #6426)");
2317        enclosing_component_for_element(element, parent_instance, _guard)
2318    }
2319}
2320
2321/// Return the component instance which hold the given element.
2322/// The difference with enclosing_component_for_element is that it takes the GlobalComponent into account.
2323pub(crate) fn enclosing_component_instance_for_element<'a, 'new_id>(
2324    element: &'a ElementRc,
2325    component_instance: &ComponentInstance<'a, '_>,
2326    guard: generativity::Guard<'new_id>,
2327) -> ComponentInstance<'a, 'new_id> {
2328    let enclosing = &element.borrow().enclosing_component.upgrade().unwrap();
2329    match component_instance {
2330        ComponentInstance::InstanceRef(component) => {
2331            if enclosing.is_global() && !Rc::ptr_eq(enclosing, &component.description.original) {
2332                ComponentInstance::GlobalComponent(
2333                    component
2334                        .description
2335                        .extra_data_offset
2336                        .apply(component.instance.get_ref())
2337                        .globals
2338                        .get()
2339                        .unwrap()
2340                        .get(enclosing.root_element.borrow().id.as_str())
2341                        .unwrap(),
2342                )
2343            } else {
2344                ComponentInstance::InstanceRef(enclosing_component_for_element(
2345                    element, *component, guard,
2346                ))
2347            }
2348        }
2349        ComponentInstance::GlobalComponent(global) => {
2350            //assert!(Rc::ptr_eq(enclosing, &global.component));
2351            ComponentInstance::GlobalComponent(global.clone())
2352        }
2353    }
2354}
2355
2356pub fn new_struct_with_bindings<ElementType: 'static + Default + corelib::rtti::BuiltinItem>(
2357    bindings: &i_slint_compiler::object_tree::BindingsMap,
2358    local_context: &mut EvalLocalContext,
2359) -> ElementType {
2360    let mut element = ElementType::default();
2361    for (prop, info) in ElementType::fields::<Value>().into_iter() {
2362        if let Some(binding) = &bindings.get(prop) {
2363            let value = eval_expression(&binding.borrow(), local_context);
2364            info.set_field(&mut element, value).unwrap();
2365        }
2366    }
2367    element
2368}
2369
2370fn convert_from_lyon_path<'a>(
2371    events_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2372    points_it: impl IntoIterator<Item = &'a i_slint_compiler::expression_tree::Expression>,
2373    local_context: &mut EvalLocalContext,
2374) -> PathData {
2375    let events = events_it
2376        .into_iter()
2377        .map(|event_expr| eval_expression(event_expr, local_context).try_into().unwrap())
2378        .collect::<SharedVector<_>>();
2379
2380    let points = points_it
2381        .into_iter()
2382        .map(|point_expr| {
2383            let point_value = eval_expression(point_expr, local_context);
2384            let point_struct: Struct = point_value.try_into().unwrap();
2385            let mut point = i_slint_core::graphics::Point::default();
2386            let x: f64 = point_struct.get_field("x").unwrap().clone().try_into().unwrap();
2387            let y: f64 = point_struct.get_field("y").unwrap().clone().try_into().unwrap();
2388            point.x = x as _;
2389            point.y = y as _;
2390            point
2391        })
2392        .collect::<SharedVector<_>>();
2393
2394    PathData::Events(events, points)
2395}
2396
2397pub fn convert_path(path: &ExprPath, local_context: &mut EvalLocalContext) -> PathData {
2398    match path {
2399        ExprPath::Elements(elements) => PathData::Elements(
2400            elements
2401                .iter()
2402                .map(|element| convert_path_element(element, local_context))
2403                .collect::<SharedVector<PathElement>>(),
2404        ),
2405        ExprPath::Events(events, points) => {
2406            convert_from_lyon_path(events.iter(), points.iter(), local_context)
2407        }
2408        ExprPath::Commands(commands) => {
2409            if let Value::String(commands) = eval_expression(commands, local_context) {
2410                PathData::Commands(commands)
2411            } else {
2412                panic!("binding to path commands does not evaluate to string");
2413            }
2414        }
2415    }
2416}
2417
2418fn convert_path_element(
2419    expr_element: &ExprPathElement,
2420    local_context: &mut EvalLocalContext,
2421) -> PathElement {
2422    match expr_element.element_type.native_class.class_name.as_str() {
2423        "MoveTo" => {
2424            PathElement::MoveTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2425        }
2426        "LineTo" => {
2427            PathElement::LineTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2428        }
2429        "ArcTo" => {
2430            PathElement::ArcTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2431        }
2432        "CubicTo" => {
2433            PathElement::CubicTo(new_struct_with_bindings(&expr_element.bindings, local_context))
2434        }
2435        "QuadraticTo" => PathElement::QuadraticTo(new_struct_with_bindings(
2436            &expr_element.bindings,
2437            local_context,
2438        )),
2439        "Close" => PathElement::Close,
2440        _ => panic!(
2441            "Cannot create unsupported path element {}",
2442            expr_element.element_type.native_class.class_name
2443        ),
2444    }
2445}
2446
2447/// Create a value suitable as the default value of a given type
2448pub fn default_value_for_type(ty: &Type) -> Value {
2449    match ty {
2450        Type::Float32 | Type::Int32 => Value::Number(0.),
2451        Type::String => Value::String(Default::default()),
2452        Type::Color | Type::Brush => Value::Brush(Default::default()),
2453        Type::Duration | Type::Angle | Type::PhysicalLength | Type::LogicalLength | Type::Rem => {
2454            Value::Number(0.)
2455        }
2456        Type::Image => Value::Image(Default::default()),
2457        Type::Bool => Value::Bool(false),
2458        Type::Callback { .. } => Value::Void,
2459        Type::Struct(s) => Value::Struct(
2460            s.fields
2461                .iter()
2462                .map(|(n, t)| (n.to_string(), default_value_for_type(t)))
2463                .collect::<Struct>(),
2464        ),
2465        Type::Array(_) | Type::Model => Value::Model(Default::default()),
2466        Type::Percent => Value::Number(0.),
2467        Type::Enumeration(e) => Value::EnumerationValue(
2468            e.name.to_string(),
2469            e.values.get(e.default_value).unwrap().to_string(),
2470        ),
2471        Type::Keys => Value::Keys(Default::default()),
2472        Type::DataTransfer => Value::DataTransfer(Default::default()),
2473        Type::Easing => Value::EasingCurve(Default::default()),
2474        Type::Void | Type::Invalid => Value::Void,
2475        Type::UnitProduct(_) => Value::Number(0.),
2476        Type::PathData => Value::PathData(Default::default()),
2477        Type::LayoutCache => Value::LayoutCache(Default::default()),
2478        Type::ArrayOfU16 => Value::ArrayOfU16(Default::default()),
2479        Type::ComponentFactory => Value::ComponentFactory(Default::default()),
2480        Type::InferredProperty
2481        | Type::InferredCallback
2482        | Type::ElementReference
2483        | Type::Function { .. } => {
2484            panic!("There can't be such property")
2485        }
2486        Type::StyledText => Value::StyledText(Default::default()),
2487    }
2488}
2489
2490fn menu_item_tree_properties(
2491    context_menu_item_tree: vtable::VRc<i_slint_core::menus::MenuVTable, MenuFromItemTree>,
2492) -> (Box<dyn Fn() -> Value>, CallbackHandler, CallbackHandler) {
2493    let context_menu_item_tree_ = context_menu_item_tree.clone();
2494    let entries = Box::new(move || {
2495        let mut entries = SharedVector::default();
2496        context_menu_item_tree_.sub_menu(None, &mut entries);
2497        Value::Model(ModelRc::new(VecModel::from(
2498            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2499        )))
2500    });
2501    let context_menu_item_tree_ = context_menu_item_tree.clone();
2502    let sub_menu = Box::new(move |args: &[Value]| -> Value {
2503        let mut entries = SharedVector::default();
2504        context_menu_item_tree_.sub_menu(Some(&args[0].clone().try_into().unwrap()), &mut entries);
2505        Value::Model(ModelRc::new(VecModel::from(
2506            entries.into_iter().map(Value::from).collect::<Vec<_>>(),
2507        )))
2508    });
2509    let activated = Box::new(move |args: &[Value]| -> Value {
2510        context_menu_item_tree.activate(&args[0].clone().try_into().unwrap());
2511        Value::Void
2512    });
2513    (entries, sub_menu, activated)
2514}