#![doc = include_str!("README.md")]
#![doc(html_logo_url = "https://slint-ui.com/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::rc::Rc;
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 typeloader;
pub mod typeregister;
pub mod passes;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EmbedResourcesKind {
OnlyBuiltinResources,
EmbedAllResources,
#[cfg(feature = "software-renderer")]
EmbedTextures,
}
#[derive(Clone)]
pub struct CompilerConfiguration {
pub embed_resources: EmbedResourcesKind,
pub include_paths: Vec<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 inline_all_elements: bool,
pub scale_factor: f64,
pub accessibility: bool,
}
impl CompilerConfiguration {
pub fn new(output_format: crate::generator::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::OnlyBuiltinResources,
false => EmbedResourcesKind::EmbedAllResources,
}
} else {
match output_format {
#[cfg(feature = "rust")]
crate::generator::OutputFormat::Rust => EmbedResourcesKind::EmbedAllResources,
_ => 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 == crate::generator::OutputFormat::Interpreter,
};
let scale_factor = std::env::var("SLINT_SCALE_FACTOR")
.ok()
.and_then(|x| x.parse::<f64>().ok())
.filter(|f| *f > 0.)
.unwrap_or(1.);
Self {
embed_resources,
include_paths: Default::default(),
style: Default::default(),
open_import_fallback: Default::default(),
inline_all_elements,
scale_factor,
accessibility: true,
}
}
}
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) {
#[cfg(feature = "software-renderer")]
if compiler_config.embed_resources == EmbedResourcesKind::EmbedTextures {
compiler_config.accessibility = false;
}
let global_type_registry = typeregister::TypeRegister::builtin();
let type_registry =
Rc::new(RefCell::new(typeregister::TypeRegister::new(&global_type_registry)));
let doc_node: parser::syntax_nodes::Document = doc_node.into();
let mut loader = typeloader::TypeLoader::new(
global_type_registry,
compiler_config.clone(),
&mut diagnostics,
);
if diagnostics.has_error() {
return (crate::object_tree::Document::default(), diagnostics);
}
let (foreign_imports, reexports) =
loader.load_dependencies_recursively(&doc_node, &mut diagnostics, &type_registry).await;
let doc = crate::object_tree::Document::from_node(
doc_node,
foreign_imports,
reexports,
&mut diagnostics,
&type_registry,
);
if let Some((_, node)) = &*doc.root_component.child_insertion_point.borrow() {
diagnostics
.push_error("@children placeholder not allowed in the final component".into(), node)
}
if !diagnostics.has_error() {
passes::run_passes(&doc, &mut diagnostics, &mut loader, &compiler_config).await;
}
diagnostics.all_loaded_files = loader.all_files().cloned().collect();
(doc, diagnostics)
}