Skip to main content

slint_interpreter/
api.rs

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