1#![doc = include_str!("README.md")]
5#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
6#![deny(unsafe_code)]
8
9#[cfg(feature = "proc_macro_span")]
10extern crate proc_macro;
11
12use core::future::Future;
13use core::pin::Pin;
14use std::cell::RefCell;
15use std::collections::HashMap;
16use std::rc::Rc;
17#[cfg(feature = "software-renderer")]
18use std::sync::Arc;
19
20pub mod builtin_macros;
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, Default)]
64#[non_exhaustive]
65pub enum ComponentSelection {
66 #[default]
72 ExportedWindows,
73
74 LastExported,
79
80 Named(String),
82}
83
84#[cfg(feature = "software-renderer")]
85pub type FontCache = Rc<
86 RefCell<
87 std::collections::HashMap<
88 (i_slint_common::sharedfontique::fontique::FamilyId, usize),
89 fontdue::FontResult<Arc<fontdue::Font>>,
90 >,
91 >,
92>;
93
94pub type OpenImportFallback =
95 Rc<dyn Fn(String) -> Pin<Box<dyn Future<Output = Option<std::io::Result<String>>>>>>;
96pub type ResourceUrlMapper = Rc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = Option<String>>>>>;
97
98#[derive(Clone)]
100pub struct CompilerConfiguration {
101 pub embed_resources: EmbedResourcesKind,
104 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
106 pub use_sdf_fonts: bool,
107 pub include_paths: Vec<std::path::PathBuf>,
109 pub library_paths: HashMap<String, std::path::PathBuf>,
111 pub style: Option<String>,
113
114 pub open_import_fallback: Option<OpenImportFallback>,
119 pub resource_url_mapper: Option<ResourceUrlMapper>,
123
124 pub inline_all_elements: bool,
129
130 pub const_scale_factor: f64,
133
134 pub accessibility: bool,
136
137 pub enable_experimental: bool,
139
140 pub translation_domain: Option<String>,
142 #[cfg(feature = "bundle-translations")]
144 pub translation_path_bundle: Option<std::path::PathBuf>,
145
146 pub no_native_menu: bool,
148
149 pub cpp_namespace: Option<String>,
151
152 pub error_on_binding_loop_with_window_layout: bool,
155
156 pub debug_info: bool,
158
159 pub debug_hooks: Option<std::hash::RandomState>,
161
162 pub components_to_generate: ComponentSelection,
163
164 #[cfg(feature = "software-renderer")]
165 pub font_cache: FontCache,
166
167 pub library_name: Option<String>,
169
170 pub rust_module: Option<String>,
172}
173
174impl CompilerConfiguration {
175 pub fn new(output_format: OutputFormat) -> Self {
176 let embed_resources = if std::env::var_os("SLINT_EMBED_TEXTURES").is_some()
177 || std::env::var_os("DEP_MCU_BOARD_SUPPORT_MCU_EMBED_TEXTURES").is_some()
178 {
179 #[cfg(not(feature = "software-renderer"))]
180 panic!("the software-renderer feature must be enabled in i-slint-compiler when embedding textures");
181 #[cfg(feature = "software-renderer")]
182 EmbedResourcesKind::EmbedTextures
183 } else if let Ok(var) = std::env::var("SLINT_EMBED_RESOURCES") {
184 let var = var.parse::<bool>().unwrap_or_else(|_|{
185 panic!("SLINT_EMBED_RESOURCES has incorrect value. Must be either unset, 'true' or 'false'")
186 });
187 match var {
188 true => EmbedResourcesKind::EmbedAllResources,
189 false => EmbedResourcesKind::OnlyBuiltinResources,
190 }
191 } else {
192 match output_format {
193 #[cfg(feature = "rust")]
194 OutputFormat::Rust => EmbedResourcesKind::EmbedAllResources,
195 OutputFormat::Interpreter => EmbedResourcesKind::Nothing,
196 _ => EmbedResourcesKind::OnlyBuiltinResources,
197 }
198 };
199
200 let inline_all_elements = match std::env::var("SLINT_INLINING") {
201 Ok(var) => var.parse::<bool>().unwrap_or_else(|_| {
202 panic!(
203 "SLINT_INLINING has incorrect value. Must be either unset, 'true' or 'false'"
204 )
205 }),
206 Err(_) => output_format == OutputFormat::Interpreter,
208 };
209
210 let const_scale_factor = std::env::var("SLINT_SCALE_FACTOR")
211 .ok()
212 .and_then(|x| x.parse::<f64>().ok())
213 .filter(|f| *f > 0.)
214 .unwrap_or(1.);
215
216 let enable_experimental = std::env::var_os("SLINT_ENABLE_EXPERIMENTAL_FEATURES").is_some();
217
218 let debug_info = std::env::var_os("SLINT_EMIT_DEBUG_INFO").is_some();
219
220 let cpp_namespace = match output_format {
221 #[cfg(feature = "cpp")]
222 OutputFormat::Cpp(config) => match config.namespace {
223 Some(namespace) => Some(namespace),
224 None => match std::env::var("SLINT_CPP_NAMESPACE") {
225 Ok(namespace) => Some(namespace),
226 Err(_) => None,
227 },
228 },
229 _ => None,
230 };
231
232 Self {
233 embed_resources,
234 include_paths: Default::default(),
235 library_paths: Default::default(),
236 style: Default::default(),
237 open_import_fallback: None,
238 resource_url_mapper: None,
239 inline_all_elements,
240 const_scale_factor,
241 accessibility: true,
242 enable_experimental,
243 translation_domain: None,
244 no_native_menu: false,
245 cpp_namespace,
246 error_on_binding_loop_with_window_layout: false,
247 debug_info,
248 debug_hooks: None,
249 components_to_generate: ComponentSelection::ExportedWindows,
250 #[cfg(feature = "software-renderer")]
251 font_cache: Default::default(),
252 #[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
253 use_sdf_fonts: false,
254 #[cfg(feature = "bundle-translations")]
255 translation_path_bundle: std::env::var("SLINT_BUNDLE_TRANSLATIONS")
256 .ok()
257 .map(|x| x.into()),
258 library_name: None,
259 rust_module: None,
260 }
261 }
262
263 #[cfg(feature = "software-renderer")]
264 fn load_font_by_id(
265 &self,
266 font: &i_slint_common::sharedfontique::fontique::QueryFont,
267 ) -> fontdue::FontResult<Arc<fontdue::Font>> {
268 self.font_cache
269 .borrow_mut()
270 .entry(font.family)
271 .or_insert_with(|| {
272 fontdue::Font::from_bytes(
273 font.blob.data(),
274 fontdue::FontSettings {
275 collection_index: font.index,
276 scale: 40.,
277 ..Default::default()
278 },
279 )
280 .map(Arc::new)
281 })
282 .clone()
283 }
284}
285
286fn prepare_for_compile(
287 diagnostics: &mut diagnostics::BuildDiagnostics,
288 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
289) -> typeloader::TypeLoader {
290 #[cfg(feature = "software-renderer")]
291 if compiler_config.embed_resources == EmbedResourcesKind::EmbedTextures {
292 compiler_config.accessibility = false;
295 }
296
297 diagnostics.enable_experimental = compiler_config.enable_experimental;
298
299 let global_type_registry = if compiler_config.enable_experimental {
300 crate::typeregister::TypeRegister::builtin_experimental()
301 } else {
302 crate::typeregister::TypeRegister::builtin()
303 };
304
305 typeloader::TypeLoader::new(global_type_registry, compiler_config, diagnostics)
306}
307
308pub async fn compile_syntax_node(
309 doc_node: parser::SyntaxNode,
310 mut diagnostics: diagnostics::BuildDiagnostics,
311 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
312) -> (object_tree::Document, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
313 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
314
315 let doc_node: parser::syntax_nodes::Document = doc_node.into();
316
317 let type_registry =
318 Rc::new(RefCell::new(typeregister::TypeRegister::new(&loader.global_type_registry)));
319 let (foreign_imports, reexports) =
320 loader.load_dependencies_recursively(&doc_node, &mut diagnostics, &type_registry).await;
321
322 let mut doc = crate::object_tree::Document::from_node(
323 doc_node,
324 foreign_imports,
325 reexports,
326 &mut diagnostics,
327 &type_registry,
328 );
329
330 if !diagnostics.has_errors() {
331 passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics).await;
332 } else {
333 passes::run_import_passes(&doc, &loader, &mut diagnostics);
335 }
336 (doc, diagnostics, loader)
337}
338
339pub async fn load_root_file(
345 path: &Path,
346 source_path: &Path,
347 source_code: String,
348 mut diagnostics: diagnostics::BuildDiagnostics,
349 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
350) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
351 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
352
353 let (path, _) =
354 loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;
355
356 (path, diagnostics, loader)
357}
358
359pub async fn load_root_file_with_raw_type_loader(
366 path: &Path,
367 source_path: &Path,
368 source_code: String,
369 mut diagnostics: diagnostics::BuildDiagnostics,
370 #[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
371) -> (
372 std::path::PathBuf,
373 diagnostics::BuildDiagnostics,
374 typeloader::TypeLoader,
375 Option<typeloader::TypeLoader>,
376) {
377 let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
378
379 let (path, raw_type_loader) =
380 loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;
381
382 (path, diagnostics, loader, raw_type_loader)
383}