Skip to main content

slint_interpreter/
eval.rs

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