1//! Attach an icon to the window of your application.
2pub use crate::core::window::icon::*;
34use crate::core::window::icon;
56use std::io;
78#[cfg(feature = "image")]
9use std::path::Path;
1011/// Creates an icon from an image file.
12///
13/// This will return an error in case the file is missing at run-time. You may prefer [`from_file_data`] instead.
14#[cfg(feature = "image")]
15pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Icon, Error> {
16let icon = image::ImageReader::open(icon_path)?.decode()?.to_rgba8();
1718Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?)
19}
2021/// Creates an icon from the content of an image file.
22///
23/// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro.
24/// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime.
25#[cfg(feature = "image")]
26pub fn from_file_data(
27 data: &[u8],
28 explicit_format: Option<image::ImageFormat>,
29) -> Result<Icon, Error> {
30let mut icon = image::ImageReader::new(std::io::Cursor::new(data));
3132let icon_with_format = match explicit_format {
33Some(format) => {
34 icon.set_format(format);
35 icon
36 }
37None => icon.with_guessed_format()?,
38 };
3940let pixels = icon_with_format.decode()?.to_rgba8();
4142Ok(icon::from_rgba(
43 pixels.to_vec(),
44 pixels.width(),
45 pixels.height(),
46 )?)
47}
4849/// An error produced when creating an [`Icon`].
50#[derive(Debug, thiserror::Error)]
51pub enum Error {
52/// The [`Icon`] is not valid.
53#[error("The icon is invalid: {0}")]
54InvalidError(#[from] icon::Error),
5556/// The underlying OS failed to create the icon.
57#[error("The underlying OS failed to create the window icon: {0}")]
58OsError(#[from] io::Error),
5960/// The `image` crate reported an error.
61#[cfg(feature = "image")]
62 #[error("Unable to create icon from a file: {0}")]
63ImageError(#[from] image::error::ImageError),
64}