Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slintpad fixes #6575

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/compiler/embedded_resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ pub struct BitmapFont {

#[derive(Debug, Clone)]
pub enum EmbeddedResourcesKind {
/// Only List the resource, co not actually embed it
ListOnly,
/// Just put the file content as a resource
RawData,
/// The data has been processed in a texture
Expand Down
1 change: 1 addition & 0 deletions internal/compiler/generator/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ fn embed_resource(
declarations: &mut Vec<Declaration>,
) {
match &resource.kind {
crate::embedded_resources::EmbeddedResourcesKind::ListOnly => {}
crate::embedded_resources::EmbeddedResourcesKind::RawData => {
let resource_file = crate::fileaccess::load_file(std::path::Path::new(path)).unwrap(); // embedding pass ensured that the file exists
let data = resource_file.read();
Expand Down
3 changes: 3 additions & 0 deletions internal/compiler/generator/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2967,6 +2967,9 @@ fn generate_resources(doc: &Document) -> Vec<TokenStream> {
.map(|(path, er)| {
let symbol = format_ident!("SLINT_EMBEDDED_RESOURCE_{}", er.id);
match &er.kind {
&crate::embedded_resources::EmbeddedResourcesKind::ListOnly => {
quote!()
},
crate::embedded_resources::EmbeddedResourcesKind::RawData => {
let data = embedded_file_tokens(path);
quote!(static #symbol: &'static [u8] = #data;)
Expand Down
10 changes: 7 additions & 3 deletions internal/compiler/passes/embed_images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn embed_images_from_expression(

fn embed_image(
global_embedded_resources: &RefCell<HashMap<String, EmbeddedResources>>,
_embed_files: EmbedResourcesKind,
embed_files: EmbedResourcesKind,
path: &str,
_scale_factor: f64,
diag: &mut BuildDiagnostics,
Expand All @@ -135,11 +135,15 @@ fn embed_image(
std::collections::hash_map::Entry::Occupied(e) => e.into_mut(),
std::collections::hash_map::Entry::Vacant(e) => {
// Check that the file exists, so that later we can unwrap safely in the generators, etc.
if let Some(_file) = crate::fileaccess::load_file(std::path::Path::new(path)) {
if embed_files == EmbedResourcesKind::ListAllResources {
// Really do nothing with the image!
e.insert(EmbeddedResources { id: maybe_id, kind: EmbeddedResourcesKind::ListOnly });
return ImageReference::None;
} else if let Some(_file) = crate::fileaccess::load_file(std::path::Path::new(path)) {
#[allow(unused_mut)]
let mut kind = EmbeddedResourcesKind::RawData;
#[cfg(feature = "software-renderer")]
if _embed_files == EmbedResourcesKind::EmbedTextures {
if embed_files == EmbedResourcesKind::EmbedTextures {
match load_image(_file, _scale_factor) {
Ok((img, source_format, original_size)) => {
kind = EmbeddedResourcesKind::TextureData(generate_texture(
Expand Down
19 changes: 7 additions & 12 deletions internal/compiler/typeloader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1028,17 +1028,6 @@ impl TypeLoader {
}
}

/// Read a file, taking the `CompilerConfiguration`s `open_import_fallback`
/// into account when necessary.
async fn read_file(&self, path: &Path) -> Result<String, std::io::Error> {
if let Some(fallback) = self.compiler_config.open_import_fallback.clone() {
let result = fallback(path.to_string_lossy().into()).await;
result.unwrap_or_else(|| std::fs::read_to_string(path))
} else {
std::fs::read_to_string(path)
}
}

async fn ensure_document_loaded<'a: 'b, 'b>(
state: &'a RefCell<BorrowedTypeLoader<'a>>,
file_to_import: &'b str,
Expand Down Expand Up @@ -1121,7 +1110,13 @@ impl TypeLoader {
.expect("internal error: embedded file is not UTF-8 source code"),
))
} else {
state.borrow().tl.read_file(&path_canon).await
let fallback = state.borrow().tl.compiler_config.open_import_fallback.clone();
if let Some(fallback) = fallback {
let result = fallback(path_canon.to_string_lossy().into()).await;
result.unwrap_or_else(|| std::fs::read_to_string(&path_canon))
} else {
std::fs::read_to_string(&path_canon)
}
};

let ok = match source_code_result {
Expand Down
Loading