1use std::{
4 any::Any,
5 borrow::Cow,
6 collections::HashMap,
7 rc::Rc,
8};
9
10use bytes::Bytes;
11use freya_engine::prelude::{
12 AlphaType,
13 ClipOp,
14 ColorType,
15 CubicResampler,
16 Data,
17 FilterMode,
18 ISize,
19 ImageInfo,
20 MipmapMode,
21 Paint,
22 SamplingOptions,
23 SkImage,
24 SkRect,
25 raster_from_data,
26};
27use rustc_hash::FxHashMap;
28use torin::prelude::Size2D;
29
30use crate::{
31 data::{
32 AccessibilityData,
33 EffectData,
34 LayoutData,
35 StyleState,
36 TextStyleData,
37 },
38 diff_key::DiffKey,
39 element::{
40 ClipContext,
41 Element,
42 ElementExt,
43 EventHandlerType,
44 LayoutContext,
45 RenderContext,
46 },
47 events::name::EventName,
48 layers::Layer,
49 prelude::{
50 AccessibilityExt,
51 ChildrenExt,
52 ContainerExt,
53 ContainerWithContentExt,
54 EffectExt,
55 EventHandlersExt,
56 ImageExt,
57 KeyExt,
58 LayerExt,
59 LayoutExt,
60 MaybeExt,
61 },
62 style::corner_radius::CornerRadius,
63 tree::DiffModifies,
64};
65
66pub fn image(image_handle: ImageHandle) -> Image {
71 let mut accessibility = AccessibilityData::default();
72 accessibility.builder.set_role(accesskit::Role::Image);
73 Image {
74 key: DiffKey::None,
75 element: ImageElement {
76 image_handle,
77 accessibility,
78 layout: LayoutData::default(),
79 event_handlers: HashMap::default(),
80 image_data: ImageData::default(),
81 relative_layer: Layer::default(),
82 effect: None,
83 corner_radius: None,
84 },
85 elements: Vec::new(),
86 }
87}
88
89#[derive(Default, Clone, Debug, PartialEq)]
91pub enum ImageCover {
92 #[default]
94 Fill,
95 Center,
97}
98
99#[derive(Default, Clone, Debug, PartialEq)]
101pub enum AspectRatio {
102 #[default]
104 Min,
105 Max,
107 Fit,
109 None,
111}
112
113#[derive(Clone, Debug, PartialEq, Eq, Hash, Default)]
115pub enum SamplingMode {
116 Nearest,
118 Bilinear,
120 #[default]
122 Trilinear,
123 Mitchell,
125 CatmullRom,
127}
128
129impl SamplingMode {
130 pub fn sampling_options(&self) -> SamplingOptions {
132 match self {
133 Self::Nearest => SamplingOptions::new(FilterMode::Nearest, MipmapMode::None),
134 Self::Bilinear => SamplingOptions::new(FilterMode::Linear, MipmapMode::None),
135 Self::Trilinear => SamplingOptions::new(FilterMode::Linear, MipmapMode::Linear),
136 Self::Mitchell => SamplingOptions::from(CubicResampler::mitchell()),
137 Self::CatmullRom => SamplingOptions::from(CubicResampler::catmull_rom()),
138 }
139 }
140}
141
142#[derive(Clone)]
144pub struct ImageHandle {
145 pub image: SkImage,
146 pub bytes: Bytes,
148}
149
150impl ImageHandle {
151 pub fn new(image: SkImage, bytes: Bytes) -> Self {
152 Self { image, bytes }
153 }
154
155 pub fn from_rgba(width: u32, height: u32, bytes: Bytes, alpha_type: AlphaType) -> Option<Self> {
157 let row_bytes = (width as usize).checked_mul(4)?;
158 if bytes.len() < row_bytes.checked_mul(height as usize)? {
159 return None;
160 }
161 let info = ImageInfo::new(
162 ISize::new(width as i32, height as i32),
163 ColorType::RGBA8888,
164 alpha_type,
165 None,
166 );
167 let data = unsafe { Data::new_bytes(&bytes) };
169 let image = raster_from_data(&info, data, row_bytes)?;
170 Some(Self::new(image, bytes))
171 }
172}
173
174impl PartialEq for ImageHandle {
175 fn eq(&self, other: &Self) -> bool {
176 self.image.unique_id() == other.image.unique_id()
177 }
178}
179
180#[derive(Debug, Default, Clone, PartialEq)]
182pub struct ImageData {
183 pub sampling_mode: SamplingMode,
184 pub aspect_ratio: AspectRatio,
185 pub image_cover: ImageCover,
186}
187
188#[derive(PartialEq, Clone)]
189pub struct ImageElement {
190 pub accessibility: AccessibilityData,
191 pub layout: LayoutData,
192 pub event_handlers: FxHashMap<EventName, EventHandlerType>,
193 pub image_handle: ImageHandle,
194 pub image_data: ImageData,
195 pub relative_layer: Layer,
196 pub effect: Option<EffectData>,
197 pub corner_radius: Option<CornerRadius>,
198}
199
200impl ElementExt for ImageElement {
201 fn changed(&self, other: &Rc<dyn ElementExt>) -> bool {
202 let Some(image) = (other.as_ref() as &dyn Any).downcast_ref::<ImageElement>() else {
203 return false;
204 };
205 self != image
206 }
207
208 fn diff(&self, other: &Rc<dyn ElementExt>) -> DiffModifies {
209 let Some(image) = (other.as_ref() as &dyn Any).downcast_ref::<ImageElement>() else {
210 return DiffModifies::all();
211 };
212
213 let mut diff = DiffModifies::empty();
214
215 if self.accessibility != image.accessibility {
216 diff.insert(DiffModifies::ACCESSIBILITY);
217 }
218
219 if self.relative_layer != image.relative_layer {
220 diff.insert(DiffModifies::LAYER);
221 }
222
223 if self.layout != image.layout {
224 diff.insert(DiffModifies::LAYOUT);
225 }
226
227 if self.image_handle != image.image_handle {
228 diff.insert(DiffModifies::STYLE);
229
230 if self.image_handle.image.dimensions() != image.image_handle.image.dimensions() {
231 diff.insert(DiffModifies::LAYOUT);
232 }
233 }
234
235 if self.effect != image.effect || self.corner_radius != image.corner_radius {
236 diff.insert(DiffModifies::STYLE);
237 }
238
239 diff
240 }
241
242 fn layout(&'_ self) -> Cow<'_, LayoutData> {
243 Cow::Borrowed(&self.layout)
244 }
245
246 fn effect(&'_ self) -> Option<Cow<'_, EffectData>> {
247 self.effect.as_ref().map(Cow::Borrowed)
248 }
249
250 fn style(&'_ self) -> Cow<'_, StyleState> {
251 Cow::Owned(StyleState {
252 corner_radius: self.corner_radius.unwrap_or_default(),
253 ..StyleState::default()
254 })
255 }
256
257 fn text_style(&'_ self) -> Cow<'_, TextStyleData> {
258 Cow::Owned(TextStyleData::default())
259 }
260
261 fn accessibility(&'_ self) -> Cow<'_, AccessibilityData> {
262 Cow::Borrowed(&self.accessibility)
263 }
264
265 fn layer(&self) -> Layer {
266 self.relative_layer
267 }
268
269 fn should_measure_inner_children(&self) -> bool {
270 true
271 }
272
273 fn should_hook_measurement(&self) -> bool {
274 true
275 }
276
277 fn measure(&self, context: LayoutContext) -> Option<(Size2D, Rc<dyn Any>)> {
278 let image = &self.image_handle.image;
279
280 let image_width = image.width() as f32;
281 let image_height = image.height() as f32;
282
283 let width_ratio = context.area_size.width / image_width;
284 let height_ratio = context.area_size.height / image_height;
285
286 let size = match self.image_data.aspect_ratio {
287 AspectRatio::Max => {
288 let ratio = width_ratio.max(height_ratio);
289
290 Size2D::new(image_width * ratio, image_height * ratio)
291 }
292 AspectRatio::Min => {
293 let ratio = width_ratio.min(height_ratio);
294
295 Size2D::new(image_width * ratio, image_height * ratio)
296 }
297 AspectRatio::Fit => Size2D::new(image_width, image_height),
298 AspectRatio::None => *context.area_size,
299 };
300
301 Some((size, Rc::new(size)))
302 }
303
304 fn clip(&self, context: ClipContext) {
305 let rrect = self.render_rect(context.visible_area, context.scale_factor as f32);
306 context.canvas.clip_rrect(rrect, ClipOp::Intersect, true);
307 }
308
309 fn render(&self, context: RenderContext) {
310 let size = context
311 .layout_node
312 .data
313 .as_ref()
314 .unwrap()
315 .downcast_ref::<Size2D>()
316 .unwrap();
317
318 let area = context.layout_node.visible_area();
319
320 let mut rect = SkRect::new(
321 area.min_x(),
322 area.min_y(),
323 area.min_x() + size.width,
324 area.min_y() + size.height,
325 );
326 if self.image_data.image_cover == ImageCover::Center {
327 let width_offset = (size.width - area.width()) / 2.;
328 let height_offset = (size.height - area.height()) / 2.;
329
330 rect.left -= width_offset;
331 rect.right -= width_offset;
332 rect.top -= height_offset;
333 rect.bottom -= height_offset;
334 }
335
336 context.canvas.save();
337 let clip_rrect = self.render_rect(&area, context.scale_factor as f32);
338 context
339 .canvas
340 .clip_rrect(clip_rrect, ClipOp::Intersect, true);
341
342 let sampling = self.image_data.sampling_mode.sampling_options();
343
344 let mut paint = Paint::default();
345 paint.set_anti_alias(true);
346
347 context.canvas.draw_image_rect_with_sampling_options(
348 &self.image_handle.image,
349 None,
350 rect,
351 sampling,
352 &paint,
353 );
354
355 context.canvas.restore();
356 }
357}
358
359impl From<Image> for Element {
360 fn from(value: Image) -> Self {
361 Element::Element {
362 key: value.key,
363 element: Rc::new(value.element),
364 elements: value.elements,
365 }
366 }
367}
368
369impl KeyExt for Image {
370 fn write_key(&mut self) -> &mut DiffKey {
371 &mut self.key
372 }
373}
374
375impl EventHandlersExt for Image {
376 fn get_event_handlers(&mut self) -> &mut FxHashMap<EventName, EventHandlerType> {
377 &mut self.element.event_handlers
378 }
379}
380
381impl AccessibilityExt for Image {
382 fn get_accessibility_data(&mut self) -> &mut AccessibilityData {
383 &mut self.element.accessibility
384 }
385}
386impl MaybeExt for Image {}
387
388impl LayoutExt for Image {
389 fn get_layout(&mut self) -> &mut LayoutData {
390 &mut self.element.layout
391 }
392}
393
394impl ContainerExt for Image {}
395impl ContainerWithContentExt for Image {}
396
397impl ImageExt for Image {
398 fn get_image_data(&mut self) -> &mut ImageData {
399 &mut self.element.image_data
400 }
401}
402
403impl ChildrenExt for Image {
404 fn get_children(&mut self) -> &mut Vec<Element> {
405 &mut self.elements
406 }
407}
408
409impl LayerExt for Image {
410 fn get_layer(&mut self) -> &mut Layer {
411 &mut self.element.relative_layer
412 }
413}
414
415impl EffectExt for Image {
416 fn get_effect(&mut self) -> &mut EffectData {
417 self.element.effect.get_or_insert_with(EffectData::default)
418 }
419}
420
421pub struct Image {
422 key: DiffKey,
423 element: ImageElement,
424 elements: Vec<Element>,
425}
426
427impl Image {
428 pub fn try_downcast(element: &dyn ElementExt) -> Option<ImageElement> {
429 (element as &dyn Any)
430 .downcast_ref::<ImageElement>()
431 .cloned()
432 }
433
434 pub fn corner_radius(mut self, corner_radius: impl Into<CornerRadius>) -> Self {
436 self.element.corner_radius = Some(corner_radius.into());
437 self
438 }
439}