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