1use 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
28pub use i_slint_compiler::DefaultTranslationContext;
31
32#[derive(Debug, Copy, Clone, PartialEq)]
35#[repr(i8)]
36#[non_exhaustive]
37pub enum ValueType {
38 Void,
40 Number,
42 String,
44 Bool,
46 Model,
48 Struct,
50 Brush,
52 Image,
54 #[doc(hidden)]
56 StyledText,
57 #[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#[derive(Clone, Default)]
99#[non_exhaustive]
100#[repr(u8)]
101pub enum Value {
102 #[default]
105 Void = 0,
106 Number(f64) = 1,
108 String(SharedString) = 2,
110 Bool(bool) = 3,
112 Image(Image) = 4,
114 Model(ModelRc<Value>) = 5,
116 Struct(Struct) = 6,
118 Brush(Brush) = 7,
120 #[doc(hidden)]
121 PathData(PathData) = 8,
123 #[doc(hidden)]
124 EasingCurve(i_slint_core::animations::EasingCurve) = 9,
126 #[doc(hidden)]
127 EnumerationValue(String, String) = 10,
130 #[doc(hidden)]
131 LayoutCache(SharedVector<f32>) = 11,
132 #[doc(hidden)]
133 ComponentFactory(ComponentFactory) = 12,
135 #[doc(hidden)] StyledText(StyledText) = 13,
138 #[doc(hidden)]
139 ArrayOfU16(SharedVector<u16>) = 14,
140}
141
142impl Value {
143 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
221macro_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
262macro_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
350macro_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 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 assert_eq!(value_model.row_data(0).unwrap(), Value::String("foo".into()));
618
619 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#[derive(Clone, PartialEq, Debug, Default)]
658pub struct Struct(pub(crate) HashMap<SmolStr, Value>);
659impl Struct {
660 pub fn get_field(&self, name: &str) -> Option<&Value> {
662 self.0.get(&*normalize_identifier(name))
663 }
664 pub fn set_field(&mut self, name: String, value: Value) {
666 self.0.insert(normalize_identifier(&name), value);
667 }
668
669 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#[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 pub fn new() -> Self {
703 Self::default()
704 }
705
706 pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
708 self.config.include_paths = include_paths;
709 }
710
711 pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
713 &self.config.include_paths
714 }
715
716 pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
718 self.config.library_paths = library_paths;
719 }
720
721 pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
723 &self.config.library_paths
724 }
725
726 pub fn set_style(&mut self, style: String) {
738 self.config.style = Some(style);
739 }
740
741 pub fn style(&self) -> Option<&String> {
743 self.config.style.as_ref()
744 }
745
746 pub fn set_translation_domain(&mut self, domain: String) {
748 self.config.translation_domain = Some(domain);
749 }
750
751 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 pub fn diagnostics(&self) -> &Vec<Diagnostic> {
772 &self.diagnostics
773 }
774
775 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 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
838pub 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 pub fn new() -> Self {
856 Self::default()
857 }
858
859 #[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 pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
873 self.config.include_paths = include_paths;
874 }
875
876 pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
878 &self.config.include_paths
879 }
880
881 pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
883 self.config.library_paths = library_paths;
884 }
885
886 pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
888 &self.config.library_paths
889 }
890
891 pub fn set_style(&mut self, style: String) {
902 self.config.style = Some(style);
903 }
904
905 pub fn style(&self) -> Option<&String> {
907 self.config.style.as_ref()
908 }
909
910 pub fn set_translation_domain(&mut self, domain: String) {
912 self.config.translation_domain = Some(domain);
913 }
914
915 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 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 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 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#[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 #[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 pub fn has_errors(&self) -> bool {
1032 self.diagnostics().any(|diag| diag.level() == DiagnosticLevel::Error)
1033 }
1034
1035 pub fn diagnostics(&self) -> impl Iterator<Item = Diagnostic> + '_ {
1039 self.diagnostics.iter().cloned()
1040 }
1041
1042 #[cfg(feature = "display-diagnostics")]
1048 pub fn print_diagnostics(&self) {
1049 print_diagnostics(&self.diagnostics)
1050 }
1051
1052 pub fn components(&self) -> impl Iterator<Item = ComponentDefinition> + '_ {
1054 self.components.values().cloned()
1055 }
1056
1057 pub fn component_names(&self) -> impl Iterator<Item = &str> + '_ {
1059 self.components.keys().map(|s| s.as_str())
1060 }
1061
1062 pub fn component(&self, name: &str) -> Option<ComponentDefinition> {
1065 self.components.get(name).cloned()
1066 }
1067
1068 #[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 #[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#[derive(Clone)]
1098pub struct ComponentDefinition {
1099 pub(crate) inner: crate::dynamic_item_tree::ErasedItemTreeDescription,
1100}
1101
1102impl ComponentDefinition {
1103 #[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 pub fn create(&self) -> Result<ComponentInstance, PlatformError> {
1118 let instance = self.create_with_options(Default::default())?;
1119 instance.inner.window_adapter_ref()?;
1121 Ok(instance)
1122 }
1123
1124 #[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 #[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 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 #[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 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 pub fn properties(&self) -> impl Iterator<Item = (String, ValueType)> + '_ {
1177 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 pub fn callbacks(&self) -> impl Iterator<Item = String> + '_ {
1191 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 pub fn functions(&self) -> impl Iterator<Item = String> + '_ {
1205 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 pub fn globals(&self) -> impl Iterator<Item = String> + '_ {
1222 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1225 self.inner.unerase(guard).global_names().map(|s| s.to_string())
1226 }
1227
1228 #[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 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 pub fn global_properties(
1258 &self,
1259 global_name: &str,
1260 ) -> Option<impl Iterator<Item = (String, ValueType)> + '_> {
1261 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 pub fn global_callbacks(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1277 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 pub fn global_functions(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1293 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 pub fn name(&self) -> &str {
1309 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1312 self.inner.unerase(guard).id()
1313 }
1314
1315 #[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 #[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 #[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#[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#[repr(C)]
1374pub struct ComponentInstance {
1375 pub(crate) inner: crate::dynamic_item_tree::DynamicComponentVRc,
1376}
1377
1378impl ComponentInstance {
1379 pub fn definition(&self) -> ComponentDefinition {
1381 generativity::make_guard!(guard);
1382 ComponentDefinition { inner: self.inner.unerase(guard).description().into() }
1383 }
1384
1385 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 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 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 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 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)? .as_ref()
1537 .get_property(&normalize_identifier(property))
1538 .map_err(|()| GetPropertyError::NoSuchProperty)
1539 }
1540
1541 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)? .as_ref()
1554 .set_property(&normalize_identifier(property), value)
1555 }
1556
1557 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)? .as_ref()
1603 .set_callback_handler(&normalize_identifier(name), Box::new(callback))
1604 .map_err(|()| SetCallbackError::NoSuchCallback)
1605 }
1606
1607 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)?; 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 #[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 #[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 #[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#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1737#[non_exhaustive]
1738pub enum GetPropertyError {
1739 #[display("no such property")]
1741 NoSuchProperty,
1742}
1743
1744#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1746#[non_exhaustive]
1747pub enum SetPropertyError {
1748 #[display("no such property")]
1750 NoSuchProperty,
1751 #[display("wrong type")]
1757 WrongType,
1758 #[display("access denied")]
1760 AccessDenied,
1761}
1762
1763#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1765#[non_exhaustive]
1766pub enum SetCallbackError {
1767 #[display("no such callback")]
1769 NoSuchCallback,
1770}
1771
1772#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1774#[non_exhaustive]
1775pub enum InvokeError {
1776 #[display("no such callback or function")]
1778 NoSuchCallable,
1779}
1780
1781pub fn run_event_loop() -> Result<(), PlatformError> {
1785 i_slint_backend_selector::with_platform(|b| b.run_event_loop())
1786}
1787
1788pub 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#[doc(hidden)]
1798pub mod testing {
1799 use super::ComponentHandle;
1800 use i_slint_core::window::WindowInner;
1801
1802 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 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 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 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 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), 35 => assert_eq!(elements.len(), 1), 71..=78 => assert_eq!(elements.len(), 1), 85..=89 => assert_eq!(elements.len(), 1), 97..=103 => assert_eq!(elements.len(), 1), _ => assert!(elements.is_empty()),
2359 }
2360 }
2361}
2362
2363#[cfg(feature = "ffi")]
2364#[allow(missing_docs)]
2365#[path = "ffi.rs"]
2366pub(crate) mod ffi;