Skip to main content

cosmic/widget/icon/
mod.rs

1// Copyright 2022 System76 <info@system76.com>
2// SPDX-License-Identifier: MPL-2.0
3
4//! Lazily-generated SVG icon widget for Iced.
5
6mod bundle;
7mod named;
8use std::sync::Arc;
9
10pub use named::{IconFallback, Named};
11
12mod handle;
13pub use handle::{Data, Handle, from_path, from_raster_bytes, from_raster_pixels, from_svg_bytes};
14
15use crate::Element;
16use derive_setters::Setters;
17use iced::widget::{Image, Svg};
18use iced::{ContentFit, Length, Radians, Rectangle};
19use iced_core::Rotation;
20
21/// Create an [`Icon`] from a pre-existing [`Handle`]
22pub fn icon(handle: Handle) -> Icon {
23    Icon {
24        content_fit: ContentFit::Fill,
25        handle,
26        height: None,
27        size: 16,
28        opacity: 1.0,
29        class: crate::theme::Svg::default(),
30        rotation: None,
31        width: None,
32    }
33}
34
35/// Create an icon handle from its XDG icon name.
36pub fn from_name(name: impl Into<Arc<str>>) -> Named {
37    Named::new(name)
38}
39
40/// An image which may be an SVG or PNG.
41#[must_use]
42#[derive(Clone, Setters)]
43pub struct Icon {
44    #[setters(skip)]
45    handle: Handle,
46    class: crate::theme::Svg,
47    #[setters(skip)]
48    pub(super) size: u16,
49    opacity: f32,
50    content_fit: ContentFit,
51    #[setters(strip_option)]
52    width: Option<Length>,
53    #[setters(strip_option)]
54    height: Option<Length>,
55    #[setters(strip_option)]
56    rotation: Option<Rotation>,
57}
58
59impl Icon {
60    #[must_use]
61    pub fn into_svg_handle(self) -> Option<crate::widget::svg::Handle> {
62        match self.handle.refreshed().data {
63            Data::Image(_) => (),
64            Data::Svg(handle) => return Some(handle),
65        }
66
67        None
68    }
69
70    pub fn size(mut self, size: u16) -> Self {
71        self.size = size;
72        self
73    }
74
75    #[must_use]
76    fn view<'a, Message: 'a>(self) -> Element<'a, Message> {
77        let handle = self.handle.refreshed();
78        let symbolic = handle.symbolic;
79        let from_image = |handle| {
80            Image::new(handle)
81                .width(
82                    self.width
83                        .unwrap_or_else(|| Length::Fixed(f32::from(self.size))),
84                )
85                .height(
86                    self.height
87                        .unwrap_or_else(|| Length::Fixed(f32::from(self.size))),
88                )
89                .opacity(self.opacity)
90                .rotation(self.rotation.unwrap_or_default())
91                .content_fit(self.content_fit)
92                .into()
93        };
94
95        let from_svg = |handle| {
96            Svg::<crate::Theme>::new(handle)
97                .class(self.class.clone())
98                .width(
99                    self.width
100                        .unwrap_or_else(|| Length::Fixed(f32::from(self.size))),
101                )
102                .height(
103                    self.height
104                        .unwrap_or_else(|| Length::Fixed(f32::from(self.size))),
105                )
106                .opacity(self.opacity)
107                .rotation(self.rotation.unwrap_or_default())
108                .content_fit(self.content_fit)
109                .symbolic(symbolic)
110                .into()
111        };
112
113        match handle.data {
114            Data::Image(handle) => from_image(handle),
115            Data::Svg(handle) => from_svg(handle),
116        }
117    }
118}
119
120impl<'a, Message: 'a> From<Icon> for Element<'a, Message> {
121    fn from(icon: Icon) -> Self {
122        icon.view::<Message>()
123    }
124}
125
126/// Draw an icon in the given bounds via the runtime's renderer.
127pub fn draw(renderer: &mut crate::Renderer, handle: &Handle, icon_bounds: Rectangle) {
128    match handle.clone().refreshed().data {
129        Data::Svg(handle) => iced_core::svg::Renderer::draw_svg(
130            renderer,
131            iced_core::svg::Svg::new(handle),
132            icon_bounds,
133            icon_bounds,
134        ),
135
136        Data::Image(handle) => {
137            iced_core::image::Renderer::draw_image(
138                renderer,
139                iced_core::Image {
140                    handle,
141                    filter_method: iced_core::image::FilterMethod::Linear,
142                    rotation: Radians(0.),
143                    border_radius: [0.0; 4].into(),
144                    opacity: 1.0,
145                    snap: true,
146                },
147                icon_bounds,
148                icon_bounds,
149            );
150        }
151    }
152}