1use std::rc::Rc;
16use std::sync::atomic::{AtomicU32, Ordering};
17
18use crate::id::ElementId;
19use crate::paint::{PaintCommand, PaintList};
20use crate::render_object::{BoxConstraints, LayoutContext, RenderObject};
21use crate::{Color, Point, Rect, Size};
22
23static NEXT_IMAGE_ID: AtomicU32 = AtomicU32::new(1);
25
26pub fn next_image_id() -> u32 {
28 NEXT_IMAGE_ID.fetch_add(1, Ordering::Relaxed)
29}
30
31#[derive(Clone)]
33pub struct ImageData {
34 pub rgba: Vec<u8>,
35 pub width: u32,
36 pub height: u32,
37}
38
39impl ImageData {
40 pub fn solid(width: u32, height: u32, color: Color) -> Self {
42 let mut rgba = Vec::with_capacity((width * height * 4) as usize);
43 for _ in 0..width * height {
44 rgba.extend_from_slice(&[
45 (color.r * 255.0) as u8,
46 (color.g * 255.0) as u8,
47 (color.b * 255.0) as u8,
48 (color.a * 255.0) as u8,
49 ]);
50 }
51 Self { rgba, width, height }
52 }
53
54 pub fn pixel_count(&self) -> u32 {
56 self.width * self.height
57 }
58}
59
60impl std::fmt::Debug for ImageData {
61 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
62 f.debug_struct("ImageData")
63 .field("width", &self.width)
64 .field("height", &self.height)
65 .field("bytes", &self.rgba.len())
66 .finish()
67 }
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
72pub enum BoxFit {
73 #[default]
75 Fill,
76 Contain,
78}
79
80pub struct ImageRenderObject {
86 pub image: Rc<ImageData>,
87 pub texture_id: u32,
88 pub width: Option<f32>,
89 pub height: Option<f32>,
90 pub fit: BoxFit,
91 pub element_id: Option<ElementId>,
92 pub cached_size: Size,
94 pub dst_rect: Rect,
96}
97
98impl ImageRenderObject {
99 pub fn new(image: Rc<ImageData>, texture_id: u32, width: Option<f32>, height: Option<f32>, fit: BoxFit) -> Self {
100 Self {
101 image,
102 texture_id,
103 width,
104 height,
105 fit,
106 element_id: None,
107 cached_size: Size::ZERO,
108 dst_rect: Rect::new(0.0, 0.0, 0.0, 0.0),
109 }
110 }
111}
112
113impl RenderObject for ImageRenderObject {
114 fn layout(&mut self, constraints: BoxConstraints, _ctx: &mut LayoutContext) -> Size {
115 let natural_w = self.image.width as f32;
116 let natural_h = self.image.height as f32;
117 let want = Size::new(self.width.unwrap_or(natural_w), self.height.unwrap_or(natural_h));
118 self.cached_size = constraints.constrain(want);
119
120 self.dst_rect = match self.fit {
122 BoxFit::Fill => Rect::new(0.0, 0.0, self.cached_size.width, self.cached_size.height),
123 BoxFit::Contain => {
124 if natural_w <= 0.0
125 || natural_h <= 0.0
126 || self.cached_size.width <= 0.0
127 || self.cached_size.height <= 0.0
128 {
129 Rect::new(0.0, 0.0, 0.0, 0.0)
130 } else {
131 let scale = (self.cached_size.width / natural_w).min(self.cached_size.height / natural_h);
132 let dw = natural_w * scale;
133 let dh = natural_h * scale;
134 Rect::new((self.cached_size.width - dw) / 2.0, (self.cached_size.height - dh) / 2.0, dw, dh)
135 }
136 }
137 };
138 self.cached_size
139 }
140
141 fn content_size(&self) -> Size {
142 self.cached_size
143 }
144
145 fn paint(&self, offset: Point, painter: &mut PaintList) {
146 let d = self.dst_rect;
147 if d.width <= 0.0 || d.height <= 0.0 {
148 return;
149 }
150 painter.push_texture(self.texture_id, Rc::clone(&self.image));
152 painter.push(PaintCommand::TexturedRect {
154 rect: Rect::new(offset.x + d.x, offset.y + d.y, d.width, d.height),
155 color: Color::WHITE,
156 uv: [0.0, 0.0, 1.0, 1.0],
157 texture_id: self.texture_id,
158 mode: crate::paint::TextureMode::Color,
159 });
160 }
161
162 fn hit_test(&self, position: Point, offset: Point) -> Option<ElementId> {
163 let rect = Rect::new(
165 offset.x + self.dst_rect.x,
166 offset.y + self.dst_rect.y,
167 self.dst_rect.width,
168 self.dst_rect.height,
169 );
170 if rect.contains(position) {
171 self.element_id
172 } else {
173 None
174 }
175 }
176
177 fn set_element_id(&mut self, id: ElementId) {
178 self.element_id = Some(id);
179 }
180
181 fn element_id(&self) -> Option<ElementId> {
182 self.element_id
183 }
184
185 fn children_count(&self) -> usize {
186 0
187 }
188}
189
190