1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use freya_engine::prelude::{
    Data,
    Image,
};
use freya_native_core::prelude::NodeImmutable;
use torin::prelude::Size2D;

use crate::{
    dom::{
        DioxusNode,
        ImagesCache,
    },
    states::{
        ReferencesState,
        StyleState,
        TransformState,
    },
    values::AspectRatio,
};

pub struct ImageData {
    pub image: Image,
    pub size: Size2D,
}

pub fn get_or_create_image(
    node_ref: &DioxusNode,
    area_size: &Size2D,
    images_cache: &mut ImagesCache,
) -> Option<ImageData> {
    let node_style = node_ref.get::<StyleState>().unwrap();
    let node_references = node_ref.get::<ReferencesState>().unwrap();

    let mut get_or_create_image = |bytes: &[u8]| -> Option<Image> {
        if let Some(image_cache_key) = &node_style.image_cache_key {
            images_cache.get(image_cache_key).cloned().or_else(|| {
                Image::from_encoded(unsafe { Data::new_bytes(bytes) }).inspect(|image| {
                    images_cache.insert(image_cache_key.clone(), image.clone());
                })
            })
        } else {
            Image::from_encoded(unsafe { Data::new_bytes(bytes) })
        }
    };

    let image = if let Some(image_ref) = &node_references.image_ref {
        let image_data = image_ref.0.lock().unwrap();
        if let Some(bytes) = image_data.as_ref() {
            get_or_create_image(bytes)
        } else {
            None
        }
    } else if let Some(image_data) = &node_style.image_data {
        get_or_create_image(image_data.as_slice())
    } else {
        None
    }?;

    let node_transform = node_ref.get::<TransformState>().unwrap();

    let image_width = image.width() as f32;
    let image_height = image.height() as f32;

    let width_ratio = area_size.width / image.width() as f32;
    let height_ratio = area_size.height / image.height() as f32;

    let size = match node_transform.aspect_ratio {
        AspectRatio::Max => {
            let ratio = width_ratio.max(height_ratio);

            Size2D::new(image_width * ratio, image_height * ratio)
        }
        AspectRatio::Min => {
            let ratio = width_ratio.min(height_ratio);

            Size2D::new(image_width * ratio, image_height * ratio)
        }
        AspectRatio::Fit => Size2D::new(image_width, image_height),
        AspectRatio::None => *area_size,
    };

    Some(ImageData { image, size })
}