Add support for importing Rust types from another crate Slint compilation (#9329)

To implement an external Slint library, the types and components implemented
in the .slint files needs to be exposed through the Rust crate.
A simple example example/app-library is added to demonstrate how to use this feature.

CC #7060
This commit is contained in:
Benny Sjöstrand 2025-09-16 09:01:44 +02:00 committed by GitHub
parent b23a657c44
commit 0bda0a64eb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
30 changed files with 766 additions and 42 deletions

View file

@ -19,6 +19,7 @@ path = "lib.rs"
[features]
default = []
experimental-module-builds = ["i-slint-compiler/experimental-library-module"]
sdf-fonts = ["i-slint-compiler/sdf-fonts"]
[dependencies]

View file

@ -205,6 +205,26 @@ impl CompilerConfiguration {
Self { config }
}
/// Configures the compiler to treat the Slint as part of a library.
///
/// Use this when the components and types of the Slint code need
/// to be accessible from other modules.
#[cfg(feature = "experimental-module-builds")]
#[must_use]
pub fn as_library(self, library_name: &str) -> Self {
let mut config = self.config;
config.library_name = Some(library_name.to_string());
Self { config }
}
/// Specify the Rust module to place the generated code in.
#[cfg(feature = "experimental-module-builds")]
#[must_use]
pub fn rust_module(self, rust_module: &str) -> Self {
let mut config = self.config;
config.rust_module = Some(rust_module.to_string());
Self { config }
}
/// Configures the compiler to use Signed Distance Field (SDF) encoding for fonts.
///
/// This flag only takes effect when `embed_resources` is set to [`EmbedResourcesKind::EmbedForSoftwareRenderer`],
@ -429,6 +449,18 @@ pub fn compile_with_config(
.with_extension("rs"),
);
#[cfg(feature = "experimental-module-builds")]
if let Some(library_name) = config.config.library_name.clone() {
println!("cargo::metadata=SLINT_LIBRARY_NAME={}", library_name);
println!(
"cargo::metadata=SLINT_LIBRARY_PACKAGE={}",
std::env::var("CARGO_PKG_NAME").ok().unwrap_or_default()
);
println!("cargo::metadata=SLINT_LIBRARY_SOURCE={}", path.display());
if let Some(rust_module) = &config.config.rust_module {
println!("cargo::metadata=SLINT_LIBRARY_MODULE={}", rust_module);
}
}
let paths_dependencies =
compile_with_output_path(path, absolute_rust_output_file_path.clone(), config)?;