Skip to main content

fission_core/ui/widgets/
image.rs

1use crate::lowering::{LoweringContext, NodeBuilder};
2use crate::ui::traits::Lower;
3use fission_ir::{
4    op::{ImageFit, LayoutOp, Op, PaintOp},
5    NodeId,
6};
7use serde::{Deserialize, Serialize};
8
9/// A raster image widget.
10///
11/// Displays an image from a URL or asset path. The `fit` property controls
12/// how the image is scaled within its layout box.
13///
14/// # Example
15///
16/// ```rust,ignore
17/// Image {
18///     source: "https://example.com/photo.jpg".into(),
19///     width: Some(200.0),
20///     height: Some(150.0),
21///     fit: Some(ImageFit::Cover),
22///     ..Default::default()
23/// }
24/// ```
25#[derive(Debug, Default, Clone, Serialize, Deserialize)]
26pub struct Image {
27    /// Explicit node identity.
28    pub id: Option<NodeId>,
29    /// URL or asset path to the image.
30    pub source: String,
31    /// Fixed width in layout points.
32    pub width: Option<f32>,
33    /// Fixed height in layout points.
34    pub height: Option<f32>,
35    /// How the image is scaled to fit its layout box (default: `Contain`).
36    pub fit: Option<ImageFit>,
37}
38
39impl Image {
40    pub fn into_node(self) -> crate::ui::Node {
41        crate::ui::Node::Image(self)
42    }
43}
44
45impl Lower for Image {
46    fn lower(&self, cx: &mut LoweringContext) -> NodeId {
47        let layout_id = self.id.unwrap_or_else(|| cx.next_node_id());
48        let paint_id = NodeBuilder::new(
49            cx.next_node_id(),
50            Op::Paint(PaintOp::DrawImage {
51                source: self.source.clone(),
52                fit: self.fit.unwrap_or(ImageFit::Contain),
53            }),
54        )
55        .build(cx);
56
57        let mut layout_builder = NodeBuilder::new(
58            layout_id,
59            Op::Layout(LayoutOp::Box {
60                width: self.width,
61                height: self.height,
62                min_width: None,
63                max_width: None,
64                min_height: None,
65                max_height: None,
66                padding: [0.0; 4],
67                flex_grow: 0.0,
68                flex_shrink: 0.0,
69                aspect_ratio: None,
70            }),
71        );
72        layout_builder.add_child(paint_id);
73        layout_builder.build(cx)
74    }
75}