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