1use crate::dynamic_item_tree::{ErasedItemTreeBox, WindowOptions};
6use i_slint_compiler::langtype::Type as LangType;
7use i_slint_core::PathData;
8use i_slint_core::component_factory::ComponentFactory;
9#[cfg(feature = "internal")]
10use i_slint_core::component_factory::FactoryContext;
11use i_slint_core::graphics::euclid::approxeq::ApproxEq as _;
12use i_slint_core::items::*;
13use i_slint_core::model::{Model, ModelExt, ModelRc};
14use i_slint_core::styled_text::StyledText;
15#[cfg(feature = "internal")]
16use i_slint_core::window::WindowInner;
17use smol_str::SmolStr;
18use std::collections::HashMap;
19use std::future::Future;
20use std::path::{Path, PathBuf};
21use std::rc::Rc;
22
23#[doc(inline)]
24pub use i_slint_compiler::diagnostics::{Diagnostic, DiagnosticLevel};
25
26pub use i_slint_backend_selector::api::*;
27pub use i_slint_core::api::*;
28
29pub use i_slint_compiler::DefaultTranslationContext;
32
33#[derive(Debug, Copy, Clone, PartialEq)]
36#[repr(i8)]
37#[non_exhaustive]
38pub enum ValueType {
39 Void,
41 Number,
43 String,
45 Bool,
47 Model,
49 Struct,
51 Brush,
53 Image,
55 #[doc(hidden)]
57 Other = -1,
58}
59
60impl From<LangType> for ValueType {
61 fn from(ty: LangType) -> Self {
62 match ty {
63 LangType::Float32
64 | LangType::Int32
65 | LangType::Duration
66 | LangType::Angle
67 | LangType::PhysicalLength
68 | LangType::LogicalLength
69 | LangType::Percent
70 | LangType::UnitProduct(_) => Self::Number,
71 LangType::String => Self::String,
72 LangType::Color => Self::Brush,
73 LangType::Brush => Self::Brush,
74 LangType::Array(_) => Self::Model,
75 LangType::Bool => Self::Bool,
76 LangType::Struct { .. } => Self::Struct,
77 LangType::Void => Self::Void,
78 LangType::Image => Self::Image,
79 _ => Self::Other,
80 }
81 }
82}
83
84#[derive(Clone, Default)]
96#[non_exhaustive]
97#[repr(u8)]
98pub enum Value {
99 #[default]
102 Void = 0,
103 Number(f64) = 1,
105 String(SharedString) = 2,
107 Bool(bool) = 3,
109 Image(Image) = 4,
111 Model(ModelRc<Value>) = 5,
113 Struct(Struct) = 6,
115 Brush(Brush) = 7,
117 #[doc(hidden)]
118 PathData(PathData) = 8,
120 #[doc(hidden)]
121 EasingCurve(i_slint_core::animations::EasingCurve) = 9,
123 #[doc(hidden)]
124 EnumerationValue(String, String) = 10,
127 #[doc(hidden)]
128 LayoutCache(SharedVector<f32>) = 11,
129 #[doc(hidden)]
130 ComponentFactory(ComponentFactory) = 12,
132 #[doc(hidden)] StyledText(StyledText) = 13,
135 #[doc(hidden)]
136 ArrayOfU16(SharedVector<u16>) = 14,
137 Keys(Keys) = 15,
139 DataTransfer(DataTransfer) = 16,
141}
142
143impl Value {
144 pub fn value_type(&self) -> ValueType {
146 match self {
147 Value::Void => ValueType::Void,
148 Value::Number(_) => ValueType::Number,
149 Value::String(_) => ValueType::String,
150 Value::Bool(_) => ValueType::Bool,
151 Value::Model(_) => ValueType::Model,
152 Value::Struct(_) => ValueType::Struct,
153 Value::Brush(_) => ValueType::Brush,
154 Value::Image(_) => ValueType::Image,
155 _ => ValueType::Other,
156 }
157 }
158}
159
160impl PartialEq for Value {
161 fn eq(&self, other: &Self) -> bool {
162 match self {
163 Value::Void => matches!(other, Value::Void),
164 Value::Number(lhs) => matches!(other, Value::Number(rhs) if lhs.approx_eq(rhs)),
165 Value::String(lhs) => matches!(other, Value::String(rhs) if lhs == rhs),
166 Value::Bool(lhs) => matches!(other, Value::Bool(rhs) if lhs == rhs),
167 Value::Image(lhs) => matches!(other, Value::Image(rhs) if lhs == rhs),
168 Value::Model(lhs) => {
169 if let Value::Model(rhs) = other {
170 lhs == rhs
171 } else {
172 false
173 }
174 }
175 Value::Struct(lhs) => matches!(other, Value::Struct(rhs) if lhs == rhs),
176 Value::Brush(lhs) => matches!(other, Value::Brush(rhs) if lhs == rhs),
177 Value::PathData(lhs) => matches!(other, Value::PathData(rhs) if lhs == rhs),
178 Value::EasingCurve(lhs) => matches!(other, Value::EasingCurve(rhs) if lhs == rhs),
179 Value::EnumerationValue(lhs_name, lhs_value) => {
180 matches!(other, Value::EnumerationValue(rhs_name, rhs_value) if lhs_name == rhs_name && lhs_value == rhs_value)
181 }
182 Value::LayoutCache(lhs) => matches!(other, Value::LayoutCache(rhs) if lhs == rhs),
183 Value::ArrayOfU16(lhs) => matches!(other, Value::ArrayOfU16(rhs) if lhs == rhs),
184 Value::ComponentFactory(lhs) => {
185 matches!(other, Value::ComponentFactory(rhs) if lhs == rhs)
186 }
187 Value::StyledText(lhs) => {
188 matches!(other, Value::StyledText(rhs) if lhs == rhs)
189 }
190 Value::Keys(lhs) => {
191 matches!(other, Value::Keys(rhs) if lhs == rhs)
192 }
193 Value::DataTransfer(lhs) => {
194 matches!(other, Value::DataTransfer(rhs) if lhs == rhs)
195 }
196 }
197 }
198}
199
200impl std::fmt::Debug for Value {
201 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
202 match self {
203 Value::Void => write!(f, "Value::Void"),
204 Value::Number(n) => write!(f, "Value::Number({n:?})"),
205 Value::String(s) => write!(f, "Value::String({s:?})"),
206 Value::Bool(b) => write!(f, "Value::Bool({b:?})"),
207 Value::Image(i) => write!(f, "Value::Image({i:?})"),
208 Value::Model(m) => {
209 write!(f, "Value::Model(")?;
210 f.debug_list().entries(m.iter()).finish()?;
211 write!(f, "])")
212 }
213 Value::Struct(s) => write!(f, "Value::Struct({s:?})"),
214 Value::Brush(b) => write!(f, "Value::Brush({b:?})"),
215 Value::PathData(e) => write!(f, "Value::PathElements({e:?})"),
216 Value::EasingCurve(c) => write!(f, "Value::EasingCurve({c:?})"),
217 Value::EnumerationValue(n, v) => write!(f, "Value::EnumerationValue({n:?}, {v:?})"),
218 Value::LayoutCache(v) => write!(f, "Value::LayoutCache({v:?})"),
219 Value::ComponentFactory(factory) => write!(f, "Value::ComponentFactory({factory:?})"),
220 Value::StyledText(text) => write!(f, "Value::StyledText({text:?})"),
221 Value::ArrayOfU16(data) => {
222 write!(f, "Value::ArrayOfU16({data:?})")
223 }
224 Value::Keys(ks) => write!(f, "Value::Keys({ks:?})"),
225 Value::DataTransfer(cd) => write!(f, "Value::DataTransfer({cd:?})"),
226 }
227 }
228}
229
230macro_rules! declare_value_conversion {
239 ( $value:ident => [$($ty:ty),*] ) => {
240 $(
241 impl From<$ty> for Value {
242 fn from(v: $ty) -> Self {
243 Value::$value(v as _)
244 }
245 }
246 impl TryFrom<Value> for $ty {
247 type Error = Value;
248 fn try_from(v: Value) -> Result<$ty, Self::Error> {
249 match v {
250 Value::$value(x) => Ok(x as _),
251 _ => Err(v)
252 }
253 }
254 }
255 )*
256 };
257}
258declare_value_conversion!(Number => [u32, u64, i32, i64, f32, f64, usize, isize] );
259declare_value_conversion!(String => [SharedString] );
260declare_value_conversion!(Bool => [bool] );
261declare_value_conversion!(Image => [Image] );
262declare_value_conversion!(Struct => [Struct] );
263declare_value_conversion!(Brush => [Brush] );
264declare_value_conversion!(PathData => [PathData]);
265declare_value_conversion!(EasingCurve => [i_slint_core::animations::EasingCurve]);
266declare_value_conversion!(LayoutCache => [SharedVector<f32>] );
267declare_value_conversion!(ComponentFactory => [ComponentFactory] );
268declare_value_conversion!(StyledText => [StyledText] );
269declare_value_conversion!(ArrayOfU16 => [SharedVector<u16>] );
270declare_value_conversion!(Keys => [Keys]);
271declare_value_conversion!(DataTransfer => [DataTransfer]);
272
273macro_rules! declare_value_struct_conversion {
275 (struct $name:path { $($field:ident),* $(, ..$extra:expr)? }) => {
276 impl From<$name> for Value {
277 fn from($name { $($field),* , .. }: $name) -> Self {
278 let mut struct_ = Struct::default();
279 $(struct_.set_field(stringify!($field).into(), $field.into());)*
280 Value::Struct(struct_)
281 }
282 }
283 impl TryFrom<Value> for $name {
284 type Error = ();
285 fn try_from(v: Value) -> Result<$name, Self::Error> {
286 #[allow(clippy::field_reassign_with_default)]
287 match v {
288 Value::Struct(x) => {
289 type Ty = $name;
290 #[allow(unused)]
291 let mut res: Ty = Ty::default();
292 $(let mut res: Ty = $extra;)?
293 $(res.$field = x.get_field(stringify!($field)).ok_or(())?.clone().try_into().map_err(|_|())?;)*
294 Ok(res)
295 }
296 _ => Err(()),
297 }
298 }
299 }
300 };
301 ($(
302 $(#[$struct_attr:meta])*
303 struct $Name:ident {
304 @name = $inner_name:expr,
305 export {
306 $( $(#[$pub_attr:meta])* $pub_field:ident : $pub_type:ty, )*
307 }
308 private { $($pri:tt)* }
309 }
310 )*) => {
311 $(
312 impl From<$Name> for Value {
313 fn from(item: $Name) -> Self {
314 let mut struct_ = Struct::default();
315 $(struct_.set_field(stringify!($pub_field).into(), item.$pub_field.into());)*
316 Value::Struct(struct_)
317 }
318 }
319 impl TryFrom<Value> for $Name {
320 type Error = ();
321 fn try_from(v: Value) -> Result<$Name, Self::Error> {
322 #[allow(clippy::field_reassign_with_default)]
323 match v {
324 Value::Struct(x) => {
325 type Ty = $Name;
326 #[allow(unused)]
327 let mut res: Ty = Ty::default();
328 $(res.$pub_field = x.get_field(stringify!($pub_field)).ok_or(())?.clone().try_into().map_err(|_|())?;)*
329 Ok(res)
330 }
331 _ => Err(()),
332 }
333 }
334 }
335 )*
336 };
337}
338
339declare_value_struct_conversion!(struct i_slint_core::layout::LayoutInfo { min, max, min_percent, max_percent, preferred, stretch });
340declare_value_struct_conversion!(struct i_slint_core::graphics::Point { x, y, ..Default::default()});
341declare_value_struct_conversion!(struct i_slint_core::api::LogicalPosition { x, y });
342declare_value_struct_conversion!(struct i_slint_core::properties::StateInfo { current_state, previous_state, change_time });
343
344i_slint_common::for_each_builtin_structs!(declare_value_struct_conversion);
345
346macro_rules! declare_value_enum_conversion {
351 ($( $(#[$enum_doc:meta])* enum $Name:ident { $($body:tt)* })*) => { $(
352 impl From<i_slint_core::items::$Name> for Value {
353 fn from(v: i_slint_core::items::$Name) -> Self {
354 Value::EnumerationValue(stringify!($Name).to_owned(), v.to_string())
355 }
356 }
357 impl TryFrom<Value> for i_slint_core::items::$Name {
358 type Error = ();
359 fn try_from(v: Value) -> Result<i_slint_core::items::$Name, ()> {
360 use std::str::FromStr;
361 match v {
362 Value::EnumerationValue(enumeration, value) => {
363 if enumeration != stringify!($Name) {
364 return Err(());
365 }
366 i_slint_core::items::$Name::from_str(value.as_str()).map_err(|_| ())
367 }
368 _ => Err(()),
369 }
370 }
371 }
372 )*};
373}
374
375i_slint_common::for_each_enums!(declare_value_enum_conversion);
376
377impl From<i_slint_core::animations::Instant> for Value {
378 fn from(value: i_slint_core::animations::Instant) -> Self {
379 Value::Number(value.0 as _)
380 }
381}
382impl TryFrom<Value> for i_slint_core::animations::Instant {
383 type Error = ();
384 fn try_from(v: Value) -> Result<i_slint_core::animations::Instant, Self::Error> {
385 match v {
386 Value::Number(x) => Ok(i_slint_core::animations::Instant(x as _)),
387 _ => Err(()),
388 }
389 }
390}
391
392impl From<()> for Value {
393 #[inline]
394 fn from(_: ()) -> Self {
395 Value::Void
396 }
397}
398impl TryFrom<Value> for () {
399 type Error = ();
400 #[inline]
401 fn try_from(_: Value) -> Result<(), Self::Error> {
402 Ok(())
403 }
404}
405
406impl From<Color> for Value {
407 #[inline]
408 fn from(c: Color) -> Self {
409 Value::Brush(Brush::SolidColor(c))
410 }
411}
412impl TryFrom<Value> for Color {
413 type Error = Value;
414 #[inline]
415 fn try_from(v: Value) -> Result<Color, Self::Error> {
416 match v {
417 Value::Brush(Brush::SolidColor(c)) => Ok(c),
418 _ => Err(v),
419 }
420 }
421}
422
423impl From<i_slint_core::lengths::LogicalLength> for Value {
424 #[inline]
425 fn from(l: i_slint_core::lengths::LogicalLength) -> Self {
426 Value::Number(l.get() as _)
427 }
428}
429impl TryFrom<Value> for i_slint_core::lengths::LogicalLength {
430 type Error = Value;
431 #[inline]
432 fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalLength, Self::Error> {
433 match v {
434 Value::Number(n) => Ok(i_slint_core::lengths::LogicalLength::new(n as _)),
435 _ => Err(v),
436 }
437 }
438}
439
440impl From<i_slint_core::lengths::LogicalPoint> for Value {
441 #[inline]
442 fn from(pt: i_slint_core::lengths::LogicalPoint) -> Self {
443 Value::Struct(Struct::from_iter([
444 ("x".to_owned(), Value::Number(pt.x as _)),
445 ("y".to_owned(), Value::Number(pt.y as _)),
446 ]))
447 }
448}
449impl TryFrom<Value> for i_slint_core::lengths::LogicalPoint {
450 type Error = Value;
451 #[inline]
452 fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalPoint, Self::Error> {
453 match v {
454 Value::Struct(s) => {
455 let x = s
456 .get_field("x")
457 .cloned()
458 .unwrap_or_else(|| Value::Number(0 as _))
459 .try_into()?;
460 let y = s
461 .get_field("y")
462 .cloned()
463 .unwrap_or_else(|| Value::Number(0 as _))
464 .try_into()?;
465 Ok(i_slint_core::lengths::LogicalPoint::new(x, y))
466 }
467 _ => Err(v),
468 }
469 }
470}
471
472impl From<i_slint_core::lengths::LogicalSize> for Value {
473 #[inline]
474 fn from(s: i_slint_core::lengths::LogicalSize) -> Self {
475 Value::Struct(Struct::from_iter([
476 ("width".to_owned(), Value::Number(s.width as _)),
477 ("height".to_owned(), Value::Number(s.height as _)),
478 ]))
479 }
480}
481impl TryFrom<Value> for i_slint_core::lengths::LogicalSize {
482 type Error = Value;
483 #[inline]
484 fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalSize, Self::Error> {
485 match v {
486 Value::Struct(s) => {
487 let width = s
488 .get_field("width")
489 .cloned()
490 .unwrap_or_else(|| Value::Number(0 as _))
491 .try_into()?;
492 let height = s
493 .get_field("height")
494 .cloned()
495 .unwrap_or_else(|| Value::Number(0 as _))
496 .try_into()?;
497 Ok(i_slint_core::lengths::LogicalSize::new(width, height))
498 }
499 _ => Err(v),
500 }
501 }
502}
503
504impl From<i_slint_core::lengths::LogicalEdges> for Value {
505 #[inline]
506 fn from(s: i_slint_core::lengths::LogicalEdges) -> Self {
507 Value::Struct(Struct::from_iter([
508 ("left".to_owned(), Value::Number(s.left as _)),
509 ("right".to_owned(), Value::Number(s.right as _)),
510 ("top".to_owned(), Value::Number(s.top as _)),
511 ("bottom".to_owned(), Value::Number(s.bottom as _)),
512 ]))
513 }
514}
515impl TryFrom<Value> for i_slint_core::lengths::LogicalEdges {
516 type Error = Value;
517 #[inline]
518 fn try_from(v: Value) -> Result<i_slint_core::lengths::LogicalEdges, Self::Error> {
519 match v {
520 Value::Struct(s) => {
521 let left = s
522 .get_field("left")
523 .cloned()
524 .unwrap_or_else(|| Value::Number(0 as _))
525 .try_into()?;
526 let right = s
527 .get_field("right")
528 .cloned()
529 .unwrap_or_else(|| Value::Number(0 as _))
530 .try_into()?;
531 let top = s
532 .get_field("top")
533 .cloned()
534 .unwrap_or_else(|| Value::Number(0 as _))
535 .try_into()?;
536 let bottom = s
537 .get_field("bottom")
538 .cloned()
539 .unwrap_or_else(|| Value::Number(0 as _))
540 .try_into()?;
541 Ok(i_slint_core::lengths::LogicalEdges::new(left, right, top, bottom))
542 }
543 _ => Err(v),
544 }
545 }
546}
547
548impl<T: Into<Value> + TryFrom<Value> + 'static> From<ModelRc<T>> for Value {
549 fn from(m: ModelRc<T>) -> Self {
550 if let Some(v) = <dyn core::any::Any>::downcast_ref::<ModelRc<Value>>(&m) {
551 Value::Model(v.clone())
552 } else {
553 Value::Model(ModelRc::new(crate::value_model::ValueMapModel(m)))
554 }
555 }
556}
557impl<T: TryFrom<Value> + Default + 'static> TryFrom<Value> for ModelRc<T> {
558 type Error = Value;
559 #[inline]
560 fn try_from(v: Value) -> Result<ModelRc<T>, Self::Error> {
561 match v {
562 Value::Model(m) => {
563 if let Some(v) = <dyn core::any::Any>::downcast_ref::<ModelRc<T>>(&m) {
564 Ok(v.clone())
565 } else if let Some(v) =
566 m.as_any().downcast_ref::<crate::value_model::ValueMapModel<T>>()
567 {
568 Ok(v.0.clone())
569 } else {
570 Ok(ModelRc::new(m.map(|v| T::try_from(v).unwrap_or_default())))
571 }
572 }
573 _ => Err(v),
574 }
575 }
576}
577
578#[test]
579fn value_model_conversion() {
580 use i_slint_core::model::*;
581 let m = ModelRc::new(VecModel::from_slice(&[Value::Number(42.), Value::Number(12.)]));
582 let v = Value::from(m.clone());
583 assert_eq!(v, Value::Model(m.clone()));
584 let m2: ModelRc<Value> = v.clone().try_into().unwrap();
585 assert_eq!(m2, m);
586
587 let int_model: ModelRc<i32> = v.clone().try_into().unwrap();
588 assert_eq!(int_model.row_count(), 2);
589 assert_eq!(int_model.iter().collect::<Vec<_>>(), vec![42, 12]);
590
591 let Value::Model(m3) = int_model.clone().into() else { panic!("not a model?") };
592 assert_eq!(m3.row_count(), 2);
593 assert_eq!(m3.iter().collect::<Vec<_>>(), vec![Value::Number(42.), Value::Number(12.)]);
594
595 let str_model: ModelRc<SharedString> = v.clone().try_into().unwrap();
596 assert_eq!(str_model.row_count(), 2);
597 assert_eq!(str_model.iter().collect::<Vec<_>>(), vec!["", ""]);
599
600 let err: Result<ModelRc<Value>, _> = Value::Bool(true).try_into();
601 assert!(err.is_err());
602
603 let model =
604 Rc::new(VecModel::<SharedString>::from_iter(["foo".into(), "bar".into(), "baz".into()]));
605
606 let value: Value = ModelRc::from(model.clone()).into();
607 let value_model: ModelRc<Value> = value.clone().try_into().unwrap();
608 assert_eq!(value_model.row_data(2).unwrap(), Value::String("baz".into()));
609 value_model.set_row_data(1, Value::String("qux".into()));
610 value_model.set_row_data(0, Value::Bool(true));
611 assert_eq!(value_model.row_data(1).unwrap(), Value::String("qux".into()));
612 assert_eq!(value_model.row_data(0).unwrap(), Value::String("foo".into()));
614
615 assert_eq!(model.row_data(1).unwrap(), SharedString::from("qux"));
617 assert_eq!(model.row_data(0).unwrap(), SharedString::from("foo"));
618
619 let the_model: ModelRc<SharedString> = value.try_into().unwrap();
620 assert_eq!(the_model.row_data(1).unwrap(), SharedString::from("qux"));
621 assert_eq!(
622 model.as_ref() as *const VecModel<SharedString>,
623 the_model.as_any().downcast_ref::<VecModel<SharedString>>().unwrap()
624 as *const VecModel<SharedString>
625 );
626}
627
628pub(crate) fn normalize_identifier(ident: &str) -> SmolStr {
629 i_slint_compiler::parser::normalize_identifier(ident)
630}
631
632#[derive(Clone, PartialEq, Debug, Default)]
654pub struct Struct(pub(crate) HashMap<SmolStr, Value>);
655impl Struct {
656 pub fn get_field(&self, name: &str) -> Option<&Value> {
658 self.0.get(&*normalize_identifier(name))
659 }
660 pub fn set_field(&mut self, name: String, value: Value) {
662 self.0.insert(normalize_identifier(&name), value);
663 }
664
665 pub fn iter(&self) -> impl Iterator<Item = (&str, &Value)> {
667 self.0.iter().map(|(a, b)| (a.as_str(), b))
668 }
669}
670
671impl FromIterator<(String, Value)> for Struct {
672 fn from_iter<T: IntoIterator<Item = (String, Value)>>(iter: T) -> Self {
673 Self(iter.into_iter().map(|(s, v)| (normalize_identifier(&s), v)).collect())
674 }
675}
676
677#[deprecated(note = "Use slint_interpreter::Compiler instead")]
679pub struct ComponentCompiler {
680 config: i_slint_compiler::CompilerConfiguration,
681 diagnostics: Vec<Diagnostic>,
682}
683
684#[allow(deprecated)]
685impl Default for ComponentCompiler {
686 fn default() -> Self {
687 let mut config = i_slint_compiler::CompilerConfiguration::new(
688 i_slint_compiler::generator::OutputFormat::Interpreter,
689 );
690 config.components_to_generate = i_slint_compiler::ComponentSelection::LastExported;
691 Self { config, diagnostics: Vec::new() }
692 }
693}
694
695#[allow(deprecated)]
696impl ComponentCompiler {
697 pub fn new() -> Self {
699 Self::default()
700 }
701
702 pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
704 self.config.include_paths = include_paths;
705 }
706
707 pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
709 &self.config.include_paths
710 }
711
712 pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
714 self.config.library_paths = library_paths;
715 }
716
717 pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
719 &self.config.library_paths
720 }
721
722 pub fn set_style(&mut self, style: String) {
734 self.config.style = Some(style);
735 }
736
737 pub fn style(&self) -> Option<&String> {
739 self.config.style.as_ref()
740 }
741
742 pub fn set_translation_domain(&mut self, domain: String) {
744 self.config.translation_domain = Some(domain);
745 }
746
747 pub fn set_file_loader(
755 &mut self,
756 file_loader_fallback: impl Fn(
757 &Path,
758 ) -> core::pin::Pin<
759 Box<dyn Future<Output = Option<std::io::Result<String>>>>,
760 > + 'static,
761 ) {
762 self.config.open_import_callback =
763 Some(Rc::new(move |path| file_loader_fallback(Path::new(path.as_str()))));
764 }
765
766 pub fn diagnostics(&self) -> &Vec<Diagnostic> {
768 &self.diagnostics
769 }
770
771 pub async fn build_from_path<P: AsRef<Path>>(
790 &mut self,
791 path: P,
792 ) -> Option<ComponentDefinition> {
793 let path = path.as_ref();
794 let source = match i_slint_compiler::diagnostics::load_from_path(path) {
795 Ok(s) => s,
796 Err(d) => {
797 self.diagnostics = vec![d];
798 return None;
799 }
800 };
801
802 let r = crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await;
803 self.diagnostics = r.diagnostics.into_iter().collect();
804 r.components.into_values().next()
805 }
806
807 pub async fn build_from_source(
824 &mut self,
825 source_code: String,
826 path: PathBuf,
827 ) -> Option<ComponentDefinition> {
828 let r = crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await;
829 self.diagnostics = r.diagnostics.into_iter().collect();
830 r.components.into_values().next()
831 }
832}
833
834pub struct Compiler {
837 config: i_slint_compiler::CompilerConfiguration,
838}
839
840impl Default for Compiler {
841 fn default() -> Self {
842 let config = i_slint_compiler::CompilerConfiguration::new(
843 i_slint_compiler::generator::OutputFormat::Interpreter,
844 );
845 Self { config }
846 }
847}
848
849impl Compiler {
850 pub fn new() -> Self {
852 Self::default()
853 }
854
855 #[cfg(feature = "internal-live-preview")]
856 pub(crate) fn set_embed_resources(
857 &mut self,
858 embed_resources: i_slint_compiler::EmbedResourcesKind,
859 ) {
860 self.config.embed_resources = embed_resources;
861 }
862
863 #[doc(hidden)]
867 #[cfg(feature = "internal")]
868 pub fn compiler_configuration(
869 &mut self,
870 _: i_slint_core::InternalToken,
871 ) -> &mut i_slint_compiler::CompilerConfiguration {
872 &mut self.config
873 }
874
875 pub fn set_include_paths(&mut self, include_paths: Vec<std::path::PathBuf>) {
877 self.config.include_paths = include_paths;
878 }
879
880 pub fn include_paths(&self) -> &Vec<std::path::PathBuf> {
882 &self.config.include_paths
883 }
884
885 pub fn set_library_paths(&mut self, library_paths: HashMap<String, PathBuf>) {
887 self.config.library_paths = library_paths;
888 }
889
890 pub fn library_paths(&self) -> &HashMap<String, PathBuf> {
892 &self.config.library_paths
893 }
894
895 pub fn set_style(&mut self, style: String) {
906 self.config.style = Some(style);
907 }
908
909 pub fn style(&self) -> Option<&String> {
911 self.config.style.as_ref()
912 }
913
914 pub fn set_translation_domain(&mut self, domain: String) {
916 self.config.translation_domain = Some(domain);
917 }
918
919 pub fn set_default_translation_context(
925 &mut self,
926 default_translation_context: DefaultTranslationContext,
927 ) {
928 self.config.default_translation_context = default_translation_context;
929 }
930
931 pub fn set_file_loader(
939 &mut self,
940 file_loader_fallback: impl Fn(
941 &Path,
942 ) -> core::pin::Pin<
943 Box<dyn Future<Output = Option<std::io::Result<String>>>>,
944 > + 'static,
945 ) {
946 self.config.open_import_callback =
947 Some(Rc::new(move |path| file_loader_fallback(Path::new(path.as_str()))));
948 }
949
950 pub async fn build_from_path<P: AsRef<Path>>(&self, path: P) -> CompilationResult {
969 let path = path.as_ref();
970 let source = match i_slint_compiler::diagnostics::load_from_path(path) {
971 Ok(s) => s,
972 Err(d) => {
973 let mut diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
974 diagnostics.push_compiler_error(d);
975 return CompilationResult {
976 components: HashMap::new(),
977 diagnostics: diagnostics.into_iter().collect(),
978 #[cfg(feature = "internal-file-watcher")]
979 watch_paths: vec![i_slint_compiler::pathutils::clean_path(path)],
980 #[cfg(feature = "internal")]
981 structs_and_enums: Vec::new(),
982 #[cfg(feature = "internal")]
983 named_exports: Vec::new(),
984 };
985 }
986 };
987
988 crate::dynamic_item_tree::load(source, path.into(), self.config.clone()).await
989 }
990
991 pub async fn build_from_source(&self, source_code: String, path: PathBuf) -> CompilationResult {
1004 crate::dynamic_item_tree::load(source_code, path, self.config.clone()).await
1005 }
1006}
1007
1008#[derive(Clone)]
1015pub struct CompilationResult {
1016 pub(crate) components: HashMap<String, ComponentDefinition>,
1017 pub(crate) diagnostics: Vec<Diagnostic>,
1018 #[cfg(feature = "internal-file-watcher")]
1019 pub(crate) watch_paths: Vec<PathBuf>,
1020 #[cfg(feature = "internal")]
1021 pub(crate) structs_and_enums: Vec<LangType>,
1022 #[cfg(feature = "internal")]
1024 pub(crate) named_exports: Vec<(String, String)>,
1025}
1026
1027impl core::fmt::Debug for CompilationResult {
1028 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1029 f.debug_struct("CompilationResult")
1030 .field("components", &self.components.keys())
1031 .field("diagnostics", &self.diagnostics)
1032 .finish()
1033 }
1034}
1035
1036impl CompilationResult {
1037 pub fn has_errors(&self) -> bool {
1040 self.diagnostics().any(|diag| diag.level() == DiagnosticLevel::Error)
1041 }
1042
1043 pub fn diagnostics(&self) -> impl Iterator<Item = Diagnostic> + '_ {
1047 self.diagnostics.iter().cloned()
1048 }
1049
1050 #[cfg(feature = "display-diagnostics")]
1056 pub fn print_diagnostics(&self) {
1057 print_diagnostics(&self.diagnostics)
1058 }
1059
1060 pub fn components(&self) -> impl Iterator<Item = ComponentDefinition> + '_ {
1062 self.components.values().cloned()
1063 }
1064
1065 pub fn component_names(&self) -> impl Iterator<Item = &str> + '_ {
1067 self.components.keys().map(|s| s.as_str())
1068 }
1069
1070 pub fn component(&self, name: &str) -> Option<ComponentDefinition> {
1073 self.components.get(name).cloned()
1074 }
1075
1076 #[doc(hidden)]
1078 #[cfg(feature = "internal-file-watcher")]
1079 pub fn watch_paths(&self, _: i_slint_core::InternalToken) -> &[PathBuf] {
1080 &self.watch_paths
1081 }
1082
1083 #[doc(hidden)]
1085 #[cfg(feature = "internal")]
1086 pub fn structs_and_enums(
1087 &self,
1088 _: i_slint_core::InternalToken,
1089 ) -> impl Iterator<Item = &LangType> {
1090 self.structs_and_enums.iter()
1091 }
1092
1093 #[doc(hidden)]
1096 #[cfg(feature = "internal")]
1097 pub fn named_exports(
1098 &self,
1099 _: i_slint_core::InternalToken,
1100 ) -> impl Iterator<Item = &(String, String)> {
1101 self.named_exports.iter()
1102 }
1103}
1104
1105#[derive(Clone)]
1113pub struct ComponentDefinition {
1114 pub(crate) inner: crate::dynamic_item_tree::ErasedItemTreeDescription,
1115}
1116
1117impl ComponentDefinition {
1118 #[doc(hidden)]
1120 #[cfg(feature = "internal")]
1121 pub fn set_debug_handler(
1122 &self,
1123 handler: impl Fn(Option<&i_slint_compiler::diagnostics::SourceLocation>, &str) + 'static,
1124 _: i_slint_core::InternalToken,
1125 ) {
1126 let handler = Rc::new(handler);
1127
1128 generativity::make_guard!(guard);
1129 self.inner.unerase(guard).recursively_set_debug_handler(handler);
1130 }
1131 pub fn create(&self) -> Result<ComponentInstance, PlatformError> {
1133 let instance = self.create_with_options(Default::default())?;
1134 if !instance.is_system_tray_rooted() {
1137 instance.inner.window_adapter_ref()?;
1139 i_slint_core::window::WindowInner::from_pub(instance.window())
1142 .ensure_tree_instantiated();
1143 }
1144 Ok(instance)
1145 }
1146
1147 #[doc(hidden)]
1149 #[cfg(feature = "internal")]
1150 pub fn create_embedded(&self, ctx: FactoryContext) -> Result<ComponentInstance, PlatformError> {
1151 self.create_with_options(WindowOptions::Embed {
1152 parent_item_tree: ctx.parent_item_tree,
1153 parent_item_tree_index: ctx.parent_item_tree_index,
1154 })
1155 }
1156
1157 #[doc(hidden)]
1159 #[cfg(feature = "internal")]
1160 pub fn create_with_existing_window(
1161 &self,
1162 window: &Window,
1163 ) -> Result<ComponentInstance, PlatformError> {
1164 self.create_with_options(WindowOptions::UseExistingWindow(
1165 WindowInner::from_pub(window).window_adapter(),
1166 ))
1167 }
1168
1169 pub(crate) fn create_with_options(
1171 &self,
1172 options: WindowOptions,
1173 ) -> Result<ComponentInstance, PlatformError> {
1174 generativity::make_guard!(guard);
1175 Ok(ComponentInstance { inner: self.inner.unerase(guard).clone().create(options)? })
1176 }
1177
1178 #[doc(hidden)]
1182 #[cfg(feature = "internal")]
1183 pub fn properties_and_callbacks(
1184 &self,
1185 ) -> impl Iterator<
1186 Item = (
1187 String,
1188 (i_slint_compiler::langtype::Type, i_slint_compiler::object_tree::PropertyVisibility),
1189 ),
1190 > + '_ {
1191 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1194 self.inner.unerase(guard).properties().map(|(s, t, v)| (s.to_string(), (t, v)))
1195 }
1196
1197 pub fn properties(&self) -> impl Iterator<Item = (String, ValueType)> + '_ {
1200 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1203 self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1204 if prop_type.is_property_type() {
1205 Some((prop_name.to_string(), prop_type.into()))
1206 } else {
1207 None
1208 }
1209 })
1210 }
1211
1212 pub fn callbacks(&self) -> impl Iterator<Item = String> + '_ {
1214 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1217 self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1218 if matches!(prop_type, LangType::Callback { .. }) {
1219 Some(prop_name.to_string())
1220 } else {
1221 None
1222 }
1223 })
1224 }
1225
1226 pub fn functions(&self) -> impl Iterator<Item = String> + '_ {
1228 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1231 self.inner.unerase(guard).properties().filter_map(|(prop_name, prop_type, _)| {
1232 if matches!(prop_type, LangType::Function { .. }) {
1233 Some(prop_name.to_string())
1234 } else {
1235 None
1236 }
1237 })
1238 }
1239
1240 pub fn globals(&self) -> impl Iterator<Item = String> + '_ {
1245 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1248 self.inner.unerase(guard).global_names().map(|s| s.to_string())
1249 }
1250
1251 #[doc(hidden)]
1255 #[cfg(feature = "internal")]
1256 pub fn global_properties_and_callbacks(
1257 &self,
1258 global_name: &str,
1259 ) -> Option<
1260 impl Iterator<
1261 Item = (
1262 String,
1263 (
1264 i_slint_compiler::langtype::Type,
1265 i_slint_compiler::object_tree::PropertyVisibility,
1266 ),
1267 ),
1268 > + '_,
1269 > {
1270 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1273 self.inner
1274 .unerase(guard)
1275 .global_properties(global_name)
1276 .map(|o| o.map(|(s, t, v)| (s.to_string(), (t, v))))
1277 }
1278
1279 pub fn global_properties(
1281 &self,
1282 global_name: &str,
1283 ) -> Option<impl Iterator<Item = (String, ValueType)> + '_> {
1284 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1287 self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1288 iter.filter_map(|(prop_name, prop_type, _)| {
1289 if prop_type.is_property_type() {
1290 Some((prop_name.to_string(), prop_type.into()))
1291 } else {
1292 None
1293 }
1294 })
1295 })
1296 }
1297
1298 pub fn global_callbacks(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1300 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1303 self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1304 iter.filter_map(|(prop_name, prop_type, _)| {
1305 if matches!(prop_type, LangType::Callback { .. }) {
1306 Some(prop_name.to_string())
1307 } else {
1308 None
1309 }
1310 })
1311 })
1312 }
1313
1314 pub fn global_functions(&self, global_name: &str) -> Option<impl Iterator<Item = String> + '_> {
1316 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1319 self.inner.unerase(guard).global_properties(global_name).map(|iter| {
1320 iter.filter_map(|(prop_name, prop_type, _)| {
1321 if matches!(prop_type, LangType::Function { .. }) {
1322 Some(prop_name.to_string())
1323 } else {
1324 None
1325 }
1326 })
1327 })
1328 }
1329
1330 pub fn name(&self) -> &str {
1332 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1335 self.inner.unerase(guard).id()
1336 }
1337
1338 #[doc(hidden)]
1342 #[cfg(feature = "internal")]
1343 pub fn is_window(&self) -> bool {
1344 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1345 !self.inner.unerase(guard).original.inherits_system_tray_icon()
1346 }
1347
1348 #[cfg(feature = "internal")]
1350 #[doc(hidden)]
1351 pub fn root_component(&self) -> Rc<i_slint_compiler::object_tree::Component> {
1352 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1353 self.inner.unerase(guard).original.clone()
1354 }
1355
1356 #[cfg(feature = "internal-highlight")]
1360 pub fn type_loader(&self) -> std::rc::Rc<i_slint_compiler::typeloader::TypeLoader> {
1361 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1362 self.inner.unerase(guard).type_loader.get().unwrap().clone()
1363 }
1364
1365 #[cfg(feature = "internal-highlight")]
1373 pub fn raw_type_loader(&self) -> Option<i_slint_compiler::typeloader::TypeLoader> {
1374 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1375 self.inner
1376 .unerase(guard)
1377 .raw_type_loader
1378 .get()
1379 .unwrap()
1380 .as_ref()
1381 .and_then(|tl| i_slint_compiler::typeloader::snapshot(tl))
1382 }
1383}
1384
1385#[cfg(feature = "display-diagnostics")]
1391pub fn print_diagnostics(diagnostics: &[Diagnostic]) {
1392 let mut build_diagnostics = i_slint_compiler::diagnostics::BuildDiagnostics::default();
1393 for d in diagnostics {
1394 build_diagnostics.push_compiler_error(d.clone())
1395 }
1396 build_diagnostics.print();
1397}
1398
1399#[repr(C)]
1407pub struct ComponentInstance {
1408 pub(crate) inner: crate::dynamic_item_tree::DynamicComponentVRc,
1409}
1410
1411impl ComponentInstance {
1412 pub fn definition(&self) -> ComponentDefinition {
1414 generativity::make_guard!(guard);
1415 ComponentDefinition { inner: self.inner.unerase(guard).description().into() }
1416 }
1417
1418 fn is_system_tray_rooted(&self) -> bool {
1419 let guard = unsafe { generativity::Guard::new(generativity::Id::new()) };
1420 self.inner.unerase(guard).description().original.inherits_system_tray_icon()
1421 }
1422
1423 pub fn get_property(&self, name: &str) -> Result<Value, GetPropertyError> {
1443 generativity::make_guard!(guard);
1444 let comp = self.inner.unerase(guard);
1445 let name = normalize_identifier(name);
1446
1447 if comp
1448 .description()
1449 .original
1450 .root_element
1451 .borrow()
1452 .property_declarations
1453 .get(&name)
1454 .is_none_or(|d| !d.expose_in_public_api)
1455 {
1456 return Err(GetPropertyError::NoSuchProperty);
1457 }
1458
1459 comp.description()
1460 .get_property(comp.borrow(), &name)
1461 .map_err(|()| GetPropertyError::NoSuchProperty)
1462 }
1463
1464 pub fn set_property(&self, name: &str, value: Value) -> Result<(), SetPropertyError> {
1466 let name = normalize_identifier(name);
1467 generativity::make_guard!(guard);
1468 let comp = self.inner.unerase(guard);
1469 let d = comp.description();
1470 let elem = d.original.root_element.borrow();
1471 let decl = elem.property_declarations.get(&name).ok_or(SetPropertyError::NoSuchProperty)?;
1472
1473 if !decl.expose_in_public_api {
1474 return Err(SetPropertyError::NoSuchProperty);
1475 } else if decl.visibility == i_slint_compiler::object_tree::PropertyVisibility::Output {
1476 return Err(SetPropertyError::AccessDenied);
1477 }
1478
1479 d.set_property(comp.borrow(), &name, value)
1480 }
1481
1482 pub fn set_callback(
1517 &self,
1518 name: &str,
1519 callback: impl Fn(&[Value]) -> Value + 'static,
1520 ) -> Result<(), SetCallbackError> {
1521 generativity::make_guard!(guard);
1522 let comp = self.inner.unerase(guard);
1523 comp.description()
1524 .set_callback_handler(comp.borrow(), &normalize_identifier(name), Box::new(callback))
1525 .map_err(|()| SetCallbackError::NoSuchCallback)
1526 }
1527
1528 pub fn invoke(&self, name: &str, args: &[Value]) -> Result<Value, InvokeError> {
1533 generativity::make_guard!(guard);
1534 let comp = self.inner.unerase(guard);
1535 comp.description()
1536 .invoke(comp.borrow(), &normalize_identifier(name), args)
1537 .map_err(|()| InvokeError::NoSuchCallable)
1538 }
1539
1540 pub fn get_global_property(
1565 &self,
1566 global: &str,
1567 property: &str,
1568 ) -> Result<Value, GetPropertyError> {
1569 generativity::make_guard!(guard);
1570 let comp = self.inner.unerase(guard);
1571 comp.description()
1572 .get_global(comp.borrow(), &normalize_identifier(global))
1573 .map_err(|()| GetPropertyError::NoSuchProperty)? .as_ref()
1575 .get_property(&normalize_identifier(property))
1576 .map_err(|()| GetPropertyError::NoSuchProperty)
1577 }
1578
1579 pub fn set_global_property(
1581 &self,
1582 global: &str,
1583 property: &str,
1584 value: Value,
1585 ) -> Result<(), SetPropertyError> {
1586 generativity::make_guard!(guard);
1587 let comp = self.inner.unerase(guard);
1588 comp.description()
1589 .get_global(comp.borrow(), &normalize_identifier(global))
1590 .map_err(|()| SetPropertyError::NoSuchProperty)? .as_ref()
1592 .set_property(&normalize_identifier(property), value)
1593 }
1594
1595 pub fn set_global_callback(
1630 &self,
1631 global: &str,
1632 name: &str,
1633 callback: impl Fn(&[Value]) -> Value + 'static,
1634 ) -> Result<(), SetCallbackError> {
1635 generativity::make_guard!(guard);
1636 let comp = self.inner.unerase(guard);
1637 comp.description()
1638 .get_global(comp.borrow(), &normalize_identifier(global))
1639 .map_err(|()| SetCallbackError::NoSuchCallback)? .as_ref()
1641 .set_callback_handler(&normalize_identifier(name), Box::new(callback))
1642 .map_err(|()| SetCallbackError::NoSuchCallback)
1643 }
1644
1645 pub fn invoke_global(
1650 &self,
1651 global: &str,
1652 callable_name: &str,
1653 args: &[Value],
1654 ) -> Result<Value, InvokeError> {
1655 generativity::make_guard!(guard);
1656 let comp = self.inner.unerase(guard);
1657 let g = comp
1658 .description()
1659 .get_global(comp.borrow(), &normalize_identifier(global))
1660 .map_err(|()| InvokeError::NoSuchCallable)?; let callable_name = normalize_identifier(callable_name);
1662 if matches!(
1663 comp.description()
1664 .original
1665 .root_element
1666 .borrow()
1667 .lookup_property(&callable_name)
1668 .property_type,
1669 LangType::Function { .. }
1670 ) {
1671 g.as_ref()
1672 .eval_function(&callable_name, args.to_vec())
1673 .map_err(|()| InvokeError::NoSuchCallable)
1674 } else {
1675 g.as_ref()
1676 .invoke_callback(&callable_name, args)
1677 .map_err(|()| InvokeError::NoSuchCallable)
1678 }
1679 }
1680
1681 #[cfg(feature = "internal-highlight")]
1685 pub fn component_positions(
1686 &self,
1687 path: &Path,
1688 offset: u32,
1689 ) -> Vec<crate::highlight::HighlightedRect> {
1690 crate::highlight::component_positions(&self.inner, path, offset)
1691 }
1692
1693 #[cfg(feature = "internal-highlight")]
1697 pub fn element_positions(
1698 &self,
1699 element: &i_slint_compiler::object_tree::ElementRc,
1700 ) -> Vec<crate::highlight::HighlightedRect> {
1701 crate::highlight::element_positions(
1702 &self.inner,
1703 element,
1704 crate::highlight::ElementPositionFilter::IncludeClipped,
1705 )
1706 }
1707
1708 #[cfg(feature = "internal-highlight")]
1712 pub fn element_node_at_source_code_position(
1713 &self,
1714 path: &Path,
1715 offset: u32,
1716 ) -> Vec<(i_slint_compiler::object_tree::ElementRc, usize)> {
1717 crate::highlight::element_node_at_source_code_position(&self.inner, path, offset)
1718 }
1719}
1720
1721impl StrongHandle for ComponentInstance {
1722 type WeakInner = vtable::VWeak<ItemTreeVTable, crate::dynamic_item_tree::ErasedItemTreeBox>;
1723
1724 fn upgrade_from_weak_inner(inner: &Self::WeakInner) -> Option<Self> {
1725 Some(Self { inner: inner.upgrade()? })
1726 }
1727}
1728
1729impl ComponentHandle for ComponentInstance {
1730 fn as_weak(&self) -> Weak<Self>
1731 where
1732 Self: Sized,
1733 {
1734 Weak::new(vtable::VRc::downgrade(&self.inner))
1735 }
1736
1737 fn clone_strong(&self) -> Self {
1738 Self { inner: self.inner.clone() }
1739 }
1740
1741 fn show(&self) -> Result<(), PlatformError> {
1742 if self.is_system_tray_rooted() {
1743 self.set_property("visible", Value::Bool(true)).expect(
1747 "setting `visible` on a SystemTrayIcon-rooted component should always succeed",
1748 );
1749 return Ok(());
1750 }
1751 self.inner.window_adapter_ref()?.window().show()
1752 }
1753
1754 fn hide(&self) -> Result<(), PlatformError> {
1755 if self.is_system_tray_rooted() {
1756 self.set_property("visible", Value::Bool(false)).expect(
1757 "setting `visible` on a SystemTrayIcon-rooted component should always succeed",
1758 );
1759 return Ok(());
1760 }
1761 self.inner.window_adapter_ref()?.window().hide()
1762 }
1763
1764 fn run(&self) -> Result<(), PlatformError> {
1765 self.show()?;
1766 run_event_loop()?;
1767 self.hide()
1768 }
1769
1770 fn window(&self) -> &Window {
1771 self.inner.window_adapter_ref().unwrap().window()
1772 }
1773
1774 fn global<'a, T: Global<'a, Self>>(&'a self) -> T
1775 where
1776 Self: Sized,
1777 {
1778 unreachable!()
1779 }
1780}
1781
1782impl From<ComponentInstance>
1783 for vtable::VRc<i_slint_core::item_tree::ItemTreeVTable, ErasedItemTreeBox>
1784{
1785 fn from(value: ComponentInstance) -> Self {
1786 value.inner
1787 }
1788}
1789
1790#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1792#[non_exhaustive]
1793pub enum GetPropertyError {
1794 #[display("no such property")]
1796 NoSuchProperty,
1797}
1798
1799#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1801#[non_exhaustive]
1802pub enum SetPropertyError {
1803 #[display("no such property")]
1805 NoSuchProperty,
1806 #[display("wrong type")]
1812 WrongType,
1813 #[display("access denied")]
1815 AccessDenied,
1816}
1817
1818#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1820#[non_exhaustive]
1821pub enum SetCallbackError {
1822 #[display("no such callback")]
1824 NoSuchCallback,
1825}
1826
1827#[derive(Debug, Clone, Copy, PartialEq, Eq, derive_more::Error, derive_more::Display)]
1829#[non_exhaustive]
1830pub enum InvokeError {
1831 #[display("no such callback or function")]
1833 NoSuchCallable,
1834}
1835
1836pub fn run_event_loop() -> Result<(), PlatformError> {
1840 i_slint_backend_selector::with_platform(|b| b.run_event_loop())
1841}
1842
1843pub fn spawn_local<F: Future + 'static>(fut: F) -> Result<JoinHandle<F::Output>, EventLoopError> {
1847 i_slint_backend_selector::with_global_context(|ctx| ctx.spawn_local(fut))
1848 .map_err(|_| EventLoopError::NoEventLoopProvider)?
1849}
1850
1851#[test]
1852fn component_definition_properties() {
1853 i_slint_backend_testing::init_no_event_loop();
1854 let mut compiler = Compiler::default();
1855 compiler.set_style("fluent".into());
1856 let comp_def = spin_on::spin_on(
1857 compiler.build_from_source(
1858 r#"
1859 export component Dummy {
1860 in-out property <string> test;
1861 in-out property <int> underscores-and-dashes_preserved: 44;
1862 callback hello;
1863 }"#
1864 .into(),
1865 "".into(),
1866 ),
1867 )
1868 .component("Dummy")
1869 .unwrap();
1870
1871 let props = comp_def.properties().collect::<Vec<(_, _)>>();
1872
1873 assert_eq!(props.len(), 2);
1874 assert_eq!(props[0].0, "test");
1875 assert_eq!(props[0].1, ValueType::String);
1876 assert_eq!(props[1].0, "underscores-and-dashes_preserved");
1877 assert_eq!(props[1].1, ValueType::Number);
1878
1879 let instance = comp_def.create().unwrap();
1880 assert_eq!(instance.get_property("underscores_and-dashes-preserved"), Ok(Value::Number(44.)));
1881 assert_eq!(
1882 instance.get_property("underscoresanddashespreserved"),
1883 Err(GetPropertyError::NoSuchProperty)
1884 );
1885 assert_eq!(
1886 instance.set_property("underscores-and_dashes-preserved", Value::Number(88.)),
1887 Ok(())
1888 );
1889 assert_eq!(
1890 instance.set_property("underscoresanddashespreserved", Value::Number(99.)),
1891 Err(SetPropertyError::NoSuchProperty)
1892 );
1893 assert_eq!(
1894 instance.set_property("underscores-and_dashes-preserved", Value::String("99".into())),
1895 Err(SetPropertyError::WrongType)
1896 );
1897 assert_eq!(instance.get_property("underscores-and-dashes-preserved"), Ok(Value::Number(88.)));
1898}
1899
1900#[test]
1901fn component_definition_properties2() {
1902 i_slint_backend_testing::init_no_event_loop();
1903 let mut compiler = Compiler::default();
1904 compiler.set_style("fluent".into());
1905 let comp_def = spin_on::spin_on(
1906 compiler.build_from_source(
1907 r#"
1908 export component Dummy {
1909 in-out property <string> sub-text <=> sub.text;
1910 sub := Text { property <int> private-not-exported; }
1911 out property <string> xreadonly: "the value";
1912 private property <string> xx: sub.text;
1913 callback hello;
1914 }"#
1915 .into(),
1916 "".into(),
1917 ),
1918 )
1919 .component("Dummy")
1920 .unwrap();
1921
1922 let props = comp_def.properties().collect::<Vec<(_, _)>>();
1923
1924 assert_eq!(props.len(), 2);
1925 assert_eq!(props[0].0, "sub-text");
1926 assert_eq!(props[0].1, ValueType::String);
1927 assert_eq!(props[1].0, "xreadonly");
1928
1929 let callbacks = comp_def.callbacks().collect::<Vec<_>>();
1930 assert_eq!(callbacks.len(), 1);
1931 assert_eq!(callbacks[0], "hello");
1932
1933 let instance = comp_def.create().unwrap();
1934 assert_eq!(
1935 instance.set_property("xreadonly", SharedString::from("XXX").into()),
1936 Err(SetPropertyError::AccessDenied)
1937 );
1938 assert_eq!(instance.get_property("xreadonly"), Ok(Value::String("the value".into())));
1939 assert_eq!(
1940 instance.set_property("xx", SharedString::from("XXX").into()),
1941 Err(SetPropertyError::NoSuchProperty)
1942 );
1943 assert_eq!(
1944 instance.set_property("background", Value::default()),
1945 Err(SetPropertyError::NoSuchProperty)
1946 );
1947
1948 assert_eq!(instance.get_property("background"), Err(GetPropertyError::NoSuchProperty));
1949 assert_eq!(instance.get_property("xx"), Err(GetPropertyError::NoSuchProperty));
1950}
1951
1952#[test]
1953fn globals() {
1954 i_slint_backend_testing::init_no_event_loop();
1955 let mut compiler = Compiler::default();
1956 compiler.set_style("fluent".into());
1957 let definition = spin_on::spin_on(
1958 compiler.build_from_source(
1959 r#"
1960 export global My-Super_Global {
1961 in-out property <int> the-property : 21;
1962 callback my-callback();
1963 }
1964 export { My-Super_Global as AliasedGlobal }
1965 export component Dummy {
1966 callback alias <=> My-Super_Global.my-callback;
1967 }"#
1968 .into(),
1969 "".into(),
1970 ),
1971 )
1972 .component("Dummy")
1973 .unwrap();
1974
1975 assert_eq!(definition.globals().collect::<Vec<_>>(), vec!["My-Super_Global", "AliasedGlobal"]);
1976
1977 assert!(definition.global_properties("not-there").is_none());
1978 {
1979 let expected_properties = vec![("the-property".to_string(), ValueType::Number)];
1980 let expected_callbacks = vec!["my-callback".to_string()];
1981
1982 let assert_properties_and_callbacks = |global_name| {
1983 assert_eq!(
1984 definition
1985 .global_properties(global_name)
1986 .map(|props| props.collect::<Vec<_>>())
1987 .as_ref(),
1988 Some(&expected_properties)
1989 );
1990 assert_eq!(
1991 definition
1992 .global_callbacks(global_name)
1993 .map(|props| props.collect::<Vec<_>>())
1994 .as_ref(),
1995 Some(&expected_callbacks)
1996 );
1997 };
1998
1999 assert_properties_and_callbacks("My-Super-Global");
2000 assert_properties_and_callbacks("My_Super-Global");
2001 assert_properties_and_callbacks("AliasedGlobal");
2002 }
2003
2004 let instance = definition.create().unwrap();
2005 assert_eq!(
2006 instance.set_global_property("My_Super-Global", "the_property", Value::Number(44.)),
2007 Ok(())
2008 );
2009 assert_eq!(
2010 instance.set_global_property("AliasedGlobal", "the_property", Value::Number(44.)),
2011 Ok(())
2012 );
2013 assert_eq!(
2014 instance.set_global_property("DontExist", "the-property", Value::Number(88.)),
2015 Err(SetPropertyError::NoSuchProperty)
2016 );
2017
2018 assert_eq!(
2019 instance.set_global_property("My_Super-Global", "theproperty", Value::Number(88.)),
2020 Err(SetPropertyError::NoSuchProperty)
2021 );
2022 assert_eq!(
2023 instance.set_global_property("AliasedGlobal", "theproperty", Value::Number(88.)),
2024 Err(SetPropertyError::NoSuchProperty)
2025 );
2026 assert_eq!(
2027 instance.set_global_property("My_Super-Global", "the_property", Value::String("88".into())),
2028 Err(SetPropertyError::WrongType)
2029 );
2030 assert_eq!(
2031 instance.get_global_property("My-Super_Global", "yoyo"),
2032 Err(GetPropertyError::NoSuchProperty)
2033 );
2034 assert_eq!(
2035 instance.get_global_property("My-Super_Global", "the-property"),
2036 Ok(Value::Number(44.))
2037 );
2038
2039 assert_eq!(
2040 instance.set_property("the-property", Value::Void),
2041 Err(SetPropertyError::NoSuchProperty)
2042 );
2043 assert_eq!(instance.get_property("the-property"), Err(GetPropertyError::NoSuchProperty));
2044
2045 assert_eq!(
2046 instance.set_global_callback("DontExist", "the-property", |_| panic!()),
2047 Err(SetCallbackError::NoSuchCallback)
2048 );
2049 assert_eq!(
2050 instance.set_global_callback("My_Super_Global", "the-property", |_| panic!()),
2051 Err(SetCallbackError::NoSuchCallback)
2052 );
2053 assert_eq!(
2054 instance.set_global_callback("My_Super_Global", "yoyo", |_| panic!()),
2055 Err(SetCallbackError::NoSuchCallback)
2056 );
2057
2058 assert_eq!(
2059 instance.invoke_global("DontExist", "the-property", &[]),
2060 Err(InvokeError::NoSuchCallable)
2061 );
2062 assert_eq!(
2063 instance.invoke_global("My_Super_Global", "the-property", &[]),
2064 Err(InvokeError::NoSuchCallable)
2065 );
2066 assert_eq!(
2067 instance.invoke_global("My_Super_Global", "yoyo", &[]),
2068 Err(InvokeError::NoSuchCallable)
2069 );
2070
2071 assert_eq!(instance.get_property("alias"), Err(GetPropertyError::NoSuchProperty));
2073}
2074
2075#[test]
2076fn call_functions() {
2077 i_slint_backend_testing::init_no_event_loop();
2078 let mut compiler = Compiler::default();
2079 compiler.set_style("fluent".into());
2080 let definition = spin_on::spin_on(
2081 compiler.build_from_source(
2082 r#"
2083 export global Gl {
2084 out property<string> q;
2085 public function foo-bar(a-a: string, b-b:int) -> string {
2086 q = a-a;
2087 return a-a + b-b;
2088 }
2089 }
2090 export component Test {
2091 out property<int> p;
2092 public function foo-bar(a: int, b:int) -> int {
2093 p = a;
2094 return a + b;
2095 }
2096 }"#
2097 .into(),
2098 "".into(),
2099 ),
2100 )
2101 .component("Test")
2102 .unwrap();
2103
2104 assert_eq!(definition.functions().collect::<Vec<_>>(), ["foo-bar"]);
2105 assert_eq!(definition.global_functions("Gl").unwrap().collect::<Vec<_>>(), ["foo-bar"]);
2106
2107 let instance = definition.create().unwrap();
2108
2109 assert_eq!(
2110 instance.invoke("foo_bar", &[Value::Number(3.), Value::Number(4.)]),
2111 Ok(Value::Number(7.))
2112 );
2113 assert_eq!(instance.invoke("p", &[]), Err(InvokeError::NoSuchCallable));
2114 assert_eq!(instance.get_property("p"), Ok(Value::Number(3.)));
2115
2116 assert_eq!(
2117 instance.invoke_global(
2118 "Gl",
2119 "foo_bar",
2120 &[Value::String("Hello".into()), Value::Number(10.)]
2121 ),
2122 Ok(Value::String("Hello10".into()))
2123 );
2124 assert_eq!(instance.get_global_property("Gl", "q"), Ok(Value::String("Hello".into())));
2125}
2126
2127#[test]
2128fn component_definition_struct_properties() {
2129 i_slint_backend_testing::init_no_event_loop();
2130 let mut compiler = Compiler::default();
2131 compiler.set_style("fluent".into());
2132 let comp_def = spin_on::spin_on(
2133 compiler.build_from_source(
2134 r#"
2135 export struct Settings {
2136 string_value: string,
2137 }
2138 export component Dummy {
2139 in-out property <Settings> test;
2140 }"#
2141 .into(),
2142 "".into(),
2143 ),
2144 )
2145 .component("Dummy")
2146 .unwrap();
2147
2148 let props = comp_def.properties().collect::<Vec<(_, _)>>();
2149
2150 assert_eq!(props.len(), 1);
2151 assert_eq!(props[0].0, "test");
2152 assert_eq!(props[0].1, ValueType::Struct);
2153
2154 let instance = comp_def.create().unwrap();
2155
2156 let valid_struct: Struct =
2157 [("string_value".to_string(), Value::String("hello".into()))].iter().cloned().collect();
2158
2159 assert_eq!(instance.set_property("test", Value::Struct(valid_struct.clone())), Ok(()));
2160 assert_eq!(instance.get_property("test").unwrap().value_type(), ValueType::Struct);
2161
2162 assert_eq!(instance.set_property("test", Value::Number(42.)), Err(SetPropertyError::WrongType));
2163
2164 let mut invalid_struct = valid_struct.clone();
2165 invalid_struct.set_field("other".into(), Value::Number(44.));
2166 assert_eq!(
2167 instance.set_property("test", Value::Struct(invalid_struct)),
2168 Err(SetPropertyError::WrongType)
2169 );
2170 let mut invalid_struct = valid_struct;
2171 invalid_struct.set_field("string_value".into(), Value::Number(44.));
2172 assert_eq!(
2173 instance.set_property("test", Value::Struct(invalid_struct)),
2174 Err(SetPropertyError::WrongType)
2175 );
2176}
2177
2178#[test]
2179fn component_definition_model_properties() {
2180 use i_slint_core::model::*;
2181 i_slint_backend_testing::init_no_event_loop();
2182 let mut compiler = Compiler::default();
2183 compiler.set_style("fluent".into());
2184 let comp_def = spin_on::spin_on(compiler.build_from_source(
2185 "export component Dummy { in-out property <[int]> prop: [42, 12]; }".into(),
2186 "".into(),
2187 ))
2188 .component("Dummy")
2189 .unwrap();
2190
2191 let props = comp_def.properties().collect::<Vec<(_, _)>>();
2192 assert_eq!(props.len(), 1);
2193 assert_eq!(props[0].0, "prop");
2194 assert_eq!(props[0].1, ValueType::Model);
2195
2196 let instance = comp_def.create().unwrap();
2197
2198 let int_model =
2199 Value::Model([Value::Number(14.), Value::Number(15.), Value::Number(16.)].into());
2200 let empty_model = Value::Model(ModelRc::new(VecModel::<Value>::default()));
2201 let model_with_string = Value::Model(VecModel::from_slice(&[
2202 Value::Number(1000.),
2203 Value::String("foo".into()),
2204 Value::Number(1111.),
2205 ]));
2206
2207 #[track_caller]
2208 fn check_model(val: Value, r: &[f64]) {
2209 if let Value::Model(m) = val {
2210 assert_eq!(r.len(), m.row_count());
2211 for (i, v) in r.iter().enumerate() {
2212 assert_eq!(m.row_data(i).unwrap(), Value::Number(*v));
2213 }
2214 } else {
2215 panic!("{val:?} not a model");
2216 }
2217 }
2218
2219 assert_eq!(instance.get_property("prop").unwrap().value_type(), ValueType::Model);
2220 check_model(instance.get_property("prop").unwrap(), &[42., 12.]);
2221
2222 instance.set_property("prop", int_model).unwrap();
2223 check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2224
2225 assert_eq!(instance.set_property("prop", Value::Number(42.)), Err(SetPropertyError::WrongType));
2226 check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2227 assert_eq!(instance.set_property("prop", model_with_string), Err(SetPropertyError::WrongType));
2228 check_model(instance.get_property("prop").unwrap(), &[14., 15., 16.]);
2229
2230 assert_eq!(instance.set_property("prop", empty_model), Ok(()));
2231 check_model(instance.get_property("prop").unwrap(), &[]);
2232}
2233
2234#[test]
2235fn lang_type_to_value_type() {
2236 use i_slint_compiler::langtype::Struct as LangStruct;
2237 use std::collections::BTreeMap;
2238
2239 assert_eq!(ValueType::from(LangType::Void), ValueType::Void);
2240 assert_eq!(ValueType::from(LangType::Float32), ValueType::Number);
2241 assert_eq!(ValueType::from(LangType::Int32), ValueType::Number);
2242 assert_eq!(ValueType::from(LangType::Duration), ValueType::Number);
2243 assert_eq!(ValueType::from(LangType::Angle), ValueType::Number);
2244 assert_eq!(ValueType::from(LangType::PhysicalLength), ValueType::Number);
2245 assert_eq!(ValueType::from(LangType::LogicalLength), ValueType::Number);
2246 assert_eq!(ValueType::from(LangType::Percent), ValueType::Number);
2247 assert_eq!(ValueType::from(LangType::UnitProduct(Vec::new())), ValueType::Number);
2248 assert_eq!(ValueType::from(LangType::String), ValueType::String);
2249 assert_eq!(ValueType::from(LangType::Color), ValueType::Brush);
2250 assert_eq!(ValueType::from(LangType::Brush), ValueType::Brush);
2251 assert_eq!(ValueType::from(LangType::Array(Rc::new(LangType::Void))), ValueType::Model);
2252 assert_eq!(ValueType::from(LangType::Bool), ValueType::Bool);
2253 assert_eq!(
2254 ValueType::from(LangType::Struct(Rc::new(LangStruct {
2255 fields: BTreeMap::default(),
2256 name: i_slint_compiler::langtype::StructName::None,
2257 }))),
2258 ValueType::Struct
2259 );
2260 assert_eq!(ValueType::from(LangType::Image), ValueType::Image);
2261}
2262
2263#[test]
2264fn test_multi_components() {
2265 i_slint_backend_testing::init_no_event_loop();
2266 let result = spin_on::spin_on(
2267 Compiler::default().build_from_source(
2268 r#"
2269 export struct Settings {
2270 string_value: string,
2271 }
2272 export global ExpGlo { in-out property <int> test: 42; }
2273 component Common {
2274 in-out property <Settings> settings: { string_value: "Hello", };
2275 }
2276 export component Xyz inherits Window {
2277 in-out property <int> aaa: 8;
2278 }
2279 export component Foo {
2280
2281 in-out property <int> test: 42;
2282 c := Common {}
2283 }
2284 export component Bar inherits Window {
2285 in-out property <int> blah: 78;
2286 c := Common {}
2287 }
2288 "#
2289 .into(),
2290 PathBuf::from("hello.slint"),
2291 ),
2292 );
2293
2294 assert!(!result.has_errors(), "Error {:?}", result.diagnostics().collect::<Vec<_>>());
2295 let mut components = result.component_names().collect::<Vec<_>>();
2296 components.sort();
2297 assert_eq!(components, vec!["Bar", "Xyz"]);
2298 let diag = result.diagnostics().collect::<Vec<_>>();
2299 assert_eq!(diag.len(), 1);
2300 assert_eq!(diag[0].level(), DiagnosticLevel::Warning);
2301 assert_eq!(
2302 diag[0].message(),
2303 "Exported component 'Foo' doesn't inherit Window. No code will be generated for it"
2304 );
2305
2306 let comp1 = result.component("Xyz").unwrap();
2307 assert_eq!(comp1.name(), "Xyz");
2308 let instance1a = comp1.create().unwrap();
2309 let comp2 = result.component("Bar").unwrap();
2310 let instance2 = comp2.create().unwrap();
2311 let instance1b = comp1.create().unwrap();
2312
2313 assert_eq!(instance1a.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2315 assert_eq!(instance1a.set_global_property("ExpGlo", "test", Value::Number(88.0)), Ok(()));
2316 assert_eq!(instance2.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2317 assert_eq!(instance1b.get_global_property("ExpGlo", "test"), Ok(Value::Number(42.0)));
2318 assert_eq!(instance1a.get_global_property("ExpGlo", "test"), Ok(Value::Number(88.0)));
2319
2320 assert!(result.component("Settings").is_none());
2321 assert!(result.component("Foo").is_none());
2322 assert!(result.component("Common").is_none());
2323 assert!(result.component("ExpGlo").is_none());
2324 assert!(result.component("xyz").is_none());
2325}
2326
2327#[cfg(all(test, feature = "internal-highlight"))]
2328fn compile(code: &str) -> (ComponentInstance, PathBuf) {
2329 i_slint_backend_testing::init_no_event_loop();
2330 let mut compiler = Compiler::default();
2331 compiler.set_style("fluent".into());
2332 let path = PathBuf::from("/tmp/test.slint");
2333
2334 let compile_result =
2335 spin_on::spin_on(compiler.build_from_source(code.to_string(), path.clone()));
2336
2337 for d in &compile_result.diagnostics {
2338 eprintln!("{d}");
2339 }
2340
2341 assert!(!compile_result.has_errors());
2342
2343 let definition = compile_result.components().next().unwrap();
2344 let instance = definition.create().unwrap();
2345
2346 (instance, path)
2347}
2348
2349#[cfg(feature = "internal-highlight")]
2350#[test]
2351fn test_element_node_at_source_code_position() {
2352 let code = r#"
2353component Bar1 {}
2354
2355component Foo1 {
2356}
2357
2358export component Foo2 inherits Window {
2359 Bar1 {}
2360 Foo1 {}
2361}"#;
2362
2363 let (handle, path) = compile(code);
2364
2365 for i in 0..code.len() as u32 {
2366 let elements = handle.element_node_at_source_code_position(&path, i);
2367 eprintln!("{i}: {}", code.as_bytes()[i as usize] as char);
2368 match i {
2369 16 => assert_eq!(elements.len(), 1), 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()),
2375 }
2376 }
2377}
2378
2379#[cfg(feature = "ffi")]
2380#[allow(missing_docs)]
2381#[path = "ffi.rs"]
2382pub(crate) mod ffi;