Skip to main content

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))]
5#![no_std]
6
7/*!
8# Slint interpreter library
9
10With this crate, you can load a .slint file at runtime and show its UI.
11
12You only need to use this crate if you do not want to use pre-compiled .slint
13code, which is the normal way to use Slint, using the `slint` crate
14
15The entry point for this crate is the [`Compiler`] type, which you can
16use to create [`CompilationResult`] with the [`Compiler::build_from_source`] or [`Compiler::build_from_path`]
17functions. [`CompilationResult`] provides access to all components declared for export. Obtain a [`ComponentDefinition`]
18for each and use [`ComponentDefinition::create()`] to instantiate a component. The returned [`ComponentInstance`]
19in turn provides access to properties, callbacks, functions, global singletons, as well as implementing [`ComponentHandle`].
20
21### Note about `async` functions
22
23Compiling a component is `async` but in practice, this is only asynchronous if [`Compiler::set_file_loader`]
24is set and its future is actually asynchronous.  If that is not used, then it is fine to use a very simple
25executor, such as the one provided by the `spin_on` crate
26
27## Examples
28
29This example loads a `.slint` dynamically from a path and show errors if any:
30
31```rust
32use slint_interpreter::{ComponentDefinition, Compiler, ComponentHandle};
33
34let compiler = Compiler::default();
35let result = spin_on::spin_on(compiler.build_from_path("hello.slint"));
36let diagnostics : Vec<_> = result.diagnostics().collect();
37# #[cfg(feature="print_diagnostics")]
38diagnostics.print();
39if let Some(definition) = result.component("Foo") {
40    let instance = definition.create().unwrap();
41    instance.run().unwrap();
42}
43```
44
45This example load a `.slint` from a string and set some properties:
46
47```rust
48# i_slint_backend_testing::init_no_event_loop();
49use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString, ComponentHandle};
50
51let code = r#"
52    export component MyWin inherits Window {
53        in property <string> my_name;
54        Text {
55            text: "Hello, " + my_name;
56        }
57    }
58"#;
59
60let mut compiler = Compiler::default();
61let result =
62    spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
63assert_eq!(result.diagnostics().count(), 0);
64let definition = result.component("MyWin");
65let instance = definition.unwrap().create().unwrap();
66instance.set_property("my_name", Value::from(SharedString::from("World"))).unwrap();
67# return; // we don't want to call run in the tests
68instance.run().unwrap();
69```
70*/
71//! ## Feature flags
72#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
73#![warn(missing_docs)]
74#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
75
76#[cfg(not(any(feature = "compat-1-18", feature = "internal-mcu-interpreter")))]
77compile_error!(
78    "The feature `compat-1-18` must be enabled to ensure \
79    forward compatibility with future version of this crate"
80);
81
82extern crate alloc;
83#[cfg(feature = "std")]
84#[macro_use]
85extern crate std;
86
87mod prelude {
88    pub use alloc::borrow::ToOwned;
89    pub use alloc::boxed::Box;
90    pub use alloc::format;
91    pub use alloc::rc::Rc;
92    pub use alloc::string::{String, ToString};
93    pub use alloc::vec;
94    pub use alloc::vec::Vec;
95    pub use core::cell::{Cell, OnceCell, RefCell};
96    pub use core::pin::Pin;
97
98    pub use crate::api::Value;
99    pub use crate::instance::Instance;
100    #[cfg(not(feature = "std"))]
101    pub use hashbrown::HashMap;
102    pub use i_slint_core::item_tree::ItemTreeVTable;
103    pub use i_slint_core::{Callback, Property, SlintContext};
104    pub use smol_str::SmolStr;
105    #[cfg(feature = "std")]
106    pub use std::collections::HashMap;
107    pub use vtable::VRc;
108}
109
110mod api;
111mod bindings;
112mod component;
113mod debug_hook;
114mod erased;
115mod eval;
116mod eval_layout;
117#[cfg(feature = "ffi")]
118#[doc(hidden)]
119pub mod ffi;
120mod globals;
121#[cfg(feature = "internal-highlight")]
122pub mod highlight;
123mod instance;
124mod item_holder;
125mod item_registry;
126mod item_tree_vtable;
127#[cfg(feature = "internal-json")]
128pub mod json;
129mod popup;
130mod public_api;
131#[cfg(feature = "internal-mcu-interpreter")]
132mod static_resources;
133mod value_model;
134
135#[doc(inline)]
136pub use api::*;
137
138#[cfg(feature = "internal")]
139#[doc(hidden)]
140pub use eval::{
141    call_runtime_builtin_function, default_value_for_struct_field, default_value_for_type,
142    radial_gradient_brush, radial_gradient_shape_to_string,
143};
144
145#[cfg(test)]
146mod tests;