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