slint_interpreter/
erased.rs1use crate::prelude::*;
10use i_slint_core::graphics::Brush;
11use i_slint_core::items::{AngleInterpolation, ItemVTable, PropertyAnimation};
12use i_slint_core::properties::InterpolatedPropertyValue;
13use i_slint_core::rtti::{AnimatedBindingKind, TwoWayBindingMapping};
14use vtable::VRef;
15
16pub trait ErasedItem {
18 fn get_property(self: Pin<&Self>, name: &str) -> Result<Value, ()>;
19
20 fn set_property(
21 self: Pin<&Self>,
22 name: &str,
23 value: Value,
24 animation: Option<PropertyAnimation>,
25 ) -> Result<(), ()>;
26
27 fn set_property_binding(
28 self: Pin<&Self>,
29 name: &str,
30 binding: Box<dyn Fn() -> Value>,
31 animation: AnimatedBindingKind,
32 ) -> Result<(), ()>;
33
34 fn prepare_property_for_two_way_binding(
35 self: Pin<&Self>,
36 name: &str,
37 ) -> Option<Pin<Rc<Property<Value>>>>;
38
39 fn link_property_two_way_with_map(
40 self: Pin<&Self>,
41 name: &str,
42 other: Pin<Rc<Property<Value>>>,
43 mapper: Option<Rc<dyn TwoWayBindingMapping<Value>>>,
44 );
45
46 fn call_callback(self: Pin<&Self>, name: &str, args: &[Value]) -> Result<Value, ()>;
47
48 fn set_callback_handler(
49 self: Pin<&Self>,
50 name: &str,
51 handler: Box<dyn Fn(&[Value]) -> Value>,
52 ) -> Result<(), ()>;
53
54 fn set_field(self: Pin<&Self>, name: &str, value: Value) -> Result<(), ()>;
56
57 fn as_item_ref(self: Pin<&Self>) -> Pin<VRef<'_, ItemVTable>>;
58}
59
60pub type ErasedItemRc = Pin<Rc<dyn ErasedItem>>;
61
62pub type SubComponentProperty = Pin<Rc<Property<Value>>>;
64
65pub type SubComponentCallback = Pin<Rc<Callback<[Value], Value>>>;
67
68impl InterpolatedPropertyValue for Value {
74 fn interpolate(&self, target: &Self, t: f32) -> Self {
75 match (self, target) {
76 (Value::Number(a), Value::Number(b)) => {
77 let (a, b) = (*a as f32, *b as f32);
78 Value::Number((a + (b - a) * t) as f64)
79 }
80 (Value::Brush(a), Value::Brush(b)) => Value::Brush(Brush::interpolate(a, b, t)),
81 _ => target.clone(),
82 }
83 }
84
85 fn interpolate_with_mode(&self, target: &Self, t: f32, mode: AngleInterpolation) -> Self {
86 match (self, target) {
87 (Value::Number(a), Value::Number(b)) => {
88 Value::Number((*a as f32).interpolate_with_mode(&(*b as f32), t, mode) as f64)
89 }
90 _ => self.interpolate(target, t),
91 }
92 }
93}