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