fission_core/ui/widgets/
image.rs1use 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#[derive(Debug, Default, Clone, Serialize, Deserialize)]
26pub struct Image {
27 pub id: Option<NodeId>,
29 pub source: String,
31 pub width: Option<f32>,
33 pub height: Option<f32>,
35 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}