1#![doc = include_str!("README.md")]
5#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
6#![cfg_attr(docsrs, feature(doc_cfg))]
7#![deny(unsafe_code)]
9
10#[cfg(feature = "proc_macro_span")]
11extern crate proc_macro;
12
13use core::future::Future;
14use core::pin::Pin;
15use std::cell::RefCell;
16use std::collections::HashMap;
17use std::rc::Rc;
18
19pub mod builtin_macros;
20pub mod data_uri;
21pub mod diagnostics;
22pub mod embedded_resources;
23pub mod expression_tree;
24pub mod fileaccess;
25pub mod generator;
26pub mod langtype;
27pub mod layout;
28pub mod lexer;
29pub mod literals;
30pub mod llr;
31pub(crate) mod load_builtins;
32pub mod lookup;
33pub mod namedreference;
34pub mod object_tree;
35pub mod parser;
36pub mod pathutils;
37#[cfg(feature = "bundle-translations")]
38pub mod translations;
39pub mod typeloader;
40pub mod typeregister;
41
42pub mod passes;
43
44use crate::generator::OutputFormat;
45use std::path::Path;
46
47#[derive(Clone, Copy, Debug, Eq, PartialEq)]
49pub enum EmbedResourcesKind {
50 Nothing,
52 OnlyBuiltinResources,
54 ListAllResources,
56 EmbedAllResources,
58 #[cfg(feature = "software-renderer")]
59 EmbedTextures,
61}
62
63#[derive(Clone, Debug, Eq, PartialEq)]
66#[non_exhaustive]
67pub enum DefaultTranslationContext {
68 ComponentName,
72 None,
77}
78
79#[derive(Clone, Debug, Eq, PartialEq, Default)]
80#[non_exhaustive]
81pub enum ComponentSelection {
82 #[default]
88 ExportedWindows,
89
90 LastExported,
95
96 Named(String),
98}
99
100pub type OpenImportCallback =
110 Rc<dyn Fn(String) -> Pin<Box<dyn Future<Output = Option<std::io::Result<String>>>>>>;
111pub type ResourceUrlMapper = Rc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = Option<String>>>>>;
112
113#[derive(Clone)]
115pub struct CompilerConfiguration {
116 pub embed_resources: EmbedResourcesKind,
119 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
121 pub use_sdf_fonts: bool,
122 pub include_paths: Vec<std::path::PathBuf>,
124 pub library_paths: HashMap<String, std::path::PathBuf>,
126 pub style: Option<String>,
128
129 pub open_import_callback: Option<OpenImportCallback>,
134 pub resource_url_mapper: Option<ResourceUrlMapper>,
138
139 pub inline_all_elements: bool,
144
145 pub const_scale_factor: Option<f32>,
148
149 pub accessibility: bool,
151
152 pub enable_experimental: bool,
154
155 pub translation_domain: Option<String>,
157 #[cfg(feature = "bundle-translations")]
159 pub translation_path_bundle: Option<std::path::PathBuf>,
160 pub default_translation_context: DefaultTranslationContext,
162
163 pub no_native_menu: bool,
165
166 pub cpp_namespace: Option<String>,
168
169 pub error_on_binding_loop_with_window_layout: bool,
172
173 pub debug_info: bool,
175
176 pub debug_hooks: Option<std::hash::RandomState>,
178
179 pub components_to_generate: ComponentSelection,
180
181 pub library_name: Option<String>,
183
184 pub rust_module: Option<String>,
186}
187
188impl CompilerConfiguration {
189 pub fn new(output_format: OutputFormat) -> Self {
190 let embed_resources = if std::env::var_os("SLINT_EMBED_TEXTURES").is_some()
191 || std::env::var_os("DEP_MCU_BOARD_SUPPORT_MCU_EMBED_TEXTURES").is_some()
192 {
193 #[cfg(not(feature = "software-renderer"))]
194 panic!(
195 "the software-renderer feature must be enabled in i-slint-compiler when embedding textures"
196 );
197 #[cfg(feature = "software-renderer")]
198 EmbedResourcesKind::EmbedTextures
199 } else if let Ok(var) = std::env::var("SLINT_EMBED_RESOURCES") {
200 let var = var.parse::<bool>().unwrap_or_else(|_|{
201 panic!("SLINT_EMBED_RESOURCES has incorrect value. Must be either unset, 'true' or 'false'")
202 });
203 match var {
204 true => EmbedResourcesKind::EmbedAllResources,
205 false => EmbedResourcesKind::OnlyBuiltinResources,
206 }
207 } else {
208 match output_format {
209 #[cfg(feature = "rust")]
210 OutputFormat::Rust => EmbedResourcesKind::EmbedAllResources,
211 OutputFormat::Interpreter => EmbedResourcesKind::Nothing,
212 _ => EmbedResourcesKind::OnlyBuiltinResources,
213 }
214 };
215
216 let inline_all_elements = match std::env::var("SLINT_INLINING") {
217 Ok(var) => var.parse::<bool>().unwrap_or_else(|_| {
218 panic!(
219 "SLINT_INLINING has incorrect value. Must be either unset, 'true' or 'false'"
220 )
221 }),
222 Err(_) => output_format == OutputFormat::Interpreter,
224 };
225
226 let const_scale_factor = std::env::var("SLINT_SCALE_FACTOR")
227 .ok()
228 .and_then(|x| x.parse::<f32>().ok())
229 .filter(|f| *f > 0.);
230
231 let enable_experimental = std::env::var_os("SLINT_ENABLE_EXPERIMENTAL_FEATURES").is_some();
232
233 let debug_info = std::env::var_os("SLINT_EMIT_DEBUG_INFO").is_some();
234
235 let cpp_namespace = match output_format {
236 #[cfg(feature = "cpp")]
237 OutputFormat::Cpp(config) => match config.namespace {
238 Some(namespace) => Some(namespace),
239 None => std::env::var("SLINT_CPP_NAMESPACE").ok(),
240 },
241 _ => None,
242 };
243
244 let style = std::env::var("SLINT_STYLE").ok();
245
246 Self {
247 embed_resources,
248 include_paths: Default::default(),
249 library_paths: Default::default(),
250 style,
251 open_import_callback: None,
252 resource_url_mapper: None,
253 inline_all_elements,
254 const_scale_factor,
255 accessibility: true,
256 enable_experimental,
257 translation_domain: None,
258 default_translation_context: DefaultTranslationContext::ComponentName,
259 no_native_menu: false,
260 cpp_namespace,
261 error_on_binding_loop_with_window_layout: false,
262 debug_info,
263 debug_hooks: None,
264 components_to_generate: ComponentSelection::ExportedWindows,
265 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
266 use_sdf_fonts: false,
267 #[cfg(feature = "bundle-translations")]
268 translation_path_bundle: std::env::var("SLINT_BUNDLE_TRANSLATIONS")
269 .ok()
270 .map(|x| x.into()),
271 library_name: None,
272 rust_module: None,
273 }
274 }
275}
276
277fn prepare_for_compile(
281 diagnostics: &mut diagnostics::BuildDiagnostics,
282 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
283) -> typeloader::TypeLoader {
284 #[cfg(feature = "software-renderer")]
285 if compiler_config.embed_resources == EmbedResourcesKind::EmbedTextures {
286 compiler_config.accessibility = false;
289 }
290
291 diagnostics.enable_experimental = compiler_config.enable_experimental;
292
293 typeloader::TypeLoader::new(compiler_config, diagnostics)
294}
295
296pub async fn compile_syntax_node(
297 doc_node: parser::SyntaxNode,
298 mut diagnostics: diagnostics::BuildDiagnostics,
299 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
300) -> (object_tree::Document, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
301 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
302
303 let doc_node: parser::syntax_nodes::Document = doc_node.into();
304
305 let type_registry =
306 Rc::new(RefCell::new(typeregister::TypeRegister::new(&loader.global_type_registry)));
307 let (foreign_imports, reexports) =
308 loader.load_dependencies_recursively(&doc_node, &mut diagnostics, &type_registry).await;
309
310 let mut doc = crate::object_tree::Document::from_node(
311 doc_node,
312 foreign_imports,
313 reexports,
314 &mut diagnostics,
315 &type_registry,
316 );
317
318 if !diagnostics.has_errors() {
319 passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics).await;
320 } else {
321 passes::run_import_passes(&doc, &loader, &mut diagnostics);
323 }
324 (doc, diagnostics, loader)
325}
326
327pub async fn load_root_file(
333 path: &Path,
334 source_path: &Path,
335 source_code: String,
336 mut diagnostics: diagnostics::BuildDiagnostics,
337 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
338) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
339 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
340
341 let (path, _) =
342 loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;
343
344 (path, diagnostics, loader)
345}
346
347pub async fn load_root_file_with_raw_type_loader(
354 path: &Path,
355 source_path: &Path,
356 source_code: String,
357 mut diagnostics: diagnostics::BuildDiagnostics,
358 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
359) -> (
360 std::path::PathBuf,
361 diagnostics::BuildDiagnostics,
362 typeloader::TypeLoader,
363 Option<typeloader::TypeLoader>,
364) {
365 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
366
367 let (path, raw_type_loader) =
368 loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;
369
370 (path, diagnostics, loader, raw_type_loader)
371}