slint/internal/interpreter/lib.rs
Olivier Goffart a7130fbdfb
Some checks are pending
autofix.ci / format_fix (push) Waiting to run
autofix.ci / lint_typecheck (push) Waiting to run
CI / python_test (windows-2022) (push) Blocked by required conditions
CI / mcu (pico-st7789, thumbv6m-none-eabi) (push) Blocked by required conditions
CI / files-changed (push) Waiting to run
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, macos-14, stable) (push) Blocked by required conditions
CI / mcu (pico2-st7789, thumbv8m.main-none-eabihf) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, windows-2022, 1.88) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, windows-2022, beta) (push) Blocked by required conditions
CI / build_and_test (--exclude ffmpeg --exclude gstreamer-player, windows-2022, stable) (push) Blocked by required conditions
CI / build_and_test (ubuntu-22.04, 1.88) (push) Blocked by required conditions
CI / build_and_test (ubuntu-22.04, nightly) (push) Blocked by required conditions
CI / node_test (macos-14) (push) Blocked by required conditions
CI / node_test (ubuntu-22.04) (push) Blocked by required conditions
CI / node_test (windows-2022) (push) Blocked by required conditions
CI / python_test (macos-14) (push) Blocked by required conditions
CI / python_test (ubuntu-22.04) (push) Blocked by required conditions
CI / cpp_test_driver (macos-14) (push) Blocked by required conditions
CI / cpp_test_driver (ubuntu-22.04) (push) Blocked by required conditions
CI / cpp_test_driver (windows-2022) (push) Blocked by required conditions
CI / cpp_cmake (macos-14, 1.88) (push) Blocked by required conditions
CI / cpp_cmake (ubuntu-22.04, stable) (push) Blocked by required conditions
CI / cpp_cmake (windows-2022, nightly) (push) Blocked by required conditions
CI / cpp_package_test (push) Blocked by required conditions
CI / vsce_build_test (push) Blocked by required conditions
CI / material-components (push) Blocked by required conditions
CI / mcu (stm32h735g, thumbv7em-none-eabihf) (push) Blocked by required conditions
CI / mcu-embassy (push) Blocked by required conditions
CI / ffi_32bit_build (push) Blocked by required conditions
CI / docs (push) Blocked by required conditions
CI / wasm (push) Blocked by required conditions
CI / wasm_demo (push) Blocked by required conditions
CI / tree-sitter (push) Blocked by required conditions
CI / android (push) Blocked by required conditions
CI / miri (push) Blocked by required conditions
CI / test-figma-inspector (push) Blocked by required conditions
CI / updater_test (0.3.0) (push) Blocked by required conditions
CI / fmt_test (push) Blocked by required conditions
CI / esp-idf-quick (push) Blocked by required conditions
Remove doc_auto_cfg feature
Rust nightly removed this:

```
error[E0557]: feature has been removed
 --> internal/interpreter/lib.rs:4:38
  |
4 | #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
  |                                      ^^^^^^^^^^^^ feature has been removed
  |
  = note: removed in CURRENT_RUSTC_VERSION; see <https://github.com/rust-lang/rust/pull/138907> for more information
  = note: merged into `doc_cfg`

```
2025-10-08 09:22:35 +02:00

107 lines
3.5 KiB
Rust

// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
#![cfg_attr(docsrs, feature(doc_cfg))]
/*!
# Slint interpreter library
With this crate, you can load a .slint file at runtime and show its UI.
You only need to use this crate if you do not want to use pre-compiled .slint
code, which is the normal way to use Slint, using the `slint` crate
The entry point for this crate is the [`Compiler`] type, which you can
use to create [`CompilationResult`] with the [`Compiler::build_from_source`] or [`Compiler::build_from_path`]
functions. [`CompilationResult`] provides access to all components declared for export. Obtain a [`ComponentDefinition`]
for each and use [`ComponentDefinition::create()`] to instantiate a component. The returned [`ComponentInstance`]
in turn provides access to properties, callbacks, functions, global singletons, as well as implementing [`ComponentHandle`].
### Note about `async` functions
Compiling a component is `async` but in practice, this is only asynchronous if [`Compiler::set_file_loader`]
is set and its future is actually asynchronous. If that is not used, then it is fine to use a very simple
executor, such as the one provided by the `spin_on` crate
## Examples
This example loads a `.slint` dynamically from a path and show errors if any:
```rust
use slint_interpreter::{ComponentDefinition, Compiler, ComponentHandle};
let compiler = Compiler::default();
let result = spin_on::spin_on(compiler.build_from_path("hello.slint"));
let diagnostics : Vec<_> = result.diagnostics().collect();
# #[cfg(feature="print_diagnostics")]
diagnostics.print();
if let Some(definition) = result.component("Foo") {
let instance = definition.create().unwrap();
instance.run().unwrap();
}
```
This example load a `.slint` from a string and set some properties:
```rust
# i_slint_backend_testing::init_no_event_loop();
use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString, ComponentHandle};
let code = r#"
export component MyWin inherits Window {
in property <string> my_name;
Text {
text: "Hello, " + my_name;
}
}
"#;
let mut compiler = Compiler::default();
let result =
spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
assert_eq!(result.diagnostics().count(), 0);
let definition = result.component("MyWin");
let instance = definition.unwrap().create().unwrap();
instance.set_property("my_name", Value::from(SharedString::from("World"))).unwrap();
# return; // we don't want to call run in the tests
instance.run().unwrap();
```
*/
//! ## Feature flags
#![cfg_attr(feature = "document-features", doc = document_features::document_features!())]
#![warn(missing_docs)]
#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
#[cfg(not(feature = "compat-1-2"))]
compile_error!(
"The feature `compat-1-2` must be enabled to ensure \
forward compatibility with future version of this crate"
);
mod api;
mod dynamic_item_tree;
mod dynamic_type;
mod eval;
mod eval_layout;
mod global_component;
#[cfg(feature = "internal-highlight")]
pub mod highlight;
#[cfg(feature = "internal-json")]
pub mod json;
#[cfg(feature = "internal-live-preview")]
pub mod live_preview;
mod value_model;
#[doc(inline)]
pub use api::*;
#[cfg(feature = "internal")]
#[doc(hidden)]
pub use eval::default_value_for_type;
/// (Re-export from corelib.)
#[doc(inline)]
pub use i_slint_core::{Brush, Color, SharedString, SharedVector};
#[cfg(test)]
mod tests;