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