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::io::Reader::open(icon_path)?
17.with_guessed_format()?
18.decode()?
19.to_rgba8();
2021Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?)
22}
2324/// Creates an icon from the content of an image file.
25///
26/// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro.
27/// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime.
28#[cfg(feature = "image")]
29pub fn from_file_data(
30 data: &[u8],
31 explicit_format: Option<image::ImageFormat>,
32) -> Result<Icon, Error> {
33let mut icon = image::io::Reader::new(std::io::Cursor::new(data));
3435let icon_with_format = match explicit_format {
36Some(format) => {
37 icon.set_format(format);
38 icon
39 }
40None => icon.with_guessed_format()?,
41 };
4243let pixels = icon_with_format.decode()?.to_rgba8();
4445Ok(icon::from_rgba(
46 pixels.to_vec(),
47 pixels.width(),
48 pixels.height(),
49 )?)
50}
5152/// An error produced when creating an [`Icon`].
53#[derive(Debug, thiserror::Error)]
54pub enum Error {
55/// The [`Icon`] is not valid.
56#[error("The icon is invalid: {0}")]
57InvalidError(#[from] icon::Error),
5859/// The underlying OS failed to create the icon.
60#[error("The underlying OS failed to create the window icon: {0}")]
61OsError(#[from] io::Error),
6263/// The `image` crate reported an error.
64#[cfg(feature = "image")]
65 #[error("Unable to create icon from a file: {0}")]
66ImageError(#[from] image::error::ImageError),
67}