Skip to main content

slint_interpreter/
api.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// cSpell: ignore theproperty underscoresanddashespreserved xreadonly
5use crate::dynamic_item_tree::{ErasedItemTreeBox, WindowOptions};
6use i_slint_compiler::langtype::Type as LangType;
7use i_slint_core::PathData;
8use i_slint_core::component_factory::ComponentFactory;
9#[cfg(feature = "internal")]
10use i_slint_core::component_factory::FactoryContext;
11use i_slint_core::graphics::euclid::approxeq::ApproxEq as _;
12use i_slint_core::items::*;
13use i_slint_core::model::{Model, ModelExt, ModelRc};
14use i_slint_core::styled_text::StyledText;
15#[cfg(feature = "internal")]
16use i_slint_core::window::WindowInner;
17use smol_str::SmolStr;
18use std::collections::HashMap;
19use std::future::Future;
20use std::path::{Path, PathBuf};
21use std::rc::Rc;
22
23#[doc(inline)]
24pub use i_slint_compiler::diagnostics::{Diagnostic, DiagnosticLevel};
25
26pub use i_slint_backend_selector::api::*;
27pub use i_slint_core::api::*;
28
29/// Argument of [`Compiler::set_default_translation_context()`]
30///
31pub use i_slint_compiler::DefaultTranslationContext;
32
33/// This enum represents the different public variants of the [`Value`] enum, without
34/// the contained values.
35#[derive(Debug, Copy, Clone, PartialEq)]
36#[repr(i8)]
37#[non_exhaustive]
38pub enum ValueType {
39    /// The variant that expresses the non-type. This is the default.
40    Void,
41    /// An `int` or a `float` (this is also used for unit based type such as `length` or `angle`)
42    Number,
43    /// Correspond to the `string` type in .slint
44    String,
45    /// Correspond to the `bool` type in .slint
46    Bool,
47    /// A model (that includes array in .slint)
48    Model,
49    /// An object
50    Struct,
51    /// Correspond to `brush` or `color` type in .slint.  For color, this is then a [`Brush::SolidColor`]
52    Brush,
53    /// Correspond to `image` type in .slint.
54    Image,
55    /// The type is not a public type but something internal.
56    #[doc(hidden)]
57    Other = -1,
58}
59
60impl From<LangType> for ValueType {
61    fn from(ty: LangType) -> Self {
62        match ty {
63            LangType::Float32
64            | LangType::Int32
65            | LangType::Duration
66            | LangType::Angle
67            | LangType::PhysicalLength
68            | LangType::LogicalLength
69            | LangType::Percent
70            | LangType::UnitProduct(_) => Self::Number,
71            LangType::String => Self::String,
72            LangType::Color => Self::Brush,
73            LangType::Brush => Self::Brush,
74            LangType::Array(_) => Self::Model,
75            LangType::Bool => Self::Bool,
76            LangType::Struct { .. } => Self::Struct,
77            LangType::Void => Self::Void,
78            LangType::Image => Self::Image,
79            _ => Self::Other,
80        }
81    }
82}
83
84/// This is a dynamically typed value used in the Slint interpreter.
85/// It can hold a value of different types, and you should use the
86/// [`From`] or [`TryFrom`] traits to access the value.
87///
88/// ```
89/// # use slint_interpreter::*;
90/// use core::convert::TryInto;
91/// // create a value containing an integer
92/// let v = Value::from(100u32);
93/// assert_eq!(v.try_into(), Ok(100u32));
94/// ```
95#[derive(Clone, Default)]
96#[non_exhaustive]
97#[repr(u8)]
98pub enum Value {
99    /// There is nothing in this value. That's the default.
100    /// For example, a function that does not return a result would return a Value::Void
101    #[default]
102    Void = 0,
103    /// An `int` or a `float` (this is also used for unit based type such as `length` or `angle`)
104    Number(f64) = 1,
105    /// Correspond to the `string` type in .slint
106    String(SharedString) = 2,
107    /// Correspond to the `bool` type in .slint
108    Bool(bool) = 3,
109    /// Correspond to the `image` type in .slint
110    Image(Image) = 4,
111    /// A model (that includes array in .slint)
112    Model(ModelRc<Value>) = 5,
113    /// An object
114    Struct(Struct) = 6,
115    /// Correspond to `brush` or `color` type in .slint.  For color, this is then a [`Brush::SolidColor`]
116    Brush(Brush) = 7,
117    #[doc(hidden)]
118    /// The elements of a path
119    PathData(PathData) = 8,
120    #[doc(hidden)]
121    /// An easing curve
122    EasingCurve(i_slint_core::animations::EasingCurve) = 9,
123    #[doc(hidden)]
124    /// An enumeration, like `TextHorizontalAlignment::align_center`, represented by `("TextHorizontalAlignment", "align_center")`.
125    /// FIXME: consider representing that with a number?
126    EnumerationValue(String, String) = 10,
127    #[doc(hidden)]
128    LayoutCache(SharedVector<f32>) = 11,
129    #[doc(hidden)]
130    /// Correspond to the `component-factory` type in .slint
131    ComponentFactory(ComponentFactory) = 12,
132    #[doc(hidden)] // make visible when we make StyledText public
133    /// Correspond to the `styled-text` type in .slint
134    StyledText(StyledText) = 13,
135    #[doc(hidden)]
136    ArrayOfU16(SharedVector<u16>) = 14,
137    /// Correspond to the `keys` type in .slint
138    Keys(Keys) = 15,
139    /// Correspond to the `data-transfer` type in .slint
140    DataTransfer(DataTransfer) = 16,
141}
142
143impl Value {
144    /// Returns the type variant that this value holds without the containing value.
145    pub fn value_type(&self) -> ValueType {
146        match self {
147            Value::Void => ValueType::Void,
148            Value::Number(_) => ValueType::Number,
149            Value::String(_) => ValueType::String,
150            Value::Bool(_) => ValueType::Bool,
151            Value::Model(_) => ValueType::Model,
152            Value::Struct(_) => ValueType::Struct,
153            Value::Brush(_) => ValueType::Brush,
154            Value::Image(_) => ValueType::Image,
155            _ => ValueType::Other,
156        }
157    }
158}
159
160impl PartialEq for Value {
161    fn eq(&self, other: &Self) -> bool {
162        match self {
163            Value::Void => matches!(other, Value::Void),
164            Value::Number(lhs) => matches!(other, Value::Number(rhs) if lhs.approx_eq(rhs)),
165            Value::String(lhs) => matches!(other, Value::String(rhs) if lhs == rhs),
166            Value::Bool(lhs) => matches!(other, Value::Bool(rhs) if lhs == rhs),
167            Value::Image(lhs) => matches!(other, Value::Image(rhs) if lhs == rhs),
168            Value::Model(lhs) => {
169                if let Value::Model(rhs) = other {
170                    lhs == rhs
171                } else {
172                    false
173                }
174            }
175            Value::Struct(lhs) => matches!(other, Value::Struct(rhs) if lhs == rhs),
176            Value::Brush(lhs) => matches!(other, Value::Brush(rhs) if lhs == rhs),
177            Value::PathData(lhs) => matches!(other, Value::PathData(rhs) if lhs == rhs),
178            Value::EasingCurve(lhs) => matches!(other, Value::EasingCurve(rhs) if lhs == rhs),
179            Value::EnumerationValue(lhs_name, lhs_value) => {
180                matches!(other, Value::EnumerationValue(rhs_name, rhs_value) if lhs_name == rhs_name && lhs_value == rhs_value)
181            }
182            Value::LayoutCache(lhs) => matches!(other, Value::LayoutCache(rhs) if lhs == rhs),
183            Value::ArrayOfU16(lhs) => matches!(other, Value::ArrayOfU16(rhs) if lhs == rhs),
184            Value::ComponentFactory(lhs) => {
185                matches!(other, Value::ComponentFactory(rhs) if lhs == rhs)
186            }
187            Value::StyledText(lhs) => {
188                matches!(other, Value::StyledText(rhs) if lhs == rhs)
189            }
190            Value::Keys(lhs) => {
191                matches!(other, Value::Keys(rhs) if lhs == rhs)
192            }
193            Value::DataTransfer(lhs) => {
194                matches!(other, Value::DataTransfer(rhs) if lhs == rhs)
195            }
196        }
197    }
198}
199
200impl std::fmt::Debug for Value {
201    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202        match self {
203            Value::Void => write!(f, "Value::Void"),
204            Value::Number(n) => write!(f, "Value::Number({n:?})"),
205            Value::String(s) => write!(f, "Value::String({s:?})"),
206            Value::Bool(b) => write!(f, "Value::Bool({b:?})"),
207            Value::Image(i) => write!(f, "Value::Image({i:?})"),
208            Value::Model(m) => {
209                write!(f, "Value::Model(")?;
210                f.debug_list().entries(m.iter()).finish()?;
211                write!(f, "])")
212            }
213            Value::Struct(s) => write!(f, "Value::Struct({s:?})"),
214            Value::Brush(b) => write!(f, "Value::Brush({b:?})"),
215            Value::PathData(e) => write!(f, "Value::PathElements({e:?})"),
216            Value::EasingCurve(c) => write!(f, "Value::EasingCurve({c:?})"),
217            Value::EnumerationValue(n, v) => write!(f, "Value::EnumerationValue({n:?}, {v:?})"),
218            Value::LayoutCache(v) => write!(f, "Value::LayoutCache({v:?})"),
219            Value::ComponentFactory(factory) => write!(f, "Value::ComponentFactory({factory:?})"),
220            Value::StyledText(text) => write!(f, "Value::StyledText({text:?})"),
221            Value::ArrayOfU16(data) => {
222                write!(f, "Value::ArrayOfU16({data:?})")
223            }
224            Value::Keys(ks) => write!(f, "Value::Keys({ks:?})"),
225            Value::DataTransfer(cd) => write!(f, "Value::DataTransfer({cd:?})"),
226        }
227    }
228}
229
230/// Helper macro to implement the From / TryFrom for Value
231///
232/// For example
233/// `declare_value_conversion!(Number => [u32, u64, i32, i64, f32, f64] );`
234/// means that `Value::Number` can be converted to / from each of the said rust types
235///
236/// For `Value::Object` mapping to a rust `struct`, one can use [`declare_value_struct_conversion!`]
237/// And for `Value::EnumerationValue` which maps to a rust `enum`, one can use [`declare_value_enum_conversion!`]
238macro_rules! declare_value_conversion {
239    ( $value:ident => [$($ty:ty),*] ) => {
240        $(
241            impl From<$ty> for Value {
242                fn from(v: $ty) -> Self {
243                    Value::$value(v as _)
244                }
245            }
246            impl TryFrom<Value> for $ty {
247                type Error = Value;
248                fn try_from(v: Value) -> Result<$ty, Self::Error> {
249                    match v {
250                        Value::$value(x) => Ok(x as _),
251                        _ => Err(v)
252                    }
253                }
254            }
255        )*
256    };
257}
258declare_value_conversion!(Number => [u32, u64, i32, i64, f32, f64, usize, isize] );
259declare_value_conversion!(String => [SharedString] );
260declare_value_conversion!(Bool => [bool] );
261declare_value_conversion!(Image => [Image] );
262declare_value_conversion!(Struct => [Struct] );
263declare_value_conversion!(Brush => [Brush] );
264declare_value_conversion!(PathData => [PathData]);
265declare_value_conversion!(EasingCurve => [i_slint_core::animations::EasingCurve]);
266declare_value_conversion!(LayoutCache => [SharedVector<f32>] );
267declare_value_conversion!(ComponentFactory => [ComponentFactory] );
268declare_value_conversion!(StyledText => [StyledText] );
269declare_value_conversion!(ArrayOfU16 => [SharedVector<u16>] );
270declare_value_conversion!(Keys => [Keys]);
271declare_value_conversion!(DataTransfer => [DataTransfer]);
272
273/// Implement From / TryFrom for Value that convert a `struct` to/from `Value::Struct`
274macro_rules! declare_value_struct_conversion {
275    (struct $name:path { $($field:ident),* $(, ..$extra:expr)? }) => {
276        impl From<$name> for Value {
277            fn from($name { $($field),* , .. }: $name) -> Self {
278                let mut struct_ = Struct::default();
279                $(struct_.set_field(stringify!($field).into(), $field.into());)*
280                Value::Struct(struct_)
281            }
282        }
283        impl TryFrom<Value> for $name {
284            type Error = ();
285            fn try_from(v: Value) -> Result<$name, Self::Error> {
286                #[allow(clippy::field_reassign_with_default)]
287                match v {
288                    Value::Struct(x) => {
289                        type Ty = $name;
290                        #[allow(unused)]
291                        let mut res: Ty = Ty::default();
292                        $(let mut res: Ty = $extra;)?
293                        $(res.$field = x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?;)*
294                        Ok(res)
295                    }
296                    _ => Err(()),
297                }
298            }
299        }
300    };
301    ($(
302        $(#[$struct_attr:meta])*
303        $vis:vis struct $Name:ident {
304            $( $(#[$field_attr:meta])* $field:ident : $field_type:ty, )*
305        }
306    )*) => {
307        $(
308            impl From<$Name> for Value {
309                fn from(item: $Name) -> Self {
310                    let mut struct_ = Struct::default();
311                    $(struct_.set_field(stringify!($field).into(), item.$field.into());)*
312                    Value::Struct(struct_)
313                }
314            }
315            impl TryFrom<Value> for $Name {
316                type Error = ();
317                fn try_from(v: Value) -> Result<$Name, Self::Error> {
318                    #[allow(clippy::field_reassign_with_default)]
319                    match v {
320                        Value::Struct(x) => {
321                            type Ty = $Name;
322                            #[allow(unused)]
323                            let mut res: Ty = Ty::default();
324                            $(res.$field = x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?;)*
325                            Ok(res)
326                        }
327                        _ => Err(()),
328                    }
329                }
330            }
331        )*
332    };
333}
334
335declare_value_struct_conversion!(struct i_slint_core::layout::LayoutInfo { min, max, min_percent, max_percent, preferred, stretch });
336declare_value_struct_conversion!(struct i_slint_core::graphics::Point { x, y, ..Default::default()});
337declare_value_struct_conversion!(struct i_slint_core::api::LogicalPosition { x, y });
338declare_value_struct_conversion!(struct i_slint_core::api::LogicalSize { width, height });
339declare_value_struct_conversion!(struct i_slint_core::properties::StateInfo { current_state, previous_state, change_time });
340
341i_slint_common::for_each_builtin_structs!(declare_value_struct_conversion);
342
343/// Implement From / TryFrom for Value that convert an `enum` to/from `Value::EnumerationValue`
344///
345/// The `enum` must derive `Display` and `FromStr`
346/// (can be done with `strum_macros::EnumString`, `strum_macros::Display` derive macro)
347macro_rules! declare_value_enum_conversion {
348    ($( $(#[$enum_doc:meta])* $vis:vis enum $Name:ident { $($body:tt)* })*) => { $(
349        impl From<i_slint_core::items::$Name> for Value {
350            fn from(v: i_slint_core::items::$Name) -> Self {
351                Value::EnumerationValue(stringify!($Name).to_owned(), v.to_string())
352            }
353        }
354        impl TryFrom<Value> for i_slint_core::items::$Name {
355            type Error = ();
356            fn try_from(v: Value) -> Result<i_slint_core::items::$Name, ()> {
357                use std::str::FromStr;
358                match v {
359                    Value::EnumerationValue(enumeration, value) => {
360                        if enumeration != stringify!($Name) {
361                            return Err(());
362                        }
363                        i_slint_core::items::$Name::from_str(value.as_str()).map_err(|_| ())
364                    }
365                    _ => Err(()),
366                }
367            }
368        }
369    )*};
370}
371
372i_slint_common::for_each_enums!(declare_value_enum_conversion);
373
374impl From<i_slint_core::animations::Instant> for Value {
375    fn from(value: i_slint_core::animations::Instant) -> Self {
376        Value::Number(value.0 as _)
377    }
378}
379impl TryFrom<Value> for i_slint_core::animations::Instant {
380    type Error = ();
381    fn try_from(v: Value) -> Result<i_slint_core::animations::Instant, Self::Error> {
382        match v {
383            Value::Number(x) => Ok(i_slint_core::animations::Instant(x as _)),
384            _ => Err(()),
385        }
386    }
387}
388
389impl From<()> for Value {
390    #[inline]
391    fn from(_: ()) -> Self {
392        Value::Void
393    }
394}
395impl TryFrom<Value> for () {
396    type Error = ();
397    #[inline]
398    fn try_from(_: Value) -> Result<(), Self::Error> {
399        Ok(())
400    }
401}
402
403impl From<Color> for Value {
404    #[inline]
405    fn from(c: Color) -> Self {
406        Value::Brush(Brush::SolidColor(c))
407    }
408}
409impl TryFrom<Value> for Color {
410    type Error = Value;
411    #[inline]
412    fn try_from(v: Value) -> Result<Color, Self::Error> {
413        match v {
414            Value::Brush(Brush::SolidColor(c)) => Ok(c),
415            _ => Err(v),
416        }
417    }
418}
419
420impl From<i_slint_core::lengths::LogicalLength> for Value {
421    #[inline]
422    fn from(l: i_slint_core::lengths::LogicalLength) -> Self {
423        Value::Number(l.get() as _)
424    }
425}
426impl TryFrom<Value> for i_slint_core::lengths::LogicalLength {
427    type Error = Value;
428    #[inline]
429    fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalLength, Self::Error> {
430        match v {
431            Value::Number(n) => Ok(i_slint_core::lengths::LogicalLength::new(n as _)),
432            _ => Err(v),
433        }
434    }
435}
436
437impl From<i_slint_core::lengths::LogicalPoint> for Value {
438    #[inline]
439    fn from(pt: i_slint_core::lengths::LogicalPoint) -> Self {
440        Value::Struct(Struct::from_iter([
441            ("x".to_owned(), Value::Number(pt.x as _)),
442            ("y".to_owned(), Value::Number(pt.y as _)),
443        ]))
444    }
445}
446impl TryFrom<Value> for i_slint_core::lengths::LogicalPoint {
447    type Error = Value;
448    #[inline]
449    fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalPoint, Self::Error> {
450        match v {
451            Value::Struct(s) => {
452                let x = s
453                    .get_field("x")
454                    .cloned()
455                    .unwrap_or_else(|| Value::Number(0 as _))
456                    .try_into()?;
457                let y = s
458                    .get_field("y")
459                    .cloned()
460                    .unwrap_or_else(|| Value::Number(0 as _))
461                    .try_into()?;
462                Ok(i_slint_core::lengths::LogicalPoint::new(x, y))
463            }
464            _ => Err(v),
465        }
466    }
467}
468
469impl From<i_slint_core::lengths::LogicalSize> for Value {
470    #[inline]
471    fn from(s: i_slint_core::lengths::LogicalSize) -> Self {
472        Value::Struct(Struct::from_iter([
473            ("width".to_owned(), Value::Number(s.width as _)),
474            ("height".to_owned(), Value::Number(s.height as _)),
475        ]))
476    }
477}
478impl TryFrom<Value> for i_slint_core::lengths::LogicalSize {
479    type Error = Value;
480    #[inline]
481    fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalSize, Self::Error> {
482        match v {
483            Value::Struct(s) => {
484                let width = s
485                    .get_field("width")
486                    .cloned()
487                    .unwrap_or_else(|| Value::Number(0 as _))
488                    .try_into()?;
489                let height = s
490                    .get_field("height")
491                    .cloned()
492                    .unwrap_or_else(|| Value::Number(0 as _))
493                    .try_into()?;
494                Ok(i_slint_core::lengths::LogicalSize::new(width, height))
495            }
496            _ => Err(v),
497        }
498    }
499}
500
501impl From<i_slint_core::lengths::LogicalEdges> for Value {
502    #[inline]
503    fn from(s: i_slint_core::lengths::LogicalEdges) -> Self {
504        Value::Struct(Struct::from_iter([
505            ("left".to_owned(), Value::Number(s.left as _)),
506            ("right".to_owned(), Value::Number(s.right as _)),
507            ("top".to_owned(), Value::Number(s.top as _)),
508            ("bottom".to_owned(), Value::Number(s.bottom as _)),
509        ]))
510    }
511}
512impl TryFrom<Value> for i_slint_core::lengths::LogicalEdges {
513    type Error = Value;
514    #[inline]
515    fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalEdges, Self::Error> {
516        match v {
517            Value::Struct(s) => {
518                let left = s
519                    .get_field("left")
520                    .cloned()
521                    .unwrap_or_else(|| Value::Number(0 as _))
522                    .try_into()?;
523                let right = s
524                    .get_field("right")
525                    .cloned()
526                    .unwrap_or_else(|| Value::Number(0 as _))
527                    .try_into()?;
528                let top = s
529                    .get_field("top")
530                    .cloned()
531                    .unwrap_or_else(|| Value::Number(0 as _))
532                    .try_into()?;
533                let bottom = s
534                    .get_field("bottom")
535                    .cloned()
536                    .unwrap_or_else(|| Value::Number(0 as _))
537                    .try_into()?;
538                Ok(i_slint_core::lengths::LogicalEdges::new(left, right, top, bottom))
539            }
540            _ => Err(v),
541        }
542    }
543}
544
545impl<T: Into<Value> + TryFrom<Value> + 'static> From<ModelRc<T>> for Value {
546    fn from(m: ModelRc<T>) -> Self {
547        if let Some(v) = <dyn core::any::Any>::downcast_ref::<ModelRc<Value>>(&m) {
548            Value::Model(v.clone())
549        } else {
550            Value::Model(ModelRc::new(crate::value_model::ValueMapModel(m)))
551        }
552    }
553}
554impl<T: TryFrom<Value> + Default + 'static> TryFrom<Value> for ModelRc<T> {
555    type Error = Value;
556    #[inline]
557    fn try_from(v: Value) -> Result<ModelRc<T>, Self::Error> {
558        match v {
559            Value::Model(m) => {
560                if let Some(v) = <dyn core::any::Any>::downcast_ref::<ModelRc<T>>(&m) {
561                    Ok(v.clone())
562                } else if let Some(v) =
563                    m.as_any().downcast_ref::<crate::value_model::ValueMapModel<T>>()
564                {
565                    Ok(v.0.clone())
566                } else {
567                    Ok(ModelRc::new(m.map(|v| T::try_from(v).unwrap_or_default())))
568                }
569            }
570            _ => Err(v),
571        }
572    }
573}
574
575#[test]
576fn value_model_conversion() {
577    use i_slint_core::model::*;
578    let m = ModelRc::new(VecModel::from_slice(&[Value::Number(42.), Value::Number(12.)]));
579    let v = Value::from(m.clone());
580    assert_eq!(v, Value::Model(m.clone()));
581    let m2: ModelRc<Value> = v.clone().try_into().unwrap();
582    assert_eq!(m2, m);
583
584    let int_model: ModelRc<i32> = v.clone().try_into().unwrap();
585    assert_eq!(int_model.row_count(), 2);
586    assert_eq!(int_model.iter().collect::<Vec<_>>(), vec![42, 12]);
587
588    let Value::Model(m3) = int_model.clone().into() else { panic!("not a model?") };
589    assert_eq!(m3.row_count(), 2);
590    assert_eq!(m3.iter().collect::<Vec<_>>(), vec![Value::Number(42.), Value::Number(12.)]);
591
592    let str_model: ModelRc<SharedString> = v.clone().try_into().unwrap();
593    assert_eq!(str_model.row_count(), 2);
594    // Value::Int doesn't convert to string, but since the mapping can't report error, we get the default constructed string
595    assert_eq!(str_model.iter().collect::<Vec<_>>(), vec!["", ""]);
596
597    let err: Result<ModelRc<Value>, _> = Value::Bool(true).try_into();
598    assert!(err.is_err());
599
600    let model =
601        Rc::new(VecModel::<SharedString>::from_iter(["foo".into(), "bar".into(), "baz".into()]));
602
603    let value: Value = ModelRc::from(model.clone()).into();
604    let value_model: ModelRc<Value> = value.clone().try_into().unwrap();
605    assert_eq!(value_model.row_data(2).unwrap(), Value::String("baz".into()));
606    value_model.set_row_data(1, Value::String("qux".into()));
607    value_model.set_row_data(0, Value::Bool(true));
608    assert_eq!(value_model.row_data(1).unwrap(), Value::String("qux".into()));
609    // This is backed by a string model, so changing to bool has no effect
610    assert_eq!(value_model.row_data(0).unwrap(), Value::String("foo".into()));
611
612    // The original values are changed
613    assert_eq!(model.row_data(1).unwrap(), SharedString::from("qux"));
614    assert_eq!(model.row_data(0).unwrap(), SharedString::from("foo"));
615
616    let the_model: ModelRc<SharedString> = value.try_into().unwrap();
617    assert_eq!(the_model.row_data(1).unwrap(), SharedString::from("qux"));
618    assert_eq!(
619        model.as_ref() as *const VecModel<SharedString>,
620        the_model.as_any().downcast_ref::<VecModel<SharedString>>().unwrap()
621            as *const VecModel<SharedString>
622    );
623}
624
625pub(crate) fn normalize_identifier(ident: &str) -> SmolStr {
626    i_slint_compiler::parser::normalize_identifier(ident)
627}
628
629/// This type represents a runtime instance of structure in `.slint`.
630///
631/// This can either be an instance of a name structure introduced
632/// with the `struct` keyword in the .slint file, or an anonymous struct
633/// written with the `{ key: value, }`  notation.
634///
635/// It can be constructed with the [`FromIterator`] trait, and converted
636/// into or from a [`Value`] with the [`From`], [`TryFrom`] trait
637///
638///
639/// ```
640/// # use slint_interpreter::*;
641/// use core::convert::TryInto;
642/// // Construct a value from a key/value iterator
643/// let value : Value = [("foo".into(), 45u32.into()), ("bar".into(), true.into())]
644///     .iter().cloned().collect::<Struct>().into();
645///
646/// // get the properties of a `{ foo: 45, bar: true }`
647/// let s : Struct = value.try_into().unwrap();
648/// assert_eq!(s.get_field("foo").cloned().unwrap().try_into(), Ok(45u32));
649/// ```
650#[derive(Clone, PartialEq, Debug, Default)]
651pub struct Struct(pub(crate) HashMap<SmolStr, Value>);
652impl Struct {
653    /// Get the value for a given struct field
654    pub fn get_field(&self, name: &str) -> Option<&Value> {
655        self.0.get(&*normalize_identifier(name))
656    }
657    /// Set the value of a given struct field
658    pub fn set_field(&mut self, name: String, value: Value) {
659        self.0.insert(normalize_identifier(&name), value);
660    }
661
662    /// Iterate over all the fields in this struct
663    pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
664        self.0.iter().map(|(a, b)| (a.as_str(), b))
665    }
666}
667
668impl FromIterator<(String, Value)> for Struct {
669    fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
670        Self(iter.into_iter().map(|(s, v)| (normalize_identifier(&s), v)).collect())
671    }
672}
673
674/// ComponentCompiler is deprecated, use [`Compiler`] instead
675#[deprecated(note = "Use slint_interpreter::Compiler instead")]
676pub struct ComponentCompiler {
677    config: i_slint_compiler::CompilerConfiguration,
678    diagnostics: Vec<Diagnostic>,
679}
680
681#[allow(deprecated)]
682impl Default for ComponentCompiler {
683    fn default() -> Self {
684        let mut config = i_slint_compiler::CompilerConfiguration::new(
685            i_slint_compiler::generator::OutputFormat::Interpreter,
686        );
687        config.components_to_generate = i_slint_compiler::ComponentSelection::LastExported;
688        Self { config, diagnostics: Vec::new() }
689    }
690}
691
692#[allow(deprecated)]
693impl ComponentCompiler {
694    /// Returns a new ComponentCompiler.
695    pub fn new() -> Self {
696        Self::default()
697    }
698
699    /// Sets the include paths used for looking up `.slint` imports to the specified vector of paths.
700    pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
701        self.config.include_paths = include_paths;
702    }
703
704    /// Returns the include paths the component compiler is currently configured with.
705    pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
706        &self.config.include_paths
707    }
708
709    /// Sets the library paths used for looking up `@library` imports to the specified map of library names to paths.
710    pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
711        self.config.library_paths = library_paths;
712    }
713
714    /// Returns the library paths the component compiler is currently configured with.
715    pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
716        &self.config.library_paths
717    }
718
719    /// Sets the style to be used for widgets.
720    ///
721    /// Use the "material" style as widget style when compiling:
722    /// ```rust
723    /// use slint_interpreter::{ComponentDefinition, ComponentCompiler, ComponentHandle};
724    ///
725    /// let mut compiler = ComponentCompiler::default();
726    /// compiler.set_style("material".into());
727    /// let definition =
728    ///     spin_on::spin_on(compiler.build_from_path("hello.slint"));
729    /// ```
730    pub fn set_style(&mut self, style: String) {
731        self.config.style = Some(style);
732    }
733
734    /// Returns the widget style the compiler is currently using when compiling .slint files.
735    pub fn style(&self) -> Option<&String> {
736        self.config.style.as_ref()
737    }
738
739    /// The domain used for translations
740    pub fn set_translation_domain(&mut self, domain: String) {
741        self.config.translation_domain = Some(domain);
742    }
743
744    /// Sets the callback that will be invoked when loading imported .slint files. The specified
745    /// `file_loader_callback` parameter will be called with a canonical file path as argument
746    /// and is expected to return a future that, when resolved, provides the source code of the
747    /// .slint file to be imported as a string.
748    /// If an error is returned, then the build will abort with that error.
749    /// If None is returned, it means the normal resolution algorithm will proceed as if the hook
750    /// was not in place (i.e: load from the file system following the include paths)
751    pub fn set_file_loader(
752        &mut self,
753        file_loader_fallback: impl Fn(
754            &Path,
755        ) -> core::pin::Pin<
756            Box<dyn Future<Output = Option<std::io::Result<String>>>>,
757        > + 'static,
758    ) {
759        self.config.open_import_callback =
760            Some(Rc::new(move |path| file_loader_fallback(Path::new(path.as_str()))));
761    }
762
763    /// Returns the diagnostics that were produced in the last call to [`Self::build_from_path`] or [`Self::build_from_source`].
764    pub fn diagnostics(&self) -> &Vec<Diagnostic> {
765        &self.diagnostics
766    }
767
768    /// Compile a .slint file into a ComponentDefinition
769    ///
770    /// Returns the compiled `ComponentDefinition` if there were no errors.
771    ///
772    /// Any diagnostics produced during the compilation, such as warnings or errors, are collected
773    /// in this ComponentCompiler and can be retrieved after the call using the [`Self::diagnostics()`]
774    /// function. The [`print_diagnostics`] function can be used to display the diagnostics
775    /// to the users.
776    ///
777    /// Diagnostics from previous calls are cleared when calling this function.
778    ///
779    /// If the path is `"-"`, the file will be read from stdin.
780    /// If the extension of the file .rs, the first `slint!` macro from a rust file will be extracted
781    ///
782    /// This function is `async` but in practice, this is only asynchronous if
783    /// [`Self::set_file_loader`] was called and its future is actually asynchronous.
784    /// If that is not used, then it is fine to use a very simple executor, such as the one
785    /// provided by the `spin_on` crate
786    pub async fn build_from_path<P: AsRef<Path>>(
787        &mut self,
788        path: P,
789    ) -> Option<ComponentDefinition> {
790        let path = path.as_ref();
791        let source = match i_slint_compiler::diagnostics::load_from_path(path) {
792            Ok(s) => s,
793            Err(d) => {
794                self.diagnostics = vec![d];
795                return None;
796            }
797        };
798
799        let r = crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await;
800        self.diagnostics = r.diagnostics.into_iter().collect();
801        r.components.into_values().next()
802    }
803
804    /// Compile some .slint code into a ComponentDefinition
805    ///
806    /// The `path` argument will be used for diagnostics and to compute relative
807    /// paths while importing.
808    ///
809    /// Any diagnostics produced during the compilation, such as warnings or errors, are collected
810    /// in this ComponentCompiler and can be retrieved after the call using the [`Self::diagnostics()`]
811    /// function. The [`print_diagnostics`] function can be used to display the diagnostics
812    /// to the users.
813    ///
814    /// Diagnostics from previous calls are cleared when calling this function.
815    ///
816    /// This function is `async` but in practice, this is only asynchronous if
817    /// [`Self::set_file_loader`] is set and its future is actually asynchronous.
818    /// If that is not used, then it is fine to use a very simple executor, such as the one
819    /// provided by the `spin_on` crate
820    pub async fn build_from_source(
821        &mut self,
822        source_code: String,
823        path: PathBuf,
824    ) -> Option<ComponentDefinition> {
825        let r = crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await;
826        self.diagnostics = r.diagnostics.into_iter().collect();
827        r.components.into_values().next()
828    }
829}
830
831/// This is the entry point of the crate, it can be used to load a `.slint` file and
832/// compile it into a [`CompilationResult`].
833pub struct Compiler {
834    config: i_slint_compiler::CompilerConfiguration,
835}
836
837impl Default for Compiler {
838    fn default() -> Self {
839        let config = i_slint_compiler::CompilerConfiguration::new(
840            i_slint_compiler::generator::OutputFormat::Interpreter,
841        );
842        Self { config }
843    }
844}
845
846impl Compiler {
847    /// Returns a new Compiler.
848    pub fn new() -> Self {
849        Self::default()
850    }
851
852    #[cfg(feature = "internal-live-preview")]
853    pub(crate) fn set_embed_resources(
854        &mut self,
855        embed_resources: i_slint_compiler::EmbedResourcesKind,
856    ) {
857        self.config.embed_resources = embed_resources;
858    }
859
860    /// Allow access to the underlying `CompilerConfiguration`
861    ///
862    /// This is an internal function without and ABI or API stability guarantees.
863    #[doc(hidden)]
864    #[cfg(feature = "internal")]
865    pub fn compiler_configuration(
866        &mut self,
867        _: i_slint_core::InternalToken,
868    ) -> &mut i_slint_compiler::CompilerConfiguration {
869        &mut self.config
870    }
871
872    /// Sets the include paths used for looking up `.slint` imports to the specified vector of paths.
873    pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
874        self.config.include_paths = include_paths;
875    }
876
877    /// Returns the include paths the component compiler is currently configured with.
878    pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
879        &self.config.include_paths
880    }
881
882    /// Sets the library paths used for looking up `@library` imports to the specified map of library names to paths.
883    pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
884        self.config.library_paths = library_paths;
885    }
886
887    /// Returns the library paths the component compiler is currently configured with.
888    pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
889        &self.config.library_paths
890    }
891
892    /// Sets the style to be used for widgets.
893    ///
894    /// Use the "material" style as widget style when compiling:
895    /// ```rust
896    /// use slint_interpreter::{ComponentDefinition, Compiler, ComponentHandle};
897    ///
898    /// let mut compiler = Compiler::default();
899    /// compiler.set_style("material".into());
900    /// let result = spin_on::spin_on(compiler.build_from_path("hello.slint"));
901    /// ```
902    pub fn set_style(&mut self, style: String) {
903        self.config.style = Some(style);
904    }
905
906    /// Returns the widget style the compiler is currently using when compiling .slint files.
907    pub fn style(&self) -> Option<&String> {
908        self.config.style.as_ref()
909    }
910
911    /// The domain used for translations
912    pub fn set_translation_domain(&mut self, domain: String) {
913        self.config.translation_domain = Some(domain);
914    }
915
916    /// Unless explicitly specified with the `@tr("context" => ...)`, the default translation context is the component name.
917    /// Use this option with [`DefaultTranslationContext::None`] to disable the default translation context.
918    ///
919    /// The translation file must also not have context
920    /// (`--no-default-translation-context` argument of `slint-tr-extractor`)
921    pub fn set_default_translation_context(
922        &mut self,
923        default_translation_context: DefaultTranslationContext,
924    ) {
925        self.config.default_translation_context = default_translation_context;
926    }
927
928    /// Sets the callback that will be invoked when loading imported .slint files. The specified
929    /// `file_loader_callback` parameter will be called with a canonical file path as argument
930    /// and is expected to return a future that, when resolved, provides the source code of the
931    /// .slint file to be imported as a string.
932    /// If an error is returned, then the build will abort with that error.
933    /// If None is returned, it means the normal resolution algorithm will proceed as if the hook
934    /// was not in place (i.e: load from the file system following the include paths)
935    pub fn set_file_loader(
936        &mut self,
937        file_loader_fallback: impl Fn(
938            &Path,
939        ) -> core::pin::Pin<
940            Box<dyn Future<Output = Option<std::io::Result<String>>>>,
941        > + 'static,
942    ) {
943        self.config.open_import_callback =
944            Some(Rc::new(move |path| file_loader_fallback(Path::new(path.as_str()))));
945    }
946
947    /// Compile a .slint file
948    ///
949    /// Returns a structure that holds the diagnostics and the compiled components.
950    ///
951    /// Any diagnostics produced during the compilation, such as warnings or errors, can be retrieved
952    /// after the call using [`CompilationResult::diagnostics()`].
953    ///
954    /// If the file was compiled without error, the list of component names can be obtained with
955    /// [`CompilationResult::component_names`], and the compiled components themselves with
956    /// [`CompilationResult::component()`].
957    ///
958    /// If the path is `"-"`, the file will be read from stdin.
959    /// If the extension of the file .rs, the first `slint!` macro from a rust file will be extracted
960    ///
961    /// This function is `async` but in practice, this is only asynchronous if
962    /// [`Self::set_file_loader`] was called and its future is actually asynchronous.
963    /// If that is not used, then it is fine to use a very simple executor, such as the one
964    /// provided by the `spin_on` crate
965    pub async fn build_from_path<P: AsRef<Path>>(&self, path: P) -> CompilationResult {
966        let path = path.as_ref();
967        let source = match i_slint_compiler::diagnostics::load_from_path(path) {
968            Ok(s) => s,
969            Err(d) => {
970                let mut diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
971                diagnostics.push_compiler_error(d);
972                return CompilationResult {
973                    components: HashMap::new(),
974                    diagnostics: diagnostics.into_iter().collect(),
975                    #[cfg(feature = "internal-file-watcher")]
976                    watch_paths: vec![i_slint_compiler::pathutils::clean_path(path)],
977                    #[cfg(feature = "internal")]
978                    structs_and_enums: Vec::new(),
979                    #[cfg(feature = "internal")]
980                    named_exports: Vec::new(),
981                };
982            }
983        };
984
985        crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await
986    }
987
988    /// Compile some .slint code
989    ///
990    /// The `path` argument will be used for diagnostics and to compute relative
991    /// paths while importing.
992    ///
993    /// Any diagnostics produced during the compilation, such as warnings or errors, can be retrieved
994    /// after the call using [`CompilationResult::diagnostics()`].
995    ///
996    /// This function is `async` but in practice, this is only asynchronous if
997    /// [`Self::set_file_loader`] is set and its future is actually asynchronous.
998    /// If that is not used, then it is fine to use a very simple executor, such as the one
999    /// provided by the `spin_on` crate
1000    pub async fn build_from_source(&self, source_code: String, path: PathBuf) -> CompilationResult {
1001        crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await
1002    }
1003}
1004
1005/// The result of a compilation
1006///
1007/// If [`Self::has_errors()`] is true, then the compilation failed.
1008/// The [`Self::diagnostics()`] function can be used to retrieve the diagnostics (errors and/or warnings)
1009/// or [`Self::print_diagnostics()`] can be used to print them to stderr.
1010/// The components can be retrieved using [`Self::components()`]
1011#[derive(Clone)]
1012pub struct CompilationResult {
1013    pub(crate) components: HashMap<String, ComponentDefinition>,
1014    pub(crate) diagnostics: Vec<Diagnostic>,
1015    #[cfg(feature = "internal-file-watcher")]
1016    pub(crate) watch_paths: Vec<PathBuf>,
1017    #[cfg(feature = "internal")]
1018    pub(crate) structs_and_enums: Vec<LangType>,
1019    /// For `export { Foo as Bar }` this vec contains tuples of (`Foo`, `Bar`)
1020    #[cfg(feature = "internal")]
1021    pub(crate) named_exports: Vec<(String, String)>,
1022}
1023
1024impl core::fmt::Debug for CompilationResult {
1025    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1026        f.debug_struct("CompilationResult")
1027            .field("components", &self.components.keys())
1028            .field("diagnostics", &self.diagnostics)
1029            .finish()
1030    }
1031}
1032
1033impl CompilationResult {
1034    /// Returns true if the compilation failed.
1035    /// The errors can be retrieved using the [`Self::diagnostics()`] function.
1036    pub fn has_errors(&self) -> bool {
1037        self.diagnostics().any(|diag| diag.level() == DiagnosticLevel::Error)
1038    }
1039
1040    /// Return an iterator over the diagnostics.
1041    ///
1042    /// You can also call [`Self::print_diagnostics()`] to output the diagnostics to stderr
1043    pub fn diagnostics(&self) -> impl Iterator<Item = Diagnostic> + '_ {
1044        self.diagnostics.iter().cloned()
1045    }
1046
1047    /// Print the diagnostics to stderr
1048    ///
1049    /// The diagnostics are printed in the same style as rustc errors
1050    ///
1051    /// This function is available when the `display-diagnostics` is enabled.
1052    #[cfg(feature = "display-diagnostics")]
1053    pub fn print_diagnostics(&self) {
1054        print_diagnostics(&self.diagnostics)
1055    }
1056
1057    /// Returns an iterator over the compiled components.
1058    pub fn components(&self) -> impl Iterator<Item = ComponentDefinition> + '_ {
1059        self.components.values().cloned()
1060    }
1061
1062    /// Returns the names of the components that were compiled.
1063    pub fn component_names(&self) -> impl Iterator<Item = &str> + '_ {
1064        self.components.keys().map(|s| s.as_str())
1065    }
1066
1067    /// Return the component definition for the given name.
1068    /// If the component does not exist, then `None` is returned.
1069    pub fn component(&self, name: &str) -> Option<ComponentDefinition> {
1070        self.components.get(name).cloned()
1071    }
1072
1073    /// This is an internal function without API stability guarantees.
1074    #[doc(hidden)]
1075    #[cfg(feature = "internal-file-watcher")]
1076    pub fn watch_paths(&self, _: i_slint_core::InternalToken) -> &[PathBuf] {
1077        &self.watch_paths
1078    }
1079
1080    /// This is an internal function without API stability guarantees.
1081    #[doc(hidden)]
1082    #[cfg(feature = "internal")]
1083    pub fn structs_and_enums(
1084        &self,
1085        _: i_slint_core::InternalToken,
1086    ) -> impl Iterator<Item = &LangType> {
1087        self.structs_and_enums.iter()
1088    }
1089
1090    /// This is an internal function without API stability guarantees.
1091    /// Returns the list of named export aliases as tuples (`export { Foo as Bar}` is (`Foo`, `Bar` tuple)).
1092    #[doc(hidden)]
1093    #[cfg(feature = "internal")]
1094    pub fn named_exports(
1095        &self,
1096        _: i_slint_core::InternalToken,
1097    ) -> impl Iterator<Item = &(String, String)> {
1098        self.named_exports.iter()
1099    }
1100}
1101
1102/// ComponentDefinition is a representation of a compiled component from .slint markup.
1103///
1104/// It can be constructed from a .slint file using the [`Compiler::build_from_path`] or [`Compiler::build_from_source`] functions.
1105/// And then it can be instantiated with the [`Self::create`] function.
1106///
1107/// The ComponentDefinition acts as a factory to create new instances. When you've finished
1108/// creating the instances it is safe to drop the ComponentDefinition.
1109#[derive(Clone)]
1110pub struct ComponentDefinition {
1111    pub(crate) inner: crate::dynamic_item_tree::ErasedItemTreeDescription,
1112}
1113
1114impl ComponentDefinition {
1115    /// Set a `debug(...)` handler
1116    #[doc(hidden)]
1117    #[cfg(feature = "internal")]
1118    pub fn set_debug_handler(
1119        &self,
1120        handler: impl Fn(Option<&i_slint_compiler::diagnostics::SourceLocation>, &str) + 'static,
1121        _: i_slint_core::InternalToken,
1122    ) {
1123        let handler = Rc::new(handler);
1124
1125        generativity::make_guard!(guard);
1126        self.inner.unerase(guard).recursively_set_debug_handler(handler);
1127    }
1128    /// Creates a new instance of the component and returns a shared handle to it.
1129    pub fn create(&self) -> Result<ComponentInstance, PlatformError> {
1130        let instance = self.create_with_options(Default::default())?;
1131        // SystemTrayIcon-rooted components don't have a real WindowAdapter.
1132        // Skip the eager window creation and tree instantiation for them.
1133        if !instance.is_system_tray_rooted() {
1134            // Make sure the window adapter is created so call to `window()` do not panic later.
1135            instance.inner.window_adapter_ref()?;
1136            // Eagerly instantiate repeaters and conditionals so that layout
1137            // bindings can see all instances without calling ensure_updated.
1138            i_slint_core::window::WindowInner::from_pub(instance.window())
1139                .ensure_tree_instantiated();
1140        }
1141        Ok(instance)
1142    }
1143
1144    /// Creates a new instance of the component and returns a shared handle to it.
1145    #[doc(hidden)]
1146    #[cfg(feature = "internal")]
1147    pub fn create_embedded(&self, ctx: FactoryContext) -> Result<ComponentInstance, PlatformError> {
1148        self.create_with_options(WindowOptions::Embed {
1149            parent_item_tree: ctx.parent_item_tree,
1150            parent_item_tree_index: ctx.parent_item_tree_index,
1151        })
1152    }
1153
1154    /// Instantiate the component using an existing window.
1155    #[doc(hidden)]
1156    #[cfg(feature = "internal")]
1157    pub fn create_with_existing_window(
1158        &self,
1159        window: &Window,
1160    ) -> Result<ComponentInstance, PlatformError> {
1161        self.create_with_options(WindowOptions::UseExistingWindow(
1162            WindowInner::from_pub(window).window_adapter(),
1163        ))
1164    }
1165
1166    /// Private implementation of create
1167    pub(crate) fn create_with_options(
1168        &self,
1169        options: WindowOptions,
1170    ) -> Result<ComponentInstance, PlatformError> {
1171        generativity::make_guard!(guard);
1172        Ok(ComponentInstance { inner: self.inner.unerase(guard).clone().create(options)? })
1173    }
1174
1175    /// List of publicly declared properties or callback.
1176    ///
1177    /// This is internal because it exposes the `Type` from compilerlib.
1178    #[doc(hidden)]
1179    #[cfg(feature = "internal")]
1180    pub fn properties_and_callbacks(
1181        &self,
1182    ) -> impl Iterator<
1183        Item = (
1184            String,
1185            (i_slint_compiler::langtype::Type, i_slint_compiler::object_tree::PropertyVisibility),
1186        ),
1187    > + '_ {
1188        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1189        // which is not required, but this is safe because there is only one instance of the unerased type
1190        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1191        self.inner.unerase(guard).properties().map(|(s, t, v)| (s.to_string(), (t, v)))
1192    }
1193
1194    /// Returns an iterator over all publicly declared properties. Each iterator item is a tuple of property name
1195    /// and property type for each of them.
1196    pub fn properties(&self) -> impl Iterator<Item = (String, ValueType)> + '_ {
1197        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1198        // which is not required, but this is safe because there is only one instance of the unerased type
1199        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1200        self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1201            if prop_type.is_property_type() {
1202                Some((prop_name.to_string(), prop_type.into()))
1203            } else {
1204                None
1205            }
1206        })
1207    }
1208
1209    /// Returns the names of all publicly declared callbacks.
1210    pub fn callbacks(&self) -> impl Iterator<Item = String> + '_ {
1211        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1212        // which is not required, but this is safe because there is only one instance of the unerased type
1213        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1214        self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1215            if matches!(prop_type, LangType::Callback { .. }) {
1216                Some(prop_name.to_string())
1217            } else {
1218                None
1219            }
1220        })
1221    }
1222
1223    /// Returns the names of all publicly declared functions.
1224    pub fn functions(&self) -> impl Iterator<Item = String> + '_ {
1225        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1226        // which is not required, but this is safe because there is only one instance of the unerased type
1227        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1228        self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1229            if matches!(prop_type, LangType::Function { .. }) {
1230                Some(prop_name.to_string())
1231            } else {
1232                None
1233            }
1234        })
1235    }
1236
1237    /// Returns the names of all exported global singletons
1238    ///
1239    /// **Note:** Only globals that are exported or re-exported from the main .slint file will
1240    /// be exposed in the API
1241    pub fn globals(&self) -> impl Iterator<Item = String> + '_ {
1242        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1243        // which is not required, but this is safe because there is only one instance of the unerased type
1244        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1245        self.inner.unerase(guard).global_names().map(|s| s.to_string())
1246    }
1247
1248    /// List of publicly declared properties or callback in the exported global singleton specified by its name.
1249    ///
1250    /// This is internal because it exposes the `Type` from compilerlib.
1251    #[doc(hidden)]
1252    #[cfg(feature = "internal")]
1253    pub fn global_properties_and_callbacks(
1254        &self,
1255        global_name: &str,
1256    ) -> Option<
1257        impl Iterator<
1258            Item = (
1259                String,
1260                (
1261                    i_slint_compiler::langtype::Type,
1262                    i_slint_compiler::object_tree::PropertyVisibility,
1263                ),
1264            ),
1265        > + '_,
1266    > {
1267        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1268        // which is not required, but this is safe because there is only one instance of the unerased type
1269        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1270        self.inner
1271            .unerase(guard)
1272            .global_properties(global_name)
1273            .map(|o| o.map(|(s, t, v)| (s.to_string(), (t, v))))
1274    }
1275
1276    /// List of publicly declared properties in the exported global singleton specified by its name.
1277    pub fn global_properties(
1278        &self,
1279        global_name: &str,
1280    ) -> Option<impl Iterator<Item = (String, ValueType)> + '_> {
1281        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1282        // which is not required, but this is safe because there is only one instance of the unerased type
1283        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1284        self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1285            iter.filter_map(|(prop_name, prop_type, _)| {
1286                if prop_type.is_property_type() {
1287                    Some((prop_name.to_string(), prop_type.into()))
1288                } else {
1289                    None
1290                }
1291            })
1292        })
1293    }
1294
1295    /// List of publicly declared callbacks in the exported global singleton specified by its name.
1296    pub fn global_callbacks(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1297        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1298        // which is not required, but this is safe because there is only one instance of the unerased type
1299        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1300        self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1301            iter.filter_map(|(prop_name, prop_type, _)| {
1302                if matches!(prop_type, LangType::Callback { .. }) {
1303                    Some(prop_name.to_string())
1304                } else {
1305                    None
1306                }
1307            })
1308        })
1309    }
1310
1311    /// List of publicly declared functions in the exported global singleton specified by its name.
1312    pub fn global_functions(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1313        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1314        // which is not required, but this is safe because there is only one instance of the unerased type
1315        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1316        self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1317            iter.filter_map(|(prop_name, prop_type, _)| {
1318                if matches!(prop_type, LangType::Function { .. }) {
1319                    Some(prop_name.to_string())
1320                } else {
1321                    None
1322                }
1323            })
1324        })
1325    }
1326
1327    /// The name of this Component as written in the .slint file
1328    pub fn name(&self) -> &str {
1329        // We create here a 'static guard, because unfortunately the returned type would be restricted to the guard lifetime
1330        // which is not required, but this is safe because there is only one instance of the unerased type
1331        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1332        self.inner.unerase(guard).id()
1333    }
1334
1335    /// True if instances of this component expose a `slint::Window`-shaped API
1336    /// (i.e. calling [`ComponentInstance::window`] is meaningful). False for
1337    /// non-windowed roots such as `SystemTrayIcon`, where `window()` would panic.
1338    #[doc(hidden)]
1339    #[cfg(feature = "internal")]
1340    pub fn is_window(&self) -> bool {
1341        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1342        !self.inner.unerase(guard).original.inherits_system_tray_icon()
1343    }
1344
1345    /// This gives access to the tree of Elements.
1346    #[cfg(feature = "internal")]
1347    #[doc(hidden)]
1348    pub fn root_component(&self) -> Rc<i_slint_compiler::object_tree::Component> {
1349        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1350        self.inner.unerase(guard).original.clone()
1351    }
1352
1353    /// Return the `TypeLoader` used when parsing the code in the interpreter.
1354    ///
1355    /// WARNING: this is not part of the public API
1356    #[cfg(feature = "internal-highlight")]
1357    pub fn type_loader(&self) -> std::rc::Rc<i_slint_compiler::typeloader::TypeLoader> {
1358        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1359        self.inner.unerase(guard).type_loader.get().unwrap().clone()
1360    }
1361
1362    /// Return the `TypeLoader` used when parsing the code in the interpreter in
1363    /// a state before most passes were applied by the compiler.
1364    ///
1365    /// Each returned type loader is a deep copy of the entire state connected to it,
1366    /// so this is a fairly expensive function!
1367    ///
1368    /// WARNING: this is not part of the public API
1369    #[cfg(feature = "internal-highlight")]
1370    pub fn raw_type_loader(&self) -> Option<i_slint_compiler::typeloader::TypeLoader> {
1371        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1372        self.inner
1373            .unerase(guard)
1374            .raw_type_loader
1375            .get()
1376            .unwrap()
1377            .as_ref()
1378            .and_then(|tl| i_slint_compiler::typeloader::snapshot(tl))
1379    }
1380}
1381
1382/// Print the diagnostics to stderr
1383///
1384/// The diagnostics are printed in the same style as rustc errors
1385///
1386/// This function is available when the `display-diagnostics` is enabled.
1387#[cfg(feature = "display-diagnostics")]
1388pub fn print_diagnostics(diagnostics: &[Diagnostic]) {
1389    let mut build_diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
1390    for d in diagnostics {
1391        build_diagnostics.push_compiler_error(d.clone())
1392    }
1393    build_diagnostics.print();
1394}
1395
1396/// This represents an instance of a dynamic component
1397///
1398/// You can create an instance with the [`ComponentDefinition::create`] function.
1399///
1400/// Properties and callback can be accessed using the associated functions.
1401///
1402/// An instance can be put on screen with the [`ComponentInstance::run`] function.
1403#[repr(C)]
1404pub struct ComponentInstance {
1405    pub(crate) inner: crate::dynamic_item_tree::DynamicComponentVRc,
1406}
1407
1408impl ComponentInstance {
1409    /// Return the [`ComponentDefinition`] that was used to create this instance.
1410    pub fn definition(&self) -> ComponentDefinition {
1411        generativity::make_guard!(guard);
1412        ComponentDefinition { inner: self.inner.unerase(guard).description().into() }
1413    }
1414
1415    fn is_system_tray_rooted(&self) -> bool {
1416        let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1417        self.inner.unerase(guard).description().original.inherits_system_tray_icon()
1418    }
1419
1420    /// Return the value for a public property of this component.
1421    ///
1422    /// ## Examples
1423    ///
1424    /// ```
1425    /// # i_slint_backend_testing::init_no_event_loop();
1426    /// use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString};
1427    /// let code = r#"
1428    ///     export component MyWin inherits Window {
1429    ///         in-out property <int> my_property: 42;
1430    ///     }
1431    /// "#;
1432    /// let mut compiler = Compiler::default();
1433    /// let result = spin_on::spin_on(
1434    ///     compiler.build_from_source(code.into(), Default::default()));
1435    /// assert_eq!(result.diagnostics().count(), 0, "{:?}", result.diagnostics().collect::<Vec<_>>());
1436    /// let instance = result.component("MyWin").unwrap().create().unwrap();
1437    /// assert_eq!(instance.get_property("my_property").unwrap(), Value::from(42));
1438    /// ```
1439    pub fn get_property(&self, name: &str) -> Result<Value, GetPropertyError> {
1440        generativity::make_guard!(guard);
1441        let comp = self.inner.unerase(guard);
1442        let name = normalize_identifier(name);
1443
1444        if comp
1445            .description()
1446            .original
1447            .root_element
1448            .borrow()
1449            .property_declarations
1450            .get(&name)
1451            .is_none_or(|d| !d.expose_in_public_api)
1452        {
1453            return Err(GetPropertyError::NoSuchProperty);
1454        }
1455
1456        comp.description()
1457            .get_property(comp.borrow(), &name)
1458            .map_err(|()| GetPropertyError::NoSuchProperty)
1459    }
1460
1461    /// Set the value for a public property of this component.
1462    pub fn set_property(&self, name: &str, value: Value) -> Result<(), SetPropertyError> {
1463        let name = normalize_identifier(name);
1464        generativity::make_guard!(guard);
1465        let comp = self.inner.unerase(guard);
1466        let d = comp.description();
1467        let elem = d.original.root_element.borrow();
1468        let decl = elem.property_declarations.get(&name).ok_or(SetPropertyError::NoSuchProperty)?;
1469
1470        if !decl.expose_in_public_api {
1471            return Err(SetPropertyError::NoSuchProperty);
1472        } else if decl.visibility == i_slint_compiler::object_tree::PropertyVisibility::Output {
1473            return Err(SetPropertyError::AccessDenied);
1474        }
1475
1476        d.set_property(comp.borrow(), &name, value)
1477    }
1478
1479    /// Set a handler for the callback with the given name. A callback with that
1480    /// name must be defined in the document otherwise an error will be returned.
1481    ///
1482    /// Note: Since the [`ComponentInstance`] holds the handler, the handler itself should not
1483    /// contain a strong reference to the instance. So if you need to capture the instance,
1484    /// you should use [`Self::as_weak`] to create a weak reference.
1485    ///
1486    /// ## Examples
1487    ///
1488    /// ```
1489    /// # i_slint_backend_testing::init_no_event_loop();
1490    /// use slint_interpreter::{Compiler, Value, SharedString, ComponentHandle};
1491    /// use core::convert::TryInto;
1492    /// let code = r#"
1493    ///     export component MyWin inherits Window {
1494    ///         callback foo(int) -> int;
1495    ///         in-out property <int> my_prop: 12;
1496    ///     }
1497    /// "#;
1498    /// let result = spin_on::spin_on(
1499    ///     Compiler::default().build_from_source(code.into(), Default::default()));
1500    /// assert_eq!(result.diagnostics().count(), 0, "{:?}", result.diagnostics().collect::<Vec<_>>());
1501    /// let instance = result.component("MyWin").unwrap().create().unwrap();
1502    /// let instance_weak = instance.as_weak();
1503    /// instance.set_callback("foo", move |args: &[Value]| -> Value {
1504    ///     let arg: u32 = args[0].clone().try_into().unwrap();
1505    ///     let my_prop = instance_weak.unwrap().get_property("my_prop").unwrap();
1506    ///     let my_prop : u32 = my_prop.try_into().unwrap();
1507    ///     Value::from(arg + my_prop)
1508    /// }).unwrap();
1509    ///
1510    /// let res = instance.invoke("foo", &[Value::from(500)]).unwrap();
1511    /// assert_eq!(res, Value::from(500+12));
1512    /// ```
1513    pub fn set_callback(
1514        &self,
1515        name: &str,
1516        callback: impl Fn(&[Value]) -> Value + 'static,
1517    ) -> Result<(), SetCallbackError> {
1518        generativity::make_guard!(guard);
1519        let comp = self.inner.unerase(guard);
1520        comp.description()
1521            .set_callback_handler(comp.borrow(), &normalize_identifier(name), Box::new(callback))
1522            .map_err(|()| SetCallbackError::NoSuchCallback)
1523    }
1524
1525    /// Call the given callback or function with the arguments
1526    ///
1527    /// ## Examples
1528    /// See the documentation of [`Self::set_callback`] for an example
1529    pub fn invoke(&self, name: &str, args: &[Value]) -> Result<Value, InvokeError> {
1530        generativity::make_guard!(guard);
1531        let comp = self.inner.unerase(guard);
1532        comp.description()
1533            .invoke(comp.borrow(), &normalize_identifier(name), args)
1534            .map_err(|()| InvokeError::NoSuchCallable)
1535    }
1536
1537    /// Return the value for a property within an exported global singleton used by this component.
1538    ///
1539    /// The `global` parameter is the exported name of the global singleton. The `property` argument
1540    /// is the name of the property
1541    ///
1542    /// ## Examples
1543    ///
1544    /// ```
1545    /// # i_slint_backend_testing::init_no_event_loop();
1546    /// use slint_interpreter::{Compiler, Value, SharedString};
1547    /// let code = r#"
1548    ///     global Glob {
1549    ///         in-out property <int> my_property: 42;
1550    ///     }
1551    ///     export { Glob as TheGlobal }
1552    ///     export component MyWin inherits Window {
1553    ///     }
1554    /// "#;
1555    /// let mut compiler = Compiler::default();
1556    /// let result = spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
1557    /// assert_eq!(result.diagnostics().count(), 0, "{:?}", result.diagnostics().collect::<Vec<_>>());
1558    /// let instance = result.component("MyWin").unwrap().create().unwrap();
1559    /// assert_eq!(instance.get_global_property("TheGlobal", "my_property").unwrap(), Value::from(42));
1560    /// ```
1561    pub fn get_global_property(
1562        &self,
1563        global: &str,
1564        property: &str,
1565    ) -> Result<Value, GetPropertyError> {
1566        generativity::make_guard!(guard);
1567        let comp = self.inner.unerase(guard);
1568        comp.description()
1569            .get_global(comp.borrow(), &normalize_identifier(global))
1570            .map_err(|()| GetPropertyError::NoSuchProperty)? // FIXME: should there be a NoSuchGlobal error?
1571            .as_ref()
1572            .get_property(&normalize_identifier(property))
1573            .map_err(|()| GetPropertyError::NoSuchProperty)
1574    }
1575
1576    /// Set the value for a property within an exported global singleton used by this component.
1577    pub fn set_global_property(
1578        &self,
1579        global: &str,
1580        property: &str,
1581        value: Value,
1582    ) -> Result<(), SetPropertyError> {
1583        generativity::make_guard!(guard);
1584        let comp = self.inner.unerase(guard);
1585        comp.description()
1586            .get_global(comp.borrow(), &normalize_identifier(global))
1587            .map_err(|()| SetPropertyError::NoSuchProperty)? // FIXME: should there be a NoSuchGlobal error?
1588            .as_ref()
1589            .set_property(&normalize_identifier(property), value)
1590    }
1591
1592    /// Set a handler for the callback in the exported global singleton. A callback with that
1593    /// name must be defined in the specified global and the global must be exported from the
1594    /// main document otherwise an error will be returned.
1595    ///
1596    /// ## Examples
1597    ///
1598    /// ```
1599    /// # i_slint_backend_testing::init_no_event_loop();
1600    /// use slint_interpreter::{Compiler, Value, SharedString};
1601    /// use core::convert::TryInto;
1602    /// let code = r#"
1603    ///     export global Logic {
1604    ///         pure callback to_uppercase(string) -> string;
1605    ///     }
1606    ///     export component MyWin inherits Window {
1607    ///         out property <string> hello: Logic.to_uppercase("world");
1608    ///     }
1609    /// "#;
1610    /// let result = spin_on::spin_on(
1611    ///     Compiler::default().build_from_source(code.into(), Default::default()));
1612    /// let instance = result.component("MyWin").unwrap().create().unwrap();
1613    /// instance.set_global_callback("Logic", "to_uppercase", |args: &[Value]| -> Value {
1614    ///     let arg: SharedString = args[0].clone().try_into().unwrap();
1615    ///     Value::from(SharedString::from(arg.to_uppercase()))
1616    /// }).unwrap();
1617    ///
1618    /// let res = instance.get_property("hello").unwrap();
1619    /// assert_eq!(res, Value::from(SharedString::from("WORLD")));
1620    ///
1621    /// let abc = instance.invoke_global("Logic", "to_uppercase", &[
1622    ///     SharedString::from("abc").into()
1623    /// ]).unwrap();
1624    /// assert_eq!(abc, Value::from(SharedString::from("ABC")));
1625    /// ```
1626    pub fn set_global_callback(
1627        &self,
1628        global: &str,
1629        name: &str,
1630        callback: impl Fn(&[Value]) -> Value + 'static,
1631    ) -> Result<(), SetCallbackError> {
1632        generativity::make_guard!(guard);
1633        let comp = self.inner.unerase(guard);
1634        comp.description()
1635            .get_global(comp.borrow(), &normalize_identifier(global))
1636            .map_err(|()| SetCallbackError::NoSuchCallback)? // FIXME: should there be a NoSuchGlobal error?
1637            .as_ref()
1638            .set_callback_handler(&normalize_identifier(name), Box::new(callback))
1639            .map_err(|()| SetCallbackError::NoSuchCallback)
1640    }
1641
1642    /// Call the given callback or function within a global singleton with the arguments
1643    ///
1644    /// ## Examples
1645    /// See the documentation of [`Self::set_global_callback`] for an example
1646    pub fn invoke_global(
1647        &self,
1648        global: &str,
1649        callable_name: &str,
1650        args: &[Value],
1651    ) -> Result<Value, InvokeError> {
1652        generativity::make_guard!(guard);
1653        let comp = self.inner.unerase(guard);
1654        let g = comp
1655            .description()
1656            .get_global(comp.borrow(), &normalize_identifier(global))
1657            .map_err(|()| InvokeError::NoSuchCallable)?; // FIXME: should there be a NoSuchGlobal error?
1658        let callable_name = normalize_identifier(callable_name);
1659        if matches!(
1660            comp.description()
1661                .original
1662                .root_element
1663                .borrow()
1664                .lookup_property(&callable_name)
1665                .property_type,
1666            LangType::Function { .. }
1667        ) {
1668            g.as_ref()
1669                .eval_function(&callable_name, args.to_vec())
1670                .map_err(|()| InvokeError::NoSuchCallable)
1671        } else {
1672            g.as_ref()
1673                .invoke_callback(&callable_name, args)
1674                .map_err(|()| InvokeError::NoSuchCallable)
1675        }
1676    }
1677
1678    /// Find all positions of the components which are pointed by a given source location.
1679    ///
1680    /// WARNING: this is not part of the public API
1681    #[cfg(feature = "internal-highlight")]
1682    pub fn component_positions(
1683        &self,
1684        path: &Path,
1685        offset: u32,
1686    ) -> Vec<crate::highlight::HighlightedRect> {
1687        crate::highlight::component_positions(&self.inner, path, offset)
1688    }
1689
1690    /// Find the position of the `element`.
1691    ///
1692    /// WARNING: this is not part of the public API
1693    #[cfg(feature = "internal-highlight")]
1694    pub fn element_positions(
1695        &self,
1696        element: &i_slint_compiler::object_tree::ElementRc,
1697    ) -> Vec<crate::highlight::HighlightedRect> {
1698        crate::highlight::element_positions(
1699            &self.inner,
1700            element,
1701            crate::highlight::ElementPositionFilter::IncludeClipped,
1702        )
1703    }
1704
1705    /// Find the `element` that was defined at the text position.
1706    ///
1707    /// WARNING: this is not part of the public API
1708    #[cfg(feature = "internal-highlight")]
1709    pub fn element_node_at_source_code_position(
1710        &self,
1711        path: &Path,
1712        offset: u32,
1713    ) -> Vec<(i_slint_compiler::object_tree::ElementRc, usize)> {
1714        crate::highlight::element_node_at_source_code_position(&self.inner, path, offset)
1715    }
1716}
1717
1718impl StrongHandle for ComponentInstance {
1719    type WeakInner = vtable::VWeak<ItemTreeVTable, crate::dynamic_item_tree::ErasedItemTreeBox>;
1720
1721    fn upgrade_from_weak_inner(inner: &Self::WeakInner) -> Option<Self> {
1722        Some(Self { inner: inner.upgrade()? })
1723    }
1724}
1725
1726impl ComponentHandle for ComponentInstance {
1727    fn as_weak(&self) -> Weak<Self>
1728    where
1729        Self: Sized,
1730    {
1731        Weak::new(vtable::VRc::downgrade(&self.inner))
1732    }
1733
1734    fn clone_strong(&self) -> Self {
1735        Self { inner: self.inner.clone() }
1736    }
1737
1738    fn show(&self) -> Result<(), PlatformError> {
1739        if self.is_system_tray_rooted() {
1740            // Mirror what the Rust/C++ generators emit for tray-rooted public
1741            // components: toggle the `visible` property; the change-tracker on
1742            // the SystemTrayIcon native item dispatches to the platform handle.
1743            self.set_property("visible", Value::Bool(true)).expect(
1744                "setting `visible` on a SystemTrayIcon-rooted component should always succeed",
1745            );
1746            return Ok(());
1747        }
1748        self.inner.window_adapter_ref()?.window().show()
1749    }
1750
1751    fn hide(&self) -> Result<(), PlatformError> {
1752        if self.is_system_tray_rooted() {
1753            self.set_property("visible", Value::Bool(false)).expect(
1754                "setting `visible` on a SystemTrayIcon-rooted component should always succeed",
1755            );
1756            return Ok(());
1757        }
1758        self.inner.window_adapter_ref()?.window().hide()
1759    }
1760
1761    fn run(&self) -> Result<(), PlatformError> {
1762        self.show()?;
1763        run_event_loop()?;
1764        self.hide()
1765    }
1766
1767    fn window(&self) -> &Window {
1768        self.inner.window_adapter_ref().unwrap().window()
1769    }
1770
1771    fn global<'a, T: Global<'a, Self>>(&'a self) -> T
1772    where
1773        Self: Sized,
1774    {
1775        unreachable!()
1776    }
1777}
1778
1779impl From<ComponentInstance>
1780    for vtable::VRc<i_slint_core::item_tree::ItemTreeVTable, ErasedItemTreeBox>
1781{
1782    fn from(value: ComponentInstance) -> Self {
1783        value.inner
1784    }
1785}
1786
1787/// Error returned by [`ComponentInstance::get_property`]
1788#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1789#[non_exhaustive]
1790pub enum GetPropertyError {
1791    /// There is no property with the given name
1792    #[display("no such property")]
1793    NoSuchProperty,
1794}
1795
1796/// Error returned by [`ComponentInstance::set_property`]
1797#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1798#[non_exhaustive]
1799pub enum SetPropertyError {
1800    /// There is no property with the given name.
1801    #[display("no such property")]
1802    NoSuchProperty,
1803    /// The property exists but does not have a type matching the dynamic value.
1804    ///
1805    /// This happens for example when assigning a source struct value to a target
1806    /// struct property, where the source doesn't have all the fields the target struct
1807    /// requires.
1808    #[display("wrong type")]
1809    WrongType,
1810    /// Attempt to set an output property.
1811    #[display("access denied")]
1812    AccessDenied,
1813}
1814
1815/// Error returned by [`ComponentInstance::set_callback`]
1816#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1817#[non_exhaustive]
1818pub enum SetCallbackError {
1819    /// There is no callback with the given name
1820    #[display("no such callback")]
1821    NoSuchCallback,
1822}
1823
1824/// Error returned by [`ComponentInstance::invoke`]
1825#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1826#[non_exhaustive]
1827pub enum InvokeError {
1828    /// There is no callback or function with the given name
1829    #[display("no such callback or function")]
1830    NoSuchCallable,
1831}
1832
1833/// Enters the main event loop. This is necessary in order to receive
1834/// events from the windowing system in order to render to the screen
1835/// and react to user input.
1836pub fn run_event_loop() -> Result<(), PlatformError> {
1837    i_slint_backend_selector::with_platform(|b| b.run_event_loop())
1838}
1839
1840/// Spawns a [`Future`] to execute in the Slint event loop.
1841///
1842/// See the documentation of `slint::spawn_local()` for more info
1843pub fn spawn_local<F: Future + 'static>(fut: F) -> Result<JoinHandle<F::Output>, EventLoopError> {
1844    i_slint_backend_selector::with_global_context(|ctx| ctx.spawn_local(fut))
1845        .map_err(|_| EventLoopError::NoEventLoopProvider)?
1846}
1847
1848#[test]
1849fn component_definition_properties() {
1850    i_slint_backend_testing::init_no_event_loop();
1851    let mut compiler = Compiler::default();
1852    compiler.set_style("fluent".into());
1853    let comp_def = spin_on::spin_on(
1854        compiler.build_from_source(
1855            r#"
1856    export component Dummy {
1857        in-out property <string> test;
1858        in-out property <int> underscores-and-dashes_preserved: 44;
1859        callback hello;
1860    }"#
1861            .into(),
1862            "".into(),
1863        ),
1864    )
1865    .component("Dummy")
1866    .unwrap();
1867
1868    let props = comp_def.properties().collect::<Vec<(_, _)>>();
1869
1870    assert_eq!(props.len(), 2);
1871    assert_eq!(props[0].0, "test");
1872    assert_eq!(props[0].1, ValueType::String);
1873    assert_eq!(props[1].0, "underscores-and-dashes_preserved");
1874    assert_eq!(props[1].1, ValueType::Number);
1875
1876    let instance = comp_def.create().unwrap();
1877    assert_eq!(instance.get_property("underscores_and-dashes-preserved"), Ok(Value::Number(44.)));
1878    assert_eq!(
1879        instance.get_property("underscoresanddashespreserved"),
1880        Err(GetPropertyError::NoSuchProperty)
1881    );
1882    assert_eq!(
1883        instance.set_property("underscores-and_dashes-preserved", Value::Number(88.)),
1884        Ok(())
1885    );
1886    assert_eq!(
1887        instance.set_property("underscoresanddashespreserved", Value::Number(99.)),
1888        Err(SetPropertyError::NoSuchProperty)
1889    );
1890    assert_eq!(
1891        instance.set_property("underscores-and_dashes-preserved", Value::String("99".into())),
1892        Err(SetPropertyError::WrongType)
1893    );
1894    assert_eq!(instance.get_property("underscores-and-dashes-preserved"), Ok(Value::Number(88.)));
1895}
1896
1897#[test]
1898fn component_definition_properties2() {
1899    i_slint_backend_testing::init_no_event_loop();
1900    let mut compiler = Compiler::default();
1901    compiler.set_style("fluent".into());
1902    let comp_def = spin_on::spin_on(
1903        compiler.build_from_source(
1904            r#"
1905    export component Dummy {
1906        in-out property <string> sub-text <=> sub.text;
1907        sub := Text { property <int> private-not-exported; }
1908        out property <string> xreadonly: "the value";
1909        private property <string> xx: sub.text;
1910        callback hello;
1911    }"#
1912            .into(),
1913            "".into(),
1914        ),
1915    )
1916    .component("Dummy")
1917    .unwrap();
1918
1919    let props = comp_def.properties().collect::<Vec<(_, _)>>();
1920
1921    assert_eq!(props.len(), 2);
1922    assert_eq!(props[0].0, "sub-text");
1923    assert_eq!(props[0].1, ValueType::String);
1924    assert_eq!(props[1].0, "xreadonly");
1925
1926    let callbacks = comp_def.callbacks().collect::<Vec<_>>();
1927    assert_eq!(callbacks.len(), 1);
1928    assert_eq!(callbacks[0], "hello");
1929
1930    let instance = comp_def.create().unwrap();
1931    assert_eq!(
1932        instance.set_property("xreadonly", SharedString::from("XXX").into()),
1933        Err(SetPropertyError::AccessDenied)
1934    );
1935    assert_eq!(instance.get_property("xreadonly"), Ok(Value::String("the value".into())));
1936    assert_eq!(
1937        instance.set_property("xx", SharedString::from("XXX").into()),
1938        Err(SetPropertyError::NoSuchProperty)
1939    );
1940    assert_eq!(
1941        instance.set_property("background", Value::default()),
1942        Err(SetPropertyError::NoSuchProperty)
1943    );
1944
1945    assert_eq!(instance.get_property("background"), Err(GetPropertyError::NoSuchProperty));
1946    assert_eq!(instance.get_property("xx"), Err(GetPropertyError::NoSuchProperty));
1947}
1948
1949#[test]
1950fn globals() {
1951    i_slint_backend_testing::init_no_event_loop();
1952    let mut compiler = Compiler::default();
1953    compiler.set_style("fluent".into());
1954    let definition = spin_on::spin_on(
1955        compiler.build_from_source(
1956            r#"
1957    export global My-Super_Global {
1958        in-out property <int> the-property : 21;
1959        callback my-callback();
1960    }
1961    export { My-Super_Global as AliasedGlobal }
1962    export component Dummy {
1963        callback alias <=> My-Super_Global.my-callback;
1964    }"#
1965            .into(),
1966            "".into(),
1967        ),
1968    )
1969    .component("Dummy")
1970    .unwrap();
1971
1972    assert_eq!(definition.globals().collect::<Vec<_>>(), vec!["My-Super_Global", "AliasedGlobal"]);
1973
1974    assert!(definition.global_properties("not-there").is_none());
1975    {
1976        let expected_properties = vec![("the-property".to_string(), ValueType::Number)];
1977        let expected_callbacks = vec!["my-callback".to_string()];
1978
1979        let assert_properties_and_callbacks = |global_name| {
1980            assert_eq!(
1981                definition
1982                    .global_properties(global_name)
1983                    .map(|props| props.collect::<Vec<_>>())
1984                    .as_ref(),
1985                Some(&expected_properties)
1986            );
1987            assert_eq!(
1988                definition
1989                    .global_callbacks(global_name)
1990                    .map(|props| props.collect::<Vec<_>>())
1991                    .as_ref(),
1992                Some(&expected_callbacks)
1993            );
1994        };
1995
1996        assert_properties_and_callbacks("My-Super-Global");
1997        assert_properties_and_callbacks("My_Super-Global");
1998        assert_properties_and_callbacks("AliasedGlobal");
1999    }
2000
2001    let instance = definition.create().unwrap();
2002    assert_eq!(
2003        instance.set_global_property("My_Super-Global", "the_property", Value::Number(44.)),
2004        Ok(())
2005    );
2006    assert_eq!(
2007        instance.set_global_property("AliasedGlobal", "the_property", Value::Number(44.)),
2008        Ok(())
2009    );
2010    assert_eq!(
2011        instance.set_global_property("DontExist", "the-property", Value::Number(88.)),
2012        Err(SetPropertyError::NoSuchProperty)
2013    );
2014
2015    assert_eq!(
2016        instance.set_global_property("My_Super-Global", "theproperty", Value::Number(88.)),
2017        Err(SetPropertyError::NoSuchProperty)
2018    );
2019    assert_eq!(
2020        instance.set_global_property("AliasedGlobal", "theproperty", Value::Number(88.)),
2021        Err(SetPropertyError::NoSuchProperty)
2022    );
2023    assert_eq!(
2024        instance.set_global_property("My_Super-Global", "the_property", Value::String("88".into())),
2025        Err(SetPropertyError::WrongType)
2026    );
2027    assert_eq!(
2028        instance.get_global_property("My-Super_Global", "yoyo"),
2029        Err(GetPropertyError::NoSuchProperty)
2030    );
2031    assert_eq!(
2032        instance.get_global_property("My-Super_Global", "the-property"),
2033        Ok(Value::Number(44.))
2034    );
2035
2036    assert_eq!(
2037        instance.set_property("the-property", Value::Void),
2038        Err(SetPropertyError::NoSuchProperty)
2039    );
2040    assert_eq!(instance.get_property("the-property"), Err(GetPropertyError::NoSuchProperty));
2041
2042    assert_eq!(
2043        instance.set_global_callback("DontExist", "the-property", |_| panic!()),
2044        Err(SetCallbackError::NoSuchCallback)
2045    );
2046    assert_eq!(
2047        instance.set_global_callback("My_Super_Global", "the-property", |_| panic!()),
2048        Err(SetCallbackError::NoSuchCallback)
2049    );
2050    assert_eq!(
2051        instance.set_global_callback("My_Super_Global", "yoyo", |_| panic!()),
2052        Err(SetCallbackError::NoSuchCallback)
2053    );
2054
2055    assert_eq!(
2056        instance.invoke_global("DontExist", "the-property", &[]),
2057        Err(InvokeError::NoSuchCallable)
2058    );
2059    assert_eq!(
2060        instance.invoke_global("My_Super_Global", "the-property", &[]),
2061        Err(InvokeError::NoSuchCallable)
2062    );
2063    assert_eq!(
2064        instance.invoke_global("My_Super_Global", "yoyo", &[]),
2065        Err(InvokeError::NoSuchCallable)
2066    );
2067
2068    // Alias to global don't crash (#8238)
2069    assert_eq!(instance.get_property("alias"), Err(GetPropertyError::NoSuchProperty));
2070}
2071
2072#[test]
2073fn call_functions() {
2074    i_slint_backend_testing::init_no_event_loop();
2075    let mut compiler = Compiler::default();
2076    compiler.set_style("fluent".into());
2077    let definition = spin_on::spin_on(
2078        compiler.build_from_source(
2079            r#"
2080    export global Gl {
2081        out property<string> q;
2082        public function foo-bar(a-a: string, b-b:int) -> string {
2083            q = a-a;
2084            return a-a + b-b;
2085        }
2086    }
2087    export component Test {
2088        out property<int> p;
2089        public function foo-bar(a: int, b:int) -> int {
2090            p = a;
2091            return a + b;
2092        }
2093    }"#
2094            .into(),
2095            "".into(),
2096        ),
2097    )
2098    .component("Test")
2099    .unwrap();
2100
2101    assert_eq!(definition.functions().collect::<Vec<_>>(), ["foo-bar"]);
2102    assert_eq!(definition.global_functions("Gl").unwrap().collect::<Vec<_>>(), ["foo-bar"]);
2103
2104    let instance = definition.create().unwrap();
2105
2106    assert_eq!(
2107        instance.invoke("foo_bar", &[Value::Number(3.), Value::Number(4.)]),
2108        Ok(Value::Number(7.))
2109    );
2110    assert_eq!(instance.invoke("p", &[]), Err(InvokeError::NoSuchCallable));
2111    assert_eq!(instance.get_property("p"), Ok(Value::Number(3.)));
2112
2113    assert_eq!(
2114        instance.invoke_global(
2115            "Gl",
2116            "foo_bar",
2117            &[Value::String("Hello".into()), Value::Number(10.)]
2118        ),
2119        Ok(Value::String("Hello10".into()))
2120    );
2121    assert_eq!(instance.get_global_property("Gl", "q"), Ok(Value::String("Hello".into())));
2122}
2123
2124#[test]
2125fn component_definition_struct_properties() {
2126    i_slint_backend_testing::init_no_event_loop();
2127    let mut compiler = Compiler::default();
2128    compiler.set_style("fluent".into());
2129    let comp_def = spin_on::spin_on(
2130        compiler.build_from_source(
2131            r#"
2132    export struct Settings {
2133        string_value: string,
2134    }
2135    export component Dummy {
2136        in-out property <Settings> test;
2137    }"#
2138            .into(),
2139            "".into(),
2140        ),
2141    )
2142    .component("Dummy")
2143    .unwrap();
2144
2145    let props = comp_def.properties().collect::<Vec<(_, _)>>();
2146
2147    assert_eq!(props.len(), 1);
2148    assert_eq!(props[0].0, "test");
2149    assert_eq!(props[0].1, ValueType::Struct);
2150
2151    let instance = comp_def.create().unwrap();
2152
2153    let valid_struct: Struct =
2154        [("string_value".to_string(), Value::String("hello".into()))].iter().cloned().collect();
2155
2156    assert_eq!(instance.set_property("test", Value::Struct(valid_struct.clone())), Ok(()));
2157    assert_eq!(instance.get_property("test").unwrap().value_type(), ValueType::Struct);
2158
2159    assert_eq!(instance.set_property("test", Value::Number(42.)), Err(SetPropertyError::WrongType));
2160
2161    let mut invalid_struct = valid_struct.clone();
2162    invalid_struct.set_field("other".into(), Value::Number(44.));
2163    assert_eq!(
2164        instance.set_property("test", Value::Struct(invalid_struct)),
2165        Err(SetPropertyError::WrongType)
2166    );
2167    let mut invalid_struct = valid_struct;
2168    invalid_struct.set_field("string_value".into(), Value::Number(44.));
2169    assert_eq!(
2170        instance.set_property("test", Value::Struct(invalid_struct)),
2171        Err(SetPropertyError::WrongType)
2172    );
2173}
2174
2175#[test]
2176fn component_definition_model_properties() {
2177    use i_slint_core::model::*;
2178    i_slint_backend_testing::init_no_event_loop();
2179    let mut compiler = Compiler::default();
2180    compiler.set_style("fluent".into());
2181    let comp_def = spin_on::spin_on(compiler.build_from_source(
2182        "export component Dummy { in-out property <[int]> prop: [42, 12]; }".into(),
2183        "".into(),
2184    ))
2185    .component("Dummy")
2186    .unwrap();
2187
2188    let props = comp_def.properties().collect::<Vec<(_, _)>>();
2189    assert_eq!(props.len(), 1);
2190    assert_eq!(props[0].0, "prop");
2191    assert_eq!(props[0].1, ValueType::Model);
2192
2193    let instance = comp_def.create().unwrap();
2194
2195    let int_model =
2196        Value::Model([Value::Number(14.), Value::Number(15.), Value::Number(16.)].into());
2197    let empty_model = Value::Model(ModelRc::new(VecModel::<Value>::default()));
2198    let model_with_string = Value::Model(VecModel::from_slice(&[
2199        Value::Number(1000.),
2200        Value::String("foo".into()),
2201        Value::Number(1111.),
2202    ]));
2203
2204    #[track_caller]
2205    fn check_model(val: Value, r: &[f64]) {
2206        if let Value::Model(m) = val {
2207            assert_eq!(r.len(), m.row_count());
2208            for (i, v) in r.iter().enumerate() {
2209                assert_eq!(m.row_data(i).unwrap(), Value::Number(*v));
2210            }
2211        } else {
2212            panic!("{val:?} not a model");
2213        }
2214    }
2215
2216    assert_eq!(instance.get_property("prop").unwrap().value_type(), ValueType::Model);
2217    check_model(instance.get_property("prop").unwrap(), &[42., 12.]);
2218
2219    instance.set_property("prop", int_model).unwrap();
2220    check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2221
2222    assert_eq!(instance.set_property("prop", Value::Number(42.)), Err(SetPropertyError::WrongType));
2223    check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2224    assert_eq!(instance.set_property("prop", model_with_string), Err(SetPropertyError::WrongType));
2225    check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2226
2227    assert_eq!(instance.set_property("prop", empty_model), Ok(()));
2228    check_model(instance.get_property("prop").unwrap(), &[]);
2229}
2230
2231#[test]
2232fn lang_type_to_value_type() {
2233    use i_slint_compiler::langtype::Struct as LangStruct;
2234    use std::collections::BTreeMap;
2235
2236    assert_eq!(ValueType::from(LangType::Void), ValueType::Void);
2237    assert_eq!(ValueType::from(LangType::Float32), ValueType::Number);
2238    assert_eq!(ValueType::from(LangType::Int32), ValueType::Number);
2239    assert_eq!(ValueType::from(LangType::Duration), ValueType::Number);
2240    assert_eq!(ValueType::from(LangType::Angle), ValueType::Number);
2241    assert_eq!(ValueType::from(LangType::PhysicalLength), ValueType::Number);
2242    assert_eq!(ValueType::from(LangType::LogicalLength), ValueType::Number);
2243    assert_eq!(ValueType::from(LangType::Percent), ValueType::Number);
2244    assert_eq!(ValueType::from(LangType::UnitProduct(Vec::new())), ValueType::Number);
2245    assert_eq!(ValueType::from(LangType::String), ValueType::String);
2246    assert_eq!(ValueType::from(LangType::Color), ValueType::Brush);
2247    assert_eq!(ValueType::from(LangType::Brush), ValueType::Brush);
2248    assert_eq!(ValueType::from(LangType::Array(Rc::new(LangType::Void))), ValueType::Model);
2249    assert_eq!(ValueType::from(LangType::Bool), ValueType::Bool);
2250    assert_eq!(
2251        ValueType::from(LangType::Struct(Rc::new(LangStruct {
2252            fields: BTreeMap::default(),
2253            name: i_slint_compiler::langtype::StructName::None,
2254        }))),
2255        ValueType::Struct
2256    );
2257    assert_eq!(ValueType::from(LangType::Image), ValueType::Image);
2258}
2259
2260#[test]
2261fn test_multi_components() {
2262    i_slint_backend_testing::init_no_event_loop();
2263    let result = spin_on::spin_on(
2264        Compiler::default().build_from_source(
2265            r#"
2266        export struct Settings {
2267            string_value: string,
2268        }
2269        export global ExpGlo { in-out property <int> test: 42; }
2270        component Common {
2271            in-out property <Settings> settings: { string_value: "Hello", };
2272        }
2273        export component Xyz inherits Window {
2274            in-out property <int> aaa: 8;
2275        }
2276        export component Foo {
2277
2278            in-out property <int> test: 42;
2279            c := Common {}
2280        }
2281        export component Bar inherits Window {
2282            in-out property <int> blah: 78;
2283            c := Common {}
2284        }
2285        "#
2286            .into(),
2287            PathBuf::from("hello.slint"),
2288        ),
2289    );
2290
2291    assert!(!result.has_errors(), "Error {:?}", result.diagnostics().collect::<Vec<_>>());
2292    let mut components = result.component_names().collect::<Vec<_>>();
2293    components.sort();
2294    assert_eq!(components, vec!["Bar", "Xyz"]);
2295    let diag = result.diagnostics().collect::<Vec<_>>();
2296    assert_eq!(diag.len(), 1);
2297    assert_eq!(diag[0].level(), DiagnosticLevel::Warning);
2298    assert_eq!(
2299        diag[0].message(),
2300        "Exported component 'Foo' doesn't inherit Window. No code will be generated for it"
2301    );
2302
2303    let comp1 = result.component("Xyz").unwrap();
2304    assert_eq!(comp1.name(), "Xyz");
2305    let instance1a = comp1.create().unwrap();
2306    let comp2 = result.component("Bar").unwrap();
2307    let instance2 = comp2.create().unwrap();
2308    let instance1b = comp1.create().unwrap();
2309
2310    // globals are not shared between instances
2311    assert_eq!(instance1a.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2312    assert_eq!(instance1a.set_global_property("ExpGlo", "test", Value::Number(88.0)), Ok(()));
2313    assert_eq!(instance2.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2314    assert_eq!(instance1b.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2315    assert_eq!(instance1a.get_global_property("ExpGlo", "test"), Ok(Value::Number(88.0)));
2316
2317    assert!(result.component("Settings").is_none());
2318    assert!(result.component("Foo").is_none());
2319    assert!(result.component("Common").is_none());
2320    assert!(result.component("ExpGlo").is_none());
2321    assert!(result.component("xyz").is_none());
2322}
2323
2324#[cfg(all(test, feature = "internal-highlight"))]
2325fn compile(code: &str) -> (ComponentInstance, PathBuf) {
2326    i_slint_backend_testing::init_no_event_loop();
2327    let mut compiler = Compiler::default();
2328    compiler.set_style("fluent".into());
2329    let path = PathBuf::from("/tmp/test.slint");
2330
2331    let compile_result =
2332        spin_on::spin_on(compiler.build_from_source(code.to_string(), path.clone()));
2333
2334    for d in &compile_result.diagnostics {
2335        eprintln!("{d}");
2336    }
2337
2338    assert!(!compile_result.has_errors());
2339
2340    let definition = compile_result.components().next().unwrap();
2341    let instance = definition.create().unwrap();
2342
2343    (instance, path)
2344}
2345
2346#[cfg(feature = "internal-highlight")]
2347#[test]
2348fn test_element_node_at_source_code_position() {
2349    let code = r#"
2350component Bar1 {}
2351
2352component Foo1 {
2353}
2354
2355export component Foo2 inherits Window  {
2356    Bar1 {}
2357    Foo1   {}
2358}"#;
2359
2360    let (handle, path) = compile(code);
2361
2362    for i in 0..code.len() as u32 {
2363        let elements = handle.element_node_at_source_code_position(&path, i);
2364        eprintln!("{i}: {}", code.as_bytes()[i as usize] as char);
2365        match i {
2366            16 => assert_eq!(elements.len(), 1),       // Bar1 (def)
2367            35 => assert_eq!(elements.len(), 1),       // Foo1 (def)
2368            71..=78 => assert_eq!(elements.len(), 1),  // Window + WS (from Foo2)
2369            85..=89 => assert_eq!(elements.len(), 1),  // Bar1 + WS (use)
2370            97..=103 => assert_eq!(elements.len(), 1), // Foo1 + WS (use)
2371            _ => assert!(elements.is_empty()),
2372        }
2373    }
2374}
2375
2376#[cfg(feature = "ffi")]
2377#[allow(missing_docs)]
2378#[path = "ffi.rs"]
2379pub(crate) mod ffi;