1use crate::core::layout;
19use crate::core::mouse;
20use crate::core::renderer;
21use crate::core::svg;
22use crate::core::widget::Tree;
23use crate::core::{
24 Color, ContentFit, Element, Layout, Length, Point, Rectangle, Rotation,
25 Size, Theme, Vector, Widget,
26};
27
28use std::path::PathBuf;
29
30pub use crate::core::svg::Handle;
31
32pub struct Svg<'a, Theme = crate::Theme>
55where
56 Theme: Catalog,
57{
58 handle: Handle,
59 width: Length,
60 height: Length,
61 content_fit: ContentFit,
62 class: Theme::Class<'a>,
63 rotation: Rotation,
64 opacity: f32,
65}
66
67impl<'a, Theme> Svg<'a, Theme>
68where
69 Theme: Catalog,
70{
71 pub fn new(handle: impl Into<Handle>) -> Self {
73 Svg {
74 handle: handle.into(),
75 width: Length::Fill,
76 height: Length::Shrink,
77 content_fit: ContentFit::Contain,
78 class: Theme::default(),
79 rotation: Rotation::default(),
80 opacity: 1.0,
81 }
82 }
83
84 #[must_use]
87 pub fn from_path(path: impl Into<PathBuf>) -> Self {
88 Self::new(Handle::from_path(path))
89 }
90
91 #[must_use]
93 pub fn width(mut self, width: impl Into<Length>) -> Self {
94 self.width = width.into();
95 self
96 }
97
98 #[must_use]
100 pub fn height(mut self, height: impl Into<Length>) -> Self {
101 self.height = height.into();
102 self
103 }
104
105 #[must_use]
109 pub fn content_fit(self, content_fit: ContentFit) -> Self {
110 Self {
111 content_fit,
112 ..self
113 }
114 }
115
116 #[must_use]
118 pub fn style(mut self, style: impl Fn(&Theme, Status) -> Style + 'a) -> Self
119 where
120 Theme::Class<'a>: From<StyleFn<'a, Theme>>,
121 {
122 self.class = (Box::new(style) as StyleFn<'a, Theme>).into();
123 self
124 }
125
126 #[cfg(feature = "advanced")]
128 #[must_use]
129 pub fn class(mut self, class: impl Into<Theme::Class<'a>>) -> Self {
130 self.class = class.into();
131 self
132 }
133
134 pub fn rotation(mut self, rotation: impl Into<Rotation>) -> Self {
136 self.rotation = rotation.into();
137 self
138 }
139
140 pub fn opacity(mut self, opacity: impl Into<f32>) -> Self {
145 self.opacity = opacity.into();
146 self
147 }
148}
149
150impl<Message, Theme, Renderer> Widget<Message, Theme, Renderer>
151 for Svg<'_, Theme>
152where
153 Renderer: svg::Renderer,
154 Theme: Catalog,
155{
156 fn size(&self) -> Size<Length> {
157 Size {
158 width: self.width,
159 height: self.height,
160 }
161 }
162
163 fn layout(
164 &mut self,
165 _tree: &mut Tree,
166 renderer: &Renderer,
167 limits: &layout::Limits,
168 ) -> layout::Node {
169 let Size { width, height } = renderer.measure_svg(&self.handle);
171 let image_size = Size::new(width as f32, height as f32);
172
173 let rotated_size = self.rotation.apply(image_size);
175
176 let raw_size = limits.resolve(self.width, self.height, rotated_size);
178
179 let full_size = self.content_fit.fit(rotated_size, raw_size);
181
182 let final_size = Size {
184 width: match self.width {
185 Length::Shrink => f32::min(raw_size.width, full_size.width),
186 _ => raw_size.width,
187 },
188 height: match self.height {
189 Length::Shrink => f32::min(raw_size.height, full_size.height),
190 _ => raw_size.height,
191 },
192 };
193
194 layout::Node::new(final_size)
195 }
196
197 fn draw(
198 &self,
199 _state: &Tree,
200 renderer: &mut Renderer,
201 theme: &Theme,
202 _style: &renderer::Style,
203 layout: Layout<'_>,
204 cursor: mouse::Cursor,
205 _viewport: &Rectangle,
206 ) {
207 let Size { width, height } = renderer.measure_svg(&self.handle);
208 let image_size = Size::new(width as f32, height as f32);
209 let rotated_size = self.rotation.apply(image_size);
210
211 let bounds = layout.bounds();
212 let adjusted_fit = self.content_fit.fit(rotated_size, bounds.size());
213 let scale = Vector::new(
214 adjusted_fit.width / rotated_size.width,
215 adjusted_fit.height / rotated_size.height,
216 );
217
218 let final_size = image_size * scale;
219
220 let position = match self.content_fit {
221 ContentFit::None => Point::new(
222 bounds.x + (rotated_size.width - adjusted_fit.width) / 2.0,
223 bounds.y + (rotated_size.height - adjusted_fit.height) / 2.0,
224 ),
225 _ => Point::new(
226 bounds.center_x() - final_size.width / 2.0,
227 bounds.center_y() - final_size.height / 2.0,
228 ),
229 };
230
231 let drawing_bounds = Rectangle::new(position, final_size);
232
233 let is_mouse_over = cursor.is_over(bounds);
234
235 let status = if is_mouse_over {
236 Status::Hovered
237 } else {
238 Status::Idle
239 };
240
241 let style = theme.style(&self.class, status);
242
243 let render = |renderer: &mut Renderer| {
244 renderer.draw_svg(
245 svg::Svg {
246 handle: self.handle.clone(),
247 color: style.color,
248 rotation: self.rotation.radians(),
249 opacity: self.opacity,
250 },
251 drawing_bounds,
252 );
253 };
254
255 if adjusted_fit.width > bounds.width
256 || adjusted_fit.height > bounds.height
257 {
258 renderer.with_layer(bounds, render);
259 } else {
260 render(renderer);
261 }
262 }
263}
264
265impl<'a, Message, Theme, Renderer> From<Svg<'a, Theme>>
266 for Element<'a, Message, Theme, Renderer>
267where
268 Theme: Catalog + 'a,
269 Renderer: svg::Renderer + 'a,
270{
271 fn from(icon: Svg<'a, Theme>) -> Element<'a, Message, Theme, Renderer> {
272 Element::new(icon)
273 }
274}
275
276#[derive(Debug, Clone, Copy, PartialEq, Eq)]
278pub enum Status {
279 Idle,
281 Hovered,
283}
284
285#[derive(Debug, Clone, Copy, PartialEq, Default)]
287pub struct Style {
288 pub color: Option<Color>,
294}
295
296pub trait Catalog {
298 type Class<'a>;
300
301 fn default<'a>() -> Self::Class<'a>;
303
304 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style;
306}
307
308impl Catalog for Theme {
309 type Class<'a> = StyleFn<'a, Self>;
310
311 fn default<'a>() -> Self::Class<'a> {
312 Box::new(|_theme, _status| Style::default())
313 }
314
315 fn style(&self, class: &Self::Class<'_>, status: Status) -> Style {
316 class(self, status)
317 }
318}
319
320pub type StyleFn<'a, Theme> = Box<dyn Fn(&Theme, Status) -> Style + 'a>;
324
325impl<Theme> From<Style> for StyleFn<'_, Theme> {
326 fn from(style: Style) -> Self {
327 Box::new(move |_theme, _status| style)
328 }
329}