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
use crate::lowering::{LoweringContext, NodeBuilder};
use crate::ui::traits::Lower;
use fission_ir::{
op::{ImageFit, LayoutOp, Op, PaintOp},
NodeId,
};
use serde::{Deserialize, Serialize};
/// A raster image widget.
///
/// Displays an image from a URL or asset path. The `fit` property controls
/// how the image is scaled within its layout box.
///
/// # Example
///
/// ```rust,ignore
/// Image {
/// source: "https://example.com/photo.jpg".into(),
/// width: Some(200.0),
/// height: Some(150.0),
/// fit: Some(ImageFit::Cover),
/// ..Default::default()
/// }
/// ```
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Image {
/// Explicit node identity.
pub id: Option<NodeId>,
/// URL or asset path to the image.
pub source: String,
/// Fixed width in layout points.
pub width: Option<f32>,
/// Fixed height in layout points.
pub height: Option<f32>,
/// How the image is scaled to fit its layout box (default: `Contain`).
pub fit: Option<ImageFit>,
}
impl Image {
pub fn into_node(self) -> crate::ui::Node {
crate::ui::Node::Image(self)
}
}
impl Lower for Image {
fn lower(&self, cx: &mut LoweringContext) -> NodeId {
let layout_id = self.id.unwrap_or_else(|| cx.next_node_id());
let paint_id = NodeBuilder::new(
cx.next_node_id(),
Op::Paint(PaintOp::DrawImage {
source: self.source.clone(),
fit: self.fit.unwrap_or(ImageFit::Contain),
}),
)
.build(cx);
let mut layout_builder = NodeBuilder::new(
layout_id,
Op::Layout(LayoutOp::Box {
width: self.width,
height: self.height,
min_width: None,
max_width: None,
min_height: None,
max_height: None,
padding: [0.0; 4],
flex_grow: 0.0,
flex_shrink: 0.0,
aspect_ratio: None,
}),
);
layout_builder.add_child(paint_id);
layout_builder.build(cx)
}
}