1pub use crate::core::window::icon::*;
3
4use crate::core::window::icon;
5
6use std::io;
7
8#[cfg(feature = "image")]
9use std::path::Path;
10
11#[cfg(feature = "image")]
15pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Icon, Error> {
16 let icon = ::image::io::Reader::open(icon_path)?
17 .with_guessed_format()?
18 .decode()?
19 .to_rgba8();
20
21 Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?)
22}
23
24#[cfg(feature = "image")]
29pub fn from_file_data(
30 data: &[u8],
31 explicit_format: Option<image::ImageFormat>,
32) -> Result<Icon, Error> {
33 let mut icon = image::io::Reader::new(std::io::Cursor::new(data));
34
35 let icon_with_format = match explicit_format {
36 Some(format) => {
37 icon.set_format(format);
38 icon
39 }
40 None => icon.with_guessed_format()?,
41 };
42
43 let pixels = icon_with_format.decode()?.to_rgba8();
44
45 Ok(icon::from_rgba(
46 pixels.to_vec(),
47 pixels.width(),
48 pixels.height(),
49 )?)
50}
51
52#[derive(Debug, thiserror::Error)]
54pub enum Error {
55 #[error("The icon is invalid: {0}")]
57 InvalidError(#[from] icon::Error),
58
59 #[error("The underlying OS failed to create the window icon: {0}")]
61 OsError(#[from] io::Error),
62
63 #[cfg(feature = "image")]
65 #[error("Unable to create icon from a file: {0}")]
66 ImageError(#[from] image::error::ImageError),
67}