Skip to content

Commit

Permalink
refactor(core): remove image dependency (#1859)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored May 19, 2021
1 parent 95d518a commit 1be37a3
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
6 changes: 6 additions & 0 deletions .changes/remove-image-crate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tauri": patch
"tauri-runtime-wry": patch
---

Removes `image` dependency. For now only `.ico` icons on Windows are supported, and we'll implement other types on demand to optimize bundle size.
5 changes: 4 additions & 1 deletion core/tauri-runtime-wry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ readme = "README.md"
wry = { version = "0.9.2", default-features = false, features = [ "file-drop", "protocol", "win32" ] }
tauri-runtime = { version = "0.1.1", path = "../tauri-runtime" }
tauri-utils = { version = "1.0.0-beta.0", path = "../tauri-utils" }
image = "0.23"
uuid = { version = "0.8.2", features = [ "v4" ] }
infer = "0.4"

[target."cfg(windows)".dependencies]
ico = "0.1"

[features]
dox = [ "wry/dox" ]
Expand Down
52 changes: 28 additions & 24 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use tauri_runtime::SystemTrayEvent;
#[cfg(feature = "system-tray")]
use wry::application::platform::system_tray::SystemTrayBuilder;

use image::{GenericImageView, Pixel};
use tauri_utils::config::WindowConfig;
use uuid::Uuid;
use wry::{
Expand All @@ -47,6 +46,7 @@ use wry::{
use std::{
collections::HashMap,
convert::TryFrom,
fs::read,
sync::{
mpsc::{channel, Receiver, Sender},
Arc, Mutex,
Expand All @@ -64,38 +64,42 @@ type MainThreadTask = Box<dyn FnOnce() + Send>;
type WindowEventHandler = Box<dyn Fn(&WindowEvent) + Send>;
type WindowEventListeners = Arc<Mutex<HashMap<Uuid, WindowEventHandler>>>;

#[repr(C)]
#[derive(Debug)]
struct PixelValue {
r: u8,
g: u8,
b: u8,
a: u8,
}

const PIXEL_SIZE: usize = std::mem::size_of::<PixelValue>();

/// Wrapper around a [`wry::application::window::Icon`] that can be created from an [`Icon`].
pub struct WryIcon(WindowIcon);

fn icon_err<E: std::error::Error + Send + 'static>(e: E) -> Error {
Error::InvalidIcon(Box::new(e))
}

impl TryFrom<Icon> for WryIcon {
type Error = Error;
fn try_from(icon: Icon) -> std::result::Result<Self, Self::Error> {
let image = match icon {
Icon::File(path) => image::open(path).map_err(|e| Error::InvalidIcon(Box::new(e)))?,
Icon::Raw(raw) => {
image::load_from_memory(&raw).map_err(|e| Error::InvalidIcon(Box::new(e)))?
}
let image_bytes = match icon {
Icon::File(path) => read(path).map_err(icon_err)?,
Icon::Raw(raw) => raw,
_ => unimplemented!(),
};
let (width, height) = image.dimensions();
let mut rgba = Vec::with_capacity((width * height) as usize * PIXEL_SIZE);
for (_, _, pixel) in image.pixels() {
rgba.extend_from_slice(&pixel.to_rgba().0);
let extension = infer::get(&image_bytes)
.expect("could not determine icon extension")
.extension();
match extension {
#[cfg(windows)]
"ico" => {
let icon_dir = ico::IconDir::read(std::io::Cursor::new(image_bytes)).map_err(icon_err)?;
let entry = &icon_dir.entries()[0];
let icon = WindowIcon::from_rgba(
entry.decode().map_err(icon_err)?.rgba_data().to_vec(),
entry.width(),
entry.height(),
)
.map_err(icon_err)?;
Ok(Self(icon))
}
_ => panic!(
"image `{}` extension not supported; please file a Tauri feature request",
extension
),
}
let icon =
WindowIcon::from_rgba(rgba, width, height).map_err(|e| Error::InvalidIcon(Box::new(e)))?;
Ok(Self(icon))
}
}

Expand Down
1 change: 0 additions & 1 deletion core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ open = "1.7.0"
shared_child = "0.3"
os_pipe = "0.9"
minisign-verify = "0.1.8"
image = "0.23"
state = "0.4"
bincode = "1.3"

Expand Down
2 changes: 1 addition & 1 deletion core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ where
}

#[cfg(not(target_os = "linux"))]
if let Some(Icon::File(bytes)) = icon {
if let Some(Icon::File(_)) = icon {
return Err(crate::Error::InvalidIcon(Box::new(Error::new(
ErrorKind::InvalidInput,
"system tray icons on non-linux platforms must be the raw bytes",
Expand Down

0 comments on commit 1be37a3

Please sign in to comment.