Skip to main content

slint_interpreter/
dynamic_item_tree.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::{CompilationResult, ComponentDefinition, Value};
5use crate::global_component::CompiledGlobalCollection;
6use crate::{dynamic_type, eval};
7use core::ffi::c_void;
8use core::ptr::NonNull;
9use dynamic_type::{Instance, InstanceBox};
10use i_slint_compiler::expression_tree::{Expression, NamedReference, TwoWayBinding};
11use i_slint_compiler::langtype::{BuiltinStruct, StructName, Type};
12use i_slint_compiler::object_tree::{ElementRc, ElementWeak, TransitionDirection};
13use i_slint_compiler::{CompilerConfiguration, generator, object_tree, parser};
14use i_slint_compiler::{diagnostics::BuildDiagnostics, object_tree::PropertyDeclaration};
15use i_slint_core::accessibility::{
16    AccessibilityAction, AccessibleStringProperty, SupportedAccessibilityAction,
17};
18use i_slint_core::api::LogicalPosition;
19use i_slint_core::component_factory::ComponentFactory;
20use i_slint_core::input::Keys;
21use i_slint_core::item_tree::{
22    IndexRange, ItemRc, ItemTree, ItemTreeNode, ItemTreeRef, ItemTreeRefPin, ItemTreeVTable,
23    ItemTreeWeak, ItemVisitorRefMut, ItemVisitorVTable, ItemWeak, TraversalOrder,
24    VisitChildrenResult,
25};
26use i_slint_core::items::{
27    AccessibleRole, ItemRef, ItemVTable, PopupClosePolicy, PropertyAnimation,
28};
29use i_slint_core::layout::{LayoutInfo, LayoutItemInfo, Orientation};
30use i_slint_core::lengths::{LogicalLength, LogicalRect};
31use i_slint_core::menus::MenuFromItemTree;
32use i_slint_core::model::{ModelRc, RepeatedItemTree, Repeater};
33use i_slint_core::platform::PlatformError;
34use i_slint_core::properties::{ChangeTracker, InterpolatedPropertyValue};
35use i_slint_core::rtti::{self, AnimatedBindingKind, FieldOffset, PropertyInfo};
36use i_slint_core::slice::Slice;
37use i_slint_core::styled_text::StyledText;
38use i_slint_core::timers::Timer;
39use i_slint_core::window::{WindowAdapterRc, WindowInner, WindowKind};
40use i_slint_core::{Brush, Color, DataTransfer, Property, SharedString, SharedVector};
41#[cfg(feature = "internal")]
42use itertools::Either;
43use once_cell::unsync::{Lazy, OnceCell};
44use smol_str::{SmolStr, ToSmolStr};
45use std::collections::BTreeMap;
46use std::collections::HashMap;
47use std::num::NonZeroU32;
48use std::rc::Weak;
49use std::{pin::Pin, rc::Rc};
50
51pub const SPECIAL_PROPERTY_INDEX: &str = "$index";
52pub const SPECIAL_PROPERTY_MODEL_DATA: &str = "$model_data";
53
54pub(crate) type CallbackHandler = Box<dyn Fn(&[Value]) -> Value>;
55
56pub struct ItemTreeBox<'id> {
57    instance: InstanceBox<'id>,
58    description: Rc<ItemTreeDescription<'id>>,
59}
60
61impl<'id> ItemTreeBox<'id> {
62    /// Borrow this instance as a `Pin<ItemTreeRef>`
63    pub fn borrow(&self) -> ItemTreeRefPin<'_> {
64        self.borrow_instance().borrow()
65    }
66
67    /// Safety: the lifetime is not unique
68    pub fn description(&self) -> Rc<ItemTreeDescription<'id>> {
69        self.description.clone()
70    }
71
72    pub fn borrow_instance<'a>(&'a self) -> InstanceRef<'a, 'id> {
73        InstanceRef { instance: self.instance.as_pin_ref(), description: &self.description }
74    }
75
76    pub fn window_adapter_ref(&self) -> Result<&WindowAdapterRc, PlatformError> {
77        let root_weak = vtable::VWeak::into_dyn(self.borrow_instance().root_weak().clone());
78        InstanceRef::get_or_init_window_adapter_ref(
79            &self.description,
80            root_weak,
81            true,
82            self.instance.as_pin_ref().get_ref(),
83        )
84    }
85}
86
87pub(crate) type ErasedItemTreeBoxWeak = vtable::VWeak<ItemTreeVTable, ErasedItemTreeBox>;
88
89pub(crate) struct ItemWithinItemTree {
90    offset: usize,
91    pub(crate) rtti: Rc<ItemRTTI>,
92    elem: ElementRc,
93}
94
95impl ItemWithinItemTree {
96    /// Safety: the pointer must be a dynamic item tree which is coming from the same description as Self
97    pub(crate) unsafe fn item_from_item_tree(
98        &self,
99        mem: *const u8,
100    ) -> Pin<vtable::VRef<'_, ItemVTable>> {
101        unsafe {
102            Pin::new_unchecked(vtable::VRef::from_raw(
103                NonNull::from(self.rtti.vtable),
104                NonNull::new(mem.add(self.offset) as _).unwrap(),
105            ))
106        }
107    }
108
109    pub(crate) fn item_index(&self) -> u32 {
110        *self.elem.borrow().item_index.get().unwrap()
111    }
112}
113
114pub(crate) struct PropertiesWithinComponent {
115    pub(crate) offset: usize,
116    pub(crate) prop: Box<dyn PropertyInfo<u8, Value>>,
117}
118
119pub(crate) struct RepeaterWithinItemTree<'par_id, 'sub_id> {
120    /// The description of the items to repeat
121    pub(crate) item_tree_to_repeat: Rc<ItemTreeDescription<'sub_id>>,
122    /// The model
123    pub(crate) model: Expression,
124    /// Offset of the `Repeater`
125    offset: FieldOffset<Instance<'par_id>, Repeater<ErasedItemTreeBox>>,
126    /// When true, it is representing a `if`, instead of a `for`.
127    /// Based on [`i_slint_compiler::object_tree::RepeatedElementInfo::is_conditional_element`]
128    is_conditional: bool,
129}
130
131impl RepeatedItemTree for ErasedItemTreeBox {
132    type Data = Value;
133
134    fn update(&self, index: usize, data: Self::Data) {
135        generativity::make_guard!(guard);
136        let s = self.unerase(guard);
137        let is_repeated = s.description.original.parent_element().is_some_and(|p| {
138            p.borrow().repeated.as_ref().is_some_and(|r| !r.is_conditional_element)
139        });
140        if is_repeated {
141            s.description.set_property(s.borrow(), SPECIAL_PROPERTY_INDEX, index.into()).unwrap();
142            s.description.set_property(s.borrow(), SPECIAL_PROPERTY_MODEL_DATA, data).unwrap();
143        }
144    }
145
146    fn init(&self) {
147        self.run_setup_code();
148    }
149
150    fn listview_layout(self: Pin<&Self>, offset_y: &mut LogicalLength) -> LogicalLength {
151        generativity::make_guard!(guard);
152        let s = self.unerase(guard);
153
154        let geom = s.description.original.root_element.borrow().geometry_props.clone().unwrap();
155
156        crate::eval::store_property(
157            s.borrow_instance(),
158            &geom.y.element(),
159            geom.y.name(),
160            Value::Number(offset_y.get() as f64),
161        )
162        .expect("cannot set y");
163
164        let h: LogicalLength = crate::eval::load_property(
165            s.borrow_instance(),
166            &geom.height.element(),
167            geom.height.name(),
168        )
169        .expect("missing height")
170        .try_into()
171        .expect("height not the right type");
172
173        *offset_y += h;
174        LogicalLength::new(self.borrow().as_ref().layout_info(Orientation::Horizontal).min)
175    }
176
177    fn layout_item_info(
178        self: Pin<&Self>,
179        o: Orientation,
180        child_index: Option<usize>,
181    ) -> LayoutItemInfo {
182        generativity::make_guard!(guard);
183        let s = self.unerase(guard);
184
185        if let Some(index) = child_index {
186            let instance_ref = s.borrow_instance();
187            let root_element = &s.description.original.root_element;
188
189            let children = root_element.borrow().children.clone();
190            if let Some(child_elem) = children.get(index) {
191                // Get the layout info for this child element
192                let layout_info = crate::eval_layout::get_layout_info(
193                    child_elem,
194                    instance_ref,
195                    &instance_ref.window_adapter(),
196                    crate::eval_layout::from_runtime(o),
197                );
198                return LayoutItemInfo { constraint: layout_info };
199            } else {
200                panic!(
201                    "child_index {} out of bounds for repeated item {}",
202                    index,
203                    s.description().id()
204                );
205            }
206        }
207
208        LayoutItemInfo { constraint: self.borrow().as_ref().layout_info(o) }
209    }
210
211    fn flexbox_layout_item_info(
212        self: Pin<&Self>,
213        o: Orientation,
214        child_index: Option<usize>,
215    ) -> i_slint_core::layout::FlexboxLayoutItemInfo {
216        generativity::make_guard!(guard);
217        let s = self.unerase(guard);
218        let instance_ref = s.borrow_instance();
219        let root_element = &s.description.original.root_element;
220
221        let load_f32 = |name: &str| -> f32 {
222            eval::load_property(instance_ref, root_element, name)
223                .ok()
224                .and_then(|v| v.try_into().ok())
225                .unwrap_or(0.0)
226        };
227
228        let flex_grow = load_f32("flex-grow");
229        let flex_shrink = load_f32("flex-shrink");
230        let flex_basis = if root_element.borrow().bindings.contains_key("flex-basis") {
231            load_f32("flex-basis")
232        } else {
233            -1.0
234        };
235        let flex_align_self = eval::load_property(instance_ref, root_element, "flex-align-self")
236            .ok()
237            .and_then(|v| v.try_into().ok())
238            .unwrap_or(i_slint_core::items::FlexboxLayoutAlignSelf::Auto);
239        let flex_order = load_f32("flex-order") as i32;
240
241        i_slint_core::layout::FlexboxLayoutItemInfo {
242            constraint: self.layout_item_info(o, child_index).constraint,
243            flex_grow,
244            flex_shrink,
245            flex_basis,
246            flex_align_self,
247            flex_order,
248        }
249    }
250}
251
252impl ItemTree for ErasedItemTreeBox {
253    fn visit_children_item(
254        self: Pin<&Self>,
255        index: isize,
256        order: TraversalOrder,
257        visitor: ItemVisitorRefMut,
258    ) -> VisitChildrenResult {
259        self.borrow().as_ref().visit_children_item(index, order, visitor)
260    }
261
262    fn layout_info(self: Pin<&Self>, orientation: Orientation) -> i_slint_core::layout::LayoutInfo {
263        self.borrow().as_ref().layout_info(orientation)
264    }
265
266    fn ensure_instantiated(self: Pin<&Self>) -> bool {
267        self.borrow().as_ref().ensure_instantiated()
268    }
269
270    fn get_item_tree(self: Pin<&Self>) -> Slice<'_, ItemTreeNode> {
271        get_item_tree(self.get_ref().borrow())
272    }
273
274    fn get_item_ref(self: Pin<&Self>, index: u32) -> Pin<ItemRef<'_>> {
275        // We're having difficulties transferring the lifetime to a pinned reference
276        // to the other ItemTreeVTable with the same life time. So skip the vtable
277        // indirection and call our implementation directly.
278        unsafe { get_item_ref(self.get_ref().borrow(), index) }
279    }
280
281    fn get_subtree_range(self: Pin<&Self>, index: u32) -> IndexRange {
282        self.borrow().as_ref().get_subtree_range(index)
283    }
284
285    fn get_subtree(self: Pin<&Self>, index: u32, subindex: usize, result: &mut ItemTreeWeak) {
286        self.borrow().as_ref().get_subtree(index, subindex, result);
287    }
288
289    fn parent_node(self: Pin<&Self>, result: &mut ItemWeak) {
290        self.borrow().as_ref().parent_node(result)
291    }
292
293    fn embed_component(
294        self: core::pin::Pin<&Self>,
295        parent_component: &ItemTreeWeak,
296        item_tree_index: u32,
297    ) -> bool {
298        self.borrow().as_ref().embed_component(parent_component, item_tree_index)
299    }
300
301    fn subtree_index(self: Pin<&Self>) -> usize {
302        self.borrow().as_ref().subtree_index()
303    }
304
305    fn item_geometry(self: Pin<&Self>, item_index: u32) -> i_slint_core::lengths::LogicalRect {
306        self.borrow().as_ref().item_geometry(item_index)
307    }
308
309    fn accessible_role(self: Pin<&Self>, index: u32) -> AccessibleRole {
310        self.borrow().as_ref().accessible_role(index)
311    }
312
313    fn accessible_string_property(
314        self: Pin<&Self>,
315        index: u32,
316        what: AccessibleStringProperty,
317        result: &mut SharedString,
318    ) -> bool {
319        self.borrow().as_ref().accessible_string_property(index, what, result)
320    }
321
322    fn window_adapter(self: Pin<&Self>, do_create: bool, result: &mut Option<WindowAdapterRc>) {
323        self.borrow().as_ref().window_adapter(do_create, result);
324    }
325
326    fn accessibility_action(self: core::pin::Pin<&Self>, index: u32, action: &AccessibilityAction) {
327        self.borrow().as_ref().accessibility_action(index, action)
328    }
329
330    fn supported_accessibility_actions(
331        self: core::pin::Pin<&Self>,
332        index: u32,
333    ) -> SupportedAccessibilityAction {
334        self.borrow().as_ref().supported_accessibility_actions(index)
335    }
336
337    fn item_element_infos(
338        self: core::pin::Pin<&Self>,
339        index: u32,
340        result: &mut SharedString,
341    ) -> bool {
342        self.borrow().as_ref().item_element_infos(index, result)
343    }
344}
345
346i_slint_core::ItemTreeVTable_static!(static COMPONENT_BOX_VT for ErasedItemTreeBox);
347
348impl Drop for ErasedItemTreeBox {
349    fn drop(&mut self) {
350        generativity::make_guard!(guard);
351        let unerase = self.unerase(guard);
352        let instance_ref = unerase.borrow_instance();
353
354        let maybe_window_adapter = instance_ref
355            .description
356            .extra_data_offset
357            .apply(instance_ref.as_ref())
358            .globals
359            .get()
360            .and_then(|globals| globals.window_adapter())
361            .and_then(|wa| wa.get());
362        if let Some(window_adapter) = maybe_window_adapter {
363            i_slint_core::item_tree::unregister_item_tree(
364                instance_ref.instance,
365                vtable::VRef::new(self),
366                instance_ref.description.item_array.as_slice(),
367                window_adapter,
368            );
369        }
370    }
371}
372
373pub type DynamicComponentVRc = vtable::VRc<ItemTreeVTable, ErasedItemTreeBox>;
374
375#[derive(Default)]
376pub(crate) struct ComponentExtraData {
377    pub(crate) globals: OnceCell<crate::global_component::GlobalStorage>,
378    pub(crate) self_weak: OnceCell<ErasedItemTreeBoxWeak>,
379    pub(crate) embedding_position: OnceCell<(ItemTreeWeak, u32)>,
380}
381
382struct ErasedRepeaterWithinComponent<'id>(RepeaterWithinItemTree<'id, 'static>);
383impl<'id, 'sub_id> From<RepeaterWithinItemTree<'id, 'sub_id>>
384    for ErasedRepeaterWithinComponent<'id>
385{
386    fn from(from: RepeaterWithinItemTree<'id, 'sub_id>) -> Self {
387        // Safety: this is safe as we erase the sub_id lifetime.
388        // As long as when we get it back we get an unique lifetime with ErasedRepeaterWithinComponent::unerase
389        Self(unsafe {
390            core::mem::transmute::<
391                RepeaterWithinItemTree<'id, 'sub_id>,
392                RepeaterWithinItemTree<'id, 'static>,
393            >(from)
394        })
395    }
396}
397impl<'id> ErasedRepeaterWithinComponent<'id> {
398    pub fn unerase<'a, 'sub_id>(
399        &'a self,
400        _guard: generativity::Guard<'sub_id>,
401    ) -> &'a RepeaterWithinItemTree<'id, 'sub_id> {
402        // Safety: we just go from 'static to an unique lifetime
403        unsafe {
404            core::mem::transmute::<
405                &'a RepeaterWithinItemTree<'id, 'static>,
406                &'a RepeaterWithinItemTree<'id, 'sub_id>,
407            >(&self.0)
408        }
409    }
410
411    /// Return a repeater with a ItemTree with a 'static lifetime
412    ///
413    /// Safety: one should ensure that the inner ItemTree is not mixed with other inner ItemTree
414    unsafe fn get_untagged(&self) -> &RepeaterWithinItemTree<'id, 'static> {
415        &self.0
416    }
417}
418
419type Callback = i_slint_core::Callback<[Value], Value>;
420
421#[derive(Clone)]
422pub struct ErasedItemTreeDescription(Rc<ItemTreeDescription<'static>>);
423impl ErasedItemTreeDescription {
424    pub fn unerase<'a, 'id>(
425        &'a self,
426        _guard: generativity::Guard<'id>,
427    ) -> &'a Rc<ItemTreeDescription<'id>> {
428        // Safety: we just go from 'static to an unique lifetime
429        unsafe {
430            core::mem::transmute::<
431                &'a Rc<ItemTreeDescription<'static>>,
432                &'a Rc<ItemTreeDescription<'id>>,
433            >(&self.0)
434        }
435    }
436}
437impl<'id> From<Rc<ItemTreeDescription<'id>>> for ErasedItemTreeDescription {
438    fn from(from: Rc<ItemTreeDescription<'id>>) -> Self {
439        // Safety: We never access the ItemTreeDescription with the static lifetime, only after we unerase it
440        Self(unsafe {
441            core::mem::transmute::<Rc<ItemTreeDescription<'id>>, Rc<ItemTreeDescription<'static>>>(
442                from,
443            )
444        })
445    }
446}
447
448/// ItemTreeDescription is a representation of a ItemTree suitable for interpretation
449///
450/// It contains information about how to create and destroy the Component.
451/// Its first member is the ItemTreeVTable for generated instance, since it is a `#[repr(C)]`
452/// structure, it is valid to cast a pointer to the ItemTreeVTable back to a
453/// ItemTreeDescription to access the extra field that are needed at runtime
454#[repr(C)]
455pub struct ItemTreeDescription<'id> {
456    pub(crate) ct: ItemTreeVTable,
457    /// INVARIANT: both dynamic_type and item_tree have the same lifetime id. Here it is erased to 'static
458    dynamic_type: Rc<dynamic_type::TypeInfo<'id>>,
459    item_tree: Vec<ItemTreeNode>,
460    item_array:
461        Vec<vtable::VOffset<crate::dynamic_type::Instance<'id>, ItemVTable, vtable::AllowPin>>,
462    pub(crate) items: HashMap<SmolStr, ItemWithinItemTree>,
463    pub(crate) custom_properties: HashMap<SmolStr, PropertiesWithinComponent>,
464    pub(crate) custom_callbacks: HashMap<SmolStr, FieldOffset<Instance<'id>, Callback>>,
465    /// For each exported callback, a `Property<()>` that tracks when the handler changes.
466    /// Calling `get()` before invoking a callback registers a dependency; calling `mark_dirty()`
467    /// after setting a handler triggers re-evaluation of dependent bindings.
468    pub(crate) callback_trackers: HashMap<SmolStr, FieldOffset<Instance<'id>, Property<()>>>,
469    repeater: Vec<ErasedRepeaterWithinComponent<'id>>,
470    /// Map the Element::id of the repeater to the index in the `repeater` vec
471    pub repeater_names: HashMap<SmolStr, usize>,
472    /// Offset to a Option<ComponentPinRef>
473    pub(crate) parent_item_tree_offset:
474        Option<FieldOffset<Instance<'id>, OnceCell<ErasedItemTreeBoxWeak>>>,
475    pub(crate) root_offset: FieldOffset<Instance<'id>, OnceCell<ErasedItemTreeBoxWeak>>,
476    /// Offset of a ComponentExtraData
477    pub(crate) extra_data_offset: FieldOffset<Instance<'id>, ComponentExtraData>,
478    /// Keep the Rc alive
479    pub(crate) original: Rc<object_tree::Component>,
480    /// Maps from an item_id to the original element it came from
481    pub(crate) original_elements: Vec<ElementRc>,
482    /// Copy of original.root_element.property_declarations, without a guarded refcell
483    public_properties: BTreeMap<SmolStr, PropertyDeclaration>,
484    change_trackers: Option<(
485        FieldOffset<Instance<'id>, OnceCell<Vec<ChangeTracker>>>,
486        Vec<(NamedReference, Expression)>,
487    )>,
488    timers: Vec<FieldOffset<Instance<'id>, Timer>>,
489    /// Map of element IDs to their active popup's ID
490    popup_ids: std::cell::RefCell<HashMap<SmolStr, NonZeroU32>>,
491
492    pub(crate) popup_menu_description: PopupMenuDescription,
493
494    /// The collection of compiled globals
495    compiled_globals: Option<Rc<CompiledGlobalCollection>>,
496
497    /// The type loader, which will be available only on the top-most `ItemTreeDescription`.
498    /// All other `ItemTreeDescription`s have `None` here.
499    #[cfg(feature = "internal-highlight")]
500    pub(crate) type_loader:
501        std::cell::OnceCell<std::rc::Rc<i_slint_compiler::typeloader::TypeLoader>>,
502    /// The type loader, which will be available only on the top-most `ItemTreeDescription`.
503    /// All other `ItemTreeDescription`s have `None` here.
504    #[cfg(feature = "internal-highlight")]
505    pub(crate) raw_type_loader:
506        std::cell::OnceCell<Option<std::rc::Rc<i_slint_compiler::typeloader::TypeLoader>>>,
507
508    pub(crate) debug_handler: std::cell::RefCell<
509        Rc<dyn Fn(Option<&i_slint_compiler::diagnostics::SourceLocation>, &str)>,
510    >,
511}
512
513#[derive(Clone, derive_more::From)]
514pub(crate) enum PopupMenuDescription {
515    Rc(Rc<ErasedItemTreeDescription>),
516    Weak(Weak<ErasedItemTreeDescription>),
517}
518impl PopupMenuDescription {
519    pub fn unerase<'id>(&self, guard: generativity::Guard<'id>) -> Rc<ItemTreeDescription<'id>> {
520        match self {
521            PopupMenuDescription::Rc(rc) => rc.unerase(guard).clone(),
522            PopupMenuDescription::Weak(weak) => weak.upgrade().unwrap().unerase(guard).clone(),
523        }
524    }
525}
526
527fn internal_properties_to_public<'a>(
528    prop_iter: impl Iterator<Item = (&'a SmolStr, &'a PropertyDeclaration)> + 'a,
529) -> impl Iterator<
530    Item = (
531        SmolStr,
532        i_slint_compiler::langtype::Type,
533        i_slint_compiler::object_tree::PropertyVisibility,
534    ),
535> + 'a {
536    prop_iter.filter(|(_, v)| v.expose_in_public_api).map(|(s, v)| {
537        let name = v
538            .node
539            .as_ref()
540            .and_then(|n| {
541                n.child_node(parser::SyntaxKind::DeclaredIdentifier)
542                    .and_then(|n| n.child_token(parser::SyntaxKind::Identifier))
543            })
544            .map(|n| n.to_smolstr())
545            .unwrap_or_else(|| s.to_smolstr());
546        (name, v.property_type.clone(), v.visibility)
547    })
548}
549
550#[derive(Default)]
551pub enum WindowOptions {
552    #[default]
553    CreateNewWindow,
554    UseExistingWindow(WindowAdapterRc),
555    Embed {
556        parent_item_tree: ItemTreeWeak,
557        parent_item_tree_index: u32,
558    },
559}
560
561impl ItemTreeDescription<'_> {
562    /// The name of this Component as written in the .slint file
563    pub fn id(&self) -> &str {
564        self.original.id.as_str()
565    }
566
567    /// List of publicly declared properties or callbacks
568    ///
569    /// We try to preserve the dashes and underscore as written in the property declaration
570    pub fn properties(
571        &self,
572    ) -> impl Iterator<
573        Item = (
574            SmolStr,
575            i_slint_compiler::langtype::Type,
576            i_slint_compiler::object_tree::PropertyVisibility,
577        ),
578    > + '_ {
579        internal_properties_to_public(self.public_properties.iter())
580    }
581
582    /// List names of exported global singletons
583    pub fn global_names(&self) -> impl Iterator<Item = SmolStr> + '_ {
584        self.compiled_globals
585            .as_ref()
586            .expect("Root component should have globals")
587            .compiled_globals
588            .iter()
589            .filter(|g| g.visible_in_public_api())
590            .flat_map(|g| g.names().into_iter())
591    }
592
593    pub fn global_properties(
594        &self,
595        name: &str,
596    ) -> Option<
597        impl Iterator<
598            Item = (
599                SmolStr,
600                i_slint_compiler::langtype::Type,
601                i_slint_compiler::object_tree::PropertyVisibility,
602            ),
603        > + '_,
604    > {
605        let g = self.compiled_globals.as_ref().expect("Root component should have globals");
606        g.exported_globals_by_name
607            .get(&crate::normalize_identifier(name))
608            .and_then(|global_idx| g.compiled_globals.get(*global_idx))
609            .map(|global| internal_properties_to_public(global.public_properties()))
610    }
611
612    /// Instantiate a runtime ItemTree from this ItemTreeDescription
613    pub fn create(
614        self: Rc<Self>,
615        options: WindowOptions,
616    ) -> Result<DynamicComponentVRc, PlatformError> {
617        i_slint_backend_selector::with_platform(|_b| {
618            // Nothing to do, just make sure a backend was created
619            Ok(())
620        })?;
621
622        let instance = instantiate(self, None, None, Some(&options), Default::default());
623        if let WindowOptions::UseExistingWindow(existing_adapter) = options {
624            WindowInner::from_pub(existing_adapter.window())
625                .set_component(&vtable::VRc::into_dyn(instance.clone()));
626        }
627        instance.run_setup_code();
628        Ok(instance)
629    }
630
631    /// Set a value to property.
632    ///
633    /// Return an error if the property with this name does not exist,
634    /// or if the value is the wrong type.
635    /// Panics if the component is not an instance corresponding to this ItemTreeDescription,
636    pub fn set_property(
637        &self,
638        component: ItemTreeRefPin,
639        name: &str,
640        value: Value,
641    ) -> Result<(), crate::api::SetPropertyError> {
642        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
643            panic!("mismatch instance and vtable");
644        }
645        generativity::make_guard!(guard);
646        let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
647        if let Some(alias) = self
648            .original
649            .root_element
650            .borrow()
651            .property_declarations
652            .get(name)
653            .and_then(|d| d.is_alias.as_ref())
654        {
655            eval::store_property(c, &alias.element(), alias.name(), value)
656        } else {
657            eval::store_property(c, &self.original.root_element, name, value)
658        }
659    }
660
661    /// Set a binding to a property
662    ///
663    /// Returns an error if the instance does not corresponds to this ItemTreeDescription,
664    /// or if the property with this name does not exist in this component
665    pub fn set_binding(
666        &self,
667        component: ItemTreeRefPin,
668        name: &str,
669        binding: Box<dyn Fn() -> Value>,
670    ) -> Result<(), ()> {
671        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
672            return Err(());
673        }
674        let x = self.custom_properties.get(name).ok_or(())?;
675        unsafe {
676            x.prop
677                .set_binding(
678                    Pin::new_unchecked(&*component.as_ptr().add(x.offset)),
679                    binding,
680                    i_slint_core::rtti::AnimatedBindingKind::NotAnimated,
681                )
682                .unwrap()
683        };
684        Ok(())
685    }
686
687    /// Return the value of a property
688    ///
689    /// Returns an error if the component is not an instance corresponding to this ItemTreeDescription,
690    /// or if a callback with this name does not exist
691    pub fn get_property(&self, component: ItemTreeRefPin, name: &str) -> Result<Value, ()> {
692        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
693            return Err(());
694        }
695        generativity::make_guard!(guard);
696        // Safety: we just verified that the component has the right vtable
697        let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
698        if let Some(alias) = self
699            .original
700            .root_element
701            .borrow()
702            .property_declarations
703            .get(name)
704            .and_then(|d| d.is_alias.as_ref())
705        {
706            eval::load_property(c, &alias.element(), alias.name())
707        } else {
708            eval::load_property(c, &self.original.root_element, name)
709        }
710    }
711
712    /// Sets an handler for a callback
713    ///
714    /// Returns an error if the component is not an instance corresponding to this ItemTreeDescription,
715    /// or if the property with this name does not exist
716    pub fn set_callback_handler(
717        &self,
718        component: Pin<ItemTreeRef>,
719        name: &str,
720        handler: CallbackHandler,
721    ) -> Result<(), ()> {
722        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
723            return Err(());
724        }
725        if let Some(alias) = self
726            .original
727            .root_element
728            .borrow()
729            .property_declarations
730            .get(name)
731            .and_then(|d| d.is_alias.as_ref())
732        {
733            generativity::make_guard!(guard);
734            // Safety: we just verified that the component has the right vtable
735            let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
736            let inst = eval::ComponentInstance::InstanceRef(c);
737            eval::set_callback_handler(&inst, &alias.element(), alias.name(), handler)?
738        } else {
739            let x = self.custom_callbacks.get(name).ok_or(())?;
740            let inst = unsafe { &*(component.as_ptr() as *const dynamic_type::Instance) };
741            let sig = x.apply(inst);
742            sig.set_handler(handler);
743            if let Some(tracker_offset) = self.callback_trackers.get(name) {
744                tracker_offset.apply_pin(unsafe { Pin::new_unchecked(inst) }).mark_dirty();
745            }
746        }
747        Ok(())
748    }
749
750    /// Invoke the specified callback or function
751    ///
752    /// Returns an error if the component is not an instance corresponding to this ItemTreeDescription,
753    /// or if the callback with this name does not exist in this component
754    pub fn invoke(
755        &self,
756        component: ItemTreeRefPin,
757        name: &SmolStr,
758        args: &[Value],
759    ) -> Result<Value, ()> {
760        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
761            return Err(());
762        }
763        generativity::make_guard!(guard);
764        // Safety: we just verified that the component has the right vtable
765        let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
766        let borrow = self.original.root_element.borrow();
767        let decl = borrow.property_declarations.get(name).ok_or(())?;
768
769        let (elem, name) = if let Some(alias) = &decl.is_alias {
770            (alias.element(), alias.name())
771        } else {
772            (self.original.root_element.clone(), name)
773        };
774
775        let inst = eval::ComponentInstance::InstanceRef(c);
776
777        if matches!(&decl.property_type, Type::Function { .. }) {
778            eval::call_function(&inst, &elem, name, args.to_vec()).ok_or(())
779        } else {
780            eval::invoke_callback(&inst, &elem, name, args).ok_or(())
781        }
782    }
783
784    // Return the global with the given name
785    pub fn get_global(
786        &self,
787        component: ItemTreeRefPin,
788        global_name: &str,
789    ) -> Result<Pin<Rc<dyn crate::global_component::GlobalComponent>>, ()> {
790        if !core::ptr::eq((&self.ct) as *const _, component.get_vtable() as *const _) {
791            return Err(());
792        }
793        generativity::make_guard!(guard);
794        // Safety: we just verified that the component has the right vtable
795        let c = unsafe { InstanceRef::from_pin_ref(component, guard) };
796        let extra_data = c.description.extra_data_offset.apply(c.instance.get_ref());
797        let g = extra_data.globals.get().unwrap().get(global_name).clone();
798        g.ok_or(())
799    }
800
801    pub fn recursively_set_debug_handler(
802        &self,
803        handler: Rc<dyn Fn(Option<&i_slint_compiler::diagnostics::SourceLocation>, &str)>,
804    ) {
805        *self.debug_handler.borrow_mut() = handler.clone();
806
807        for r in &self.repeater {
808            generativity::make_guard!(guard);
809            r.unerase(guard).item_tree_to_repeat.recursively_set_debug_handler(handler.clone());
810        }
811    }
812}
813
814#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
815extern "C" fn visit_children_item(
816    component: ItemTreeRefPin,
817    index: isize,
818    order: TraversalOrder,
819    v: ItemVisitorRefMut,
820) -> VisitChildrenResult {
821    generativity::make_guard!(guard);
822    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
823    let comp_rc = instance_ref.self_weak().get().unwrap().upgrade().unwrap();
824    i_slint_core::item_tree::visit_item_tree(
825        instance_ref.instance,
826        &vtable::VRc::into_dyn(comp_rc),
827        get_item_tree(component).as_slice(),
828        index,
829        order,
830        v,
831        |_, order, visitor, index| {
832            if index as usize >= instance_ref.description.repeater.len() {
833                // Do nothing: We are ComponentContainer and Our parent already did all the work!
834                VisitChildrenResult::CONTINUE
835            } else {
836                generativity::make_guard!(guard);
837                let rep_in_comp = instance_ref.description.repeater[index as usize].unerase(guard);
838                let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
839                repeater.visit(order, visitor)
840            }
841        },
842    )
843}
844
845/// Information attached to a builtin item
846pub(crate) struct ItemRTTI {
847    vtable: &'static ItemVTable,
848    type_info: dynamic_type::StaticTypeInfo,
849    pub(crate) properties: HashMap<&'static str, Box<dyn eval::ErasedPropertyInfo>>,
850    pub(crate) callbacks: HashMap<&'static str, Box<dyn eval::ErasedCallbackInfo>>,
851}
852
853fn rtti_for<T: 'static + Default + rtti::BuiltinItem + vtable::HasStaticVTable<ItemVTable>>()
854-> (&'static str, Rc<ItemRTTI>) {
855    let rtti = ItemRTTI {
856        vtable: T::static_vtable(),
857        type_info: dynamic_type::StaticTypeInfo::new::<T>(),
858        properties: T::properties()
859            .into_iter()
860            .map(|(k, v)| (k, Box::new(v) as Box<dyn eval::ErasedPropertyInfo>))
861            .collect(),
862        callbacks: T::callbacks()
863            .into_iter()
864            .map(|(k, v)| (k, Box::new(v) as Box<dyn eval::ErasedCallbackInfo>))
865            .collect(),
866    };
867    (T::name(), Rc::new(rtti))
868}
869
870/// Create a ItemTreeDescription from a source.
871/// The path corresponding to the source need to be passed as well (path is used for diagnostics
872/// and loading relative assets)
873pub async fn load(
874    source: String,
875    path: std::path::PathBuf,
876    mut compiler_config: CompilerConfiguration,
877) -> CompilationResult {
878    // If the native style should be Qt, resolve it here as we know that we have it
879    let is_native = compiler_config.style.as_deref() == Some("native");
880    if is_native {
881        // On wasm, look at the browser user agent
882        #[cfg(target_arch = "wasm32")]
883        let target = web_sys::window()
884            .and_then(|window| window.navigator().platform().ok())
885            .map_or("wasm", |platform| {
886                let platform = platform.to_ascii_lowercase();
887                if platform.contains("mac")
888                    || platform.contains("iphone")
889                    || platform.contains("ipad")
890                {
891                    "apple"
892                } else if platform.contains("android") {
893                    "android"
894                } else if platform.contains("win") {
895                    "windows"
896                } else if platform.contains("linux") {
897                    "linux"
898                } else {
899                    "wasm"
900                }
901            });
902        #[cfg(not(target_arch = "wasm32"))]
903        let target = "";
904        compiler_config.style = Some(
905            i_slint_common::get_native_style(i_slint_backend_selector::HAS_NATIVE_STYLE, target)
906                .to_string(),
907        );
908    }
909
910    let diag = BuildDiagnostics::default();
911    #[cfg(feature = "internal-highlight")]
912    let (path, mut diag, loader, raw_type_loader) =
913        i_slint_compiler::load_root_file_with_raw_type_loader(
914            &path,
915            &path,
916            source,
917            diag,
918            compiler_config,
919        )
920        .await;
921    #[cfg(not(feature = "internal-highlight"))]
922    let (path, mut diag, loader) =
923        i_slint_compiler::load_root_file(&path, &path, source, diag, compiler_config).await;
924    #[cfg(feature = "internal-file-watcher")]
925    let watch_paths = loader.all_files_to_watch().into_iter().collect();
926    if diag.has_errors() {
927        return CompilationResult {
928            components: HashMap::new(),
929            diagnostics: diag.into_iter().collect(),
930            #[cfg(feature = "internal-file-watcher")]
931            watch_paths,
932            #[cfg(feature = "internal")]
933            structs_and_enums: Vec::new(),
934            #[cfg(feature = "internal")]
935            named_exports: Vec::new(),
936        };
937    }
938
939    #[cfg(feature = "internal-highlight")]
940    let loader = Rc::new(loader);
941    #[cfg(feature = "internal-highlight")]
942    let raw_type_loader = raw_type_loader.map(Rc::new);
943
944    let doc = loader.get_document(&path).unwrap();
945
946    let compiled_globals = Rc::new(CompiledGlobalCollection::compile(doc));
947    let mut components = HashMap::new();
948
949    let popup_menu_description = if let Some(popup_menu_impl) = &doc.popup_menu_impl {
950        PopupMenuDescription::Rc(Rc::new_cyclic(|weak| {
951            generativity::make_guard!(guard);
952            ErasedItemTreeDescription::from(generate_item_tree(
953                popup_menu_impl,
954                Some(compiled_globals.clone()),
955                PopupMenuDescription::Weak(weak.clone()),
956                true,
957                guard,
958            ))
959        }))
960    } else {
961        PopupMenuDescription::Weak(Default::default())
962    };
963
964    for c in doc.exported_roots() {
965        generativity::make_guard!(guard);
966        #[allow(unused_mut)]
967        let mut it = generate_item_tree(
968            &c,
969            Some(compiled_globals.clone()),
970            popup_menu_description.clone(),
971            false,
972            guard,
973        );
974        #[cfg(feature = "internal-highlight")]
975        {
976            let _ = it.type_loader.set(loader.clone());
977            let _ = it.raw_type_loader.set(raw_type_loader.clone());
978        }
979        components.insert(c.id.to_string(), ComponentDefinition { inner: it.into() });
980    }
981
982    if components.is_empty() {
983        diag.push_error_with_span("No component found".into(), Default::default());
984    };
985
986    #[cfg(feature = "internal")]
987    let structs_and_enums = doc.used_types.borrow().structs_and_enums.clone();
988
989    #[cfg(feature = "internal")]
990    let named_exports = doc
991        .exports
992        .iter()
993        .filter_map(|export| match &export.1 {
994            Either::Left(component) if !component.is_global() => {
995                Some((&export.0.name, &component.id))
996            }
997            Either::Right(ty) => match &ty {
998                Type::Struct(s) if s.node().is_some() => {
999                    if let StructName::User { name, .. } = &s.name {
1000                        Some((&export.0.name, name))
1001                    } else {
1002                        None
1003                    }
1004                }
1005                Type::Enumeration(en) => Some((&export.0.name, &en.name)),
1006                _ => None,
1007            },
1008            _ => None,
1009        })
1010        .filter(|(export_name, type_name)| *export_name != *type_name)
1011        .map(|(export_name, type_name)| (type_name.to_string(), export_name.to_string()))
1012        .collect::<Vec<_>>();
1013
1014    CompilationResult {
1015        diagnostics: diag.into_iter().collect(),
1016        components,
1017        #[cfg(feature = "internal-file-watcher")]
1018        watch_paths,
1019        #[cfg(feature = "internal")]
1020        structs_and_enums,
1021        #[cfg(feature = "internal")]
1022        named_exports,
1023    }
1024}
1025
1026fn generate_rtti() -> HashMap<&'static str, Rc<ItemRTTI>> {
1027    let mut rtti = HashMap::new();
1028    use i_slint_core::items::*;
1029    rtti.extend(
1030        [
1031            rtti_for::<ComponentContainer>(),
1032            rtti_for::<Empty>(),
1033            rtti_for::<ImageItem>(),
1034            rtti_for::<ClippedImage>(),
1035            rtti_for::<ComplexText>(),
1036            rtti_for::<StyledTextItem>(),
1037            rtti_for::<SimpleText>(),
1038            rtti_for::<Rectangle>(),
1039            rtti_for::<BasicBorderRectangle>(),
1040            rtti_for::<BorderRectangle>(),
1041            rtti_for::<TouchArea>(),
1042            rtti_for::<TooltipArea>(),
1043            rtti_for::<FocusScope>(),
1044            rtti_for::<KeyBinding>(),
1045            rtti_for::<SwipeGestureHandler>(),
1046            rtti_for::<ScaleRotateGestureHandler>(),
1047            rtti_for::<Path>(),
1048            rtti_for::<Flickable>(),
1049            rtti_for::<WindowItem>(),
1050            rtti_for::<TextInput>(),
1051            rtti_for::<Clip>(),
1052            rtti_for::<BoxShadow>(),
1053            rtti_for::<Transform>(),
1054            rtti_for::<Opacity>(),
1055            rtti_for::<Layer>(),
1056            rtti_for::<DragArea>(),
1057            rtti_for::<DropArea>(),
1058            rtti_for::<ContextMenu>(),
1059            rtti_for::<MenuItem>(),
1060            rtti_for::<SystemTrayIcon>(),
1061        ]
1062        .iter()
1063        .cloned(),
1064    );
1065
1066    trait NativeHelper {
1067        fn push(rtti: &mut HashMap<&str, Rc<ItemRTTI>>);
1068    }
1069    impl NativeHelper for () {
1070        fn push(_rtti: &mut HashMap<&str, Rc<ItemRTTI>>) {}
1071    }
1072    impl<
1073        T: 'static + Default + rtti::BuiltinItem + vtable::HasStaticVTable<ItemVTable>,
1074        Next: NativeHelper,
1075    > NativeHelper for (T, Next)
1076    {
1077        fn push(rtti: &mut HashMap<&str, Rc<ItemRTTI>>) {
1078            let info = rtti_for::<T>();
1079            rtti.insert(info.0, info.1);
1080            Next::push(rtti);
1081        }
1082    }
1083    i_slint_backend_selector::NativeWidgets::push(&mut rtti);
1084
1085    rtti
1086}
1087
1088pub(crate) fn generate_item_tree<'id>(
1089    component: &Rc<object_tree::Component>,
1090    compiled_globals: Option<Rc<CompiledGlobalCollection>>,
1091    popup_menu_description: PopupMenuDescription,
1092    is_popup_menu_impl: bool,
1093    guard: generativity::Guard<'id>,
1094) -> Rc<ItemTreeDescription<'id>> {
1095    //dbg!(&*component.root_element.borrow());
1096
1097    thread_local! {
1098        static RTTI: Lazy<HashMap<&'static str, Rc<ItemRTTI>>> = Lazy::new(generate_rtti);
1099    }
1100
1101    struct TreeBuilder<'id> {
1102        tree_array: Vec<ItemTreeNode>,
1103        item_array:
1104            Vec<vtable::VOffset<crate::dynamic_type::Instance<'id>, ItemVTable, vtable::AllowPin>>,
1105        original_elements: Vec<ElementRc>,
1106        items_types: HashMap<SmolStr, ItemWithinItemTree>,
1107        type_builder: dynamic_type::TypeBuilder<'id>,
1108        repeater: Vec<ErasedRepeaterWithinComponent<'id>>,
1109        repeater_names: HashMap<SmolStr, usize>,
1110        change_callbacks: Vec<(NamedReference, Expression)>,
1111        popup_menu_description: PopupMenuDescription,
1112    }
1113    impl generator::ItemTreeBuilder for TreeBuilder<'_> {
1114        type SubComponentState = ();
1115
1116        fn push_repeated_item(
1117            &mut self,
1118            item_rc: &ElementRc,
1119            repeater_count: u32,
1120            parent_index: u32,
1121            _component_state: &Self::SubComponentState,
1122        ) {
1123            self.tree_array.push(ItemTreeNode::DynamicTree { index: repeater_count, parent_index });
1124            self.original_elements.push(item_rc.clone());
1125            let item = item_rc.borrow();
1126            let base_component = item.base_type.as_component();
1127            self.repeater_names.insert(item.id.clone(), self.repeater.len());
1128            generativity::make_guard!(guard);
1129            let repeated_element_info = item.repeated.as_ref().unwrap();
1130            self.repeater.push(
1131                RepeaterWithinItemTree {
1132                    item_tree_to_repeat: generate_item_tree(
1133                        base_component,
1134                        None,
1135                        self.popup_menu_description.clone(),
1136                        false,
1137                        guard,
1138                    ),
1139                    offset: self.type_builder.add_field_type::<Repeater<ErasedItemTreeBox>>(),
1140                    model: repeated_element_info.model.clone(),
1141                    is_conditional: repeated_element_info.is_conditional_element,
1142                }
1143                .into(),
1144            );
1145        }
1146
1147        fn push_native_item(
1148            &mut self,
1149            rc_item: &ElementRc,
1150            child_offset: u32,
1151            parent_index: u32,
1152            _component_state: &Self::SubComponentState,
1153        ) {
1154            let item = rc_item.borrow();
1155            let rt = RTTI.with(|rtti| {
1156                rtti.get(&*item.base_type.as_native().class_name)
1157                    .unwrap_or_else(|| {
1158                        panic!(
1159                            "Native type not registered: {}",
1160                            item.base_type.as_native().class_name
1161                        )
1162                    })
1163                    .clone()
1164            });
1165
1166            let offset = self.type_builder.add_field(rt.type_info);
1167
1168            self.tree_array.push(ItemTreeNode::Item {
1169                is_accessible: !item.accessibility_props.0.is_empty(),
1170                children_index: child_offset,
1171                children_count: item.children.len() as u32,
1172                parent_index,
1173                item_array_index: self.item_array.len() as u32,
1174            });
1175            self.item_array.push(unsafe { vtable::VOffset::from_raw(rt.vtable, offset) });
1176            self.original_elements.push(rc_item.clone());
1177            debug_assert_eq!(self.original_elements.len(), self.tree_array.len());
1178            self.items_types.insert(
1179                item.id.clone(),
1180                ItemWithinItemTree { offset, rtti: rt, elem: rc_item.clone() },
1181            );
1182            for (prop, expr) in &item.change_callbacks {
1183                self.change_callbacks.push((
1184                    NamedReference::new(rc_item, prop.clone()),
1185                    Expression::CodeBlock(expr.borrow().clone()),
1186                ));
1187            }
1188        }
1189
1190        fn enter_component(
1191            &mut self,
1192            _item: &ElementRc,
1193            _sub_component: &Rc<object_tree::Component>,
1194            _children_offset: u32,
1195            _component_state: &Self::SubComponentState,
1196        ) -> Self::SubComponentState {
1197            /* nothing to do */
1198        }
1199
1200        fn enter_component_children(
1201            &mut self,
1202            _item: &ElementRc,
1203            _repeater_count: u32,
1204            _component_state: &Self::SubComponentState,
1205            _sub_component_state: &Self::SubComponentState,
1206        ) {
1207            todo!()
1208        }
1209    }
1210
1211    let mut builder = TreeBuilder {
1212        tree_array: Vec::new(),
1213        item_array: Vec::new(),
1214        original_elements: Vec::new(),
1215        items_types: HashMap::new(),
1216        type_builder: dynamic_type::TypeBuilder::new(guard),
1217        repeater: Vec::new(),
1218        repeater_names: HashMap::new(),
1219        change_callbacks: Vec::new(),
1220        popup_menu_description,
1221    };
1222
1223    if !component.is_global() {
1224        generator::build_item_tree(component, &(), &mut builder);
1225    } else {
1226        for (prop, expr) in component.root_element.borrow().change_callbacks.iter() {
1227            builder.change_callbacks.push((
1228                NamedReference::new(&component.root_element, prop.clone()),
1229                Expression::CodeBlock(expr.borrow().clone()),
1230            ));
1231        }
1232    }
1233
1234    let mut custom_properties = HashMap::new();
1235    let mut custom_callbacks = HashMap::new();
1236    let mut callback_trackers = HashMap::new();
1237    fn property_info<T>() -> (Box<dyn PropertyInfo<u8, Value>>, dynamic_type::StaticTypeInfo)
1238    where
1239        T: PartialEq + Clone + Default + std::convert::TryInto<Value> + 'static,
1240        Value: std::convert::TryInto<T>,
1241    {
1242        // Fixme: using u8 in PropertyInfo<> is not sound, we would need to materialize a type for out component
1243        (
1244            Box::new(unsafe {
1245                vtable::FieldOffset::<u8, Property<T>, _>::new_from_offset_pinned(0)
1246            }),
1247            dynamic_type::StaticTypeInfo::new::<Property<T>>(),
1248        )
1249    }
1250    fn animated_property_info<T>()
1251    -> (Box<dyn PropertyInfo<u8, Value>>, dynamic_type::StaticTypeInfo)
1252    where
1253        T: Clone + Default + InterpolatedPropertyValue + std::convert::TryInto<Value> + 'static,
1254        Value: std::convert::TryInto<T>,
1255    {
1256        // Fixme: using u8 in PropertyInfo<> is not sound, we would need to materialize a type for out component
1257        (
1258            Box::new(unsafe {
1259                rtti::MaybeAnimatedPropertyInfoWrapper(
1260                    vtable::FieldOffset::<u8, Property<T>, _>::new_from_offset_pinned(0),
1261                )
1262            }),
1263            dynamic_type::StaticTypeInfo::new::<Property<T>>(),
1264        )
1265    }
1266
1267    fn property_info_for_type(
1268        ty: &Type,
1269        name: &str,
1270    ) -> Option<(Box<dyn PropertyInfo<u8, Value>>, dynamic_type::StaticTypeInfo)> {
1271        Some(match ty {
1272            Type::Float32 => animated_property_info::<f32>(),
1273            Type::Int32 => animated_property_info::<i32>(),
1274            Type::String => property_info::<SharedString>(),
1275            Type::Color => animated_property_info::<Color>(),
1276            Type::Brush => animated_property_info::<Brush>(),
1277            Type::Duration => animated_property_info::<i64>(),
1278            Type::Angle => animated_property_info::<f32>(),
1279            Type::PhysicalLength => animated_property_info::<f32>(),
1280            Type::LogicalLength => animated_property_info::<f32>(),
1281            Type::Rem => animated_property_info::<f32>(),
1282            Type::Image => property_info::<i_slint_core::graphics::Image>(),
1283            Type::Bool => property_info::<bool>(),
1284            Type::ComponentFactory => property_info::<ComponentFactory>(),
1285            Type::Struct(s) if matches!(s.name, StructName::Builtin(BuiltinStruct::StateInfo)) => {
1286                property_info::<i_slint_core::properties::StateInfo>()
1287            }
1288            Type::Struct(_) => property_info::<Value>(),
1289            Type::Array(_) => property_info::<Value>(),
1290            Type::Easing => property_info::<i_slint_core::animations::EasingCurve>(),
1291            Type::Percent => animated_property_info::<f32>(),
1292            Type::Enumeration(e) => {
1293                macro_rules! match_enum_type {
1294                    ($( $(#[$enum_doc:meta])* $vis:vis enum $Name:ident { $($body:tt)* })*) => {
1295                        match e.name.as_str() {
1296                            $(
1297                                stringify!($Name) => property_info::<i_slint_core::items::$Name>(),
1298                            )*
1299                            x => unreachable!("Unknown non-builtin enum {x}"),
1300                        }
1301                    }
1302                }
1303
1304                if e.node.is_some() {
1305                    property_info::<Value>()
1306                } else {
1307                    i_slint_common::for_each_enums!(match_enum_type)
1308                }
1309            }
1310            Type::Keys => property_info::<Keys>(),
1311            Type::DataTransfer => property_info::<DataTransfer>(),
1312            Type::LayoutCache => property_info::<SharedVector<f32>>(),
1313            Type::ArrayOfU16 => property_info::<SharedVector<u16>>(),
1314            Type::Function { .. } | Type::Callback { .. } => return None,
1315            Type::StyledText => property_info::<StyledText>(),
1316            // These can't be used in properties
1317            Type::Invalid
1318            | Type::Void
1319            | Type::InferredProperty
1320            | Type::InferredCallback
1321            | Type::Model
1322            | Type::PathData
1323            | Type::UnitProduct(_)
1324            | Type::ElementReference => panic!("bad type {ty:?} for property {name}"),
1325        })
1326    }
1327
1328    for (name, decl) in &component.root_element.borrow().property_declarations {
1329        if decl.is_alias.is_some() {
1330            continue;
1331        }
1332        if matches!(&decl.property_type, Type::Callback { .. }) {
1333            custom_callbacks
1334                .insert(name.clone(), builder.type_builder.add_field_type::<Callback>());
1335            if decl.expose_in_public_api {
1336                callback_trackers
1337                    .insert(name.clone(), builder.type_builder.add_field_type::<Property<()>>());
1338            }
1339            continue;
1340        }
1341        let Some((prop, type_info)) = property_info_for_type(&decl.property_type, name) else {
1342            continue;
1343        };
1344        custom_properties.insert(
1345            name.clone(),
1346            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1347        );
1348    }
1349    if let Some(parent_element) = component.parent_element()
1350        && let Some(r) = &parent_element.borrow().repeated
1351        && !r.is_conditional_element
1352    {
1353        let (prop, type_info) = property_info::<u32>();
1354        custom_properties.insert(
1355            SPECIAL_PROPERTY_INDEX.into(),
1356            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1357        );
1358
1359        let model_ty = Expression::RepeaterModelReference {
1360            element: component.parent_element.borrow().clone(),
1361        }
1362        .ty();
1363        let (prop, type_info) =
1364            property_info_for_type(&model_ty, SPECIAL_PROPERTY_MODEL_DATA).unwrap();
1365        custom_properties.insert(
1366            SPECIAL_PROPERTY_MODEL_DATA.into(),
1367            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1368        );
1369    }
1370
1371    let parent_item_tree_offset = if component.parent_element().is_some() || is_popup_menu_impl {
1372        Some(builder.type_builder.add_field_type::<OnceCell<ErasedItemTreeBoxWeak>>())
1373    } else {
1374        None
1375    };
1376
1377    let root_offset = builder.type_builder.add_field_type::<OnceCell<ErasedItemTreeBoxWeak>>();
1378    let extra_data_offset = builder.type_builder.add_field_type::<ComponentExtraData>();
1379
1380    let change_trackers = (!builder.change_callbacks.is_empty()).then(|| {
1381        (
1382            builder.type_builder.add_field_type::<OnceCell<Vec<ChangeTracker>>>(),
1383            builder.change_callbacks,
1384        )
1385    });
1386    let timers = component
1387        .timers
1388        .borrow()
1389        .iter()
1390        .map(|_| builder.type_builder.add_field_type::<Timer>())
1391        .collect();
1392
1393    // only the public exported component needs the public property list
1394    let public_properties = if component.parent_element().is_none() {
1395        component.root_element.borrow().property_declarations.clone()
1396    } else {
1397        Default::default()
1398    };
1399
1400    let t = ItemTreeVTable {
1401        visit_children_item,
1402        layout_info,
1403        ensure_instantiated,
1404        get_item_ref,
1405        get_item_tree,
1406        get_subtree_range,
1407        get_subtree,
1408        parent_node,
1409        embed_component,
1410        subtree_index,
1411        item_geometry,
1412        accessible_role,
1413        accessible_string_property,
1414        accessibility_action,
1415        supported_accessibility_actions,
1416        item_element_infos,
1417        window_adapter,
1418        drop_in_place,
1419        dealloc,
1420    };
1421    let t = ItemTreeDescription {
1422        ct: t,
1423        dynamic_type: builder.type_builder.build(),
1424        item_tree: builder.tree_array,
1425        item_array: builder.item_array,
1426        items: builder.items_types,
1427        custom_properties,
1428        custom_callbacks,
1429        callback_trackers,
1430        original: component.clone(),
1431        original_elements: builder.original_elements,
1432        repeater: builder.repeater,
1433        repeater_names: builder.repeater_names,
1434        parent_item_tree_offset,
1435        root_offset,
1436        extra_data_offset,
1437        public_properties,
1438        compiled_globals,
1439        change_trackers,
1440        timers,
1441        popup_ids: std::cell::RefCell::new(HashMap::new()),
1442        popup_menu_description: builder.popup_menu_description,
1443        #[cfg(feature = "internal-highlight")]
1444        type_loader: std::cell::OnceCell::new(),
1445        #[cfg(feature = "internal-highlight")]
1446        raw_type_loader: std::cell::OnceCell::new(),
1447        debug_handler: std::cell::RefCell::new(Rc::new(|_, text| {
1448            i_slint_core::debug_log!("{text}")
1449        })),
1450    };
1451
1452    Rc::new(t)
1453}
1454
1455pub fn animation_for_property(
1456    component: InstanceRef,
1457    animation: &Option<i_slint_compiler::object_tree::PropertyAnimation>,
1458) -> AnimatedBindingKind {
1459    match animation {
1460        Some(i_slint_compiler::object_tree::PropertyAnimation::Static(anim_elem)) => {
1461            AnimatedBindingKind::Animation(Box::new({
1462                let component_ptr = component.as_ptr();
1463                let vtable = NonNull::from(&component.description.ct).cast();
1464                let anim_elem = Rc::clone(anim_elem);
1465                move || -> PropertyAnimation {
1466                    generativity::make_guard!(guard);
1467                    let component = unsafe {
1468                        InstanceRef::from_pin_ref(
1469                            Pin::new_unchecked(vtable::VRef::from_raw(
1470                                vtable,
1471                                NonNull::new_unchecked(component_ptr as *mut u8),
1472                            )),
1473                            guard,
1474                        )
1475                    };
1476
1477                    eval::new_struct_with_bindings(
1478                        &anim_elem.borrow().bindings,
1479                        &mut eval::EvalLocalContext::from_component_instance(component),
1480                    )
1481                }
1482            }))
1483        }
1484        Some(i_slint_compiler::object_tree::PropertyAnimation::Transition {
1485            animations,
1486            state_ref,
1487        }) => {
1488            let component_ptr = component.as_ptr();
1489            let vtable = NonNull::from(&component.description.ct).cast();
1490            let animations = animations.clone();
1491            let state_ref = state_ref.clone();
1492            AnimatedBindingKind::Transition(Box::new(
1493                move || -> (PropertyAnimation, i_slint_core::animations::Instant) {
1494                    generativity::make_guard!(guard);
1495                    let component = unsafe {
1496                        InstanceRef::from_pin_ref(
1497                            Pin::new_unchecked(vtable::VRef::from_raw(
1498                                vtable,
1499                                NonNull::new_unchecked(component_ptr as *mut u8),
1500                            )),
1501                            guard,
1502                        )
1503                    };
1504
1505                    let mut context = eval::EvalLocalContext::from_component_instance(component);
1506                    let state = eval::eval_expression(&state_ref, &mut context);
1507                    let state_info: i_slint_core::properties::StateInfo = state.try_into().unwrap();
1508                    for a in &animations {
1509                        let is_previous_state = a.state_id == state_info.previous_state;
1510                        let is_current_state = a.state_id == state_info.current_state;
1511                        match (a.direction, is_previous_state, is_current_state) {
1512                            (TransitionDirection::In, false, true)
1513                            | (TransitionDirection::Out, true, false)
1514                            | (TransitionDirection::InOut, false, true)
1515                            | (TransitionDirection::InOut, true, false) => {
1516                                return (
1517                                    eval::new_struct_with_bindings(
1518                                        &a.animation.borrow().bindings,
1519                                        &mut context,
1520                                    ),
1521                                    state_info.change_time,
1522                                );
1523                            }
1524                            _ => {}
1525                        }
1526                    }
1527                    Default::default()
1528                },
1529            ))
1530        }
1531        None => AnimatedBindingKind::NotAnimated,
1532    }
1533}
1534
1535fn make_callback_eval_closure(
1536    expr: Expression,
1537    self_weak: ErasedItemTreeBoxWeak,
1538) -> impl Fn(&[Value]) -> Value {
1539    move |args| {
1540        let self_rc = self_weak.upgrade().unwrap();
1541        generativity::make_guard!(guard);
1542        let self_ = self_rc.unerase(guard);
1543        let instance_ref = self_.borrow_instance();
1544        let mut local_context =
1545            eval::EvalLocalContext::from_function_arguments(instance_ref, args.to_vec());
1546        eval::eval_expression(&expr, &mut local_context)
1547    }
1548}
1549
1550fn make_binding_eval_closure(
1551    expr: Expression,
1552    self_weak: ErasedItemTreeBoxWeak,
1553) -> impl Fn() -> Value {
1554    move || {
1555        let self_rc = self_weak.upgrade().unwrap();
1556        generativity::make_guard!(guard);
1557        let self_ = self_rc.unerase(guard);
1558        let instance_ref = self_.borrow_instance();
1559        eval::eval_expression(
1560            &expr,
1561            &mut eval::EvalLocalContext::from_component_instance(instance_ref),
1562        )
1563    }
1564}
1565
1566pub fn instantiate(
1567    description: Rc<ItemTreeDescription>,
1568    parent_ctx: Option<ErasedItemTreeBoxWeak>,
1569    root: Option<ErasedItemTreeBoxWeak>,
1570    window_options: Option<&WindowOptions>,
1571    globals: crate::global_component::GlobalStorage,
1572) -> DynamicComponentVRc {
1573    let instance = description.dynamic_type.clone().create_instance();
1574
1575    let component_box = ItemTreeBox { instance, description: description.clone() };
1576
1577    let self_rc = vtable::VRc::new(ErasedItemTreeBox::from(component_box));
1578    let self_weak = vtable::VRc::downgrade(&self_rc);
1579
1580    generativity::make_guard!(guard);
1581    let comp = self_rc.unerase(guard);
1582    let instance_ref = comp.borrow_instance();
1583    instance_ref.self_weak().set(self_weak.clone()).ok();
1584    let description = comp.description();
1585
1586    if let Some(WindowOptions::UseExistingWindow(existing_adapter)) = &window_options
1587        && let Err((a, b)) = globals.window_adapter().unwrap().try_insert(existing_adapter.clone())
1588    {
1589        assert!(Rc::ptr_eq(a, &b), "window not the same as parent window");
1590    }
1591
1592    if let Some(parent) = parent_ctx {
1593        description
1594            .parent_item_tree_offset
1595            .unwrap()
1596            .apply(instance_ref.as_ref())
1597            .set(parent)
1598            .ok()
1599            .unwrap();
1600    } else if let Some(g) = description.compiled_globals.as_ref() {
1601        for g in g.compiled_globals.iter() {
1602            crate::global_component::instantiate(g, &globals, self_weak.clone());
1603        }
1604    }
1605    let extra_data = description.extra_data_offset.apply(instance_ref.as_ref());
1606    extra_data.globals.set(globals).ok().unwrap();
1607    if let Some(WindowOptions::Embed { parent_item_tree, parent_item_tree_index }) = window_options
1608    {
1609        vtable::VRc::borrow_pin(&self_rc)
1610            .as_ref()
1611            .embed_component(parent_item_tree, *parent_item_tree_index);
1612        description.root_offset.apply(instance_ref.as_ref()).set(self_weak.clone()).ok().unwrap();
1613    } else {
1614        generativity::make_guard!(guard);
1615        let root = root
1616            .or_else(|| {
1617                instance_ref.parent_instance(guard).map(|parent| parent.root_weak().clone())
1618            })
1619            .unwrap_or_else(|| self_weak.clone());
1620        description.root_offset.apply(instance_ref.as_ref()).set(root).ok().unwrap();
1621    }
1622
1623    if !description.original.is_global() {
1624        let maybe_window_adapter =
1625            if let Some(WindowOptions::UseExistingWindow(adapter)) = window_options.as_ref() {
1626                Some(adapter.clone())
1627            } else {
1628                extra_data.globals.get().unwrap().window_adapter().and_then(|wa| wa.get().cloned())
1629            };
1630
1631        let component_rc = vtable::VRc::into_dyn(self_rc.clone());
1632        i_slint_core::item_tree::register_item_tree(&component_rc, maybe_window_adapter);
1633    }
1634
1635    // Some properties are generated as Value, but for which the default constructed Value must be initialized
1636    for (prop_name, decl) in &description.original.root_element.borrow().property_declarations {
1637        if !matches!(
1638            decl.property_type,
1639            Type::Struct { .. } | Type::Array(_) | Type::Enumeration(_)
1640        ) || decl.is_alias.is_some()
1641        {
1642            continue;
1643        }
1644        if let Some(b) = description.original.root_element.borrow().bindings.get(prop_name)
1645            && b.borrow().two_way_bindings.is_empty()
1646        {
1647            continue;
1648        }
1649        let p = description.custom_properties.get(prop_name).unwrap();
1650        unsafe {
1651            let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(p.offset));
1652            p.prop.set(item, eval::default_value_for_type(&decl.property_type), None).unwrap();
1653        }
1654    }
1655
1656    #[cfg(slint_debug_property)]
1657    {
1658        let component_id = description.original.id.as_str();
1659
1660        // Set debug names on custom (root element) properties
1661        for (prop_name, prop_info) in &description.custom_properties {
1662            let name = format!("{}.{}", component_id, prop_name);
1663            unsafe {
1664                let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(prop_info.offset));
1665                prop_info.prop.set_debug_name(item, name);
1666            }
1667        }
1668
1669        // Set debug names on built-in item properties
1670        for (item_name, item_within_component) in &description.items {
1671            let item = unsafe { item_within_component.item_from_item_tree(instance_ref.as_ptr()) };
1672            for (prop_name, prop_rtti) in &item_within_component.rtti.properties {
1673                let name = format!("{}::{}.{}", component_id, item_name, prop_name);
1674                prop_rtti.set_debug_name(item, name);
1675            }
1676        }
1677    }
1678
1679    generator::handle_property_bindings_init(
1680        &description.original,
1681        |elem, prop_name, binding| unsafe {
1682            let is_root = Rc::ptr_eq(
1683                elem,
1684                &elem.borrow().enclosing_component.upgrade().unwrap().root_element,
1685            );
1686            let elem = elem.borrow();
1687            let is_const = binding.analysis.as_ref().is_some_and(|a| a.is_const);
1688
1689            let property_type = elem.lookup_property(prop_name).property_type;
1690            if let Type::Function { .. } = property_type {
1691                // function don't need initialization
1692            } else if let Type::Callback { .. } = property_type {
1693                if !matches!(binding.expression, Expression::Invalid) {
1694                    let expr = binding.expression.clone();
1695                    let description = description.clone();
1696                    if let Some(callback_offset) =
1697                        description.custom_callbacks.get(prop_name).filter(|_| is_root)
1698                    {
1699                        let callback = callback_offset.apply(instance_ref.as_ref());
1700                        callback.set_handler(make_callback_eval_closure(expr, self_weak.clone()));
1701                    } else {
1702                        let item_within_component = &description.items[&elem.id];
1703                        let item = item_within_component.item_from_item_tree(instance_ref.as_ptr());
1704                        if let Some(callback) =
1705                            item_within_component.rtti.callbacks.get(prop_name.as_str())
1706                        {
1707                            callback.set_handler(
1708                                item,
1709                                Box::new(make_callback_eval_closure(expr, self_weak.clone())),
1710                            );
1711                        } else {
1712                            panic!("unknown callback {prop_name}")
1713                        }
1714                    }
1715                }
1716            } else if let Some(PropertiesWithinComponent { offset, prop: prop_info, .. }) =
1717                description.custom_properties.get(prop_name).filter(|_| is_root)
1718            {
1719                let is_state_info = matches!(&property_type, Type::Struct (s) if matches!(s.name, StructName::Builtin(BuiltinStruct::StateInfo)));
1720                if is_state_info {
1721                    let prop = Pin::new_unchecked(
1722                        &*(instance_ref.as_ptr().add(*offset)
1723                            as *const Property<i_slint_core::properties::StateInfo>),
1724                    );
1725                    let e = binding.expression.clone();
1726                    let state_binding = make_binding_eval_closure(e, self_weak.clone());
1727                    i_slint_core::properties::set_state_binding(prop, move || {
1728                        state_binding().try_into().unwrap()
1729                    });
1730                    return;
1731                }
1732
1733                let maybe_animation = animation_for_property(instance_ref, &binding.animation);
1734                let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(*offset));
1735
1736                if !matches!(binding.expression, Expression::Invalid) {
1737                    if is_const {
1738                        let v = eval::eval_expression(
1739                            &binding.expression,
1740                            &mut eval::EvalLocalContext::from_component_instance(instance_ref),
1741                        );
1742                        prop_info.set(item, v, None).unwrap();
1743                    } else {
1744                        let e = binding.expression.clone();
1745                        prop_info
1746                            .set_binding(
1747                                item,
1748                                Box::new(make_binding_eval_closure(e, self_weak.clone())),
1749                                maybe_animation,
1750                            )
1751                            .unwrap();
1752                    }
1753                }
1754                for twb in &binding.two_way_bindings {
1755                    match twb {
1756                        TwoWayBinding::Property { property, field_access }
1757                            if field_access.is_empty()
1758                                && !matches!(
1759                                    &property_type,
1760                                    Type::Struct(..) | Type::Array(..)
1761                                ) =>
1762                        {
1763                            // Safety: The compiler ensured that the properties exist and have
1764                            // the same type (except for struct/array, which may map to a Value).
1765                            prop_info.link_two_ways(item, get_property_ptr(property, instance_ref));
1766                        }
1767                        TwoWayBinding::Property { property, field_access } => {
1768                            let (common, map) =
1769                                prepare_for_two_way_binding(instance_ref, property, field_access);
1770                            prop_info.link_two_way_with_map(item, common, map);
1771                        }
1772                        TwoWayBinding::ModelData { repeated_element, field_access } => {
1773                            let (getter, setter) = prepare_model_two_way_binding(
1774                                instance_ref,
1775                                repeated_element,
1776                                field_access,
1777                            );
1778                            prop_info.link_two_way_to_model_data(item, getter, setter);
1779                        }
1780                    }
1781                }
1782            } else {
1783                let item_within_component = &description.items[&elem.id];
1784                let item = item_within_component.item_from_item_tree(instance_ref.as_ptr());
1785                if let Some(prop_rtti) =
1786                    item_within_component.rtti.properties.get(prop_name.as_str())
1787                {
1788                    let maybe_animation = animation_for_property(instance_ref, &binding.animation);
1789
1790                    for twb in &binding.two_way_bindings {
1791                        match twb {
1792                            TwoWayBinding::Property { property, field_access }
1793                                if field_access.is_empty()
1794                                    && !matches!(
1795                                        &property_type,
1796                                        Type::Struct(..) | Type::Array(..)
1797                                    ) =>
1798                            {
1799                                // Safety: The compiler ensured that the properties exist and
1800                                // have the same type.
1801                                prop_rtti
1802                                    .link_two_ways(item, get_property_ptr(property, instance_ref));
1803                            }
1804                            TwoWayBinding::Property { property, field_access } => {
1805                                let (common, map) = prepare_for_two_way_binding(
1806                                    instance_ref,
1807                                    property,
1808                                    field_access,
1809                                );
1810                                prop_rtti.link_two_way_with_map(item, common, map);
1811                            }
1812                            TwoWayBinding::ModelData { repeated_element, field_access } => {
1813                                let (getter, setter) = prepare_model_two_way_binding(
1814                                    instance_ref,
1815                                    repeated_element,
1816                                    field_access,
1817                                );
1818                                prop_rtti.link_two_way_to_model_data(item, getter, setter);
1819                            }
1820                        }
1821                    }
1822                    if !matches!(binding.expression, Expression::Invalid) {
1823                        if is_const {
1824                            prop_rtti
1825                                .set(
1826                                    item,
1827                                    eval::eval_expression(
1828                                        &binding.expression,
1829                                        &mut eval::EvalLocalContext::from_component_instance(
1830                                            instance_ref,
1831                                        ),
1832                                    ),
1833                                    maybe_animation.as_animation(),
1834                                )
1835                                .unwrap();
1836                        } else {
1837                            let e = binding.expression.clone();
1838                            prop_rtti.set_binding(
1839                                item,
1840                                Box::new(make_binding_eval_closure(e, self_weak.clone())),
1841                                maybe_animation,
1842                            );
1843                        }
1844                    }
1845                } else {
1846                    panic!("unknown property {} in {}", prop_name, elem.id);
1847                }
1848            }
1849        },
1850    );
1851
1852    for rep_in_comp in &description.repeater {
1853        generativity::make_guard!(guard);
1854        let rep_in_comp = rep_in_comp.unerase(guard);
1855
1856        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
1857        let expr = rep_in_comp.model.clone();
1858        let model_binding_closure = make_binding_eval_closure(expr, self_weak.clone());
1859        if rep_in_comp.is_conditional {
1860            let bool_model = Rc::new(crate::value_model::BoolModel::default());
1861            repeater.set_model_binding(move || {
1862                let v = model_binding_closure();
1863                bool_model.set_value(v.try_into().expect("condition model is bool"));
1864                ModelRc::from(bool_model.clone())
1865            });
1866        } else {
1867            repeater.set_model_binding(move || {
1868                let m = model_binding_closure();
1869                if let Value::Model(m) = m {
1870                    m
1871                } else {
1872                    ModelRc::new(crate::value_model::ValueModel::new(m))
1873                }
1874            });
1875        }
1876    }
1877    self_rc
1878}
1879
1880fn prepare_for_two_way_binding(
1881    instance_ref: InstanceRef,
1882    property: &NamedReference,
1883    field_access: &[SmolStr],
1884) -> (Pin<Rc<Property<Value>>>, Option<Rc<dyn rtti::TwoWayBindingMapping<Value>>>) {
1885    let element = property.element();
1886    let name = property.name().as_str();
1887
1888    generativity::make_guard!(guard);
1889    let enclosing_component = eval::enclosing_component_instance_for_element(
1890        &element,
1891        &eval::ComponentInstance::InstanceRef(instance_ref),
1892        guard,
1893    );
1894    let map: Option<Rc<dyn rtti::TwoWayBindingMapping<Value>>> = if field_access.is_empty() {
1895        None
1896    } else {
1897        struct FieldAccess(Vec<SmolStr>);
1898        impl rtti::TwoWayBindingMapping<Value> for FieldAccess {
1899            fn map_to(&self, value: &Value) -> Value {
1900                walk_struct_field_path(value.clone(), &self.0).unwrap_or_default()
1901            }
1902            fn map_from(&self, root: &mut Value, from: &Value) {
1903                if let Some(leaf) = walk_struct_field_path_mut(root, &self.0) {
1904                    *leaf = from.clone();
1905                }
1906            }
1907        }
1908        Some(Rc::new(FieldAccess(field_access.to_vec())))
1909    };
1910    let common = match enclosing_component {
1911        eval::ComponentInstance::InstanceRef(enclosing_component) => {
1912            let element = element.borrow();
1913            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
1914                && let Some(x) = enclosing_component.description.custom_properties.get(name)
1915            {
1916                let item =
1917                    unsafe { Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset)) };
1918                let common = x.prop.prepare_for_two_way_binding(item);
1919                return (common, map);
1920            }
1921            let item_info = enclosing_component
1922                .description
1923                .items
1924                .get(element.id.as_str())
1925                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, name));
1926            let prop_info = item_info
1927                .rtti
1928                .properties
1929                .get(name)
1930                .unwrap_or_else(|| panic!("Property {} not in {}", name, element.id));
1931            core::mem::drop(element);
1932            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1933            prop_info.prepare_for_two_way_binding(item)
1934        }
1935        eval::ComponentInstance::GlobalComponent(glob) => {
1936            glob.as_ref().prepare_for_two_way_binding(name).unwrap()
1937        }
1938    };
1939    (common, map)
1940}
1941
1942/// Build a (getter, setter) pair for a `TwoWayBinding::ModelData`. The
1943/// setter writes the whole row back through the field-access path, and
1944/// skips the write if the leaf value is unchanged.
1945fn prepare_model_two_way_binding(
1946    instance_ref: InstanceRef,
1947    repeated_element: &i_slint_compiler::object_tree::ElementWeak,
1948    field_access: &[SmolStr],
1949) -> (Box<dyn Fn() -> Option<Value>>, Box<dyn Fn(&Value)>) {
1950    let self_weak = instance_ref.self_weak().get().unwrap().clone();
1951    let repeated_element = repeated_element.clone();
1952    let field_access: Vec<SmolStr> = field_access.to_vec();
1953
1954    let getter = {
1955        let self_weak = self_weak.clone();
1956        let repeated_element = repeated_element.clone();
1957        let field_access = field_access.clone();
1958        Box::new(move || -> Option<Value> {
1959            with_repeater_row(&self_weak, &repeated_element, |repeater, row| {
1960                walk_struct_field_path(repeater.model_row_data(row)?, &field_access)
1961            })
1962        })
1963    };
1964
1965    let setter = Box::new(move |new_value: &Value| {
1966        with_repeater_row(&self_weak, &repeated_element, |repeater, row| {
1967            let mut data = repeater.model_row_data(row)?;
1968            // Short-circuit identical writes to avoid spurious change notifications.
1969            let leaf = walk_struct_field_path_mut(&mut data, &field_access)?;
1970            if &*leaf == new_value {
1971                return Some(());
1972            }
1973            *leaf = new_value.clone();
1974            repeater.model_set_row_data(row, data);
1975            Some(())
1976        });
1977    });
1978
1979    (getter, setter)
1980}
1981
1982/// Resolve the repeater that backs `repeated_element` and its current row
1983/// index, then run `f`. Returns `None` if any link is unavailable.
1984fn with_repeater_row<R>(
1985    self_weak: &ErasedItemTreeBoxWeak,
1986    repeated_element: &i_slint_compiler::object_tree::ElementWeak,
1987    f: impl FnOnce(Pin<&Repeater<ErasedItemTreeBox>>, usize) -> Option<R>,
1988) -> Option<R> {
1989    let self_rc = self_weak.upgrade()?;
1990    generativity::make_guard!(guard);
1991    let s = self_rc.unerase(guard);
1992    let instance = s.borrow_instance();
1993    let element = repeated_element.upgrade()?;
1994    let index = crate::eval::load_property(
1995        instance,
1996        &element.borrow().base_type.as_component().root_element,
1997        crate::dynamic_item_tree::SPECIAL_PROPERTY_INDEX,
1998    )
1999    .ok()?;
2000    let row = usize::try_from(i32::try_from(index).ok()?).ok()?;
2001    generativity::make_guard!(guard);
2002    let enclosing = crate::eval::enclosing_component_for_element(&element, instance, guard);
2003    generativity::make_guard!(guard);
2004    let (repeater, _) = get_repeater_by_name(enclosing, element.borrow().id.as_str(), guard);
2005    f(repeater, row)
2006}
2007
2008/// Follow a chain of struct field accesses on `value`.
2009fn walk_struct_field_path(mut value: Value, fields: &[SmolStr]) -> Option<Value> {
2010    for f in fields {
2011        match value {
2012            Value::Struct(o) => value = o.get_field(f).cloned().unwrap_or_default(),
2013            Value::Void => return None,
2014            _ => return None,
2015        }
2016    }
2017    Some(value)
2018}
2019
2020/// Mutable counterpart of [`walk_struct_field_path`].
2021fn walk_struct_field_path_mut<'a>(
2022    mut value: &'a mut Value,
2023    fields: &[SmolStr],
2024) -> Option<&'a mut Value> {
2025    for f in fields {
2026        match value {
2027            Value::Struct(o) => value = o.0.get_mut(f)?,
2028            _ => return None,
2029        }
2030    }
2031    Some(value)
2032}
2033
2034pub(crate) fn get_property_ptr(nr: &NamedReference, instance: InstanceRef) -> *const c_void {
2035    let element = nr.element();
2036    generativity::make_guard!(guard);
2037    let enclosing_component = eval::enclosing_component_instance_for_element(
2038        &element,
2039        &eval::ComponentInstance::InstanceRef(instance),
2040        guard,
2041    );
2042    match enclosing_component {
2043        eval::ComponentInstance::InstanceRef(enclosing_component) => {
2044            let element = element.borrow();
2045            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2046                && let Some(x) = enclosing_component.description.custom_properties.get(nr.name())
2047            {
2048                return unsafe { enclosing_component.as_ptr().add(x.offset).cast() };
2049            };
2050            let item_info = enclosing_component
2051                .description
2052                .items
2053                .get(element.id.as_str())
2054                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, nr.name()));
2055            let prop_info = item_info
2056                .rtti
2057                .properties
2058                .get(nr.name().as_str())
2059                .unwrap_or_else(|| panic!("Property {} not in {}", nr.name(), element.id));
2060            core::mem::drop(element);
2061            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2062            unsafe { item.as_ptr().add(prop_info.offset()).cast() }
2063        }
2064        eval::ComponentInstance::GlobalComponent(glob) => glob.as_ref().get_property_ptr(nr.name()),
2065    }
2066}
2067
2068pub struct ErasedItemTreeBox(ItemTreeBox<'static>);
2069impl ErasedItemTreeBox {
2070    pub fn unerase<'a, 'id>(
2071        &'a self,
2072        _guard: generativity::Guard<'id>,
2073    ) -> Pin<&'a ItemTreeBox<'id>> {
2074        Pin::new(
2075            //Safety: 'id is unique because of `_guard`
2076            unsafe { core::mem::transmute::<&ItemTreeBox<'static>, &ItemTreeBox<'id>>(&self.0) },
2077        )
2078    }
2079
2080    pub fn borrow(&self) -> ItemTreeRefPin<'_> {
2081        // Safety: it is safe to access self.0 here because the 'id lifetime does not leak
2082        self.0.borrow()
2083    }
2084
2085    pub fn window_adapter_ref(&self) -> Result<&WindowAdapterRc, PlatformError> {
2086        self.0.window_adapter_ref()
2087    }
2088
2089    pub fn run_setup_code(&self) {
2090        generativity::make_guard!(guard);
2091        let compo_box = self.unerase(guard);
2092        let instance_ref = compo_box.borrow_instance();
2093        for extra_init_code in self.0.description.original.init_code.borrow().iter() {
2094            eval::eval_expression(
2095                extra_init_code,
2096                &mut eval::EvalLocalContext::from_component_instance(instance_ref),
2097            );
2098        }
2099        if let Some(cts) = instance_ref.description.change_trackers.as_ref() {
2100            let self_weak = instance_ref.self_weak().get().unwrap();
2101            let v = cts
2102                .1
2103                .iter()
2104                .enumerate()
2105                .map(|(idx, _)| {
2106                    let ct = ChangeTracker::default();
2107                    ct.init(
2108                        self_weak.clone(),
2109                        move |self_weak| {
2110                            let s = self_weak.upgrade().unwrap();
2111                            generativity::make_guard!(guard);
2112                            let compo_box = s.unerase(guard);
2113                            let instance_ref = compo_box.borrow_instance();
2114                            let nr = &s.0.description.change_trackers.as_ref().unwrap().1[idx].0;
2115                            eval::load_property(instance_ref, &nr.element(), nr.name()).unwrap()
2116                        },
2117                        move |self_weak, _| {
2118                            let s = self_weak.upgrade().unwrap();
2119                            generativity::make_guard!(guard);
2120                            let compo_box = s.unerase(guard);
2121                            let instance_ref = compo_box.borrow_instance();
2122                            let e = &s.0.description.change_trackers.as_ref().unwrap().1[idx].1;
2123                            eval::eval_expression(
2124                                e,
2125                                &mut eval::EvalLocalContext::from_component_instance(instance_ref),
2126                            );
2127                        },
2128                    );
2129                    ct
2130                })
2131                .collect::<Vec<_>>();
2132            cts.0
2133                .apply_pin(instance_ref.instance)
2134                .set(v)
2135                .unwrap_or_else(|_| panic!("run_setup_code called twice?"));
2136        }
2137        update_timers(instance_ref);
2138    }
2139}
2140impl<'id> From<ItemTreeBox<'id>> for ErasedItemTreeBox {
2141    fn from(inner: ItemTreeBox<'id>) -> Self {
2142        // Safety: Nothing access the component directly, we only access it through unerased where
2143        // the lifetime is unique again
2144        unsafe {
2145            ErasedItemTreeBox(core::mem::transmute::<ItemTreeBox<'id>, ItemTreeBox<'static>>(inner))
2146        }
2147    }
2148}
2149
2150pub fn get_repeater_by_name<'a, 'id>(
2151    instance_ref: InstanceRef<'a, '_>,
2152    name: &str,
2153    guard: generativity::Guard<'id>,
2154) -> (std::pin::Pin<&'a Repeater<ErasedItemTreeBox>>, Rc<ItemTreeDescription<'id>>) {
2155    let rep_index = instance_ref.description.repeater_names[name];
2156    let rep_in_comp = instance_ref.description.repeater[rep_index].unerase(guard);
2157    (rep_in_comp.offset.apply_pin(instance_ref.instance), rep_in_comp.item_tree_to_repeat.clone())
2158}
2159
2160#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2161extern "C" fn ensure_instantiated(component: ItemTreeRefPin) -> bool {
2162    generativity::make_guard!(guard);
2163    // Safety: called through the vtable of our own ItemTreeDescription.
2164    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2165
2166    let mut changed = false;
2167    for (tree_index, node) in instance_ref.description.item_tree.iter().enumerate() {
2168        if !matches!(node, ItemTreeNode::Item { .. }) {
2169            continue;
2170        }
2171        let item_ref = component.as_ref().get_item_ref(tree_index as u32);
2172        if let Some(container) = i_slint_core::items::ItemRef::downcast_pin::<
2173            i_slint_core::items::ComponentContainer,
2174        >(item_ref)
2175        {
2176            changed |= container.ensure_updated();
2177        }
2178    }
2179
2180    for rep_in_comp in &instance_ref.description.repeater {
2181        // Safety: we do not mix the repeater with a different component id.
2182        let rep_in_comp = unsafe { rep_in_comp.get_untagged() };
2183        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
2184        let init = || {
2185            let extra_data =
2186                instance_ref.description.extra_data_offset.apply(instance_ref.as_ref());
2187            instantiate(
2188                rep_in_comp.item_tree_to_repeat.clone(),
2189                instance_ref.self_weak().get().cloned(),
2190                None,
2191                None,
2192                extra_data.globals.get().unwrap().clone(),
2193            )
2194        };
2195        if let Some(lv) = &rep_in_comp
2196            .item_tree_to_repeat
2197            .original
2198            .parent_element
2199            .borrow()
2200            .upgrade()
2201            .unwrap()
2202            .borrow()
2203            .repeated
2204            .as_ref()
2205            .unwrap()
2206            .is_listview
2207        {
2208            let assume_property_logical_length =
2209                |prop| unsafe { Pin::new_unchecked(&*(prop as *const Property<LogicalLength>)) };
2210            changed |= repeater.ensure_updated_listview(
2211                init,
2212                assume_property_logical_length(get_property_ptr(&lv.viewport_width, instance_ref)),
2213                assume_property_logical_length(get_property_ptr(&lv.viewport_height, instance_ref)),
2214                assume_property_logical_length(get_property_ptr(&lv.viewport_y, instance_ref)),
2215                eval::load_property(
2216                    instance_ref,
2217                    &lv.listview_width.element(),
2218                    lv.listview_width.name(),
2219                )
2220                .unwrap()
2221                .try_into()
2222                .unwrap(),
2223                assume_property_logical_length(get_property_ptr(&lv.listview_height, instance_ref)),
2224            );
2225        } else {
2226            changed |= repeater.ensure_updated(init);
2227        }
2228    }
2229    changed
2230}
2231
2232#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2233extern "C" fn layout_info(component: ItemTreeRefPin, orientation: Orientation) -> LayoutInfo {
2234    generativity::make_guard!(guard);
2235    // This is fine since we can only be called with a component that with our vtable which is a ItemTreeDescription
2236    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2237    let orientation = crate::eval_layout::from_runtime(orientation);
2238
2239    // The vtable layout_info path is taken e.g. for repeater cells. When
2240    // the component root has a parameterized layout-info function, route
2241    // through it: reading the bare `layoutinfo-{h,v}` would cycle on
2242    // `self.{w,h}` for the cross-axis case, and we have no explicit
2243    // constraint at this entry point. `f32::MAX` (i.e. "unconstrained")
2244    // tells the runtime's flex algorithm to behave as if items don't
2245    // need to wrap, which gives the natural max-cell-cross-axis result
2246    // — much closer to correct than the `sqrt(item-areas)` heuristic
2247    // that a `-1` sentinel would trigger.
2248    let root = &instance_ref.description.original.root_element;
2249    let cross_axis_constraint = match orientation {
2250        i_slint_compiler::layout::Orientation::Vertical => {
2251            root.borrow().layout_info_v_with_constraint.is_some().then_some(f32::MAX)
2252        }
2253        i_slint_compiler::layout::Orientation::Horizontal => {
2254            root.borrow().layout_info_h_with_constraint.is_some().then_some(f32::MAX)
2255        }
2256    };
2257    let mut result = crate::eval_layout::get_layout_info_with_constraint(
2258        root,
2259        instance_ref,
2260        &instance_ref.window_adapter(),
2261        orientation,
2262        cross_axis_constraint,
2263    );
2264
2265    let constraints = instance_ref.description.original.root_constraints.borrow();
2266    if constraints.has_explicit_restrictions(orientation) {
2267        crate::eval_layout::fill_layout_info_constraints(
2268            &mut result,
2269            &constraints,
2270            orientation,
2271            &|nr: &NamedReference| {
2272                eval::load_property(instance_ref, &nr.element(), nr.name())
2273                    .unwrap()
2274                    .try_into()
2275                    .unwrap()
2276            },
2277        );
2278    }
2279    result
2280}
2281
2282#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2283unsafe extern "C" fn get_item_ref(component: ItemTreeRefPin, index: u32) -> Pin<ItemRef> {
2284    let tree = get_item_tree(component);
2285    match &tree[index as usize] {
2286        ItemTreeNode::Item { item_array_index, .. } => unsafe {
2287            generativity::make_guard!(guard);
2288            let instance_ref = InstanceRef::from_pin_ref(component, guard);
2289            core::mem::transmute::<Pin<ItemRef>, Pin<ItemRef>>(
2290                instance_ref.description.item_array[*item_array_index as usize]
2291                    .apply_pin(instance_ref.instance),
2292            )
2293        },
2294        ItemTreeNode::DynamicTree { .. } => panic!("get_item_ref called on dynamic tree"),
2295    }
2296}
2297
2298#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2299extern "C" fn get_subtree_range(component: ItemTreeRefPin, index: u32) -> IndexRange {
2300    generativity::make_guard!(guard);
2301    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2302    if index as usize >= instance_ref.description.repeater.len() {
2303        let container_index = {
2304            let tree_node = &component.as_ref().get_item_tree()[index as usize];
2305            if let ItemTreeNode::DynamicTree { parent_index, .. } = tree_node {
2306                *parent_index
2307            } else {
2308                u32::MAX
2309            }
2310        };
2311        let container = component.as_ref().get_item_ref(container_index);
2312        let container = i_slint_core::items::ItemRef::downcast_pin::<
2313            i_slint_core::items::ComponentContainer,
2314        >(container)
2315        .unwrap();
2316        container.subtree_range()
2317    } else {
2318        generativity::make_guard!(guard);
2319        let rep_in_comp = instance_ref.description.repeater[index as usize].unerase(guard);
2320
2321        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
2322        repeater.track_instance_changes();
2323        repeater.range().into()
2324    }
2325}
2326
2327#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2328extern "C" fn get_subtree(
2329    component: ItemTreeRefPin,
2330    index: u32,
2331    subtree_index: usize,
2332    result: &mut ItemTreeWeak,
2333) {
2334    generativity::make_guard!(guard);
2335    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2336    if index as usize >= instance_ref.description.repeater.len() {
2337        let container_index = {
2338            let tree_node = &component.as_ref().get_item_tree()[index as usize];
2339            if let ItemTreeNode::DynamicTree { parent_index, .. } = tree_node {
2340                *parent_index
2341            } else {
2342                u32::MAX
2343            }
2344        };
2345        let container = component.as_ref().get_item_ref(container_index);
2346        let container = i_slint_core::items::ItemRef::downcast_pin::<
2347            i_slint_core::items::ComponentContainer,
2348        >(container)
2349        .unwrap();
2350        if subtree_index == 0 {
2351            *result = container.subtree_component();
2352        }
2353    } else {
2354        generativity::make_guard!(guard);
2355        let rep_in_comp = instance_ref.description.repeater[index as usize].unerase(guard);
2356
2357        let repeater = rep_in_comp.offset.apply(&instance_ref.instance);
2358        if let Some(instance_at) = repeater.instance_at(subtree_index) {
2359            *result = vtable::VRc::downgrade(&vtable::VRc::into_dyn(instance_at))
2360        }
2361    }
2362}
2363
2364#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2365extern "C" fn get_item_tree(component: ItemTreeRefPin) -> Slice<ItemTreeNode> {
2366    generativity::make_guard!(guard);
2367    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2368    let tree = instance_ref.description.item_tree.as_slice();
2369    unsafe { core::mem::transmute::<&[ItemTreeNode], &[ItemTreeNode]>(tree) }.into()
2370}
2371
2372#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2373extern "C" fn subtree_index(component: ItemTreeRefPin) -> usize {
2374    generativity::make_guard!(guard);
2375    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2376    if let Ok(value) = instance_ref.description.get_property(component, SPECIAL_PROPERTY_INDEX) {
2377        value.try_into().unwrap()
2378    } else {
2379        usize::MAX
2380    }
2381}
2382
2383#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2384unsafe extern "C" fn parent_node(component: ItemTreeRefPin, result: &mut ItemWeak) {
2385    generativity::make_guard!(guard);
2386    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2387
2388    let component_and_index = {
2389        // Normal inner-compilation unit case:
2390        if let Some(parent_offset) = instance_ref.description.parent_item_tree_offset {
2391            let parent_item_index = instance_ref
2392                .description
2393                .original
2394                .parent_element
2395                .borrow()
2396                .upgrade()
2397                .and_then(|e| e.borrow().item_index.get().cloned())
2398                .unwrap_or(u32::MAX);
2399            let parent_component = parent_offset
2400                .apply(instance_ref.as_ref())
2401                .get()
2402                .and_then(|p| p.upgrade())
2403                .map(vtable::VRc::into_dyn);
2404
2405            (parent_component, parent_item_index)
2406        } else if let Some((parent_component, parent_index)) = instance_ref
2407            .description
2408            .extra_data_offset
2409            .apply(instance_ref.as_ref())
2410            .embedding_position
2411            .get()
2412        {
2413            (parent_component.upgrade(), *parent_index)
2414        } else {
2415            (None, u32::MAX)
2416        }
2417    };
2418
2419    if let (Some(component), index) = component_and_index {
2420        *result = ItemRc::new(component, index).downgrade();
2421    }
2422}
2423
2424#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2425unsafe extern "C" fn embed_component(
2426    component: ItemTreeRefPin,
2427    parent_component: &ItemTreeWeak,
2428    parent_item_tree_index: u32,
2429) -> bool {
2430    generativity::make_guard!(guard);
2431    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2432
2433    if instance_ref.description.parent_item_tree_offset.is_some() {
2434        // We are not the root of the compilation unit tree... Can not embed this!
2435        return false;
2436    }
2437
2438    {
2439        // sanity check parent:
2440        let prc = parent_component.upgrade().unwrap();
2441        let pref = vtable::VRc::borrow_pin(&prc);
2442        let it = pref.as_ref().get_item_tree();
2443        if !matches!(
2444            it.get(parent_item_tree_index as usize),
2445            Some(ItemTreeNode::DynamicTree { .. })
2446        ) {
2447            panic!("Trying to embed into a non-dynamic index in the parents item tree")
2448        }
2449    }
2450
2451    let extra_data = instance_ref.description.extra_data_offset.apply(instance_ref.as_ref());
2452    extra_data.embedding_position.set((parent_component.clone(), parent_item_tree_index)).is_ok()
2453}
2454
2455#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2456extern "C" fn item_geometry(component: ItemTreeRefPin, item_index: u32) -> LogicalRect {
2457    generativity::make_guard!(guard);
2458    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2459
2460    let e = instance_ref.description.original_elements[item_index as usize].borrow();
2461    let g = e.geometry_props.as_ref().unwrap();
2462
2463    let load_f32 = |nr: &NamedReference| -> f32 {
2464        crate::eval::load_property(instance_ref, &nr.element(), nr.name())
2465            .unwrap()
2466            .try_into()
2467            .unwrap()
2468    };
2469
2470    LogicalRect {
2471        origin: (load_f32(&g.x), load_f32(&g.y)).into(),
2472        size: (load_f32(&g.width), load_f32(&g.height)).into(),
2473    }
2474}
2475
2476// silence the warning despite `AccessibleRole` is a `#[non_exhaustive]` enum from another crate.
2477#[allow(improper_ctypes_definitions)]
2478#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2479extern "C" fn accessible_role(component: ItemTreeRefPin, item_index: u32) -> AccessibleRole {
2480    generativity::make_guard!(guard);
2481    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2482    let nr = instance_ref.description.original_elements[item_index as usize]
2483        .borrow()
2484        .accessibility_props
2485        .0
2486        .get("accessible-role")
2487        .cloned();
2488    match nr {
2489        Some(nr) => crate::eval::load_property(instance_ref, &nr.element(), nr.name())
2490            .unwrap()
2491            .try_into()
2492            .unwrap(),
2493        None => AccessibleRole::default(),
2494    }
2495}
2496
2497#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2498extern "C" fn accessible_string_property(
2499    component: ItemTreeRefPin,
2500    item_index: u32,
2501    what: AccessibleStringProperty,
2502    result: &mut SharedString,
2503) -> bool {
2504    generativity::make_guard!(guard);
2505    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2506    let prop_name = format!("accessible-{what}");
2507    let nr = instance_ref.description.original_elements[item_index as usize]
2508        .borrow()
2509        .accessibility_props
2510        .0
2511        .get(&prop_name)
2512        .cloned();
2513    if let Some(nr) = nr {
2514        let value = crate::eval::load_property(instance_ref, &nr.element(), nr.name()).unwrap();
2515        match value {
2516            Value::String(s) => *result = s,
2517            Value::Bool(b) => *result = if b { "true" } else { "false" }.into(),
2518            Value::Number(x) => *result = x.to_string().into(),
2519            Value::EnumerationValue(_, v) => *result = v.into(),
2520            _ => unimplemented!("invalid type for accessible_string_property"),
2521        };
2522        true
2523    } else {
2524        false
2525    }
2526}
2527
2528#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2529extern "C" fn accessibility_action(
2530    component: ItemTreeRefPin,
2531    item_index: u32,
2532    action: &AccessibilityAction,
2533) {
2534    let perform = |prop_name, args: &[Value]| {
2535        generativity::make_guard!(guard);
2536        let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2537        let nr = instance_ref.description.original_elements[item_index as usize]
2538            .borrow()
2539            .accessibility_props
2540            .0
2541            .get(prop_name)
2542            .cloned();
2543        if let Some(nr) = nr {
2544            let instance_ref = eval::ComponentInstance::InstanceRef(instance_ref);
2545            crate::eval::invoke_callback(&instance_ref, &nr.element(), nr.name(), args).unwrap();
2546        }
2547    };
2548
2549    match action {
2550        AccessibilityAction::Default => perform("accessible-action-default", &[]),
2551        AccessibilityAction::Decrement => perform("accessible-action-decrement", &[]),
2552        AccessibilityAction::Increment => perform("accessible-action-increment", &[]),
2553        AccessibilityAction::Expand => perform("accessible-action-expand", &[]),
2554        AccessibilityAction::ReplaceSelectedText(_a) => {
2555            //perform("accessible-action-replace-selected-text", &[Value::String(a.clone())])
2556            i_slint_core::debug_log!(
2557                "AccessibilityAction::ReplaceSelectedText not implemented in interpreter's accessibility_action"
2558            );
2559        }
2560        AccessibilityAction::SetValue(a) => {
2561            perform("accessible-action-set-value", &[Value::String(a.clone())])
2562        }
2563    };
2564}
2565
2566#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2567extern "C" fn supported_accessibility_actions(
2568    component: ItemTreeRefPin,
2569    item_index: u32,
2570) -> SupportedAccessibilityAction {
2571    generativity::make_guard!(guard);
2572    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2573    instance_ref.description.original_elements[item_index as usize]
2574        .borrow()
2575        .accessibility_props
2576        .0
2577        .keys()
2578        .filter_map(|x| x.strip_prefix("accessible-action-"))
2579        .fold(SupportedAccessibilityAction::default(), |acc, value| {
2580            SupportedAccessibilityAction::from_name(&i_slint_compiler::generator::to_pascal_case(
2581                value,
2582            ))
2583            .unwrap_or_else(|| panic!("Not an accessible action: {value:?}"))
2584                | acc
2585        })
2586}
2587
2588#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2589extern "C" fn item_element_infos(
2590    component: ItemTreeRefPin,
2591    item_index: u32,
2592    result: &mut SharedString,
2593) -> bool {
2594    generativity::make_guard!(guard);
2595    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2596    *result = instance_ref.description.original_elements[item_index as usize]
2597        .borrow()
2598        .element_infos()
2599        .into();
2600    true
2601}
2602
2603#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2604extern "C" fn window_adapter(
2605    component: ItemTreeRefPin,
2606    do_create: bool,
2607    result: &mut Option<WindowAdapterRc>,
2608) {
2609    generativity::make_guard!(guard);
2610    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2611    if do_create {
2612        *result = Some(instance_ref.window_adapter());
2613    } else {
2614        *result = instance_ref.maybe_window_adapter();
2615    }
2616}
2617
2618#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2619unsafe extern "C" fn drop_in_place(component: vtable::VRefMut<ItemTreeVTable>) -> vtable::Layout {
2620    unsafe {
2621        let instance_ptr = component.as_ptr() as *mut Instance<'static>;
2622        let layout = (*instance_ptr).type_info().layout();
2623        dynamic_type::TypeInfo::drop_in_place(instance_ptr);
2624        layout.into()
2625    }
2626}
2627
2628#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2629unsafe extern "C" fn dealloc(_vtable: &ItemTreeVTable, ptr: *mut u8, layout: vtable::Layout) {
2630    unsafe { std::alloc::dealloc(ptr, layout.try_into().unwrap()) };
2631}
2632
2633#[derive(Copy, Clone)]
2634pub struct InstanceRef<'a, 'id> {
2635    pub instance: Pin<&'a Instance<'id>>,
2636    pub description: &'a ItemTreeDescription<'id>,
2637}
2638
2639impl<'a, 'id> InstanceRef<'a, 'id> {
2640    pub unsafe fn from_pin_ref(
2641        component: ItemTreeRefPin<'a>,
2642        _guard: generativity::Guard<'id>,
2643    ) -> Self {
2644        unsafe {
2645            Self {
2646                instance: Pin::new_unchecked(
2647                    &*(component.as_ref().as_ptr() as *const Instance<'id>),
2648                ),
2649                description: &*(Pin::into_inner_unchecked(component).get_vtable()
2650                    as *const ItemTreeVTable
2651                    as *const ItemTreeDescription<'id>),
2652            }
2653        }
2654    }
2655
2656    pub fn as_ptr(&self) -> *const u8 {
2657        (&*self.instance.as_ref()) as *const Instance as *const u8
2658    }
2659
2660    pub fn as_ref(&self) -> &Instance<'id> {
2661        &self.instance
2662    }
2663
2664    /// Borrow this component as a `Pin<ItemTreeRef>`
2665    pub fn borrow(self) -> ItemTreeRefPin<'a> {
2666        unsafe {
2667            Pin::new_unchecked(vtable::VRef::from_raw(
2668                NonNull::from(&self.description.ct).cast(),
2669                NonNull::from(self.instance.get_ref()).cast(),
2670            ))
2671        }
2672    }
2673
2674    pub fn self_weak(&self) -> &OnceCell<ErasedItemTreeBoxWeak> {
2675        let extra_data = self.description.extra_data_offset.apply(self.as_ref());
2676        &extra_data.self_weak
2677    }
2678
2679    pub fn root_weak(&self) -> &ErasedItemTreeBoxWeak {
2680        self.description.root_offset.apply(self.as_ref()).get().unwrap()
2681    }
2682
2683    pub fn window_adapter(&self) -> WindowAdapterRc {
2684        let root_weak = vtable::VWeak::into_dyn(self.root_weak().clone());
2685        let root = self.root_weak().upgrade().unwrap();
2686        generativity::make_guard!(guard);
2687        let comp = root.unerase(guard);
2688        Self::get_or_init_window_adapter_ref(
2689            &comp.description,
2690            root_weak,
2691            true,
2692            comp.instance.as_pin_ref().get_ref(),
2693        )
2694        .unwrap()
2695        .clone()
2696    }
2697
2698    pub fn get_or_init_window_adapter_ref<'b, 'id2>(
2699        description: &'b ItemTreeDescription<'id2>,
2700        root_weak: ItemTreeWeak,
2701        do_create: bool,
2702        instance: &'b Instance<'id2>,
2703    ) -> Result<&'b WindowAdapterRc, PlatformError> {
2704        // We are the actual root: Generate and store a window_adapter if necessary
2705        description
2706            .extra_data_offset
2707            .apply(instance)
2708            .globals
2709            .get()
2710            .unwrap()
2711            .window_adapter()
2712            .unwrap()
2713            .get_or_try_init(|| {
2714                let mut parent_node = ItemWeak::default();
2715                if let Some(rc) = vtable::VWeak::upgrade(&root_weak) {
2716                    vtable::VRc::borrow_pin(&rc).as_ref().parent_node(&mut parent_node);
2717                }
2718
2719                if let Some(parent) = parent_node.upgrade() {
2720                    // We are embedded: Get window adapter from our parent
2721                    let mut result = None;
2722                    vtable::VRc::borrow_pin(parent.item_tree())
2723                        .as_ref()
2724                        .window_adapter(do_create, &mut result);
2725                    result.ok_or(PlatformError::NoPlatform)
2726                } else if do_create {
2727                    let extra_data = description.extra_data_offset.apply(instance);
2728                    let window_adapter = // We are the root: Create a window adapter
2729                    i_slint_backend_selector::with_platform(|_b| {
2730                        _b.create_window_adapter()
2731                    })?;
2732
2733                    let comp_rc = extra_data.self_weak.get().unwrap().upgrade().unwrap();
2734                    WindowInner::from_pub(window_adapter.window())
2735                        .set_component(&vtable::VRc::into_dyn(comp_rc));
2736                    Ok(window_adapter)
2737                } else {
2738                    Err(PlatformError::NoPlatform)
2739                }
2740            })
2741    }
2742
2743    pub fn maybe_window_adapter(&self) -> Option<WindowAdapterRc> {
2744        let root_weak = vtable::VWeak::into_dyn(self.root_weak().clone());
2745        let root = self.root_weak().upgrade()?;
2746        generativity::make_guard!(guard);
2747        let comp = root.unerase(guard);
2748        Self::get_or_init_window_adapter_ref(
2749            &comp.description,
2750            root_weak,
2751            false,
2752            comp.instance.as_pin_ref().get_ref(),
2753        )
2754        .ok()
2755        .cloned()
2756    }
2757
2758    pub fn access_window<R>(
2759        self,
2760        callback: impl FnOnce(&'_ i_slint_core::window::WindowInner) -> R,
2761    ) -> R {
2762        callback(WindowInner::from_pub(self.window_adapter().window()))
2763    }
2764
2765    pub fn parent_instance<'id2>(
2766        &self,
2767        _guard: generativity::Guard<'id2>,
2768    ) -> Option<InstanceRef<'a, 'id2>> {
2769        // we need a 'static guard in order to be able to re-borrow with lifetime 'a.
2770        // Safety: This is the only 'static Id in scope.
2771        if let Some(parent_offset) = self.description.parent_item_tree_offset
2772            && let Some(parent) =
2773                parent_offset.apply(self.as_ref()).get().and_then(vtable::VWeak::upgrade)
2774        {
2775            let parent_instance = parent.unerase(_guard);
2776            // And also assume that the parent lives for at least 'a.  FIXME: this may not be sound
2777            let parent_instance = unsafe {
2778                std::mem::transmute::<InstanceRef<'_, 'id2>, InstanceRef<'a, 'id2>>(
2779                    parent_instance.borrow_instance(),
2780                )
2781            };
2782            return Some(parent_instance);
2783        }
2784        None
2785    }
2786}
2787
2788/// Show the popup with a lazily evaluated location.
2789pub fn show_popup(
2790    element: ElementRc,
2791    instance: InstanceRef,
2792    popup: &object_tree::PopupWindow,
2793    pos_getter: impl Fn(InstanceRef<'_, '_>) -> LogicalPosition + 'static,
2794    close_policy: PopupClosePolicy,
2795    parent_comp: ErasedItemTreeBoxWeak,
2796    parent_window_adapter: WindowAdapterRc,
2797    parent_item: &ItemRc,
2798) {
2799    generativity::make_guard!(guard);
2800    let debug_handler = instance.description.debug_handler.borrow().clone();
2801
2802    // FIXME: we should compile once and keep the cached compiled component
2803    let compiled = generate_item_tree(
2804        &popup.component,
2805        None,
2806        parent_comp.upgrade().unwrap().0.description().popup_menu_description.clone(),
2807        false,
2808        guard,
2809    );
2810    compiled.recursively_set_debug_handler(debug_handler);
2811
2812    let extra_data = instance.description.extra_data_offset.apply(instance.as_ref());
2813    // Use the newly created window adapter if we are able to create one. Otherwise use the parent's one.
2814    // Tooltips skip this to share the parent's adapter, ensuring they use the ChildWindow path
2815    // and renderer caches stay consistent.
2816    let window_kind = if popup.is_tooltip { WindowKind::ToolTip } else { WindowKind::Popup };
2817    let globals = if let Some(window_adapter) =
2818        WindowInner::from_pub(parent_window_adapter.window())
2819            .create_child_window_adapter(window_kind)
2820    {
2821        extra_data.globals.get().unwrap().clone_with_window_adapter(window_adapter)
2822    } else {
2823        extra_data.globals.get().unwrap().clone()
2824    };
2825
2826    let popup_window_adapter = globals
2827        .window_adapter()
2828        .and_then(|window_adapter| window_adapter.get().cloned())
2829        .unwrap_or_else(|| parent_window_adapter.clone());
2830
2831    let inst = instantiate(
2832        compiled,
2833        Some(parent_comp),
2834        None,
2835        Some(&WindowOptions::UseExistingWindow(popup_window_adapter)),
2836        globals,
2837    );
2838    let inst_for_position = inst.clone();
2839    let access_position = Box::new(move || {
2840        generativity::make_guard!(guard);
2841        let compo_box = inst_for_position.unerase(guard);
2842        let instance_ref = compo_box.borrow_instance();
2843        pos_getter(instance_ref)
2844    });
2845    close_popup(element.clone(), instance, parent_window_adapter.clone());
2846    let window_kind = if popup.is_tooltip { WindowKind::ToolTip } else { WindowKind::Popup };
2847    instance.description.popup_ids.borrow_mut().insert(
2848        element.borrow().id.clone(),
2849        WindowInner::from_pub(parent_window_adapter.window()).show_popup(
2850            &vtable::VRc::into_dyn(inst.clone()),
2851            access_position,
2852            close_policy,
2853            parent_item,
2854            window_kind,
2855        ),
2856    );
2857    inst.run_setup_code();
2858}
2859
2860pub fn close_popup(
2861    element: ElementRc,
2862    instance: InstanceRef,
2863    parent_window_adapter: WindowAdapterRc,
2864) {
2865    if let Some(current_id) =
2866        instance.description.popup_ids.borrow_mut().remove(&element.borrow().id)
2867    {
2868        WindowInner::from_pub(parent_window_adapter.window()).close_popup(current_id);
2869    }
2870}
2871
2872pub fn make_menu_item_tree(
2873    menu_item_tree: &Rc<object_tree::Component>,
2874    enclosing_component: &InstanceRef,
2875    condition: Option<&Expression>,
2876    visible: Option<&Expression>,
2877) -> vtable::VRc<i_slint_core::menus::MenuVTable, MenuFromItemTree> {
2878    generativity::make_guard!(guard);
2879    let mit_compiled = generate_item_tree(
2880        menu_item_tree,
2881        None,
2882        enclosing_component.description.popup_menu_description.clone(),
2883        false,
2884        guard,
2885    );
2886    let enclosing_component_weak = enclosing_component.self_weak().get().unwrap();
2887    let extra_data =
2888        enclosing_component.description.extra_data_offset.apply(enclosing_component.as_ref());
2889    let mit_inst = instantiate(
2890        mit_compiled.clone(),
2891        Some(enclosing_component_weak.clone()),
2892        None,
2893        None,
2894        extra_data.globals.get().unwrap().clone(),
2895    );
2896    mit_inst.run_setup_code();
2897    let item_tree = vtable::VRc::into_dyn(mit_inst);
2898    let condition = condition.map(|condition| {
2899        let binding =
2900            make_binding_eval_closure(condition.clone(), enclosing_component_weak.clone());
2901        move || binding().try_into().unwrap()
2902    });
2903    let visible = visible.map(|visible| {
2904        let binding = make_binding_eval_closure(visible.clone(), enclosing_component_weak.clone());
2905        move || binding().try_into().unwrap()
2906    });
2907    let menu = match (condition, visible) {
2908        (None, None) => MenuFromItemTree::new(item_tree),
2909        (None, Some(visible)) => {
2910            MenuFromItemTree::new_with_condition_and_visible(item_tree, || true, visible)
2911        }
2912        (Some(condition), None) => {
2913            MenuFromItemTree::new_with_condition_and_visible(item_tree, condition, || true)
2914        }
2915        (Some(condition), Some(visible)) => {
2916            MenuFromItemTree::new_with_condition_and_visible(item_tree, condition, visible)
2917        }
2918    };
2919    vtable::VRc::new(menu)
2920}
2921
2922pub fn update_timers(instance: InstanceRef) {
2923    let ts = instance.description.original.timers.borrow();
2924    for (desc, offset) in ts.iter().zip(&instance.description.timers) {
2925        let timer = offset.apply(instance.as_ref());
2926        let running =
2927            eval::load_property(instance, &desc.running.element(), desc.running.name()).unwrap();
2928        if matches!(running, Value::Bool(true)) {
2929            let millis: i64 =
2930                eval::load_property(instance, &desc.interval.element(), desc.interval.name())
2931                    .unwrap()
2932                    .try_into()
2933                    .expect("interval must be a duration");
2934            if millis < 0 {
2935                timer.stop();
2936                continue;
2937            }
2938            let interval = core::time::Duration::from_millis(millis as _);
2939            if !timer.running() || interval != timer.interval() {
2940                let callback = desc.triggered.clone();
2941                let self_weak = instance.self_weak().get().unwrap().clone();
2942                timer.start(i_slint_core::timers::TimerMode::Repeated, interval, move || {
2943                    if let Some(instance) = self_weak.upgrade() {
2944                        generativity::make_guard!(guard);
2945                        let c = instance.unerase(guard);
2946                        let c = c.borrow_instance();
2947                        let inst = eval::ComponentInstance::InstanceRef(c);
2948                        eval::invoke_callback(&inst, &callback.element(), callback.name(), &[])
2949                            .unwrap();
2950                    }
2951                });
2952            }
2953        } else {
2954            timer.stop();
2955        }
2956    }
2957}
2958
2959pub fn restart_timer(element: ElementWeak, instance: InstanceRef) {
2960    let timers = instance.description.original.timers.borrow();
2961    if let Some((_, offset)) = timers
2962        .iter()
2963        .zip(&instance.description.timers)
2964        .find(|(desc, _)| Weak::ptr_eq(&desc.element, &element))
2965    {
2966        let timer = offset.apply(instance.as_ref());
2967        timer.restart();
2968    }
2969}