ui-egui: share the preview-cache PNG writer

This commit is contained in:
Terra854 committed 2026-11-24 00:33:39 +08:00
1 parent 73bbd6f1e9
commit 01a6704dc6
5 files changed
+36 -2

No files matched your search

Generated
+10
View File
@@ -1009,6 +1009,15 @@ dependencies = [
"web-sys",
]
[[package]]
name = "craft-png"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=c42c34bfa127b095fb0a8d5ccaba0c168c254062#c42c34bfa127b095fb0a8d5ccaba0c168c254062"
dependencies = [
"png",
"thiserror 2.0.21",
]
[[package]]
name = "craft-quit"
version = "0.1.0"
@@ -3002,6 +3011,7 @@ dependencies = [
"craft-dialogs",
"craft-keymap",
"craft-platform",
"craft-png",
"craft-quit",
"craft-ui",
"craft-update",
+1
View File
@@ -109,6 +109,7 @@ craft-window = { git = "https://github.com/storytold/craft-libs", rev = "453b0da
craft-update = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc", features = ["egui"] }
# Atomic persistence, at the suite's current revision
craft-atomic = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
craft-png = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "c42c34bfa127b095fb0a8d5ccaba0c168c254062" }
# Parse the optional shared font catalog at build time; the font repository is not a dependency
craft-fonts-build = { git = "https://github.com/storytold/craft-libs", rev = "453b0da782ccef39be9a6dac8d27c80b553ef1cc" }
# Float image planes and texture sampling for node cooking: storytold/craft-libs, MIT OR Apache-2.0
+1
View File
@@ -25,6 +25,7 @@ zip = { workspace = true }
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
craft-atomic = { workspace = true }
craft-png = { workspace = true }
sha2 = { workspace = true }
sys-locale = "0.3"
craft-update = { workspace = true }
+2 -2
View File
@@ -7,7 +7,7 @@ use std::sync::OnceLock;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, SystemTime};
use image::{ImageDecoder, ImageEncoder};
use image::ImageDecoder;
use sha2::{Digest, Sha256};
use super::preview_cache_settings::{PreviewCacheLimits, PreviewCacheSettings, PreviewCacheState};
@@ -128,7 +128,7 @@ impl PreviewDiskCache {
}
let mut bytes = Vec::new();
image::codecs::png::PngEncoder::new(&mut bytes).write_image(pixels, side, side, image::ExtendedColorType::Rgba8).map_err(io::Error::other)?;
craft_png::write_8bit(&mut bytes, side, side, craft_png::Layout::Rgba, pixels).map_err(io::Error::other)?;
if bytes.len() as u64 > MAX_FILE_BYTES {
return Ok(());
}
@@ -192,6 +192,28 @@ fn cache_budget_evicts_old_files_and_preserves_unrelated_files() {
assert!(!oversized.exists());
}
#[test]
fn encoded_preview_keeps_legacy_bytes_and_budget_boundary() {
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);
let root = tempfile::tempdir().unwrap();
let cache = cache(root.path());
let side = MAX_SIDE;
let pixels: Vec<u8> = (0..side * side * 4).map(|i| (i * 17 + i / 31) as u8).collect();
let mut legacy = Vec::new();
image::ImageEncoder::write_image(image::codecs::png::PngEncoder::new(&mut legacy), &pixels, side, side, image::ExtendedColorType::Rgba8).unwrap();
let bytes = legacy.len() as u64;
let path = cache.path([1; 32], side);
cache.settings.update(PreviewCacheLimits { budget_bytes: bytes - 1, max_entries: 1 }, false);
cache.write(&path, side, &pixels).unwrap();
assert!(!path.exists());
cache.settings.update(PreviewCacheLimits { budget_bytes: bytes, max_entries: 1 }, false);
cache.write(&path, side, &pixels).unwrap();
assert_eq!(fs::read(&path).unwrap(), legacy);
assert_eq!(cache.read(&path, side), Some(pixels));
}
#[test]
fn unavailable_or_busy_cache_still_returns_rendered_pixels() {
let _serial = TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner);