2020-07-01 06:09:39 +02:00
|
|
|
//! Attach an icon to the window of your application.
|
2023-04-17 23:41:12 +02:00
|
|
|
pub use crate::core::window::icon::*;
|
2023-04-12 18:47:53 +12:00
|
|
|
|
2023-04-17 23:41:12 +02:00
|
|
|
use crate::core::window::icon;
|
2023-04-12 18:47:53 +12:00
|
|
|
|
2020-07-01 06:09:39 +02:00
|
|
|
use std::io;
|
|
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
#[cfg(feature = "image")]
|
2021-12-29 00:15:18 +01:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
/// Creates an icon from an image file.
|
|
|
|
|
///
|
2023-09-09 12:24:47 +02:00
|
|
|
/// This will return an error in case the file is missing at run-time. You may prefer [`from_file_data`] instead.
|
2023-04-12 18:47:53 +12:00
|
|
|
#[cfg(feature = "image")]
|
|
|
|
|
pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Icon, Error> {
|
2023-09-04 12:58:41 +02:00
|
|
|
let icon = image::io::Reader::open(icon_path)?.decode()?.to_rgba8();
|
2020-07-01 06:09:39 +02:00
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
Ok(icon::from_rgba(icon.to_vec(), icon.width(), icon.height())?)
|
2023-02-23 09:31:48 -08:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
/// Creates an icon from the content of an image file.
|
|
|
|
|
///
|
|
|
|
|
/// This content can be included in your application at compile-time, e.g. using the `include_bytes!` macro.
|
|
|
|
|
/// You can pass an explicit file format. Otherwise, the file format will be guessed at runtime.
|
|
|
|
|
#[cfg(feature = "image")]
|
|
|
|
|
pub fn from_file_data(
|
|
|
|
|
data: &[u8],
|
2023-09-04 12:58:41 +02:00
|
|
|
explicit_format: Option<image::ImageFormat>,
|
2023-04-12 18:47:53 +12:00
|
|
|
) -> Result<Icon, Error> {
|
2023-09-04 12:58:41 +02:00
|
|
|
let mut icon = image::io::Reader::new(std::io::Cursor::new(data));
|
|
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
let icon_with_format = match explicit_format {
|
|
|
|
|
Some(format) => {
|
|
|
|
|
icon.set_format(format);
|
|
|
|
|
icon
|
|
|
|
|
}
|
|
|
|
|
None => icon.with_guessed_format()?,
|
|
|
|
|
};
|
2021-12-29 00:15:18 +01:00
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
let pixels = icon_with_format.decode()?.to_rgba8();
|
2021-12-29 00:15:18 +01:00
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
Ok(icon::from_rgba(
|
|
|
|
|
pixels.to_vec(),
|
|
|
|
|
pixels.width(),
|
|
|
|
|
pixels.height(),
|
|
|
|
|
)?)
|
2020-07-01 06:09:39 +02:00
|
|
|
}
|
|
|
|
|
|
2023-04-12 18:47:53 +12:00
|
|
|
/// An error produced when creating an [`Icon`].
|
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
2020-07-01 06:09:39 +02:00
|
|
|
pub enum Error {
|
2023-04-12 18:47:53 +12:00
|
|
|
/// The [`Icon`] is not valid.
|
|
|
|
|
#[error("The icon is invalid: {0}")]
|
|
|
|
|
InvalidError(#[from] icon::Error),
|
2020-07-01 06:09:39 +02:00
|
|
|
|
|
|
|
|
/// The underlying OS failed to create the icon.
|
2024-04-19 14:53:38 +08:00
|
|
|
#[error("The underlying OS failed to create the window icon: {0}")]
|
2023-04-12 18:47:53 +12:00
|
|
|
OsError(#[from] io::Error),
|
|
|
|
|
|
|
|
|
|
/// The `image` crate reported an error.
|
|
|
|
|
#[cfg(feature = "image")]
|
|
|
|
|
#[error("Unable to create icon from a file: {0}")]
|
2023-09-04 12:58:41 +02:00
|
|
|
ImageError(#[from] image::error::ImageError),
|
2020-07-01 06:09:39 +02:00
|
|
|
}
|