slint_interpreter/
lib.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
5
6/*!
7# Slint interpreter library
8
9With this crate, you can load a .slint file at runtime and show its UI.
10
11You only need to use this crate if you do not want to use pre-compiled .slint
12code, which is the normal way to use Slint, using the `slint` crate
13
14The entry point for this crate is the [`Compiler`] type, which you can
15use to create [`CompilationResult`] with the [`Compiler::build_from_source`] or [`Compiler::build_from_path`]
16functions. [`CompilationResult`] provides access to all components declared for export. Obtain a [`ComponentDefinition`]
17for each and use [`ComponentDefinition::create()`] to instantiate a component. The returned [`ComponentInstance`]
18in turn provides access to properties, callbacks, functions, global singletons, as well as implementing [`ComponentHandle`].
19
20### Note about `async` functions
21
22Compiling a component is `async` but in practice, this is only asynchronous if [`Compiler::set_file_loader`]
23is set and its future is actually asynchronous.  If that is not used, then it is fine to use a very simple
24executor, such as the one provided by the `spin_on` crate
25
26## Examples
27
28This example loads a `.slint` dynamically from a path and show errors if any:
29
30```rust
31use slint_interpreter::{ComponentDefinition, Compiler, ComponentHandle};
32
33let compiler = Compiler::default();
34let result = spin_on::spin_on(compiler.build_from_path("hello.slint"));
35let diagnostics : Vec<_> = result.diagnostics().collect();
36# #[cfg(feature="print_diagnostics")]
37diagnostics.print();
38if let Some(definition) = result.component("Foo") {
39    let instance = definition.create().unwrap();
40    instance.run().unwrap();
41}
42```
43
44This example load a `.slint` from a string and set some properties:
45
46```rust
47# i_slint_backend_testing::init_no_event_loop();
48use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString, ComponentHandle};
49
50let code = r#"
51    export component MyWin inherits Window {
52        in property <string> my_name;
53        Text {
54            text: "Hello, " + my_name;
55        }
56    }
57"#;
58
59let mut compiler = Compiler::default();
60let result =
61    spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
62assert_eq!(result.diagnostics().count(), 0);
63let definition = result.component("MyWin");
64let instance = definition.unwrap().create().unwrap();
65instance.set_property("my_name", Value::from(SharedString::from("World"))).unwrap();
66# return; // we don't want to call run in the tests
67instance.run().unwrap();
68```
69*/
70//! ## Feature flags
71#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
72#![warn(missing_docs)]
73#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
74
75#[cfg(not(feature = "compat-1-2"))]
76compile_error!(
77    "The feature `compat-1-2` must be enabled to ensure \
78    forward compatibility with future version of this crate"
79);
80
81mod api;
82mod dynamic_item_tree;
83mod dynamic_type;
84mod eval;
85mod eval_layout;
86mod global_component;
87#[cfg(feature = "internal-highlight")]
88pub mod highlight;
89#[cfg(feature = "internal-json")]
90pub mod json;
91mod value_model;
92
93#[doc(inline)]
94pub use api::*;
95
96#[cfg(feature = "internal")]
97#[doc(hidden)]
98pub use eval::default_value_for_type;
99
100/// (Re-export from corelib.)
101#[doc(inline)]
102pub use i_slint_core::{Brush, Color, SharedString, SharedVector};
103
104#[cfg(test)]
105mod tests;