image.rs

 1use crate::{
 2    geometry::{rect::RectF, vector::Vector2F},
 3    json::{json, ToJson},
 4    scene, DebugContext, Element, Event, EventContext, ImageData, LayoutContext, PaintContext,
 5    SizeConstraint,
 6};
 7use std::sync::Arc;
 8
 9pub struct Image(Arc<ImageData>);
10
11impl Image {
12    pub fn new(data: Arc<ImageData>) -> Self {
13        Self(data)
14    }
15}
16
17impl Element for Image {
18    type LayoutState = ();
19    type PaintState = ();
20
21    fn layout(
22        &mut self,
23        constraint: SizeConstraint,
24        _: &mut LayoutContext,
25    ) -> (Vector2F, Self::LayoutState) {
26        (constraint.max, ())
27    }
28
29    fn paint(
30        &mut self,
31        bounds: RectF,
32        _: RectF,
33        _: &mut Self::LayoutState,
34        cx: &mut PaintContext,
35    ) -> Self::PaintState {
36        cx.scene.push_image(scene::Image {
37            bounds,
38            data: self.0.clone(),
39        });
40    }
41
42    fn dispatch_event(
43        &mut self,
44        _: &Event,
45        _: RectF,
46        _: &mut Self::LayoutState,
47        _: &mut Self::PaintState,
48        _: &mut EventContext,
49    ) -> bool {
50        false
51    }
52
53    fn debug(
54        &self,
55        bounds: RectF,
56        _: &Self::LayoutState,
57        _: &Self::PaintState,
58        _: &DebugContext,
59    ) -> serde_json::Value {
60        json!({
61            "type": "Image",
62            "bounds": bounds.to_json(),
63        })
64    }
65}