summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar daladim <daladim@users.noreply.github.com>2021-12-29 00:15:18 +0100
committerLibravatar daladim <daladim@users.noreply.github.com>2022-03-01 22:15:16 +0100
commitf3e7781cb1bb7a70f5c87ea37b13ac248b50cc0a (patch)
treebde7e96792533efe8457050073daed1fcaf96733
parentee6f4cb2ac7f322ef708fe0655835638bac841ea (diff)
downloadiced-f3e7781cb1bb7a70f5c87ea37b13ac248b50cc0a.tar.gz
iced-f3e7781cb1bb7a70f5c87ea37b13ac248b50cc0a.tar.bz2
iced-f3e7781cb1bb7a70f5c87ea37b13ac248b50cc0a.zip
Added convenience functions for window::icon::Icon
-rw-r--r--Cargo.toml7
-rw-r--r--src/window/icon.rs57
2 files changed, 63 insertions, 1 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 7c222fbb..60c60fbb 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -17,7 +17,7 @@ default = ["wgpu"]
# Enables the `iced_wgpu` renderer
wgpu = ["iced_wgpu"]
# Enables the `Image` widget
-image = ["iced_wgpu/image"]
+image = ["iced_wgpu/image", "image_rs"]
# Enables the `Svg` widget
svg = ["iced_wgpu/svg"]
# Enables the `Canvas` widget
@@ -98,6 +98,11 @@ iced_glutin = { version = "0.2", path = "glutin", optional = true }
iced_glow = { version = "0.2", path = "glow", optional = true }
thiserror = "1.0"
+[dependencies.image_rs]
+version = "0.23"
+package = "image"
+optional = true
+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
iced_wgpu = { version = "0.4", path = "wgpu", optional = true }
diff --git a/src/window/icon.rs b/src/window/icon.rs
index aacadfca..bacad41a 100644
--- a/src/window/icon.rs
+++ b/src/window/icon.rs
@@ -2,6 +2,9 @@
use std::fmt;
use std::io;
+#[cfg(feature = "image_rs")]
+use std::path::Path;
+
/// The icon of a window.
#[derive(Debug, Clone)]
pub struct Icon(iced_winit::winit::window::Icon);
@@ -18,6 +21,39 @@ impl Icon {
Ok(Icon(raw))
}
+
+ /// Creates an icon from an image file.
+ ///
+ /// This will return an error in case the file is missing at run-time. You may prefer [`Self::from_file_data`] instead.
+ #[cfg(feature = "image_rs")]
+ pub fn from_file<P: AsRef<Path>>(icon_path: P) -> Result<Self, Error> {
+ let icon = image_rs::io::Reader::open(icon_path)?.decode()?.to_rgba8();
+
+ Self::from_rgba(icon.to_vec(), icon.width(), icon.height())
+ }
+
+ /// 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_rs")]
+ pub fn from_file_data(
+ data: &[u8],
+ explicit_format: Option<image_rs::ImageFormat>,
+ ) -> Result<Self, Error> {
+ let mut icon = image_rs::io::Reader::new(std::io::Cursor::new(data));
+ let icon_with_format = match explicit_format {
+ Some(format) => {
+ icon.set_format(format);
+ icon
+ }
+ None => icon.with_guessed_format()?,
+ };
+
+ let pixels = icon_with_format.decode()?.to_rgba8();
+
+ Self::from_rgba(pixels.to_vec(), pixels.width(), pixels.height())
+ }
}
/// An error produced when using `Icon::from_rgba` with invalid arguments.
@@ -43,6 +79,16 @@ pub enum Error {
/// The underlying OS failed to create the icon.
OsError(io::Error),
+
+ /// The `image` crate reported an error
+ #[cfg(feature = "image_rs")]
+ ImageError(image_rs::error::ImageError),
+}
+
+impl From<std::io::Error> for Error {
+ fn from(os_error: std::io::Error) -> Self {
+ Error::OsError(os_error)
+ }
}
impl From<iced_winit::winit::window::BadIcon> for Error {
@@ -74,6 +120,13 @@ impl From<Icon> for iced_winit::winit::window::Icon {
}
}
+#[cfg(feature = "image_rs")]
+impl From<image_rs::error::ImageError> for Error {
+ fn from(image_error: image_rs::error::ImageError) -> Self {
+ Self::ImageError(image_error)
+ }
+}
+
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -104,6 +157,10 @@ impl fmt::Display for Error {
icon: {:?}",
e
),
+ #[cfg(feature = "image_rs")]
+ Error::ImageError(e) => {
+ write!(f, "Unable to create icon from a file: {:?}", e)
+ }
}
}
}