slint_interpreter/
item_holder.rs1use crate::erased::{ErasedItem, ErasedItemRc};
10use crate::prelude::*;
11use i_slint_core::items::{ItemVTable, PropertyAnimation};
12use i_slint_core::rtti::{
13 AnimatedBindingKind, BuiltinItem, CallbackInfo, PropertyInfo, TwoWayBindingMapping,
14};
15use vtable::{HasStaticVTable, VRef};
16
17fn find_property<T: 'static + BuiltinItem>(
18 name: &str,
19) -> Option<&'static dyn PropertyInfo<T, Value>> {
20 T::properties::<Value>().into_iter().find_map(|(n, info)| (n == name).then_some(info))
21}
22
23fn find_callback<T: 'static + BuiltinItem>(
24 name: &str,
25) -> Option<&'static dyn CallbackInfo<T, Value>> {
26 T::callbacks::<Value>().into_iter().find_map(|(n, info)| (n == name).then_some(info))
27}
28
29impl<T> ErasedItem for T
30where
31 T: 'static + BuiltinItem + HasStaticVTable<ItemVTable>,
32{
33 fn get_property(self: Pin<&Self>, name: &str) -> Result<Value, ()> {
34 find_property::<T>(name).ok_or(())?.get(self)
35 }
36
37 fn set_property(
38 self: Pin<&Self>,
39 name: &str,
40 value: Value,
41 animation: Option<PropertyAnimation>,
42 ) -> Result<(), ()> {
43 find_property::<T>(name).ok_or(())?.set(self, value, animation)
44 }
45
46 fn set_property_binding(
47 self: Pin<&Self>,
48 name: &str,
49 binding: Box<dyn Fn() -> Value>,
50 animation: AnimatedBindingKind,
51 ) -> Result<(), ()> {
52 find_property::<T>(name).ok_or(())?.set_binding(self, binding, animation)
53 }
54
55 fn prepare_property_for_two_way_binding(
56 self: Pin<&Self>,
57 name: &str,
58 ) -> Option<Pin<Rc<Property<Value>>>> {
59 Some(find_property::<T>(name)?.prepare_for_two_way_binding(self))
60 }
61
62 fn link_property_two_way_with_map(
63 self: Pin<&Self>,
64 name: &str,
65 other: Pin<Rc<Property<Value>>>,
66 mapper: Option<Rc<dyn TwoWayBindingMapping<Value>>>,
67 ) {
68 if let Some(info) = find_property::<T>(name) {
69 info.link_two_way_with_map(self, other, mapper);
70 }
71 }
72
73 fn call_callback(self: Pin<&Self>, name: &str, args: &[Value]) -> Result<Value, ()> {
74 find_callback::<T>(name).ok_or(())?.call(self, args)
75 }
76
77 fn set_callback_handler(
78 self: Pin<&Self>,
79 name: &str,
80 handler: Box<dyn Fn(&[Value]) -> Value>,
81 ) -> Result<(), ()> {
82 find_callback::<T>(name).ok_or(())?.set_handler(self, handler)
83 }
84
85 fn set_field(self: Pin<&Self>, _name: &str, _value: Value) -> Result<(), ()> {
86 Err(())
90 }
91
92 fn as_item_ref(self: Pin<&Self>) -> Pin<VRef<'_, ItemVTable>> {
93 VRef::new_pin(self)
94 }
95}
96
97pub fn make_item<T>() -> ErasedItemRc
99where
100 T: 'static + Default + BuiltinItem + HasStaticVTable<ItemVTable>,
101{
102 Rc::pin(T::default())
103}