1use crate::{
2 div, AnyElement, BorrowWindow, Bounds, Div, DivState, Element, ElementFocus, ElementId,
3 ElementInteraction, FocusDisabled, FocusEnabled, FocusListeners, Focusable, IntoAnyElement,
4 LayoutId, Pixels, SharedString, StatefulInteraction, StatefulInteractive, StatelessInteraction,
5 StatelessInteractive, StyleRefinement, Styled, ViewContext,
6};
7use futures::FutureExt;
8use util::ResultExt;
9
10pub struct Img<
11 V: 'static,
12 I: ElementInteraction<V> = StatelessInteraction<V>,
13 F: ElementFocus<V> = FocusDisabled,
14> {
15 base: Div<V, I, F>,
16 uri: Option<SharedString>,
17 grayscale: bool,
18}
19
20pub fn img<V: 'static>() -> Img<V, StatelessInteraction<V>, FocusDisabled> {
21 Img {
22 base: div(),
23 uri: None,
24 grayscale: false,
25 }
26}
27
28impl<V, I, F> Img<V, I, F>
29where
30 V: 'static,
31 I: ElementInteraction<V>,
32 F: ElementFocus<V>,
33{
34 pub fn uri(mut self, uri: impl Into<SharedString>) -> Self {
35 self.uri = Some(uri.into());
36 self
37 }
38
39 pub fn grayscale(mut self, grayscale: bool) -> Self {
40 self.grayscale = grayscale;
41 self
42 }
43}
44
45impl<V, F> Img<V, StatelessInteraction<V>, F>
46where
47 F: ElementFocus<V>,
48{
49 pub fn id(self, id: impl Into<ElementId>) -> Img<V, StatefulInteraction<V>, F> {
50 Img {
51 base: self.base.id(id),
52 uri: self.uri,
53 grayscale: self.grayscale,
54 }
55 }
56}
57
58impl<V, I, F> IntoAnyElement<V> for Img<V, I, F>
59where
60 I: ElementInteraction<V>,
61 F: ElementFocus<V>,
62{
63 fn into_any(self) -> AnyElement<V> {
64 AnyElement::new(self)
65 }
66}
67
68impl<V, I, F> Element for Img<V, I, F>
69where
70 I: ElementInteraction<V>,
71 F: ElementFocus<V>,
72{
73 type ViewState = V;
74 type ElementState = DivState;
75
76 fn id(&self) -> Option<crate::ElementId> {
77 self.base.id()
78 }
79
80 fn initialize(
81 &mut self,
82 view_state: &mut V,
83 element_state: Option<Self::ElementState>,
84 cx: &mut ViewContext<V>,
85 ) -> Self::ElementState {
86 self.base.initialize(view_state, element_state, cx)
87 }
88
89 fn layout(
90 &mut self,
91 view_state: &mut V,
92 element_state: &mut Self::ElementState,
93 cx: &mut ViewContext<Self::ViewState>,
94 ) -> LayoutId {
95 self.base.layout(view_state, element_state, cx)
96 }
97
98 fn paint(
99 &mut self,
100 bounds: Bounds<Pixels>,
101 view: &mut V,
102 element_state: &mut Self::ElementState,
103 cx: &mut ViewContext<V>,
104 ) {
105 cx.stack(0, |cx| {
106 self.base.paint(bounds, view, element_state, cx);
107 });
108
109 let style = self.base.compute_style(bounds, element_state, cx);
110 let corner_radii = style.corner_radii;
111
112 if let Some(uri) = self.uri.clone() {
113 let image_future = cx.image_cache.get(uri);
114 if let Some(data) = image_future
115 .clone()
116 .now_or_never()
117 .and_then(ResultExt::log_err)
118 {
119 let corner_radii = corner_radii.to_pixels(bounds.size, cx.rem_size());
120 cx.stack(1, |cx| {
121 cx.paint_image(bounds, corner_radii, data, self.grayscale)
122 .log_err()
123 });
124 } else {
125 cx.spawn(|_, mut cx| async move {
126 if image_future.await.log_err().is_some() {
127 cx.on_next_frame(|cx| cx.notify());
128 }
129 })
130 .detach()
131 }
132 }
133 }
134}
135
136impl<V, I, F> Styled for Img<V, I, F>
137where
138 I: ElementInteraction<V>,
139 F: ElementFocus<V>,
140{
141 fn style(&mut self) -> &mut StyleRefinement {
142 self.base.style()
143 }
144}
145
146impl<V, I, F> StatelessInteractive for Img<V, I, F>
147where
148 I: ElementInteraction<V>,
149 F: ElementFocus<V>,
150{
151 fn stateless_interaction(&mut self) -> &mut StatelessInteraction<V> {
152 self.base.stateless_interaction()
153 }
154}
155
156impl<V, F> StatefulInteractive for Img<V, StatefulInteraction<V>, F>
157where
158 F: ElementFocus<V>,
159{
160 fn stateful_interaction(&mut self) -> &mut StatefulInteraction<Self::ViewState> {
161 self.base.stateful_interaction()
162 }
163}
164
165impl<V, I> Focusable for Img<V, I, FocusEnabled<V>>
166where
167 V: 'static,
168 I: ElementInteraction<V>,
169{
170 fn focus_listeners(&mut self) -> &mut FocusListeners<Self::ViewState> {
171 self.base.focus_listeners()
172 }
173
174 fn set_focus_style(&mut self, style: StyleRefinement) {
175 self.base.set_focus_style(style)
176 }
177
178 fn set_focus_in_style(&mut self, style: StyleRefinement) {
179 self.base.set_focus_in_style(style)
180 }
181
182 fn set_in_focus_style(&mut self, style: StyleRefinement) {
183 self.base.set_in_focus_style(style)
184 }
185}