Skip to main content

slint_interpreter/
erased.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
4//! Trait object for manipulating native items without knowing their type.
5//!
6//! Every item, property and callback is heap-allocated in its own `Pin<Rc<…>>`.
7//! The trait methods dispatch by name to the item's `rtti::BuiltinItem` metadata.
8
9use 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
16/// Erased access to a boxed native item such as `Rectangle`, `Text` or `TouchArea`.
17pub 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    /// Write a plain (non-`Property`) field, e.g. the fields updated by layouts.
55    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
62/// A sub-component's own `Property<Value>`.
63pub type SubComponentProperty = Pin<Rc<Property<Value>>>;
64
65/// A sub-component's own callback, with a `Vec<Value>` argument tuple.
66pub type SubComponentCallback = Pin<Rc<Callback<[Value], Value>>>;
67
68/// Animated bindings on sub-component properties need `Value` to support
69/// interpolation. Numeric and brush variants interpolate; everything else
70/// snaps. Numbers interpolate in `f32` because animated numeric properties
71/// are `f32`-based — an `f64` blend would leak rounding noise like
72/// `99.50000001117587` instead of `99.5`.
73impl 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}