#![doc = include_str!("README.md")]
#![doc(html_logo_url = "https://slint.dev/logo/slint-logo-square-light.svg")]
#![deny(unsafe_code)]
#[cfg(feature = "proc_macro_span")]
extern crate proc_macro;
use core::future::Future;
use core::pin::Pin;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
#[cfg(feature = "software-renderer")]
use std::sync::Arc;
pub mod builtin_macros;
pub mod diagnostics;
pub mod embedded_resources;
pub mod expression_tree;
pub mod fileaccess;
pub mod generator;
pub mod langtype;
pub mod layout;
pub mod lexer;
pub mod literals;
pub mod llr;
pub(crate) mod load_builtins;
pub mod lookup;
pub mod namedreference;
pub mod object_tree;
pub mod parser;
pub mod pathutils;
#[cfg(feature = "bundle-translations")]
pub mod translations;
pub mod typeloader;
pub mod typeregister;
pub mod passes;
use crate::generator::OutputFormat;
use std::path::Path;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EmbedResourcesKind {
Nothing,
OnlyBuiltinResources,
ListAllResources,
EmbedAllResources,
#[cfg(feature = "software-renderer")]
EmbedTextures,
}
#[derive(Clone, Debug, Eq, PartialEq, Default)]
#[non_exhaustive]
pub enum ComponentSelection {
#[default]
ExportedWindows,
LastExported,
Named(String),
}
#[cfg(feature = "software-renderer")]
pub type FontCache = Rc<
RefCell<
std::collections::HashMap<
i_slint_common::sharedfontdb::fontdb::ID,
fontdue::FontResult<(Arc<fontdue::Font>, Arc<dyn AsRef<[u8]> + Send + Sync>, u32)>,
>,
>,
>;
#[derive(Clone)]
pub struct CompilerConfiguration {
pub embed_resources: EmbedResourcesKind,
#[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
pub use_sdf_fonts: bool,
pub include_paths: Vec<std::path::PathBuf>,
pub library_paths: HashMap<String, std::path::PathBuf>,
pub style: Option<String>,
pub open_import_fallback: Option<
Rc<dyn Fn(String) -> Pin<Box<dyn Future<Output = Option<std::io::Result<String>>>>>>,
>,
pub resource_url_mapper:
Option<Rc<dyn Fn(&str) -> Pin<Box<dyn Future<Output = Option<String>>>>>>,
pub inline_all_elements: bool,
pub const_scale_factor: f64,
pub accessibility: bool,
pub enable_experimental: bool,
pub translation_domain: Option<String>,
#[cfg(feature = "bundle-translations")]
pub translation_path_bundle: Option<std::path::PathBuf>,
pub no_native_menu: bool,
pub cpp_namespace: Option<String>,
pub error_on_binding_loop_with_window_layout: bool,
pub debug_info: bool,
pub debug_hooks: Option<std::hash::RandomState>,
pub components_to_generate: ComponentSelection,
#[cfg(feature = "software-renderer")]
pub font_cache: FontCache,
}
impl CompilerConfiguration {
pub fn new(output_format: OutputFormat) -> Self {
let embed_resources = if std::env::var_os("SLINT_EMBED_TEXTURES").is_some()
|| std::env::var_os("DEP_MCU_BOARD_SUPPORT_MCU_EMBED_TEXTURES").is_some()
{
#[cfg(not(feature = "software-renderer"))]
panic!("the software-renderer feature must be enabled in i-slint-compiler when embedding textures");
#[cfg(feature = "software-renderer")]
EmbedResourcesKind::EmbedTextures
} else if let Ok(var) = std::env::var("SLINT_EMBED_RESOURCES") {
let var = var.parse::<bool>().unwrap_or_else(|_|{
panic!("SLINT_EMBED_RESOURCES has incorrect value. Must be either unset, 'true' or 'false'")
});
match var {
true => EmbedResourcesKind::EmbedAllResources,
false => EmbedResourcesKind::OnlyBuiltinResources,
}
} else {
match output_format {
#[cfg(feature = "rust")]
OutputFormat::Rust => EmbedResourcesKind::EmbedAllResources,
OutputFormat::Interpreter => EmbedResourcesKind::Nothing,
_ => EmbedResourcesKind::OnlyBuiltinResources,
}
};
let inline_all_elements = match std::env::var("SLINT_INLINING") {
Ok(var) => var.parse::<bool>().unwrap_or_else(|_| {
panic!(
"SLINT_INLINING has incorrect value. Must be either unset, 'true' or 'false'"
)
}),
Err(_) => output_format == OutputFormat::Interpreter,
};
let const_scale_factor = std::env::var("SLINT_SCALE_FACTOR")
.ok()
.and_then(|x| x.parse::<f64>().ok())
.filter(|f| *f > 0.)
.unwrap_or(1.);
let enable_experimental = std::env::var_os("SLINT_ENABLE_EXPERIMENTAL_FEATURES").is_some();
let debug_info = std::env::var_os("SLINT_EMIT_DEBUG_INFO").is_some();
let cpp_namespace = match output_format {
#[cfg(feature = "cpp")]
OutputFormat::Cpp(config) => match config.namespace {
Some(namespace) => Some(namespace),
None => match std::env::var("SLINT_CPP_NAMESPACE") {
Ok(namespace) => Some(namespace),
Err(_) => None,
},
},
_ => None,
};
Self {
embed_resources,
include_paths: Default::default(),
library_paths: Default::default(),
style: Default::default(),
open_import_fallback: None,
resource_url_mapper: None,
inline_all_elements,
const_scale_factor,
accessibility: true,
enable_experimental,
translation_domain: None,
no_native_menu: false,
cpp_namespace,
error_on_binding_loop_with_window_layout: false,
debug_info,
debug_hooks: None,
components_to_generate: ComponentSelection::ExportedWindows,
#[cfg(feature = "software-renderer")]
font_cache: Default::default(),
#[cfg(all(feature = "software-renderer", feature = "sdf-fonts"))]
use_sdf_fonts: false,
#[cfg(feature = "bundle-translations")]
translation_path_bundle: std::env::var("SLINT_BUNDLE_TRANSLATIONS")
.ok()
.map(|x| x.into()),
}
}
#[cfg(feature = "software-renderer")]
fn load_font_by_id(
&self,
face_id: i_slint_common::sharedfontdb::fontdb::ID,
) -> fontdue::FontResult<(Arc<fontdue::Font>, Arc<dyn AsRef<[u8]> + Send + Sync>, u32)> {
self.font_cache
.borrow_mut()
.entry(face_id)
.or_insert_with(|| {
i_slint_common::sharedfontdb::FONT_DB.with(|fontdb| {
fontdb
.borrow()
.with_face_data(face_id, |font_data, face_index| {
fontdue::Font::from_bytes(
font_data,
fontdue::FontSettings {
collection_index: face_index,
scale: 40.,
..Default::default()
},
)
.map(|fontdue_font| {
(
Arc::new(fontdue_font),
Arc::new(font_data.to_vec())
as Arc<dyn AsRef<[u8]> + Send + Sync>,
face_index,
)
})
})
.unwrap_or_else(|| fontdue::FontResult::Err("internal error: corrupt font"))
})
})
.clone()
}
}
fn prepare_for_compile(
diagnostics: &mut diagnostics::BuildDiagnostics,
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
) -> typeloader::TypeLoader {
#[cfg(feature = "software-renderer")]
if compiler_config.embed_resources == EmbedResourcesKind::EmbedTextures {
compiler_config.accessibility = false;
}
diagnostics.enable_experimental = compiler_config.enable_experimental;
let global_type_registry = if compiler_config.enable_experimental {
crate::typeregister::TypeRegister::builtin_experimental()
} else {
crate::typeregister::TypeRegister::builtin()
};
typeloader::TypeLoader::new(global_type_registry, compiler_config, diagnostics)
}
pub async fn compile_syntax_node(
doc_node: parser::SyntaxNode,
mut diagnostics: diagnostics::BuildDiagnostics,
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
) -> (object_tree::Document, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
let doc_node: parser::syntax_nodes::Document = doc_node.into();
let type_registry =
Rc::new(RefCell::new(typeregister::TypeRegister::new(&loader.global_type_registry)));
let (foreign_imports, reexports) =
loader.load_dependencies_recursively(&doc_node, &mut diagnostics, &type_registry).await;
let mut doc = crate::object_tree::Document::from_node(
doc_node,
foreign_imports,
reexports,
&mut diagnostics,
&type_registry,
);
if !diagnostics.has_errors() {
passes::run_passes(&mut doc, &mut loader, false, &mut diagnostics).await;
} else {
passes::run_import_passes(&doc, &loader, &mut diagnostics);
}
(doc, diagnostics, loader)
}
pub async fn load_root_file(
path: &Path,
source_path: &Path,
source_code: String,
mut diagnostics: diagnostics::BuildDiagnostics,
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
) -> (std::path::PathBuf, diagnostics::BuildDiagnostics, typeloader::TypeLoader) {
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
let (path, _) =
loader.load_root_file(path, source_path, source_code, false, &mut diagnostics).await;
(path, diagnostics, loader)
}
pub async fn load_root_file_with_raw_type_loader(
path: &Path,
source_path: &Path,
source_code: String,
mut diagnostics: diagnostics::BuildDiagnostics,
#[allow(unused_mut)] mut compiler_config: CompilerConfiguration,
) -> (
std::path::PathBuf,
diagnostics::BuildDiagnostics,
typeloader::TypeLoader,
Option<typeloader::TypeLoader>,
) {
let mut loader = prepare_for_compile(&mut diagnostics, compiler_config);
let (path, raw_type_loader) =
loader.load_root_file(path, source_path, source_code, true, &mut diagnostics).await;
(path, diagnostics, loader, raw_type_loader)
}