1use crate::{
2 Bounds, Element, ElementId, InteractiveElement, InteractiveElementState, Interactivity,
3 IntoElement, LayoutId, Pixels, SharedString, StyleRefinement, Styled, WindowContext,
4};
5use util::ResultExt;
6
7pub struct Svg {
8 interactivity: Interactivity,
9 path: Option<SharedString>,
10}
11
12pub fn svg() -> Svg {
13 Svg {
14 interactivity: Interactivity::default(),
15 path: None,
16 }
17}
18
19impl Svg {
20 pub fn path(mut self, path: impl Into<SharedString>) -> Self {
21 self.path = Some(path.into());
22 self
23 }
24}
25
26impl Element for Svg {
27 type State = InteractiveElementState;
28
29 fn layout(
30 &mut self,
31 element_state: Option<Self::State>,
32 cx: &mut WindowContext,
33 ) -> (LayoutId, Self::State) {
34 self.interactivity.layout(element_state, cx, |style, cx| {
35 cx.request_layout(&style, None)
36 })
37 }
38
39 fn paint(self, bounds: Bounds<Pixels>, element_state: &mut Self::State, cx: &mut WindowContext)
40 where
41 Self: Sized,
42 {
43 self.interactivity
44 .paint(bounds, bounds.size, element_state, cx, |style, _, cx| {
45 if let Some((path, color)) = self.path.as_ref().zip(style.text.color) {
46 cx.paint_svg(bounds, path.clone(), color).log_err();
47 }
48 })
49 }
50}
51
52impl IntoElement for Svg {
53 type Element = Self;
54
55 fn element_id(&self) -> Option<ElementId> {
56 self.interactivity.element_id.clone()
57 }
58
59 fn into_element(self) -> Self::Element {
60 self
61 }
62}
63
64impl Styled for Svg {
65 fn style(&mut self) -> &mut StyleRefinement {
66 &mut self.interactivity.base_style
67 }
68}
69
70impl InteractiveElement for Svg {
71 fn interactivity(&mut self) -> &mut Interactivity {
72 &mut self.interactivity
73 }
74}