svg.rs

 1use crate::{
 2    geometry::{
 3        rect::RectF,
 4        vector::{vec2f, Vector2F},
 5    },
 6    AfterLayoutContext, AppContext, Element, Event, EventContext, LayoutContext, MutableAppContext,
 7    PaintContext, SizeConstraint,
 8};
 9use std::rc::Rc;
10
11pub struct Svg {
12    path: String,
13    // tree: Option<Rc<usvg::Tree>>,
14    size: Option<Vector2F>,
15}
16
17impl Svg {
18    pub fn new(path: String) -> Self {
19        Self {
20            path,
21            // tree: None,
22            size: None,
23        }
24    }
25}
26
27impl Element for Svg {
28    fn layout(
29        &mut self,
30        constraint: SizeConstraint,
31        ctx: &mut LayoutContext,
32        _: &AppContext,
33    ) -> Vector2F {
34        // let size;
35        // match ctx.asset_cache.svg(&self.path) {
36        //     Ok(tree) => {
37        //         size = if constraint.max.x().is_infinite() && constraint.max.y().is_infinite() {
38        //             let rect = usvg_rect_to_euclid_rect(&tree.svg_node().view_box.rect);
39        //             rect.size()
40        //         } else {
41        //             let max_size = constraint.max;
42        //             let svg_size = usvg_rect_to_euclid_rect(&tree.svg_node().view_box.rect).size();
43
44        //             if max_size.x().is_infinite()
45        //                 || max_size.x() / max_size.y() > svg_size.x() / svg_size.y()
46        //             {
47        //                 vec2f(svg_size.x() * max_size.y() / svg_size.y(), max_size.y())
48        //             } else {
49        //                 vec2f(max_size.x(), svg_size.y() * max_size.x() / svg_size.x())
50        //             }
51        //         };
52        //         self.tree = Some(tree);
53        //     }
54        //     Err(error) => {
55        //         log::error!("{}", error);
56        //         size = constraint.min;
57        //     }
58        // };
59
60        // self.size = Some(size);
61        // size
62        todo!()
63    }
64
65    fn after_layout(&mut self, _: &mut AfterLayoutContext, _: &mut MutableAppContext) {}
66
67    fn paint(&mut self, origin: Vector2F, ctx: &mut PaintContext, _: &AppContext) {
68        if let Some(tree) = self.tree.as_ref() {
69            ctx.canvas
70                .draw_svg(tree, RectF::new(origin, self.size.unwrap()));
71        }
72    }
73
74    fn size(&self) -> Option<Vector2F> {
75        self.size
76    }
77
78    fn dispatch_event(&self, _: &Event, _: &mut EventContext, _: &AppContext) -> bool {
79        false
80    }
81}