freya_core/render/utils/
image.rs1use freya_engine::prelude::{
2 Data,
3 Image,
4};
5use freya_native_core::prelude::NodeImmutable;
6use torin::prelude::Size2D;
7
8use crate::{
9 dom::{
10 DioxusNode,
11 ImagesCache,
12 },
13 states::ImageState,
14 values::AspectRatio,
15};
16
17pub struct ImageData {
18 pub image: Image,
19 pub size: Size2D,
20}
21
22pub fn get_or_create_image(
23 node_ref: &DioxusNode,
24 area_size: &Size2D,
25 images_cache: &mut ImagesCache,
26) -> Option<ImageData> {
27 let image_state = node_ref.get::<ImageState>().unwrap();
28
29 let mut get_or_create_image = |bytes: &[u8]| -> Option<Image> {
30 if let Some(image_cache_key) = &image_state.image_cache_key {
31 images_cache.get(image_cache_key).cloned().or_else(|| {
32 Image::from_encoded(unsafe { Data::new_bytes(bytes) }).inspect(|image| {
33 images_cache.insert(image_cache_key.clone(), image.clone());
34 })
35 })
36 } else {
37 Image::from_encoded(unsafe { Data::new_bytes(bytes) })
38 }
39 };
40
41 let image = if let Some(image_ref) = &image_state.image_ref {
42 let image_data = image_ref.0.lock().unwrap();
43 if let Some(bytes) = image_data.as_ref() {
44 get_or_create_image(bytes)
45 } else {
46 None
47 }
48 } else if let Some(image_data) = &image_state.image_data {
49 get_or_create_image(image_data.as_slice())
50 } else {
51 None
52 }?;
53
54 let image_width = image.width() as f32;
55 let image_height = image.height() as f32;
56
57 let width_ratio = area_size.width / image.width() as f32;
58 let height_ratio = area_size.height / image.height() as f32;
59
60 let size = match image_state.aspect_ratio {
61 AspectRatio::Max => {
62 let ratio = width_ratio.max(height_ratio);
63
64 Size2D::new(image_width * ratio, image_height * ratio)
65 }
66 AspectRatio::Min => {
67 let ratio = width_ratio.min(height_ratio);
68
69 Size2D::new(image_width * ratio, image_height * ratio)
70 }
71 AspectRatio::Fit => Size2D::new(image_width, image_height),
72 AspectRatio::None => *area_size,
73 };
74
75 Some(ImageData { image, size })
76}