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