1use crate::platform_impl::PlatformIcon;
8use muda::NativeIcon;
9use std::{error::Error, fmt, io, mem};
10
11#[repr(C)]
12#[derive(Debug)]
13pub(crate) struct Pixel {
14 pub(crate) r: u8,
15 pub(crate) g: u8,
16 pub(crate) b: u8,
17 pub(crate) a: u8,
18}
19
20pub(crate) const PIXEL_SIZE: usize = mem::size_of::<Pixel>();
21
22#[derive(Debug)]
23pub enum BadIcon {
25 ByteCountNotDivisibleBy4 { byte_count: usize },
28 DimensionsVsPixelCount {
31 width: u32,
32 height: u32,
33 width_x_height: usize,
34 pixel_count: usize,
35 },
36 OsError(io::Error),
38}
39
40impl fmt::Display for BadIcon {
41 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
42 match self {
43 BadIcon::ByteCountNotDivisibleBy4 { byte_count } => write!(f,
44 "The length of the `rgba` argument ({:?}) isn't divisible by 4, making it impossible to interpret as 32bpp RGBA pixels.",
45 byte_count,
46 ),
47 BadIcon::DimensionsVsPixelCount {
48 width,
49 height,
50 width_x_height,
51 pixel_count,
52 } => write!(f,
53 "The specified dimensions ({:?}x{:?}) don't match the number of pixels supplied by the `rgba` argument ({:?}). For those dimensions, the expected pixel count is {:?}.",
54 width, height, pixel_count, width_x_height,
55 ),
56 BadIcon::OsError(e) => write!(f, "OS error when instantiating the icon: {:?}", e),
57 }
58 }
59}
60
61impl Error for BadIcon {
62 fn source(&self) -> Option<&(dyn Error + 'static)> {
63 match self {
64 BadIcon::OsError(e) => Some(e),
65 _ => None,
66 }
67 }
68}
69
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub(crate) struct RgbaIcon {
72 pub(crate) rgba: Vec<u8>,
73 pub(crate) width: u32,
74 pub(crate) height: u32,
75}
76
77#[derive(Debug, Clone, PartialEq, Eq)]
79pub(crate) struct NoIcon;
80
81#[allow(dead_code)] mod constructors {
83 use super::*;
84
85 impl RgbaIcon {
86 pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
87 if !rgba.len().is_multiple_of(PIXEL_SIZE) {
88 return Err(BadIcon::ByteCountNotDivisibleBy4 {
89 byte_count: rgba.len(),
90 });
91 }
92 let pixel_count = rgba.len() / PIXEL_SIZE;
93 if pixel_count != (width * height) as usize {
94 Err(BadIcon::DimensionsVsPixelCount {
95 width,
96 height,
97 width_x_height: (width * height) as usize,
98 pixel_count,
99 })
100 } else {
101 Ok(RgbaIcon {
102 rgba,
103 width,
104 height,
105 })
106 }
107 }
108 }
109
110 impl NoIcon {
111 pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
112 let _ = RgbaIcon::from_rgba(rgba, width, height)?;
114 Ok(NoIcon)
115 }
116 }
117}
118
119#[derive(Debug, Clone)]
120pub(crate) enum IconType {
121 Custom(Icon),
122 Native(NativeIcon),
123}
124
125#[derive(Clone)]
127pub struct Icon {
128 pub(crate) inner: PlatformIcon,
129}
130
131impl fmt::Debug for Icon {
132 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
133 fmt::Debug::fmt(&self.inner, formatter)
134 }
135}
136
137impl Icon {
138 pub fn from_rgba(rgba: Vec<u8>, width: u32, height: u32) -> Result<Self, BadIcon> {
143 Ok(Icon {
144 inner: PlatformIcon::from_rgba(rgba, width, height)?,
145 })
146 }
147
148 #[cfg(windows)]
156 pub fn from_path<P: AsRef<std::path::Path>>(
157 path: P,
158 size: Option<(u32, u32)>,
159 ) -> Result<Self, BadIcon> {
160 let win_icon = PlatformIcon::from_path(path, size)?;
161 Ok(Icon { inner: win_icon })
162 }
163
164 #[cfg(windows)]
172 pub fn from_resource(ordinal: u16, size: Option<(u32, u32)>) -> Result<Self, BadIcon> {
173 let win_icon = PlatformIcon::from_resource(ordinal, size)?;
174 Ok(Icon { inner: win_icon })
175 }
176
177 #[cfg(windows)]
180 pub fn from_resource_name(
181 resource_name: &str,
182 size: Option<(u32, u32)>,
183 ) -> Result<Self, BadIcon> {
184 let win_icon = PlatformIcon::from_resource_name(resource_name, size)?;
185 Ok(Icon { inner: win_icon })
186 }
187
188 #[cfg(windows)]
190 pub fn from_handle(handle: isize) -> Self {
191 let win_icon = PlatformIcon::from_handle(handle as _);
192 Icon { inner: win_icon }
193 }
194}