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")]
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")]
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")]
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
1325            | Type::Predicate => panic!("bad type {ty:?} for property {name}"),
1326        })
1327    }
1328
1329    for (name, decl) in &component.root_element.borrow().property_declarations {
1330        if decl.is_alias.is_some() {
1331            continue;
1332        }
1333        if matches!(&decl.property_type, Type::Callback { .. }) {
1334            custom_callbacks
1335                .insert(name.clone(), builder.type_builder.add_field_type::<Callback>());
1336            if decl.expose_in_public_api {
1337                callback_trackers
1338                    .insert(name.clone(), builder.type_builder.add_field_type::<Property<()>>());
1339            }
1340            continue;
1341        }
1342        let Some((prop, type_info)) = property_info_for_type(&decl.property_type, name) else {
1343            continue;
1344        };
1345        custom_properties.insert(
1346            name.clone(),
1347            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1348        );
1349    }
1350    if let Some(parent_element) = component.parent_element()
1351        && let Some(r) = &parent_element.borrow().repeated
1352        && !r.is_conditional_element
1353    {
1354        let (prop, type_info) = property_info::<u32>();
1355        custom_properties.insert(
1356            SPECIAL_PROPERTY_INDEX.into(),
1357            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1358        );
1359
1360        let model_ty = Expression::RepeaterModelReference {
1361            element: component.parent_element.borrow().clone(),
1362        }
1363        .ty();
1364        let (prop, type_info) =
1365            property_info_for_type(&model_ty, SPECIAL_PROPERTY_MODEL_DATA).unwrap();
1366        custom_properties.insert(
1367            SPECIAL_PROPERTY_MODEL_DATA.into(),
1368            PropertiesWithinComponent { offset: builder.type_builder.add_field(type_info), prop },
1369        );
1370    }
1371
1372    let parent_item_tree_offset = if component.parent_element().is_some() || is_popup_menu_impl {
1373        Some(builder.type_builder.add_field_type::<OnceCell<ErasedItemTreeBoxWeak>>())
1374    } else {
1375        None
1376    };
1377
1378    let root_offset = builder.type_builder.add_field_type::<OnceCell<ErasedItemTreeBoxWeak>>();
1379    let extra_data_offset = builder.type_builder.add_field_type::<ComponentExtraData>();
1380
1381    let change_trackers = (!builder.change_callbacks.is_empty()).then(|| {
1382        (
1383            builder.type_builder.add_field_type::<OnceCell<Vec<ChangeTracker>>>(),
1384            builder.change_callbacks,
1385        )
1386    });
1387    let timers = component
1388        .timers
1389        .borrow()
1390        .iter()
1391        .map(|_| builder.type_builder.add_field_type::<Timer>())
1392        .collect();
1393
1394    // only the public exported component needs the public property list
1395    let public_properties = if component.parent_element().is_none() {
1396        component.root_element.borrow().property_declarations.clone()
1397    } else {
1398        Default::default()
1399    };
1400
1401    let t = ItemTreeVTable {
1402        visit_children_item,
1403        layout_info,
1404        ensure_instantiated,
1405        get_item_ref,
1406        get_item_tree,
1407        get_subtree_range,
1408        get_subtree,
1409        parent_node,
1410        embed_component,
1411        subtree_index,
1412        item_geometry,
1413        accessible_role,
1414        accessible_string_property,
1415        accessibility_action,
1416        supported_accessibility_actions,
1417        item_element_infos,
1418        window_adapter,
1419        drop_in_place,
1420        dealloc,
1421    };
1422    let t = ItemTreeDescription {
1423        ct: t,
1424        dynamic_type: builder.type_builder.build(),
1425        item_tree: builder.tree_array,
1426        item_array: builder.item_array,
1427        items: builder.items_types,
1428        custom_properties,
1429        custom_callbacks,
1430        callback_trackers,
1431        original: component.clone(),
1432        original_elements: builder.original_elements,
1433        repeater: builder.repeater,
1434        repeater_names: builder.repeater_names,
1435        parent_item_tree_offset,
1436        root_offset,
1437        extra_data_offset,
1438        public_properties,
1439        compiled_globals,
1440        change_trackers,
1441        timers,
1442        popup_ids: std::cell::RefCell::new(HashMap::new()),
1443        popup_menu_description: builder.popup_menu_description,
1444        #[cfg(feature = "internal-highlight")]
1445        type_loader: std::cell::OnceCell::new(),
1446        #[cfg(feature = "internal-highlight")]
1447        raw_type_loader: std::cell::OnceCell::new(),
1448        debug_handler: std::cell::RefCell::new(Rc::new(|_, text| {
1449            i_slint_core::debug_log!("{text}")
1450        })),
1451    };
1452
1453    Rc::new(t)
1454}
1455
1456pub fn animation_for_property(
1457    component: InstanceRef,
1458    animation: &Option<i_slint_compiler::object_tree::PropertyAnimation>,
1459) -> AnimatedBindingKind {
1460    match animation {
1461        Some(i_slint_compiler::object_tree::PropertyAnimation::Static(anim_elem)) => {
1462            AnimatedBindingKind::Animation(Box::new({
1463                let component_ptr = component.as_ptr();
1464                let vtable = NonNull::from(&component.description.ct).cast();
1465                let anim_elem = Rc::clone(anim_elem);
1466                move || -> PropertyAnimation {
1467                    generativity::make_guard!(guard);
1468                    let component = unsafe {
1469                        InstanceRef::from_pin_ref(
1470                            Pin::new_unchecked(vtable::VRef::from_raw(
1471                                vtable,
1472                                NonNull::new_unchecked(component_ptr as *mut u8),
1473                            )),
1474                            guard,
1475                        )
1476                    };
1477
1478                    eval::new_struct_with_bindings(
1479                        &anim_elem.borrow().bindings,
1480                        &mut eval::EvalLocalContext::from_component_instance(component),
1481                    )
1482                }
1483            }))
1484        }
1485        Some(i_slint_compiler::object_tree::PropertyAnimation::Transition {
1486            animations,
1487            state_ref,
1488        }) => {
1489            let component_ptr = component.as_ptr();
1490            let vtable = NonNull::from(&component.description.ct).cast();
1491            let animations = animations.clone();
1492            let state_ref = state_ref.clone();
1493            AnimatedBindingKind::Transition(Box::new(
1494                move || -> (PropertyAnimation, i_slint_core::animations::Instant) {
1495                    generativity::make_guard!(guard);
1496                    let component = unsafe {
1497                        InstanceRef::from_pin_ref(
1498                            Pin::new_unchecked(vtable::VRef::from_raw(
1499                                vtable,
1500                                NonNull::new_unchecked(component_ptr as *mut u8),
1501                            )),
1502                            guard,
1503                        )
1504                    };
1505
1506                    let mut context = eval::EvalLocalContext::from_component_instance(component);
1507                    let state = eval::eval_expression(&state_ref, &mut context);
1508                    let state_info: i_slint_core::properties::StateInfo = state.try_into().unwrap();
1509                    for a in &animations {
1510                        let is_previous_state = a.state_id == state_info.previous_state;
1511                        let is_current_state = a.state_id == state_info.current_state;
1512                        match (a.direction, is_previous_state, is_current_state) {
1513                            (TransitionDirection::In, false, true)
1514                            | (TransitionDirection::Out, true, false)
1515                            | (TransitionDirection::InOut, false, true)
1516                            | (TransitionDirection::InOut, true, false) => {
1517                                return (
1518                                    eval::new_struct_with_bindings(
1519                                        &a.animation.borrow().bindings,
1520                                        &mut context,
1521                                    ),
1522                                    state_info.change_time,
1523                                );
1524                            }
1525                            _ => {}
1526                        }
1527                    }
1528                    Default::default()
1529                },
1530            ))
1531        }
1532        None => AnimatedBindingKind::NotAnimated,
1533    }
1534}
1535
1536fn make_callback_eval_closure(
1537    expr: Expression,
1538    self_weak: ErasedItemTreeBoxWeak,
1539) -> impl Fn(&[Value]) -> Value {
1540    move |args| {
1541        let self_rc = self_weak.upgrade().unwrap();
1542        generativity::make_guard!(guard);
1543        let self_ = self_rc.unerase(guard);
1544        let instance_ref = self_.borrow_instance();
1545        let mut local_context =
1546            eval::EvalLocalContext::from_function_arguments(instance_ref, args.to_vec());
1547        eval::eval_expression(&expr, &mut local_context)
1548    }
1549}
1550
1551fn make_binding_eval_closure(
1552    expr: Expression,
1553    self_weak: ErasedItemTreeBoxWeak,
1554) -> impl Fn() -> Value {
1555    move || {
1556        let self_rc = self_weak.upgrade().unwrap();
1557        generativity::make_guard!(guard);
1558        let self_ = self_rc.unerase(guard);
1559        let instance_ref = self_.borrow_instance();
1560        eval::eval_expression(
1561            &expr,
1562            &mut eval::EvalLocalContext::from_component_instance(instance_ref),
1563        )
1564    }
1565}
1566
1567pub fn instantiate(
1568    description: Rc<ItemTreeDescription>,
1569    parent_ctx: Option<ErasedItemTreeBoxWeak>,
1570    root: Option<ErasedItemTreeBoxWeak>,
1571    window_options: Option<&WindowOptions>,
1572    globals: crate::global_component::GlobalStorage,
1573) -> DynamicComponentVRc {
1574    let instance = description.dynamic_type.clone().create_instance();
1575
1576    let component_box = ItemTreeBox { instance, description: description.clone() };
1577
1578    let self_rc = vtable::VRc::new(ErasedItemTreeBox::from(component_box));
1579    let self_weak = vtable::VRc::downgrade(&self_rc);
1580
1581    generativity::make_guard!(guard);
1582    let comp = self_rc.unerase(guard);
1583    let instance_ref = comp.borrow_instance();
1584    instance_ref.self_weak().set(self_weak.clone()).ok();
1585    let description = comp.description();
1586
1587    if let Some(WindowOptions::UseExistingWindow(existing_adapter)) = &window_options
1588        && let Err((a, b)) = globals.window_adapter().unwrap().try_insert(existing_adapter.clone())
1589    {
1590        assert!(Rc::ptr_eq(a, &b), "window not the same as parent window");
1591    }
1592
1593    if let Some(parent) = parent_ctx {
1594        description
1595            .parent_item_tree_offset
1596            .unwrap()
1597            .apply(instance_ref.as_ref())
1598            .set(parent)
1599            .ok()
1600            .unwrap();
1601    } else if let Some(g) = description.compiled_globals.as_ref() {
1602        for g in g.compiled_globals.iter() {
1603            crate::global_component::instantiate(g, &globals, self_weak.clone());
1604        }
1605    }
1606    let extra_data = description.extra_data_offset.apply(instance_ref.as_ref());
1607    extra_data.globals.set(globals).ok().unwrap();
1608    if let Some(WindowOptions::Embed { parent_item_tree, parent_item_tree_index }) = window_options
1609    {
1610        vtable::VRc::borrow_pin(&self_rc)
1611            .as_ref()
1612            .embed_component(parent_item_tree, *parent_item_tree_index);
1613        description.root_offset.apply(instance_ref.as_ref()).set(self_weak.clone()).ok().unwrap();
1614    } else {
1615        generativity::make_guard!(guard);
1616        let root = root
1617            .or_else(|| {
1618                instance_ref.parent_instance(guard).map(|parent| parent.root_weak().clone())
1619            })
1620            .unwrap_or_else(|| self_weak.clone());
1621        description.root_offset.apply(instance_ref.as_ref()).set(root).ok().unwrap();
1622    }
1623
1624    if !description.original.is_global() {
1625        let maybe_window_adapter =
1626            if let Some(WindowOptions::UseExistingWindow(adapter)) = window_options.as_ref() {
1627                Some(adapter.clone())
1628            } else {
1629                extra_data.globals.get().unwrap().window_adapter().and_then(|wa| wa.get().cloned())
1630            };
1631
1632        let component_rc = vtable::VRc::into_dyn(self_rc.clone());
1633        i_slint_core::item_tree::register_item_tree(&component_rc, maybe_window_adapter);
1634    }
1635
1636    // Some properties are generated as Value, but for which the default constructed Value must be initialized
1637    for (prop_name, decl) in &description.original.root_element.borrow().property_declarations {
1638        if !matches!(
1639            decl.property_type,
1640            Type::Struct { .. } | Type::Array(_) | Type::Enumeration(_)
1641        ) || decl.is_alias.is_some()
1642        {
1643            continue;
1644        }
1645        if let Some(b) = description.original.root_element.borrow().bindings.get(prop_name)
1646            && b.borrow().two_way_bindings.is_empty()
1647        {
1648            continue;
1649        }
1650        let p = description.custom_properties.get(prop_name).unwrap();
1651        unsafe {
1652            let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(p.offset));
1653            p.prop.set(item, eval::default_value_for_type(&decl.property_type), None).unwrap();
1654        }
1655    }
1656
1657    #[cfg(slint_debug_property)]
1658    {
1659        let component_id = description.original.id.as_str();
1660
1661        // Set debug names on custom (root element) properties
1662        for (prop_name, prop_info) in &description.custom_properties {
1663            let name = format!("{}.{}", component_id, prop_name);
1664            unsafe {
1665                let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(prop_info.offset));
1666                prop_info.prop.set_debug_name(item, name);
1667            }
1668        }
1669
1670        // Set debug names on built-in item properties
1671        for (item_name, item_within_component) in &description.items {
1672            let item = unsafe { item_within_component.item_from_item_tree(instance_ref.as_ptr()) };
1673            for (prop_name, prop_rtti) in &item_within_component.rtti.properties {
1674                let name = format!("{}::{}.{}", component_id, item_name, prop_name);
1675                prop_rtti.set_debug_name(item, name);
1676            }
1677        }
1678    }
1679
1680    generator::handle_property_bindings_init(
1681        &description.original,
1682        |elem, prop_name, binding| unsafe {
1683            let is_root = Rc::ptr_eq(
1684                elem,
1685                &elem.borrow().enclosing_component.upgrade().unwrap().root_element,
1686            );
1687            let elem = elem.borrow();
1688            let is_const = binding.analysis.as_ref().is_some_and(|a| a.is_const);
1689
1690            let property_type = elem.lookup_property(prop_name).property_type;
1691            if let Type::Function { .. } = property_type {
1692                // function don't need initialization
1693            } else if let Type::Callback { .. } = property_type {
1694                if !matches!(binding.expression, Expression::Invalid) {
1695                    let expr = binding.expression.clone();
1696                    let description = description.clone();
1697                    if let Some(callback_offset) =
1698                        description.custom_callbacks.get(prop_name).filter(|_| is_root)
1699                    {
1700                        let callback = callback_offset.apply(instance_ref.as_ref());
1701                        callback.set_handler(make_callback_eval_closure(expr, self_weak.clone()));
1702                    } else {
1703                        let item_within_component = &description.items[&elem.id];
1704                        let item = item_within_component.item_from_item_tree(instance_ref.as_ptr());
1705                        if let Some(callback) =
1706                            item_within_component.rtti.callbacks.get(prop_name.as_str())
1707                        {
1708                            callback.set_handler(
1709                                item,
1710                                Box::new(make_callback_eval_closure(expr, self_weak.clone())),
1711                            );
1712                        } else {
1713                            panic!("unknown callback {prop_name}")
1714                        }
1715                    }
1716                }
1717            } else if let Some(PropertiesWithinComponent { offset, prop: prop_info, .. }) =
1718                description.custom_properties.get(prop_name).filter(|_| is_root)
1719            {
1720                let is_state_info = matches!(&property_type, Type::Struct (s) if matches!(s.name, StructName::Builtin(BuiltinStruct::StateInfo)));
1721                if is_state_info {
1722                    let prop = Pin::new_unchecked(
1723                        &*(instance_ref.as_ptr().add(*offset)
1724                            as *const Property<i_slint_core::properties::StateInfo>),
1725                    );
1726                    let e = binding.expression.clone();
1727                    let state_binding = make_binding_eval_closure(e, self_weak.clone());
1728                    i_slint_core::properties::set_state_binding(prop, move || {
1729                        state_binding().try_into().unwrap()
1730                    });
1731                    return;
1732                }
1733
1734                let maybe_animation = animation_for_property(instance_ref, &binding.animation);
1735                let item = Pin::new_unchecked(&*instance_ref.as_ptr().add(*offset));
1736
1737                if !matches!(binding.expression, Expression::Invalid) {
1738                    if is_const {
1739                        let v = eval::eval_expression(
1740                            &binding.expression,
1741                            &mut eval::EvalLocalContext::from_component_instance(instance_ref),
1742                        );
1743                        prop_info.set(item, v, None).unwrap();
1744                    } else {
1745                        let e = binding.expression.clone();
1746                        prop_info
1747                            .set_binding(
1748                                item,
1749                                Box::new(make_binding_eval_closure(e, self_weak.clone())),
1750                                maybe_animation,
1751                            )
1752                            .unwrap();
1753                    }
1754                }
1755                for twb in &binding.two_way_bindings {
1756                    match twb {
1757                        TwoWayBinding::Property { property, field_access }
1758                            if field_access.is_empty()
1759                                && !matches!(
1760                                    &property_type,
1761                                    Type::Struct(..) | Type::Array(..)
1762                                ) =>
1763                        {
1764                            // Safety: The compiler ensured that the properties exist and have
1765                            // the same type (except for struct/array, which may map to a Value).
1766                            prop_info.link_two_ways(item, get_property_ptr(property, instance_ref));
1767                        }
1768                        TwoWayBinding::Property { property, field_access } => {
1769                            let (common, map) =
1770                                prepare_for_two_way_binding(instance_ref, property, field_access);
1771                            prop_info.link_two_way_with_map(item, common, map);
1772                        }
1773                        TwoWayBinding::ModelData { repeated_element, field_access } => {
1774                            let (getter, setter) = prepare_model_two_way_binding(
1775                                instance_ref,
1776                                repeated_element,
1777                                field_access,
1778                            );
1779                            prop_info.link_two_way_to_model_data(item, getter, setter);
1780                        }
1781                    }
1782                }
1783            } else {
1784                let item_within_component = &description.items[&elem.id];
1785                let item = item_within_component.item_from_item_tree(instance_ref.as_ptr());
1786                if let Some(prop_rtti) =
1787                    item_within_component.rtti.properties.get(prop_name.as_str())
1788                {
1789                    let maybe_animation = animation_for_property(instance_ref, &binding.animation);
1790
1791                    for twb in &binding.two_way_bindings {
1792                        match twb {
1793                            TwoWayBinding::Property { property, field_access }
1794                                if field_access.is_empty()
1795                                    && !matches!(
1796                                        &property_type,
1797                                        Type::Struct(..) | Type::Array(..)
1798                                    ) =>
1799                            {
1800                                // Safety: The compiler ensured that the properties exist and
1801                                // have the same type.
1802                                prop_rtti
1803                                    .link_two_ways(item, get_property_ptr(property, instance_ref));
1804                            }
1805                            TwoWayBinding::Property { property, field_access } => {
1806                                let (common, map) = prepare_for_two_way_binding(
1807                                    instance_ref,
1808                                    property,
1809                                    field_access,
1810                                );
1811                                prop_rtti.link_two_way_with_map(item, common, map);
1812                            }
1813                            TwoWayBinding::ModelData { repeated_element, field_access } => {
1814                                let (getter, setter) = prepare_model_two_way_binding(
1815                                    instance_ref,
1816                                    repeated_element,
1817                                    field_access,
1818                                );
1819                                prop_rtti.link_two_way_to_model_data(item, getter, setter);
1820                            }
1821                        }
1822                    }
1823                    if !matches!(binding.expression, Expression::Invalid) {
1824                        if is_const {
1825                            prop_rtti
1826                                .set(
1827                                    item,
1828                                    eval::eval_expression(
1829                                        &binding.expression,
1830                                        &mut eval::EvalLocalContext::from_component_instance(
1831                                            instance_ref,
1832                                        ),
1833                                    ),
1834                                    maybe_animation.as_animation(),
1835                                )
1836                                .unwrap();
1837                        } else {
1838                            let e = binding.expression.clone();
1839                            prop_rtti.set_binding(
1840                                item,
1841                                Box::new(make_binding_eval_closure(e, self_weak.clone())),
1842                                maybe_animation,
1843                            );
1844                        }
1845                    }
1846                } else {
1847                    panic!("unknown property {} in {}", prop_name, elem.id);
1848                }
1849            }
1850        },
1851    );
1852
1853    for rep_in_comp in &description.repeater {
1854        generativity::make_guard!(guard);
1855        let rep_in_comp = rep_in_comp.unerase(guard);
1856
1857        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
1858        let expr = rep_in_comp.model.clone();
1859        let model_binding_closure = make_binding_eval_closure(expr, self_weak.clone());
1860        if rep_in_comp.is_conditional {
1861            let bool_model = Rc::new(crate::value_model::BoolModel::default());
1862            repeater.set_model_binding(move || {
1863                let v = model_binding_closure();
1864                bool_model.set_value(v.try_into().expect("condition model is bool"));
1865                ModelRc::from(bool_model.clone())
1866            });
1867        } else {
1868            repeater.set_model_binding(move || {
1869                let m = model_binding_closure();
1870                if let Value::Model(m) = m {
1871                    m
1872                } else {
1873                    ModelRc::new(crate::value_model::ValueModel::new(m))
1874                }
1875            });
1876        }
1877    }
1878    self_rc
1879}
1880
1881fn prepare_for_two_way_binding(
1882    instance_ref: InstanceRef,
1883    property: &NamedReference,
1884    field_access: &[SmolStr],
1885) -> (Pin<Rc<Property<Value>>>, Option<Rc<dyn rtti::TwoWayBindingMapping<Value>>>) {
1886    let element = property.element();
1887    let name = property.name().as_str();
1888
1889    generativity::make_guard!(guard);
1890    let enclosing_component = eval::enclosing_component_instance_for_element(
1891        &element,
1892        &eval::ComponentInstance::InstanceRef(instance_ref),
1893        guard,
1894    );
1895    let map: Option<Rc<dyn rtti::TwoWayBindingMapping<Value>>> = if field_access.is_empty() {
1896        None
1897    } else {
1898        struct FieldAccess(Vec<SmolStr>);
1899        impl rtti::TwoWayBindingMapping<Value> for FieldAccess {
1900            fn map_to(&self, value: &Value) -> Value {
1901                walk_struct_field_path(value.clone(), &self.0).unwrap_or_default()
1902            }
1903            fn map_from(&self, root: &mut Value, from: &Value) {
1904                if let Some(leaf) = walk_struct_field_path_mut(root, &self.0) {
1905                    *leaf = from.clone();
1906                }
1907            }
1908        }
1909        Some(Rc::new(FieldAccess(field_access.to_vec())))
1910    };
1911    let common = match enclosing_component {
1912        eval::ComponentInstance::InstanceRef(enclosing_component) => {
1913            let element = element.borrow();
1914            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
1915                && let Some(x) = enclosing_component.description.custom_properties.get(name)
1916            {
1917                let item =
1918                    unsafe { Pin::new_unchecked(&*enclosing_component.as_ptr().add(x.offset)) };
1919                let common = x.prop.prepare_for_two_way_binding(item);
1920                return (common, map);
1921            }
1922            let item_info = enclosing_component
1923                .description
1924                .items
1925                .get(element.id.as_str())
1926                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, name));
1927            let prop_info = item_info
1928                .rtti
1929                .properties
1930                .get(name)
1931                .unwrap_or_else(|| panic!("Property {} not in {}", name, element.id));
1932            core::mem::drop(element);
1933            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
1934            prop_info.prepare_for_two_way_binding(item)
1935        }
1936        eval::ComponentInstance::GlobalComponent(glob) => {
1937            glob.as_ref().prepare_for_two_way_binding(name).unwrap()
1938        }
1939    };
1940    (common, map)
1941}
1942
1943/// Build a (getter, setter) pair for a `TwoWayBinding::ModelData`. The
1944/// setter writes the whole row back through the field-access path, and
1945/// skips the write if the leaf value is unchanged.
1946fn prepare_model_two_way_binding(
1947    instance_ref: InstanceRef,
1948    repeated_element: &i_slint_compiler::object_tree::ElementWeak,
1949    field_access: &[SmolStr],
1950) -> (Box<dyn Fn() -> Option<Value>>, Box<dyn Fn(&Value)>) {
1951    let self_weak = instance_ref.self_weak().get().unwrap().clone();
1952    let repeated_element = repeated_element.clone();
1953    let field_access: Vec<SmolStr> = field_access.to_vec();
1954
1955    let getter = {
1956        let self_weak = self_weak.clone();
1957        let repeated_element = repeated_element.clone();
1958        let field_access = field_access.clone();
1959        Box::new(move || -> Option<Value> {
1960            with_repeater_row(&self_weak, &repeated_element, |repeater, row| {
1961                walk_struct_field_path(repeater.model_row_data(row)?, &field_access)
1962            })
1963        })
1964    };
1965
1966    let setter = Box::new(move |new_value: &Value| {
1967        with_repeater_row(&self_weak, &repeated_element, |repeater, row| {
1968            let mut data = repeater.model_row_data(row)?;
1969            // Short-circuit identical writes to avoid spurious change notifications.
1970            let leaf = walk_struct_field_path_mut(&mut data, &field_access)?;
1971            if &*leaf == new_value {
1972                return Some(());
1973            }
1974            *leaf = new_value.clone();
1975            repeater.model_set_row_data(row, data);
1976            Some(())
1977        });
1978    });
1979
1980    (getter, setter)
1981}
1982
1983/// Resolve the repeater that backs `repeated_element` and its current row
1984/// index, then run `f`. Returns `None` if any link is unavailable.
1985fn with_repeater_row<R>(
1986    self_weak: &ErasedItemTreeBoxWeak,
1987    repeated_element: &i_slint_compiler::object_tree::ElementWeak,
1988    f: impl FnOnce(Pin<&Repeater<ErasedItemTreeBox>>, usize) -> Option<R>,
1989) -> Option<R> {
1990    let self_rc = self_weak.upgrade()?;
1991    generativity::make_guard!(guard);
1992    let s = self_rc.unerase(guard);
1993    let instance = s.borrow_instance();
1994    let element = repeated_element.upgrade()?;
1995    let index = crate::eval::load_property(
1996        instance,
1997        &element.borrow().base_type.as_component().root_element,
1998        crate::dynamic_item_tree::SPECIAL_PROPERTY_INDEX,
1999    )
2000    .ok()?;
2001    let row = usize::try_from(i32::try_from(index).ok()?).ok()?;
2002    generativity::make_guard!(guard);
2003    let enclosing = crate::eval::enclosing_component_for_element(&element, instance, guard);
2004    generativity::make_guard!(guard);
2005    let (repeater, _) = get_repeater_by_name(enclosing, element.borrow().id.as_str(), guard);
2006    f(repeater, row)
2007}
2008
2009/// Follow a chain of struct field accesses on `value`.
2010fn walk_struct_field_path(mut value: Value, fields: &[SmolStr]) -> Option<Value> {
2011    for f in fields {
2012        match value {
2013            Value::Struct(o) => value = o.get_field(f).cloned().unwrap_or_default(),
2014            Value::Void => return None,
2015            _ => return None,
2016        }
2017    }
2018    Some(value)
2019}
2020
2021/// Mutable counterpart of [`walk_struct_field_path`].
2022fn walk_struct_field_path_mut<'a>(
2023    mut value: &'a mut Value,
2024    fields: &[SmolStr],
2025) -> Option<&'a mut Value> {
2026    for f in fields {
2027        match value {
2028            Value::Struct(o) => value = o.0.get_mut(f)?,
2029            _ => return None,
2030        }
2031    }
2032    Some(value)
2033}
2034
2035pub(crate) fn get_property_ptr(nr: &NamedReference, instance: InstanceRef) -> *const c_void {
2036    let element = nr.element();
2037    generativity::make_guard!(guard);
2038    let enclosing_component = eval::enclosing_component_instance_for_element(
2039        &element,
2040        &eval::ComponentInstance::InstanceRef(instance),
2041        guard,
2042    );
2043    match enclosing_component {
2044        eval::ComponentInstance::InstanceRef(enclosing_component) => {
2045            let element = element.borrow();
2046            if element.id == element.enclosing_component.upgrade().unwrap().root_element.borrow().id
2047                && let Some(x) = enclosing_component.description.custom_properties.get(nr.name())
2048            {
2049                return unsafe { enclosing_component.as_ptr().add(x.offset).cast() };
2050            };
2051            let item_info = enclosing_component
2052                .description
2053                .items
2054                .get(element.id.as_str())
2055                .unwrap_or_else(|| panic!("Unknown element for {}.{}", element.id, nr.name()));
2056            let prop_info = item_info
2057                .rtti
2058                .properties
2059                .get(nr.name().as_str())
2060                .unwrap_or_else(|| panic!("Property {} not in {}", nr.name(), element.id));
2061            core::mem::drop(element);
2062            let item = unsafe { item_info.item_from_item_tree(enclosing_component.as_ptr()) };
2063            unsafe { item.as_ptr().add(prop_info.offset()).cast() }
2064        }
2065        eval::ComponentInstance::GlobalComponent(glob) => glob.as_ref().get_property_ptr(nr.name()),
2066    }
2067}
2068
2069pub struct ErasedItemTreeBox(ItemTreeBox<'static>);
2070impl ErasedItemTreeBox {
2071    pub fn unerase<'a, 'id>(
2072        &'a self,
2073        _guard: generativity::Guard<'id>,
2074    ) -> Pin<&'a ItemTreeBox<'id>> {
2075        Pin::new(
2076            //Safety: 'id is unique because of `_guard`
2077            unsafe { core::mem::transmute::<&ItemTreeBox<'static>, &ItemTreeBox<'id>>(&self.0) },
2078        )
2079    }
2080
2081    pub fn borrow(&self) -> ItemTreeRefPin<'_> {
2082        // Safety: it is safe to access self.0 here because the 'id lifetime does not leak
2083        self.0.borrow()
2084    }
2085
2086    pub fn window_adapter_ref(&self) -> Result<&WindowAdapterRc, PlatformError> {
2087        self.0.window_adapter_ref()
2088    }
2089
2090    pub fn run_setup_code(&self) {
2091        generativity::make_guard!(guard);
2092        let compo_box = self.unerase(guard);
2093        let instance_ref = compo_box.borrow_instance();
2094        for extra_init_code in self.0.description.original.init_code.borrow().iter() {
2095            eval::eval_expression(
2096                extra_init_code,
2097                &mut eval::EvalLocalContext::from_component_instance(instance_ref),
2098            );
2099        }
2100        if let Some(cts) = instance_ref.description.change_trackers.as_ref() {
2101            let self_weak = instance_ref.self_weak().get().unwrap();
2102            let v = cts
2103                .1
2104                .iter()
2105                .enumerate()
2106                .map(|(idx, _)| {
2107                    let ct = ChangeTracker::default();
2108                    ct.init(
2109                        self_weak.clone(),
2110                        move |self_weak| {
2111                            let s = self_weak.upgrade().unwrap();
2112                            generativity::make_guard!(guard);
2113                            let compo_box = s.unerase(guard);
2114                            let instance_ref = compo_box.borrow_instance();
2115                            let nr = &s.0.description.change_trackers.as_ref().unwrap().1[idx].0;
2116                            eval::load_property(instance_ref, &nr.element(), nr.name()).unwrap()
2117                        },
2118                        move |self_weak, _| {
2119                            let s = self_weak.upgrade().unwrap();
2120                            generativity::make_guard!(guard);
2121                            let compo_box = s.unerase(guard);
2122                            let instance_ref = compo_box.borrow_instance();
2123                            let e = &s.0.description.change_trackers.as_ref().unwrap().1[idx].1;
2124                            eval::eval_expression(
2125                                e,
2126                                &mut eval::EvalLocalContext::from_component_instance(instance_ref),
2127                            );
2128                        },
2129                    );
2130                    ct
2131                })
2132                .collect::<Vec<_>>();
2133            cts.0
2134                .apply_pin(instance_ref.instance)
2135                .set(v)
2136                .unwrap_or_else(|_| panic!("run_setup_code called twice?"));
2137        }
2138        update_timers(instance_ref);
2139    }
2140}
2141impl<'id> From<ItemTreeBox<'id>> for ErasedItemTreeBox {
2142    fn from(inner: ItemTreeBox<'id>) -> Self {
2143        // Safety: Nothing access the component directly, we only access it through unerased where
2144        // the lifetime is unique again
2145        unsafe {
2146            ErasedItemTreeBox(core::mem::transmute::<ItemTreeBox<'id>, ItemTreeBox<'static>>(inner))
2147        }
2148    }
2149}
2150
2151pub fn get_repeater_by_name<'a, 'id>(
2152    instance_ref: InstanceRef<'a, '_>,
2153    name: &str,
2154    guard: generativity::Guard<'id>,
2155) -> (std::pin::Pin<&'a Repeater<ErasedItemTreeBox>>, Rc<ItemTreeDescription<'id>>) {
2156    let rep_index = instance_ref.description.repeater_names[name];
2157    let rep_in_comp = instance_ref.description.repeater[rep_index].unerase(guard);
2158    (rep_in_comp.offset.apply_pin(instance_ref.instance), rep_in_comp.item_tree_to_repeat.clone())
2159}
2160
2161#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2162extern "C" fn ensure_instantiated(component: ItemTreeRefPin) -> bool {
2163    generativity::make_guard!(guard);
2164    // Safety: called through the vtable of our own ItemTreeDescription.
2165    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2166
2167    let mut changed = false;
2168    for (tree_index, node) in instance_ref.description.item_tree.iter().enumerate() {
2169        if !matches!(node, ItemTreeNode::Item { .. }) {
2170            continue;
2171        }
2172        let item_ref = component.as_ref().get_item_ref(tree_index as u32);
2173        if let Some(container) = i_slint_core::items::ItemRef::downcast_pin::<
2174            i_slint_core::items::ComponentContainer,
2175        >(item_ref)
2176        {
2177            changed |= container.ensure_updated();
2178        }
2179    }
2180
2181    for rep_in_comp in &instance_ref.description.repeater {
2182        // Safety: we do not mix the repeater with a different component id.
2183        let rep_in_comp = unsafe { rep_in_comp.get_untagged() };
2184        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
2185        let init = || {
2186            let extra_data =
2187                instance_ref.description.extra_data_offset.apply(instance_ref.as_ref());
2188            instantiate(
2189                rep_in_comp.item_tree_to_repeat.clone(),
2190                instance_ref.self_weak().get().cloned(),
2191                None,
2192                None,
2193                extra_data.globals.get().unwrap().clone(),
2194            )
2195        };
2196        if let Some(lv) = &rep_in_comp
2197            .item_tree_to_repeat
2198            .original
2199            .parent_element
2200            .borrow()
2201            .upgrade()
2202            .unwrap()
2203            .borrow()
2204            .repeated
2205            .as_ref()
2206            .unwrap()
2207            .is_listview
2208        {
2209            let assume_property_logical_length =
2210                |prop| unsafe { Pin::new_unchecked(&*(prop as *const Property<LogicalLength>)) };
2211            changed |= repeater.ensure_updated_listview(
2212                init,
2213                assume_property_logical_length(get_property_ptr(&lv.viewport_width, instance_ref)),
2214                assume_property_logical_length(get_property_ptr(&lv.viewport_height, instance_ref)),
2215                assume_property_logical_length(get_property_ptr(&lv.viewport_y, instance_ref)),
2216                eval::load_property(
2217                    instance_ref,
2218                    &lv.listview_width.element(),
2219                    lv.listview_width.name(),
2220                )
2221                .unwrap()
2222                .try_into()
2223                .unwrap(),
2224                assume_property_logical_length(get_property_ptr(&lv.listview_height, instance_ref)),
2225            );
2226        } else {
2227            changed |= repeater.ensure_updated(init);
2228        }
2229    }
2230    changed
2231}
2232
2233#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2234extern "C" fn layout_info(component: ItemTreeRefPin, orientation: Orientation) -> LayoutInfo {
2235    generativity::make_guard!(guard);
2236    // This is fine since we can only be called with a component that with our vtable which is a ItemTreeDescription
2237    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2238    let orientation = crate::eval_layout::from_runtime(orientation);
2239
2240    // The vtable layout_info path is taken e.g. for repeater cells. When
2241    // the component root has a parameterized layout-info function, route
2242    // through it: reading the bare `layoutinfo-{h,v}` would cycle on
2243    // `self.{w,h}` for the cross-axis case, and we have no explicit
2244    // constraint at this entry point. `f32::MAX` (i.e. "unconstrained")
2245    // tells the runtime's flex algorithm to behave as if items don't
2246    // need to wrap, which gives the natural max-cell-cross-axis result
2247    // — much closer to correct than the `sqrt(item-areas)` heuristic
2248    // that a `-1` sentinel would trigger.
2249    let root = &instance_ref.description.original.root_element;
2250    let cross_axis_constraint = match orientation {
2251        i_slint_compiler::layout::Orientation::Vertical => {
2252            root.borrow().layout_info_v_with_constraint.is_some().then_some(f32::MAX)
2253        }
2254        i_slint_compiler::layout::Orientation::Horizontal => {
2255            root.borrow().layout_info_h_with_constraint.is_some().then_some(f32::MAX)
2256        }
2257    };
2258    let mut result = crate::eval_layout::get_layout_info_with_constraint(
2259        root,
2260        instance_ref,
2261        &instance_ref.window_adapter(),
2262        orientation,
2263        cross_axis_constraint,
2264    );
2265
2266    let constraints = instance_ref.description.original.root_constraints.borrow();
2267    if constraints.has_explicit_restrictions(orientation) {
2268        crate::eval_layout::fill_layout_info_constraints(
2269            &mut result,
2270            &constraints,
2271            orientation,
2272            &|nr: &NamedReference| {
2273                eval::load_property(instance_ref, &nr.element(), nr.name())
2274                    .unwrap()
2275                    .try_into()
2276                    .unwrap()
2277            },
2278        );
2279    }
2280    result
2281}
2282
2283#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2284unsafe extern "C" fn get_item_ref(component: ItemTreeRefPin, index: u32) -> Pin<ItemRef> {
2285    let tree = get_item_tree(component);
2286    match &tree[index as usize] {
2287        ItemTreeNode::Item { item_array_index, .. } => unsafe {
2288            generativity::make_guard!(guard);
2289            let instance_ref = InstanceRef::from_pin_ref(component, guard);
2290            core::mem::transmute::<Pin<ItemRef>, Pin<ItemRef>>(
2291                instance_ref.description.item_array[*item_array_index as usize]
2292                    .apply_pin(instance_ref.instance),
2293            )
2294        },
2295        ItemTreeNode::DynamicTree { .. } => panic!("get_item_ref called on dynamic tree"),
2296    }
2297}
2298
2299#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2300extern "C" fn get_subtree_range(component: ItemTreeRefPin, index: u32) -> IndexRange {
2301    generativity::make_guard!(guard);
2302    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2303    if index as usize >= instance_ref.description.repeater.len() {
2304        let container_index = {
2305            let tree_node = &component.as_ref().get_item_tree()[index as usize];
2306            if let ItemTreeNode::DynamicTree { parent_index, .. } = tree_node {
2307                *parent_index
2308            } else {
2309                u32::MAX
2310            }
2311        };
2312        let container = component.as_ref().get_item_ref(container_index);
2313        let container = i_slint_core::items::ItemRef::downcast_pin::<
2314            i_slint_core::items::ComponentContainer,
2315        >(container)
2316        .unwrap();
2317        container.subtree_range()
2318    } else {
2319        generativity::make_guard!(guard);
2320        let rep_in_comp = instance_ref.description.repeater[index as usize].unerase(guard);
2321
2322        let repeater = rep_in_comp.offset.apply_pin(instance_ref.instance);
2323        repeater.track_instance_changes();
2324        repeater.range().into()
2325    }
2326}
2327
2328#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2329extern "C" fn get_subtree(
2330    component: ItemTreeRefPin,
2331    index: u32,
2332    subtree_index: usize,
2333    result: &mut ItemTreeWeak,
2334) {
2335    generativity::make_guard!(guard);
2336    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2337    if index as usize >= instance_ref.description.repeater.len() {
2338        let container_index = {
2339            let tree_node = &component.as_ref().get_item_tree()[index as usize];
2340            if let ItemTreeNode::DynamicTree { parent_index, .. } = tree_node {
2341                *parent_index
2342            } else {
2343                u32::MAX
2344            }
2345        };
2346        let container = component.as_ref().get_item_ref(container_index);
2347        let container = i_slint_core::items::ItemRef::downcast_pin::<
2348            i_slint_core::items::ComponentContainer,
2349        >(container)
2350        .unwrap();
2351        if subtree_index == 0 {
2352            *result = container.subtree_component();
2353        }
2354    } else {
2355        generativity::make_guard!(guard);
2356        let rep_in_comp = instance_ref.description.repeater[index as usize].unerase(guard);
2357
2358        let repeater = rep_in_comp.offset.apply(&instance_ref.instance);
2359        if let Some(instance_at) = repeater.instance_at(subtree_index) {
2360            *result = vtable::VRc::downgrade(&vtable::VRc::into_dyn(instance_at))
2361        }
2362    }
2363}
2364
2365#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2366extern "C" fn get_item_tree(component: ItemTreeRefPin) -> Slice<ItemTreeNode> {
2367    generativity::make_guard!(guard);
2368    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2369    let tree = instance_ref.description.item_tree.as_slice();
2370    unsafe { core::mem::transmute::<&[ItemTreeNode], &[ItemTreeNode]>(tree) }.into()
2371}
2372
2373#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2374extern "C" fn subtree_index(component: ItemTreeRefPin) -> usize {
2375    generativity::make_guard!(guard);
2376    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2377    if let Ok(value) = instance_ref.description.get_property(component, SPECIAL_PROPERTY_INDEX) {
2378        value.try_into().unwrap()
2379    } else {
2380        usize::MAX
2381    }
2382}
2383
2384#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2385unsafe extern "C" fn parent_node(component: ItemTreeRefPin, result: &mut ItemWeak) {
2386    generativity::make_guard!(guard);
2387    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2388
2389    let component_and_index = {
2390        // Normal inner-compilation unit case:
2391        if let Some(parent_offset) = instance_ref.description.parent_item_tree_offset {
2392            let parent_item_index = instance_ref
2393                .description
2394                .original
2395                .parent_element
2396                .borrow()
2397                .upgrade()
2398                .and_then(|e| e.borrow().item_index.get().cloned())
2399                .unwrap_or(u32::MAX);
2400            let parent_component = parent_offset
2401                .apply(instance_ref.as_ref())
2402                .get()
2403                .and_then(|p| p.upgrade())
2404                .map(vtable::VRc::into_dyn);
2405
2406            (parent_component, parent_item_index)
2407        } else if let Some((parent_component, parent_index)) = instance_ref
2408            .description
2409            .extra_data_offset
2410            .apply(instance_ref.as_ref())
2411            .embedding_position
2412            .get()
2413        {
2414            (parent_component.upgrade(), *parent_index)
2415        } else {
2416            (None, u32::MAX)
2417        }
2418    };
2419
2420    if let (Some(component), index) = component_and_index {
2421        *result = ItemRc::new(component, index).downgrade();
2422    }
2423}
2424
2425#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2426unsafe extern "C" fn embed_component(
2427    component: ItemTreeRefPin,
2428    parent_component: &ItemTreeWeak,
2429    parent_item_tree_index: u32,
2430) -> bool {
2431    generativity::make_guard!(guard);
2432    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2433
2434    if instance_ref.description.parent_item_tree_offset.is_some() {
2435        // We are not the root of the compilation unit tree... Can not embed this!
2436        return false;
2437    }
2438
2439    {
2440        // sanity check parent:
2441        let prc = parent_component.upgrade().unwrap();
2442        let pref = vtable::VRc::borrow_pin(&prc);
2443        let it = pref.as_ref().get_item_tree();
2444        if !matches!(
2445            it.get(parent_item_tree_index as usize),
2446            Some(ItemTreeNode::DynamicTree { .. })
2447        ) {
2448            panic!("Trying to embed into a non-dynamic index in the parents item tree")
2449        }
2450    }
2451
2452    let extra_data = instance_ref.description.extra_data_offset.apply(instance_ref.as_ref());
2453    extra_data.embedding_position.set((parent_component.clone(), parent_item_tree_index)).is_ok()
2454}
2455
2456#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2457extern "C" fn item_geometry(component: ItemTreeRefPin, item_index: u32) -> LogicalRect {
2458    generativity::make_guard!(guard);
2459    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2460
2461    let e = instance_ref.description.original_elements[item_index as usize].borrow();
2462    let g = e.geometry_props.as_ref().unwrap();
2463
2464    let load_f32 = |nr: &NamedReference| -> f32 {
2465        crate::eval::load_property(instance_ref, &nr.element(), nr.name())
2466            .unwrap()
2467            .try_into()
2468            .unwrap()
2469    };
2470
2471    LogicalRect {
2472        origin: (load_f32(&g.x), load_f32(&g.y)).into(),
2473        size: (load_f32(&g.width), load_f32(&g.height)).into(),
2474    }
2475}
2476
2477// silence the warning despite `AccessibleRole` is a `#[non_exhaustive]` enum from another crate.
2478#[allow(improper_ctypes_definitions)]
2479#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2480extern "C" fn accessible_role(component: ItemTreeRefPin, item_index: u32) -> AccessibleRole {
2481    generativity::make_guard!(guard);
2482    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2483    let nr = instance_ref.description.original_elements[item_index as usize]
2484        .borrow()
2485        .accessibility_props
2486        .0
2487        .get("accessible-role")
2488        .cloned();
2489    match nr {
2490        Some(nr) => crate::eval::load_property(instance_ref, &nr.element(), nr.name())
2491            .unwrap()
2492            .try_into()
2493            .unwrap(),
2494        None => AccessibleRole::default(),
2495    }
2496}
2497
2498#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2499extern "C" fn accessible_string_property(
2500    component: ItemTreeRefPin,
2501    item_index: u32,
2502    what: AccessibleStringProperty,
2503    result: &mut SharedString,
2504) -> bool {
2505    generativity::make_guard!(guard);
2506    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2507    let prop_name = format!("accessible-{what}");
2508    let nr = instance_ref.description.original_elements[item_index as usize]
2509        .borrow()
2510        .accessibility_props
2511        .0
2512        .get(&prop_name)
2513        .cloned();
2514    if let Some(nr) = nr {
2515        let value = crate::eval::load_property(instance_ref, &nr.element(), nr.name()).unwrap();
2516        match value {
2517            Value::String(s) => *result = s,
2518            Value::Bool(b) => *result = if b { "true" } else { "false" }.into(),
2519            Value::Number(x) => *result = x.to_string().into(),
2520            Value::EnumerationValue(_, v) => *result = v.into(),
2521            _ => unimplemented!("invalid type for accessible_string_property"),
2522        };
2523        true
2524    } else {
2525        false
2526    }
2527}
2528
2529#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2530extern "C" fn accessibility_action(
2531    component: ItemTreeRefPin,
2532    item_index: u32,
2533    action: &AccessibilityAction,
2534) {
2535    let perform = |prop_name, args: &[Value]| {
2536        generativity::make_guard!(guard);
2537        let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2538        let nr = instance_ref.description.original_elements[item_index as usize]
2539            .borrow()
2540            .accessibility_props
2541            .0
2542            .get(prop_name)
2543            .cloned();
2544        if let Some(nr) = nr {
2545            let instance_ref = eval::ComponentInstance::InstanceRef(instance_ref);
2546            crate::eval::invoke_callback(&instance_ref, &nr.element(), nr.name(), args).unwrap();
2547        }
2548    };
2549
2550    match action {
2551        AccessibilityAction::Default => perform("accessible-action-default", &[]),
2552        AccessibilityAction::Decrement => perform("accessible-action-decrement", &[]),
2553        AccessibilityAction::Increment => perform("accessible-action-increment", &[]),
2554        AccessibilityAction::Expand => perform("accessible-action-expand", &[]),
2555        AccessibilityAction::ReplaceSelectedText(_a) => {
2556            //perform("accessible-action-replace-selected-text", &[Value::String(a.clone())])
2557            i_slint_core::debug_log!(
2558                "AccessibilityAction::ReplaceSelectedText not implemented in interpreter's accessibility_action"
2559            );
2560        }
2561        AccessibilityAction::SetValue(a) => {
2562            perform("accessible-action-set-value", &[Value::String(a.clone())])
2563        }
2564    };
2565}
2566
2567#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2568extern "C" fn supported_accessibility_actions(
2569    component: ItemTreeRefPin,
2570    item_index: u32,
2571) -> SupportedAccessibilityAction {
2572    generativity::make_guard!(guard);
2573    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2574    instance_ref.description.original_elements[item_index as usize]
2575        .borrow()
2576        .accessibility_props
2577        .0
2578        .keys()
2579        .filter_map(|x| x.strip_prefix("accessible-action-"))
2580        .fold(SupportedAccessibilityAction::default(), |acc, value| {
2581            SupportedAccessibilityAction::from_name(&i_slint_compiler::generator::to_pascal_case(
2582                value,
2583            ))
2584            .unwrap_or_else(|| panic!("Not an accessible action: {value:?}"))
2585                | acc
2586        })
2587}
2588
2589#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2590extern "C" fn item_element_infos(
2591    component: ItemTreeRefPin,
2592    item_index: u32,
2593    result: &mut SharedString,
2594) -> bool {
2595    generativity::make_guard!(guard);
2596    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2597    *result = instance_ref.description.original_elements[item_index as usize]
2598        .borrow()
2599        .element_infos()
2600        .into();
2601    true
2602}
2603
2604#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2605extern "C" fn window_adapter(
2606    component: ItemTreeRefPin,
2607    do_create: bool,
2608    result: &mut Option<WindowAdapterRc>,
2609) {
2610    generativity::make_guard!(guard);
2611    let instance_ref = unsafe { InstanceRef::from_pin_ref(component, guard) };
2612    if do_create {
2613        *result = Some(instance_ref.window_adapter());
2614    } else {
2615        *result = instance_ref.maybe_window_adapter();
2616    }
2617}
2618
2619#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2620unsafe extern "C" fn drop_in_place(component: vtable::VRefMut<ItemTreeVTable>) -> vtable::Layout {
2621    unsafe {
2622        let instance_ptr = component.as_ptr() as *mut Instance<'static>;
2623        let layout = (*instance_ptr).type_info().layout();
2624        dynamic_type::TypeInfo::drop_in_place(instance_ptr);
2625        layout.into()
2626    }
2627}
2628
2629#[cfg_attr(not(feature = "ffi"), i_slint_core_macros::remove_extern)]
2630unsafe extern "C" fn dealloc(_vtable: &ItemTreeVTable, ptr: *mut u8, layout: vtable::Layout) {
2631    unsafe { std::alloc::dealloc(ptr, layout.try_into().unwrap()) };
2632}
2633
2634#[derive(Copy, Clone)]
2635pub struct InstanceRef<'a, 'id> {
2636    pub instance: Pin<&'a Instance<'id>>,
2637    pub description: &'a ItemTreeDescription<'id>,
2638}
2639
2640impl<'a, 'id> InstanceRef<'a, 'id> {
2641    pub unsafe fn from_pin_ref(
2642        component: ItemTreeRefPin<'a>,
2643        _guard: generativity::Guard<'id>,
2644    ) -> Self {
2645        unsafe {
2646            Self {
2647                instance: Pin::new_unchecked(
2648                    &*(component.as_ref().as_ptr() as *const Instance<'id>),
2649                ),
2650                description: &*(Pin::into_inner_unchecked(component).get_vtable()
2651                    as *const ItemTreeVTable
2652                    as *const ItemTreeDescription<'id>),
2653            }
2654        }
2655    }
2656
2657    pub fn as_ptr(&self) -> *const u8 {
2658        (&*self.instance.as_ref()) as *const Instance as *const u8
2659    }
2660
2661    pub fn as_ref(&self) -> &Instance<'id> {
2662        &self.instance
2663    }
2664
2665    /// Borrow this component as a `Pin<ItemTreeRef>`
2666    pub fn borrow(self) -> ItemTreeRefPin<'a> {
2667        unsafe {
2668            Pin::new_unchecked(vtable::VRef::from_raw(
2669                NonNull::from(&self.description.ct).cast(),
2670                NonNull::from(self.instance.get_ref()).cast(),
2671            ))
2672        }
2673    }
2674
2675    pub fn self_weak(&self) -> &OnceCell<ErasedItemTreeBoxWeak> {
2676        let extra_data = self.description.extra_data_offset.apply(self.as_ref());
2677        &extra_data.self_weak
2678    }
2679
2680    pub fn root_weak(&self) -> &ErasedItemTreeBoxWeak {
2681        self.description.root_offset.apply(self.as_ref()).get().unwrap()
2682    }
2683
2684    pub fn window_adapter(&self) -> WindowAdapterRc {
2685        let root_weak = vtable::VWeak::into_dyn(self.root_weak().clone());
2686        let root = self.root_weak().upgrade().unwrap();
2687        generativity::make_guard!(guard);
2688        let comp = root.unerase(guard);
2689        Self::get_or_init_window_adapter_ref(
2690            &comp.description,
2691            root_weak,
2692            true,
2693            comp.instance.as_pin_ref().get_ref(),
2694        )
2695        .unwrap()
2696        .clone()
2697    }
2698
2699    pub fn get_or_init_window_adapter_ref<'b, 'id2>(
2700        description: &'b ItemTreeDescription<'id2>,
2701        root_weak: ItemTreeWeak,
2702        do_create: bool,
2703        instance: &'b Instance<'id2>,
2704    ) -> Result<&'b WindowAdapterRc, PlatformError> {
2705        // We are the actual root: Generate and store a window_adapter if necessary
2706        description
2707            .extra_data_offset
2708            .apply(instance)
2709            .globals
2710            .get()
2711            .unwrap()
2712            .window_adapter()
2713            .unwrap()
2714            .get_or_try_init(|| {
2715                let mut parent_node = ItemWeak::default();
2716                if let Some(rc) = vtable::VWeak::upgrade(&root_weak) {
2717                    vtable::VRc::borrow_pin(&rc).as_ref().parent_node(&mut parent_node);
2718                }
2719
2720                if let Some(parent) = parent_node.upgrade() {
2721                    // We are embedded: Get window adapter from our parent
2722                    let mut result = None;
2723                    vtable::VRc::borrow_pin(parent.item_tree())
2724                        .as_ref()
2725                        .window_adapter(do_create, &mut result);
2726                    result.ok_or(PlatformError::NoPlatform)
2727                } else if do_create {
2728                    let extra_data = description.extra_data_offset.apply(instance);
2729                    let window_adapter = // We are the root: Create a window adapter
2730                    i_slint_backend_selector::with_platform(|_b| {
2731                        _b.create_window_adapter()
2732                    })?;
2733
2734                    let comp_rc = extra_data.self_weak.get().unwrap().upgrade().unwrap();
2735                    WindowInner::from_pub(window_adapter.window())
2736                        .set_component(&vtable::VRc::into_dyn(comp_rc));
2737                    Ok(window_adapter)
2738                } else {
2739                    Err(PlatformError::NoPlatform)
2740                }
2741            })
2742    }
2743
2744    pub fn maybe_window_adapter(&self) -> Option<WindowAdapterRc> {
2745        let root_weak = vtable::VWeak::into_dyn(self.root_weak().clone());
2746        let root = self.root_weak().upgrade()?;
2747        generativity::make_guard!(guard);
2748        let comp = root.unerase(guard);
2749        Self::get_or_init_window_adapter_ref(
2750            &comp.description,
2751            root_weak,
2752            false,
2753            comp.instance.as_pin_ref().get_ref(),
2754        )
2755        .ok()
2756        .cloned()
2757    }
2758
2759    pub fn access_window<R>(
2760        self,
2761        callback: impl FnOnce(&'_ i_slint_core::window::WindowInner) -> R,
2762    ) -> R {
2763        callback(WindowInner::from_pub(self.window_adapter().window()))
2764    }
2765
2766    pub fn parent_instance<'id2>(
2767        &self,
2768        _guard: generativity::Guard<'id2>,
2769    ) -> Option<InstanceRef<'a, 'id2>> {
2770        // we need a 'static guard in order to be able to re-borrow with lifetime 'a.
2771        // Safety: This is the only 'static Id in scope.
2772        if let Some(parent_offset) = self.description.parent_item_tree_offset
2773            && let Some(parent) =
2774                parent_offset.apply(self.as_ref()).get().and_then(vtable::VWeak::upgrade)
2775        {
2776            let parent_instance = parent.unerase(_guard);
2777            // And also assume that the parent lives for at least 'a.  FIXME: this may not be sound
2778            let parent_instance = unsafe {
2779                std::mem::transmute::<InstanceRef<'_, 'id2>, InstanceRef<'a, 'id2>>(
2780                    parent_instance.borrow_instance(),
2781                )
2782            };
2783            return Some(parent_instance);
2784        }
2785        None
2786    }
2787}
2788
2789/// Show the popup with a lazily evaluated location.
2790pub fn show_popup(
2791    element: ElementRc,
2792    instance: InstanceRef,
2793    popup: &object_tree::PopupWindow,
2794    pos_getter: impl Fn(InstanceRef<'_, '_>) -> LogicalPosition + 'static,
2795    close_policy: PopupClosePolicy,
2796    parent_comp: ErasedItemTreeBoxWeak,
2797    parent_window_adapter: WindowAdapterRc,
2798    parent_item: &ItemRc,
2799) {
2800    generativity::make_guard!(guard);
2801    let debug_handler = instance.description.debug_handler.borrow().clone();
2802
2803    // FIXME: we should compile once and keep the cached compiled component
2804    let compiled = generate_item_tree(
2805        &popup.component,
2806        None,
2807        parent_comp.upgrade().unwrap().0.description().popup_menu_description.clone(),
2808        false,
2809        guard,
2810    );
2811    compiled.recursively_set_debug_handler(debug_handler);
2812
2813    let extra_data = instance.description.extra_data_offset.apply(instance.as_ref());
2814    // Use the newly created window adapter if we are able to create one. Otherwise use the parent's one.
2815    // Tooltips skip this to share the parent's adapter, ensuring they use the ChildWindow path
2816    // and renderer caches stay consistent.
2817    let window_kind = if popup.is_tooltip { WindowKind::ToolTip } else { WindowKind::Popup };
2818    let globals = if let Some(window_adapter) =
2819        WindowInner::from_pub(parent_window_adapter.window())
2820            .create_child_window_adapter(window_kind)
2821    {
2822        extra_data.globals.get().unwrap().clone_with_window_adapter(window_adapter)
2823    } else {
2824        extra_data.globals.get().unwrap().clone()
2825    };
2826
2827    let popup_window_adapter = globals
2828        .window_adapter()
2829        .and_then(|window_adapter| window_adapter.get().cloned())
2830        .unwrap_or_else(|| parent_window_adapter.clone());
2831
2832    let inst = instantiate(
2833        compiled,
2834        Some(parent_comp),
2835        None,
2836        Some(&WindowOptions::UseExistingWindow(popup_window_adapter)),
2837        globals,
2838    );
2839    let inst_for_position = inst.clone();
2840    let access_position = Box::new(move || {
2841        generativity::make_guard!(guard);
2842        let compo_box = inst_for_position.unerase(guard);
2843        let instance_ref = compo_box.borrow_instance();
2844        pos_getter(instance_ref)
2845    });
2846    close_popup(element.clone(), instance, parent_window_adapter.clone());
2847    let window_kind = if popup.is_tooltip { WindowKind::ToolTip } else { WindowKind::Popup };
2848    instance.description.popup_ids.borrow_mut().insert(
2849        element.borrow().id.clone(),
2850        WindowInner::from_pub(parent_window_adapter.window()).show_popup(
2851            &vtable::VRc::into_dyn(inst.clone()),
2852            access_position,
2853            close_policy,
2854            parent_item,
2855            window_kind,
2856        ),
2857    );
2858    inst.run_setup_code();
2859}
2860
2861pub fn close_popup(
2862    element: ElementRc,
2863    instance: InstanceRef,
2864    parent_window_adapter: WindowAdapterRc,
2865) {
2866    if let Some(current_id) =
2867        instance.description.popup_ids.borrow_mut().remove(&element.borrow().id)
2868    {
2869        WindowInner::from_pub(parent_window_adapter.window()).close_popup(current_id);
2870    }
2871}
2872
2873pub fn make_menu_item_tree(
2874    menu_item_tree: &Rc<object_tree::Component>,
2875    enclosing_component: &InstanceRef,
2876    condition: Option<&Expression>,
2877    visible: Option<&Expression>,
2878) -> vtable::VRc<i_slint_core::menus::MenuVTable, MenuFromItemTree> {
2879    generativity::make_guard!(guard);
2880    let mit_compiled = generate_item_tree(
2881        menu_item_tree,
2882        None,
2883        enclosing_component.description.popup_menu_description.clone(),
2884        false,
2885        guard,
2886    );
2887    let enclosing_component_weak = enclosing_component.self_weak().get().unwrap();
2888    let extra_data =
2889        enclosing_component.description.extra_data_offset.apply(enclosing_component.as_ref());
2890    let mit_inst = instantiate(
2891        mit_compiled.clone(),
2892        Some(enclosing_component_weak.clone()),
2893        None,
2894        None,
2895        extra_data.globals.get().unwrap().clone(),
2896    );
2897    mit_inst.run_setup_code();
2898    let item_tree = vtable::VRc::into_dyn(mit_inst);
2899    let condition = condition.map(|condition| {
2900        let binding =
2901            make_binding_eval_closure(condition.clone(), enclosing_component_weak.clone());
2902        move || binding().try_into().unwrap()
2903    });
2904    let visible = visible.map(|visible| {
2905        let binding = make_binding_eval_closure(visible.clone(), enclosing_component_weak.clone());
2906        move || binding().try_into().unwrap()
2907    });
2908    let menu = match (condition, visible) {
2909        (None, None) => MenuFromItemTree::new(item_tree),
2910        (None, Some(visible)) => {
2911            MenuFromItemTree::new_with_condition_and_visible(item_tree, || true, visible)
2912        }
2913        (Some(condition), None) => {
2914            MenuFromItemTree::new_with_condition_and_visible(item_tree, condition, || true)
2915        }
2916        (Some(condition), Some(visible)) => {
2917            MenuFromItemTree::new_with_condition_and_visible(item_tree, condition, visible)
2918        }
2919    };
2920    vtable::VRc::new(menu)
2921}
2922
2923pub fn update_timers(instance: InstanceRef) {
2924    let ts = instance.description.original.timers.borrow();
2925    for (desc, offset) in ts.iter().zip(&instance.description.timers) {
2926        let timer = offset.apply(instance.as_ref());
2927        let running =
2928            eval::load_property(instance, &desc.running.element(), desc.running.name()).unwrap();
2929        if matches!(running, Value::Bool(true)) {
2930            let millis: i64 =
2931                eval::load_property(instance, &desc.interval.element(), desc.interval.name())
2932                    .unwrap()
2933                    .try_into()
2934                    .expect("interval must be a duration");
2935            if millis < 0 {
2936                timer.stop();
2937                continue;
2938            }
2939            let interval = core::time::Duration::from_millis(millis as _);
2940            if !timer.running() || interval != timer.interval() {
2941                let callback = desc.triggered.clone();
2942                let self_weak = instance.self_weak().get().unwrap().clone();
2943                timer.start(i_slint_core::timers::TimerMode::Repeated, interval, move || {
2944                    if let Some(instance) = self_weak.upgrade() {
2945                        generativity::make_guard!(guard);
2946                        let c = instance.unerase(guard);
2947                        let c = c.borrow_instance();
2948                        let inst = eval::ComponentInstance::InstanceRef(c);
2949                        eval::invoke_callback(&inst, &callback.element(), callback.name(), &[])
2950                            .unwrap();
2951                    }
2952                });
2953            }
2954        } else {
2955            timer.stop();
2956        }
2957    }
2958}
2959
2960pub fn restart_timer(element: ElementWeak, instance: InstanceRef) {
2961    let timers = instance.description.original.timers.borrow();
2962    if let Some((_, offset)) = timers
2963        .iter()
2964        .zip(&instance.description.timers)
2965        .find(|(desc, _)| Weak::ptr_eq(&desc.element, &element))
2966    {
2967        let timer = offset.apply(instance.as_ref());
2968        timer.restart();
2969    }
2970}